<?php

if (!defined('SHORTCODABLE_DIR')) {
    define('SHORTCODABLE_DIR', rtrim(basename(dirname(__FILE__))));
}
if (SHORTCODABLE_DIR != 'shortcodable') {
    throw new Exception('The edit shortcodable module is not installed in correct directory. The directory should be named "shortcodable"');
}
// enable shortcodable buttons and add to HtmlEditorConfig
$htmlEditorNames = Config::inst()->get('Shortcodable', 'htmleditor_names');
if (is_array($htmlEditorNames)) {
    foreach ($htmlEditorNames as $htmlEditorName) {
        HtmlEditorConfig::get($htmlEditorName)->enablePlugins(array('shortcodable' => sprintf('../../../%s/javascript/editor_plugin.js', SHORTCODABLE_DIR)));
        HtmlEditorConfig::get($htmlEditorName)->addButtonsToLine(1, 'shortcodable');
    }
}
// register classes added via yml config
$classes = Config::inst()->get('Shortcodable', 'shortcodable_classes');
Shortcodable::register_classes($classes);
 public function onBeforeRender()
 {
     $this->owner->setAttribute('data-placeholderclasses', implode(',', Shortcodable::get_shortcodable_classes_with_placeholders()));
 }
 /**
  * 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;
 }