CKEditor.php
1.78 KB
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
<?php
/**
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\ckeditor;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* CKEditor renders a CKEditor js plugin for classic editing.
* @see http://docs.ckeditor.com/
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\ckeditor
*/
class CKEditor extends InputWidget
{
use CKEditorTrait;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->initOptions();
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
echo Html::textarea($this->name, $this->value, $this->options);
}
$this->registerPlugin();
}
/**
* Registers CKEditor plugin
* @codeCoverageIgnore
*/
protected function registerPlugin()
{
$js = [];
$view = $this->getView();
CKEditorWidgetAsset::register($view);
$id = $this->options['id'];
$options = $this->clientOptions !== false && !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '{}';
$js[] = "CKEDITOR.replace('$id', $options);";
$js[] = "dosamigos.ckEditorWidget.registerOnChangeHandler('$id');";
if (isset($this->clientOptions['filebrowserUploadUrl'])) {
$js[] = "dosamigos.ckEditorWidget.registerCsrfImageUploadHandler();";
}
$view->registerJs(implode("\n", $js));
}
}