forked from webvimark/ckeditor
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CKEditor.php
146 lines (127 loc) · 4.44 KB
/
CKEditor.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
<?php
/**
* @link https://github.com/MadAnd/yii2-ckeditor-kcfinder
* @copyright Copyright (c) 2014 HimikLab, MadAnd
* @license http://opensource.org/licenses/MIT MIT
*/
namespace MadAnd\ckeditor;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* WYSIWYG HTML input widget with file browser plugin (KCFinder) for Yii2.
* Using as field in ActiveForm:
*
* ```php
* echo $form->field($model, 'text')->widget(CKEditor::className(), [
* 'editorOptions' => ['height' => '500px'],
* ]);
* ```
*
* Using inline:
*
* ```php
* echo CKEditor::widget([
* 'name' => 'comment',
* 'value' => 'Please write your comment',
* 'editorOptions' => ['height' => '500px'],
* ]);
* ```
*
* Using without KCFinder:
*
* ```php
* echo $form->field($model, 'text')->widget(CKEditor::className(), [
* 'editorOptions' => ['height' => '500px'],
* 'enabledKCFinder' => false,
* ]);
*
* @author MadAnd
* @package MadAnd\ckeditor
*/
class CKEditor extends InputWidget
{
/**
* Whether add configuration that enables KCFinder. Defaults to TRUE.
* @see http://kcfinder.sunhater.com/
* @var bool
*/
public $enabledKCFinder = true;
/**
* @var array the options for the CKEditor 4 JS plugin
* @see http://docs.ckeditor.com/#!/guide/dev_installation
*/
public $editorOptions = [];
private $configuredSession = false;
public function init()
{
parent::init();
$this->configureSession();
$view = $this->getView();
$id = Json::encode($this->options['id']);
if ($this->enabledKCFinder) {
$kcFinderBundle = KCFinderAsset::register($view);
$kcFinderBaseUrl = $kcFinderBundle->baseUrl;
// Add KCFinder-specific config for CKEditor
$this->editorOptions = ArrayHelper::merge(
$this->editorOptions,
[
'filebrowserBrowseUrl' => $kcFinderBaseUrl
. '/browse.php?opener=ckeditor&type=files',
'filebrowserImageBrowseUrl' => $kcFinderBaseUrl
. '/browse.php?opener=ckeditor&type=images',
'filebrowserFlashBrowseUrl' => $kcFinderBaseUrl
. '/browse.php?opener=ckeditor&type=flash',
'filebrowserUploadUrl' => $kcFinderBaseUrl
. '/upload.php?opener=ckeditor&type=files',
'filebrowserImageUploadUrl' => $kcFinderBaseUrl
. '/upload.php?opener=ckeditor&type=images',
'filebrowserFlashUploadUrl' => $kcFinderBaseUrl
. '/upload.php?opener=ckeditor&type=flash',
'allowedContent' => true,
]
);
}
$jsData = "CKEDITOR.replace($id";
$jsData .= empty($this->editorOptions) ? '' : (', ' . Json::encode($this->editorOptions));
$jsData .= ").on('blur', function(){this.updateElement(); jQuery(this.element.$).trigger('blur');});";
$view->registerJs($jsData);
CKEditorAsset::register($view);
}
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);
}
}
private function configureSession() {
if ($this->configuredSession) {
return;
}
if (isset(Yii::$app->params['KCFinderConfig'])) {
$kcFinderConfig = Yii::$app->params['KCFinderConfig'];
if (!is_array($kcFinderConfig)) {
throw new \RuntimeException('KCFinderConfig param must be an array.');
}
foreach (['uploadURL', 'uploadDir'] as $confKey) {
if (isset($kcFinderConfig[$confKey])) {
$kcFinderConfig[$confKey] = Yii::getAlias($kcFinderConfig[$confKey]);
}
}
if (isset(Yii::$app->session['KCFINDER'])) {
Yii::$app->session['KCFINDER'] = ArrayHelper::merge(
(array) Yii::$app->session['KCFINDER'],
$kcFinderConfig
);
} else {
Yii::$app->session['KCFINDER'] = $kcFinderConfig;
}
}
$this->configuredSession = true;
}
}