/
TinyMce.php
138 lines (121 loc) · 4.65 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
<?php
namespace kuakling\keditor;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
*
* TinyMCE renders a tinyMCE js plugin for WYSIWYG editing.
*
*/
class TinyMce extends InputWidget
{
public $language;
public $clientOptions = [];
public $triggerSaveOnBeforeValidateForm = true;
public $enableFilemanager = false;
public $folderName = ['file' => 'file', 'image'=>'image', 'media' => 'media'];
protected $uploadDir = '';
protected $uploadURL = '';
public function init()
{
parent::init();
if(empty($this->uploadDir)){
$this->uploadDir = Yii::getAlias('@webroot/UserFiles');
}
if(empty($this->uploadURL)){
$this->uploadURL = Yii::getAlias('@web/UserFiles');
}
}
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->registerClientScript();
}
/**
* Registers tinyMCE js plugin
*/
protected function registerClientScript()
{
$js = [];
$view = $this->getView();
TinyMceAsset::register($view);
$id = $this->options['id'];
$this->clientOptions = [
'menubar' => !Yii::$app->user->isGuest,
'statusbar' => !Yii::$app->user->isGuest,
'plugins' => [
"advlist autolink link image lists charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars insertdatetime media nonbreaking",
"table contextmenu directionality emoticons paste textcolor code"
],
'toolbar1' => "undo redo | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | styleselect",
'toolbar2' => "| link unlink anchor | image media | forecolor backcolor ",
'height' => '500px',
'relative_urls' => false,
];
$this->clientOptions['selector'] = "#$id";
// @codeCoverageIgnoreStart
/*if ($this->language !== null) {
$langFile = "langs/{$this->language}.js";
$langAssetBundle = TinyMceLangAsset::register($view);
$langAssetBundle->js[] = $langFile;
$this->clientOptions['language_url'] = $langAssetBundle->baseUrl . "/{$langFile}";
}*/
// @codeCoverageIgnoreEnd
if($this->enableFilemanager && !Yii::$app->user->isGuest){
$kcfinder = KCFinderAsset::register($view);
$this->clientOptions['file_browser_callback'] = new \yii\web\JsExpression("
function(field, url, type, win) {
var fileType = ".Json::encode($this->folderName).";
tinyMCE.activeEditor.windowManager.open({
file: '".$kcfinder->baseUrl."/browse.php?opener=tinymce4&field=' + field + '&type=' + fileType[type],
title: 'File manager',
width: 700,
height: 500,
inline: true,
close_previous: false
}, {
window: win,
input: field
});
return false;
}"
);
$kcfOptions = [
'disabled' => false,
'uploadDir' => $this->uploadDir,
'uploadURL' => $this->uploadURL,
'types' => array(
// (F)CKEditor types
'files' => "",
'flash' => "swf",
'images' => "*img",
// TinyMCE types
'file' => "",
'media' => "swf flv avi mpg mpeg qt mov wmv asf rm",
'image' => "*img",
//Other
'File' => "",
'Media' => "swf flv avi mpg mpeg qt mov wmv asf rm",
'Image' => "*img",
),
];
Yii::$app->session->set('KCFINDER', $kcfOptions);
$htSource = __DIR__.'/upload.htaccess';
$htDest = $kcfinder->basePath.'/conf/upload.htaccess';
copy($htSource, $htDest);
}
$options = Json::encode($this->clientOptions);
$js[] = "tinymce.init($options);";
if ($this->triggerSaveOnBeforeValidateForm) {
$js[] = "$('#{$id}').parents('form').on('beforeValidate', function() { tinymce.triggerSave(); });";
}
$view->registerJs(implode("\n", $js));
}
}