1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018 Andrey Girnik |
4
|
|
|
* @author Andrey Girnik <[email protected]> |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
6
|
|
|
*/ |
7
|
|
|
namespace Itstructure\CKEditor; |
8
|
|
|
|
9
|
|
|
use yii\widgets\InputWidget; |
10
|
|
|
use yii\helpers\{Html, Json}; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CKEditor |
14
|
|
|
* Widget class for displaying CKEditor block. |
15
|
|
|
* |
16
|
|
|
* @see http://docs.ckeditor.com/ |
17
|
|
|
* |
18
|
|
|
* @package Itstructure\CKEditor |
19
|
|
|
*/ |
20
|
|
|
class CKEditor extends InputWidget |
21
|
|
|
{ |
22
|
|
|
use OptionsTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function init() |
28
|
|
|
{ |
29
|
|
|
parent::init(); |
30
|
|
|
$this->loadOptions(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function run() |
37
|
|
|
{ |
38
|
|
|
echo $this->hasModel() ? $this->getActiveTextarea() : $this->getTextarea(); |
39
|
|
|
|
40
|
|
|
$this->initEditor(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initialize CKEditor in textarea field with asset. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
protected function initEditor(): void |
49
|
|
|
{ |
50
|
|
|
$view = $this->getView(); |
51
|
|
|
|
52
|
|
|
CKEditorAsset::register($view); |
53
|
|
|
CKEditorAssetAddition::register($view); |
54
|
|
|
|
55
|
|
|
$id = $this->options['id']; |
56
|
|
|
|
57
|
|
|
$clientOptions = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '{}'; |
58
|
|
|
|
59
|
|
|
$js = []; |
60
|
|
|
|
61
|
|
|
$js[] = "CKEDITOR.replace('$id', $clientOptions);"; |
62
|
|
|
$js[] = "initItstructureOnChangeHandler('$id');"; |
63
|
|
|
|
64
|
|
|
if (array_key_exists('filebrowserUploadUrl', $this->clientOptions) || |
65
|
|
|
array_key_exists('filebrowserImageUploadUrl', $this->clientOptions)) { |
66
|
|
|
$js[] = "initItstructureCsrfHandler();"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$view->registerJs(implode("\n", $js)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get active textarea field with model. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getActiveTextarea(): string |
78
|
|
|
{ |
79
|
|
|
return Html::activeTextarea($this->model, $this->attribute, $this->options); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get textarea field without model, |
84
|
|
|
* and with name and value. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function getTextarea(): string |
89
|
|
|
{ |
90
|
|
|
return Html::textarea($this->name, $this->value, $this->options); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|