/
Tinymce.php
99 lines (84 loc) · 3.72 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
<?php
namespace xvs32x\tinymce;
use Yii;
use yii\bootstrap\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\widgets\InputWidget;
class Tinymce extends InputWidget
{
public $pluginOptions = [];
public $fileManagerOptions = [];
public function init()
{
parent::init();
if (!$this->pluginOptions) {
$this->pluginOptions = [
'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 responsivefilemanager code"
],
'toolbar1' => "undo redo | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | styleselect",
'toolbar2' => "| responsivefilemanager | link unlink anchor | image media | forecolor backcolor ",
'image_advtab' => true,
'filemanager_title' => "Filemanager",
'language' => ArrayHelper::getValue(explode('-', Yii::$app->language), '0', Yii::$app->language),
];
}
}
public function run()
{
if ($this->hasModel()) {
if (!ArrayHelper::getValue($this->options, 'id')) {
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
}
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
if (!ArrayHelper::getValue($this->options, 'id')) {
$this->options['id'] = Html::getAttributeName($this->name . rand(1, 9999));
}
echo Html::textarea($this->name, $this->value, $this->options);
}
$this->registerAssets();
}
/**
* @param TinymceAsset $instance
* @return string
* */
public function setOptions($instance)
{
//set up filemanager path
if (!ArrayHelper::getValue($this->pluginOptions, 'external_filemanager_path')) {
$this->pluginOptions['external_filemanager_path'] = $instance->baseUrl . '/filemanager/';
}
//set up external plugin
$this->pluginOptions['external_plugins']['filemanager'] = $instance->baseUrl . '/filemanager/plugin.min.js';
//relative path from web folder
if (ArrayHelper::getValue($this->fileManagerOptions, 'configPath')) {
$configPath = ArrayHelper::getValue($this->fileManagerOptions, 'configPath');
} else {
$configPath = [
//path from base_url to base of upload folder with start and final /
'upload_dir' => '/uploads/filemanager/source/',
//relative path from filemanager folder to upload folder with final /
'current_path' => '../../../uploads/filemanager/source/',
//relative path from filemanager folder to thumbs folder with final / (DO NOT put inside upload folder)
'thumbs_base_path' => '../../../uploads/filemanager/thumbs/',
//base url (only domain) of site (without final /)
'base_url' => false,
];
}
//decode and send as access_key
$this->pluginOptions['filemanager_access_key'] = urlencode(serialize($configPath));
$id = $this->options['id'];
return Json::encode(ArrayHelper::merge(['selector' => '#' . $id], $this->pluginOptions));
}
public function registerAssets()
{
$view = $this->getView();
$instance = TinymceAsset::register($view);
$options = $this->setOptions($instance);
$view->registerJs("tinymce.init($options)");
}
}