Version: current

      1 <?php
      2
      3 class SimpleTinyMCEField extends TextareaField
      4 {
      5
      6   private static $default_plugins = "safari,paste";
      7   private static $default_theme = "advanced";
      8   private static $default_buttons = array(
      9     'bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,styleselect,formatselect',
     10     'cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,link,unlink,anchor,image,|,code'
     11   );
     12   private static $default_toolbar_location = "top";
     13   private static $default_toolbar_align = "left";
     14   private static $default_statusbar_location = "bottom";
     15   private static $default_advanced_resizing = false;
     16   private static $default_content_css;
     17   private static $default_extra_options;
     18
     19   private $plugins;
     20   private $theme;
     21   private $buttons;
     22   private $toolbarLocation;
     23   private $toolbarAlign;
     24   private $statusbarLocation;
     25   private $advancedResizing;
     26   private $contentCSS;
     27   private $extraOptions;
     28
     29
     30
     31     function __construct($name, $title = null, $config = array(), $rows = 15, $cols = 55, $value = "", $form = null)
     32     {
     33         parent::__construct($name, $title, $rows, $cols, $value, $form);
     34   }
     35
     36   public static function set_default_plugins($value)
     37   {
     38     self::$default_plugins = $value;
     39   }
     40
     41   public static function set_default_theme($value)
     42   {
     43     self::$default_theme = $value;
     44   }
     45
     46   public static function set_default_buttons($buttons)
     47   {
     48     if(!is_array($buttons))
     49       die("<strong>".__CLASS__."::set_default_buttons()</strong> Value must be passed as an array of rows.");
     50     self::$default_buttons = $buttons;
     51   }
     52
     53   public static function set_default_toolbar_align($value)
     54   {
     55     self::$default_toolbar_align = $value;
     56   }
     57
     58   public static function set_default_statusbar_location($value)
     59   {
     60     self::$default_statusbar_location = $value;
     61   }
     62
     63   public static function set_default_advanced_resizing($bool)
     64   {
     65     self::$default_advanced_resizing = $bool;
     66   }
     67
     68   public static function set_default_content_css($value)
     69   {
     70     self::$default_content_css = $value;
     71   }
     72
     73   public static function set_default_extra_options($value)
     74   {
     75     self::$default_extra_options = $value;
     76   }
     77
     78   private function getPlugins()
     79   {
     80     return $this->plugins ? $this->plugins : self::$default_plugins;
     81   }
     82
     83   private function getTheme()
     84   {
     85     return $this->theme ? $this->theme : self::$default_theme;
     86   }
     87
     88   private function getButtons()
     89   {
     90     $buttons = $this->buttons ? $this->buttons : self::$default_buttons;
     91     if(sizeof($buttons < 4)) {
     92       for($i=0;$i<4;$i++)
     93         if(!isset($buttons[$i])) $buttons[$i] = "";
     94     }
     95     $ret = "";
     96     $first = true;
     97     foreach($buttons as $index => $line) {
     98       $ret .= sprintf("%stheme_%s_buttons%d : '%s'",
     99         $first ? "" : ",",
    100         $this->getTheme(),
    101         $index+1,
    102         $line
    103       );
    104       $first = false;
    105     }
    106     return $ret;
    107   }
    108
    109   public function setPlugins($plugins)
    110   {
    111     $this->plugins = $plugins;
    112   }
    113
    114   public function setTheme($theme)
    115   {
    116     $this->theme = $theme;
    117   }
    118
    119   public function setButtons($buttons)
    120   {
    121     if(!is_array($buttons))
    122       die("<strong>{$this->class}::setButtons()</strong> Value must be passed as an array of rows.");
    123     $this->buttons = $buttons;
    124   }
    125
    126   private function getToolbarLocation()
    127   {
    128     return $this->toolbarLocation ? $this->toolbarLocation : self::$default_toolbar_location;
    129   }
    130
    131   private function getToolbarAlign()
    132   {
    133     return $this->toolbarAlign ? $this->toolbarAlign : self::$default_toolbar_align;
    134   }
    135
    136   private function getStatusbarLocation()
    137   {
    138     return $this->statusbarLocation ? $this->statusbarLocation : self::$default_statusbar_location;
    139   }
    140
    141   private function getAdvancedResizing()
    142   {
    143     $bool = $this->advancedResizing !== null ? $this->advancedResizing : self::$default_advanced_resizing;
    144     return $bool ? "true" : "false";
    145   }
    146
    147   private function getContentCSS()
    148   {
    149     return $this->contentCSS ? $this->contentCSS : self::$default_content_css;
    150   }
    151
    152   private function getExtraOptions()
    153   {
    154     $value = $this->extraOptions ? $this->extraOptions : self::$default_extra_options;
    155     return $value ? ",".$value : "";
    156   }
    157
    158   public function setToolbarLocation($loc)
    159   {
    160     $this->toolbarLocation = $loc;
    161   }
    162
    163   public function setStatusbarLocation($loc)
    164   {
    165     $this->statusbarLocation = $loc;
    166   }
    167
    168   public function setAdvancedResizing($bool)
    169   {
    170     $this->advancedResizing = $bool;
    171   }
    172
    173   public function setContentCSS($css)
    174   {
    175     $this->contentCSS = $css;
    176   }
    177
    178   public function setExtraOptions($opts)
    179   {
    180     $this->extraOptions = $opts;
    181   }
    182
    183   private function buildJS()
    184   {
    185     $js = sprintf("
    186       $(function() {
    187                 $('#%s').tinymce({
    188                   plugins : '%s',
    189                   theme : '%s',
    190                 %s,
    191                theme_advanced_toolbar_location : '%s',
    192              theme_advanced_toolbar_align : '%s',
    193              theme_advanced_statusbar_location : '%s',
    194              theme_advanced_resizing : %s,
    195            paste_auto_cleanup_on_paste : true,
    196            paste_remove_spans: true,
    197            paste_remove_styles: true,
    198                content_css : '%s'
    199                %s
    200             });
    201           });",
    202           $this->Id(),
    203           $this->getPlugins(),
    204           $this->getTheme(),
    205           $this->getButtons(),
    206           $this->getToolbarLocation(),
    207           $this->getToolbarAlign(),
    208           $this->getStatusbarLocation(),
    209           $this->getAdvancedResizing(),
    210           $this->getContentCSS(),
    211           $this->getExtraOptions()
    212
    213         );
    214     return $js;
    215   }
    216
    217   function Field()
    218   {
    219     Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
    220     Requirements::javascript("dataobject_manager/code/simple_tinymce_field/javascript/tiny_mce/jquery.tinymce.js");
    221     Requirements::javascript("dataobject_manager/code/simple_tinymce_field/javascript/tiny_mce/tiny_mce.js");
    222     Requirements::customScript($this->buildJS());
    223     return parent::Field();
    224   }
    225
    226 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -