Source of file MarkdownField.php
Size: 6,341 Bytes - Last Modified: 2021-12-23T10:57:30+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/MarkdownField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 | <?php namespace WebbuildersGroup\DeploymentNotes\Forms; use ParsedownExtra; use SilverStripe\View\Requirements; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\Director; use SilverStripe\Security\SecurityToken; use SilverStripe\Core\Convert; use SilverStripe\Assets\Upload; use SilverStripe\Assets\File; use SilverStripe\Forms\TextareaField; class MarkdownField extends TextareaField { private static $allowed_actions=array( 'markdown_preview', 'image_upload' ); private $_imageSupportEnabled=false; private $_uploadDir='Uploads'; private $_maxUploadSize=0; protected $rows=30; public function FieldHolder($properties=array()) { $this->extraClasses[]='stacked'; $this->extraClasses[]='textarea'; Requirements::css('webbuilders-group/silverstripe-deployment-notes:css/MarkdownField.css'); Requirements::javascript('webbuilders-group/silverstripe-deployment-notes:javascript/MarkdownField.js'); return parent::FieldHolder($properties); } /** * Generates markdown for previewing * @param {SS_HTTPRequest} $request HTTP Request Object * @return {string} HTML Response */ public function markdown_preview(HTTPRequest $request) { //Verify the request is ajax and the security token is good if(Director::is_ajax()==false || SecurityToken::inst()->checkRequest($request)==false) { return $this->httpError(403); } //If the markdown is empty just return $markdown=$this->request->postVar('rawtext'); if(empty($markdown)) { return ''; } //Get rendered HTML $parser=new \ParsedownExtra(); $parser->setBreaksEnabled(true); return $parser->text(Convert::raw2xml($markdown)); } /** * Handles uploading of images for the editor * @param {SS_HTTPRequest} $request HTTP Request * @return {mixed} Response */ public function image_upload(HTTPRequest $request) { //If image support is not enabled block the request if($this->_imageSupportEnabled==false) { return $this->httpError(403); } //Validate the security token if(!SecurityToken::inst()->checkRequest($request)) { return $this->httpError(403); } //Initialize the upload handler $upload=new Upload(); //Restrict to images $upload->getValidator()->setAllowedExtensions(array('jpg', 'gif', 'png')); //Set the max file size if($this->_maxUploadSize<=0) { // get the lower max size $maxUpload=File::ini2bytes(ini_get('upload_max_filesize')); $maxPost=File::ini2bytes(ini_get('post_max_size')); $upload->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost)); }else { $upload->getValidator()->setAllowedMaxFileSize($this->_maxUploadSize); } //Attempt to load the uploaded files, note that Upload::load() handles validation of the upload if(array_key_exists('image', $_FILES)) { if($upload->load($_FILES['image'], $this->_uploadDir)) { $response=array( 'url'=>$upload->getFile()->getURL(), 'alt'=>$upload->getFile()->Title ); }else { $response=array( 'errors'=>$upload->getErrors() ); } }else { $response=array( 'errors'=>array( 'No image was uploaded' ) ); } //Set the response to json and encode the response header('Content-Type: application/json'); return json_encode($response); } /** * Adds the image-support class to the classes if it is supported * @return {string} CSS class names * @see FormField::extraClass() */ public function extraClass() { $classes=parent::extraClass(); $classes.=' deployment-notes-markdown'; if($this->_imageSupportEnabled) { $classes.=' image-support'; } return $classes; } /** * Enables or disables uploading of images * @param {bool} $value Boolean true for enabled false otherwise * @return {MarkdownField} Returns self */ public function setImageUploadEnabled($value) { $this->_imageSupportEnabled=$value; return $this; } /** * Gets whether uploading of images is supported or not * @return {bool} Boolean true for enabled false otherwise */ public function getImageUploadEnabled() { return $this->_imageSupportEnabled; } /** * Sets the upload destination folder for images * @param {string} $value Destination folder for images relative to /assets * @return {MarkdownField} Returns self */ public function setUploadFolder($value) { $this->_uploadDir=$value; return $this; } /** * Gets the upload destination folder for images * @return {string} Destination folder for images relative to /assets */ public function getUploadFolder() { return $this->_uploadDir; } /** * Sets the upload max file size for images * @param {int|array} $value Maximum upload file size for images in bytes, could be an array of file extensions to sizes * @return {MarkdownField} Returns self */ public function setUploadMaxSize($value) { $this->_maxUploadSize=$value; return $this; } /** * Gets the upload max file size for images * @return {int|array} Maximum upload file size for images in bytes, could be an array of file extensions to sizes */ public function getUploadMaxSize() { return $this->_maxUploadSize; } } ?> |