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