/
CKEditor.php
89 lines (78 loc) · 2.79 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
<?php
namespace kuakling\keditor;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\widgets\InputWidget;
class CKEditor extends InputWidget
{
use CKEditorTrait;
/**
* @inheritdoc
*/
public $filemanager = false;
public $uploadDir = '';
public $uploadURL = '';
public function init()
{
parent::init();
$this->initOptions();
if(empty($this->uploadDir)){
//$this->uploadDir = Yii::$app->project->basePath.DIRECTORY_SEPARATOR.'UserFiles';
$this->uploadDir = Yii::getAlias('@webroot/UserFiles');
}
if(empty($this->uploadURL)){
$this->uploadURL = Yii::getAlias('@web/UserFiles');
}
}
/**
* @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();
CKEditorAsset::register($view);
$id = $this->options['id'];
if ($this->filemanager) {
$kcfinder = KCFinderAsset::register($view);
$browse = [
'filebrowserBrowseUrl' => $kcfinder->baseUrl.'/browse.php?opener=ckeditor&type=files',
'filebrowserImageBrowseUrl' => $kcfinder->baseUrl.'/browse.php?opener=ckeditor&type=images',
'filebrowserFlashBrowseUrl' => $kcfinder->baseUrl.'/browse.php?opener=ckeditor&type=flash',
'filebrowserUploadUrl' => $kcfinder->baseUrl.'/upload.php?opener=ckeditor&type=files',
'filebrowserImageUploadUrl' => $kcfinder->baseUrl.'/upload.php?opener=ckeditor&type=images',
'filebrowserFlashUploadUrl' => $kcfinder->baseUrl.'/upload.php?opener=ckeditor&type=flash',
];
$this->clientOptions = ArrayHelper::merge($this->clientOptions, $browse);
$kcfOptions = [
'disabled' => false,
'uploadDir' => $this->uploadDir,
'uploadURL' => $this->uploadURL,
];
Yii::$app->session->set('KCFINDER', $kcfOptions);
$htSource = __DIR__.'/upload.htaccess';
$htDest = $kcfinder->basePath.'/conf/upload.htaccess';
copy($htSource, $htDest);
}
$options = $this->clientOptions !== false && !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '{}';
$js[] = "CKEDITOR.replace('$id', $options);";
$view->registerJs(implode("\n", $js));
}
}