PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/vendor/moonlandsoft/yii2-tinymce/TinyMCE.php

https://gitlab.com/Sang240892/ecommerce
PHP | 208 lines | 137 code | 24 blank | 47 comment | 16 complexity | 1fa01910543def2cb6a600d20a983c66 MD5 | raw file
  1. <?php
  2. namespace moonland\tinymce;
  3. use yii\helpers\ArrayHelper;
  4. use yii\widgets\InputWidget;
  5. use yii\helpers\Html;
  6. use moonland\helpers\JSON;
  7. /**
  8. * TinyMCE Extension.
  9. * TinyMCE is a platform independent web based Javascript HTML WYSIWYG editor control.
  10. * TinyMCE has the ability to convert HTML TEXTAREA fields or other HTML elements to editor instances.
  11. *
  12. *
  13. * ~~~
  14. * [php]
  15. * use moonland\tinymce\TinyMCE;
  16. *
  17. * echo TinyMCE::widget(['name' => 'text-content']);
  18. *
  19. * $form->field($model, 'attribute')->widget(TinyMCE::widget());
  20. * ~~~
  21. *
  22. *
  23. * @author Moh Khoirul Anam <moh.khoirul.anaam@gmail.com>
  24. * @copyright moonlandsoft 2014
  25. * @since 1
  26. */
  27. class TinyMCE extends InputWidget
  28. {
  29. public $config = [];
  30. public $selector;
  31. /**
  32. * @var boolean to enable or disable advanced tab in image.
  33. */
  34. public $showAdvancedImageTab = true;
  35. /**
  36. * @var string theme of tiny mce
  37. */
  38. public $theme = 'modern';
  39. /**
  40. * @var array to toggling a textarea to tinyMCE and to textarea.
  41. */
  42. protected $toggle = [
  43. 'active' => false,
  44. 'button' => [
  45. 'show' => true,
  46. 'toggle' => ['label' => 'Tiny MCE', 'options' => ['class' => 'btn btn-default']],
  47. 'unToggle' => ['label' => 'Textarea', 'options' => ['class' => 'btn btn-default']]
  48. ],
  49. 'addon' => [
  50. 'before' => '',
  51. 'after' => '',
  52. ],
  53. 'tinyStart' => true,
  54. ];
  55. /**
  56. * @var array tiny mce plugin
  57. */
  58. public $plugins = [
  59. 'advlist autolink lists link image charmap print preview hr anchor pagebreak',
  60. 'searchreplace wordcount visualblocks visualchars code fullscreen',
  61. 'insertdatetime media nonbreaking save table contextmenu directionality',
  62. 'emoticons template paste textcolor',
  63. ];
  64. /**
  65. * @var array to show the toolbar.
  66. */
  67. public $toolbar = [
  68. 'undo redo',
  69. 'fontsizeselect',
  70. 'bold italic underline',
  71. 'alignleft aligncenter alignright alignjustify',
  72. 'bullist numlist outdent indent',
  73. 'link image',
  74. 'forecolor backcolor',
  75. 'print preview media',
  76. ];
  77. /**
  78. * @var array to remove the default toolbar
  79. */
  80. public $removeToolbar = [];
  81. /**
  82. * @var array text template
  83. */
  84. public $templates = [];
  85. /**
  86. * @var integer set the tinyMCE height.
  87. */
  88. public $height = 300;
  89. /**
  90. * @var string set this to call this tiny mce with function.
  91. */
  92. public $functionName;
  93. public function __set($name, $value)
  94. {
  95. if (method_exists($this, 'set' . ucfirst($name)))
  96. parent::__set($name, $value);
  97. elseif (isset($this->{$name}))
  98. $this->{$name} = $value;
  99. else
  100. $this->config[$name] = $value;
  101. }
  102. public function setToggle($value)
  103. {
  104. $this->toggle = array_merge($this->toggle, $value);
  105. }
  106. public function init()
  107. {
  108. if (isset($this->name) || isset($this->model) || isset($this->attribute)) {
  109. parent::init();
  110. }
  111. $this->config = ArrayHelper::merge([
  112. 'style_formats_merge' => true,
  113. 'theme' => $this->theme,
  114. 'plugins' => $this->plugins,
  115. 'templates' => $this->templates,
  116. 'height' => $this->height,
  117. ], $this->config);
  118. $this->config['fontsize_formats'] = "6pt 7pt 8pt 9pt 10pt 11pt 12pt 13pt 14pt 15pt 16pt 18pt 20pt 24pt 28pt 36pt 40pt 48pt";
  119. if (!isset($this->options['rows']))
  120. $this->options['rows'] = 10;
  121. if (!empty($this->toolbar))
  122. $this->config['toolbar'] = implode(' | ', $this->toolbar);
  123. if (!empty($this->removeToolbar)) {
  124. foreach ($this->removeToolbar as $toolbar) {
  125. $this->config['toolbar'] = str_replace($toolbar, '', $this->config['toolbar']);
  126. }
  127. }
  128. if ($this->toggle['active'])
  129. {
  130. if (!isset($this->toggle['id']))
  131. $this->toggle['id'] = $this->getId();
  132. }
  133. if ($this->showAdvancedImageTab)
  134. $this->config['image_advtab'] = 'true';
  135. }
  136. public function run()
  137. {
  138. $this->registerPlugin();
  139. return $this->renderInput();
  140. }
  141. protected function renderInput()
  142. {
  143. if ($this->hasModel())
  144. $input = Html::activeTextarea($this->model, $this->attribute, $this->options);
  145. else
  146. $input = Html::textarea($this->name, $this->value, $this->options);
  147. if ($this->toggle['active'])
  148. $input = $this->prepareToggle($input);
  149. return $input;
  150. }
  151. protected function prepareToggle($input)
  152. {
  153. $buttonToggle = $this->toggle['button']['toggle'];
  154. $buttonUnToggle = $this->toggle['button']['unToggle'];
  155. $buttonToggle['options']['onclick'] = 'js:toggleTiny'.$this->toggle['id'].'()';
  156. $buttonUnToggle['options']['onclick'] = 'js:unToggleTiny'.$this->toggle['id'].'()';
  157. $toggle = Html::a($buttonToggle['label'], '#', $buttonToggle['options']);
  158. $unToggle = Html::a($buttonUnToggle['label'], '#', $buttonUnToggle['options']);
  159. $toggle = $this->toggle['addon']['before'] . $toggle . $unToggle . $this->toggle['addon']['after'];
  160. return $toggle.$input;
  161. }
  162. protected function registerPlugin()
  163. {
  164. $view = $this->getView();
  165. if (isset($this->selector))
  166. $id = $this->selector;
  167. else
  168. $id = '#' . $this->options['id'];
  169. $this->config = ArrayHelper::merge(['selector' => $id], $this->config);
  170. $options = JSON::encode($this->config);
  171. TinyMCEAsset::register($view);
  172. if ($this->toggle['active']) {
  173. $toggle = $this->toggle['id'];
  174. $start = '';
  175. if ($this->toggle['tinyStart']) {
  176. $start = 'toggleTiny'.$toggle . '();';
  177. }
  178. $view->registerJs("toggleTiny{$toggle}=function(){tinymce.init({$options});};\nunToggleTiny{$toggle}=function(){tinymce.remove('{$id}')};\n$start");
  179. } elseif (isset($this->functionName)) {
  180. $view->registerJs("{$this->functionName}=function(){tinymce.init({$options})}");
  181. } else
  182. $view->registerJs("tinymce.init({$options})");
  183. }
  184. }