1
|
|
|
<?php |
2
|
|
|
namespace roboapp\redactor; |
3
|
|
|
|
4
|
|
|
use yii\helpers\Html; |
5
|
|
|
use yii\helpers\Json; |
6
|
|
|
use yii\widgets\InputWidget; |
7
|
|
|
|
8
|
|
|
class Redactor extends InputWidget |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array the options for the Imperavi Redactor JS plugin. |
12
|
|
|
* Please refer to the Imperavi Redactor plugin Web page for possible options. |
13
|
|
|
* @see {@link http://imperavi.com/redactor/docs/ redactor options}. |
14
|
|
|
*/ |
15
|
|
|
public $clientOptions = []; |
16
|
|
|
|
17
|
|
|
/** @inheritdoc */ |
18
|
4 |
|
public function init() |
19
|
|
|
{ |
20
|
4 |
|
$request = \Yii::$app->getRequest(); |
21
|
|
|
|
22
|
4 |
|
if ($request->enableCsrfValidation) { |
23
|
1 |
|
$this->clientOptions['uploadImageFields'][$request->csrfParam] = $request->getCsrfToken(); |
24
|
1 |
|
$this->clientOptions['uploadFileFields'][$request->csrfParam] = $request->getCsrfToken(); |
25
|
1 |
|
} |
26
|
|
|
|
27
|
4 |
|
parent::init(); |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** @inheritdoc */ |
31
|
4 |
|
public function run() |
32
|
|
|
{ |
33
|
4 |
|
$this->registerClientScripts(); |
34
|
|
|
|
35
|
4 |
|
if ($this->hasModel()) { |
36
|
1 |
|
return Html::activeTextarea($this->model, $this->attribute, $this->options); |
37
|
|
|
} else { |
38
|
3 |
|
return Html::textarea($this->name, $this->value, $this->options); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register widget asset. |
44
|
|
|
*/ |
45
|
4 |
|
protected function registerClientScripts() |
46
|
|
|
{ |
47
|
4 |
|
$view = $this->getView(); |
48
|
|
|
/** @var RedactorAsset $asset */ |
49
|
4 |
|
$asset = \Yii::$container->get(RedactorAsset::class); |
50
|
4 |
|
forward_static_call([$asset, 'register'], $view); |
51
|
|
|
|
52
|
4 |
|
if (isset($this->clientOptions['lang'])) { |
53
|
2 |
|
$asset->language = $this->clientOptions['lang']; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
4 |
|
if (isset($this->clientOptions['plugins']) && !empty($this->clientOptions['plugins'])) { |
57
|
2 |
|
$asset->plugins = $this->clientOptions['plugins']; |
58
|
2 |
|
} |
59
|
|
|
|
60
|
4 |
|
$id = $this->options['id']; |
61
|
4 |
|
$settings = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : ''; |
62
|
|
|
|
63
|
4 |
|
$view->registerJs("jQuery('#$id').redactor($settings);"); |
64
|
4 |
|
} |
65
|
|
|
} |
66
|
|
|
|