Source of file ShortcodableController.php
Size: 8,455 Bytes - Last Modified: 2021-12-23T10:20:24+00:00
/var/www/docs.ssmods.com/process/src/src/Controller/ShortcodableController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 | <?php namespace Silverstripe\Shortcodable\Controller; use Silverstripe\Shortcodable; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\Form; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Permission; /** * ShortcodableController. * * @author shea@livesource.co.nz **/ class ShortcodableController extends LeftAndMain { private static $sc_url_segment = 'shortcodable'; private static $required_permission_codes = 'CMS_ACCESS_LeftAndMain'; /** * @var array */ private static $allowed_actions = array( 'ShortcodeForm' => 'CMS_ACCESS_LeftAndMain', 'handleEdit' => 'CMS_ACCESS_LeftAndMain', 'shortcodePlaceHolder' => 'CMS_ACCESS_LeftAndMain' ); /** * @var array */ private static $url_handlers = array( 'edit/$ShortcodeType!/$Action//$ID/$OtherID' => 'handleEdit' ); /** * @var string */ protected $shortcodableclass; /** * @var boolean */ protected $isnew = true; /** * @var array */ protected $shortcodedata; /** * Get the shortcodable class by whatever means possible. * Determine if this is a new shortcode, or editing an existing one. */ public function init() { parent::init(); if ($data = $this->getShortcodeData()) { $this->isnew = false; $this->shortcodableclass = $data['name']; } elseif ($type = $this->request->requestVar('ShortcodeType')) { $this->shortcodableclass = $type; } else { $this->shortcodableclass = $this->request->param('ShortcodeType'); } } /** * Point to edit link, if shortcodable class exists. */ public function Link($action = null) { if ($this->shortcodableclass) { return Controller::join_links( $this->config()->url_base, $this->config()->sc_url_segment, 'edit', $this->shortcodableclass ); } return Controller::join_links($this->config()->url_base, $this->config()->sc_url_segment, $action); } /** * handleEdit */ public function handleEdit(HTTPRequest $request) { $this->shortcodableclass = $request->param('ShortcodeType'); return $this->handleAction($request, $action = $request->param('Action')); } /** * Get the shortcode data from the request. * @return array shortcodedata */ protected function getShortcodeData() { if($this->shortcodedata){ return $this->shortcodedata; } $data = false; if($shortcode = $this->request->requestVar('Shortcode')){ //remove BOM inside string on cursor position... $shortcode = str_replace("\xEF\xBB\xBF", '', $shortcode); $data = singleton('\Silverstripe\Shortcodable\ShortcodableParser')->the_shortcodes(array(), $shortcode); if(isset($data[0])){ $this->shortcodedata = $data[0]; return $this->shortcodedata; } } } /** * Provides a GUI for the insert/edit shortcode popup. * * @return Form **/ public function ShortcodeForm() { Config::inst()->update('SSViewer', 'theme_enabled', false); $classes = Shortcodable::get_shortcodable_classes_fordropdown(); $classname = $this->shortcodableclass; if ($this->isnew) { $headingText = _t('Shortcodable.EDITSHORTCODE', 'Edit Shortcode'); } else { $headingText = sprintf( _t('Shortcodable.EDITSHORTCODE', 'Edit %s Shortcode'), singleton($this->shortcodableclass)->singular_name() ); } // essential fields $fields = FieldList::create(array( CompositeField::create( LiteralField::create( 'Heading', sprintf('<h3 class="htmleditorfield-shortcodeform-heading insert">%s</h3>', $headingText) ) )->addExtraClass('CompositeField composite cms-content-header nolabel'), LiteralField::create('shortcodablefields', '<div class="ss-shortcodable content">'), DropdownField::create('ShortcodeType', _t('Shortcodable.SHORTCODETYPE', 'Shortcode type'), $classes, $classname) ->setHasEmptyDefault(true) ->addExtraClass('shortcode-type') )); // attribute and object id fields if ($classname && class_exists($classname)) { $class = singleton($classname); if (is_subclass_of($class, 'DataObject')) { if (singleton($classname)->hasMethod('getShortcodableRecords')) { $dataObjectSource = singleton($classname)->getShortcodableRecords(); } else { $dataObjectSource = $classname::get()->map()->toArray(); } $fields->push( DropdownField::create('id', $class->singular_name(), $dataObjectSource) ->setHasEmptyDefault(true) ); } if (singleton($classname)->hasMethod('getShortcodeFields')) { if ($attrFields = singleton($classname)->getShortcodeFields()) { $fields->push( CompositeField::create($attrFields) ->addExtraClass('attributes-composite') ->setName('AttributesCompositeField') ); } } } // actions $actions = FieldList::create(array( FormAction::create('insert', _t('Shortcodable.BUTTONINSERTSHORTCODE', 'Insert shortcode')) ->addExtraClass('ss-ui-action-constructive') ->setAttribute('data-icon', 'accept') ->setUseButtonTag(true) )); // form $form = Form::create($this, 'ShortcodeForm', $fields, $actions) ->loadDataFrom($this) ->addExtraClass('htmleditorfield-form htmleditorfield-shortcodable cms-dialog-content'); $this->extend('updateShortcodeForm', $form); $fields->push(LiteralField::create('shortcodablefieldsend', '</div>')); if ($data = $this->getShortcodeData()) { $form->loadDataFrom($data['atts']); // special treatment for setting value of UploadFields foreach ($form->Fields()->dataFields() as $field) { if (is_a($field, 'UploadField') && isset($data['atts'][$field->getName()])) { $field->setValue(array('Files' => explode(',', $data['atts'][$field->getName()]))); } } } return $form; } /** * Generates shortcode placeholder to display inside TinyMCE instead of the shortcode. * * @return void */ public function shortcodePlaceHolder($request) { if (!Permission::check('CMS_ACCESS_CMSMain')) { return; } $classname = $request->param('ID'); $id = $request->param('OtherID'); if (!class_exists($classname)) { return; } if ($id && is_subclass_of($classname, DataObject::class)) { $object = $classname::get()->byID($id); } else { $object = singleton($classname); } if ($object->hasMethod('getShortcodePlaceHolder')) { $attributes = null; if ($shortcode = $request->requestVar('Shortcode')) { $shortcode = str_replace("\xEF\xBB\xBF", '', $shortcode); //remove BOM inside string on cursor position... $shortcodeData = singleton('\Silverstripe\Shortcodable\ShortcodableParser')->the_shortcodes(array(), $shortcode); if (isset($shortcodeData[0])) { $attributes = $shortcodeData[0]['atts']; } } $link = $object->getShortcodePlaceholder($attributes); return $this->redirect($link); } } } |