Completed
Pull Request — master (#42)
by Dmitry
12:11
created

TinyMce::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 9
cp 0.7778
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0438
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
namespace dosamigos\tinymce;
8
9
use yii\helpers\Html;
10
use yii\helpers\Json;
11
use yii\widgets\InputWidget;
12
13
/**
14
 *
15
 * TinyMCE renders a tinyMCE js plugin for WYSIWYG editing.
16
 *
17
 * @author Antonio Ramirez <[email protected]>
18
 * @link http://www.ramirezcobos.com/
19
 * @link http://www.2amigos.us/
20
 */
21
class TinyMce extends InputWidget
22
{
23
    /**
24
     * @var string the language to use. Defaults to null (en).
25
     */
26
    public $language;
27
    /**
28
     * @var array the options for the TinyMCE JS plugin.
29
     * Please refer to the TinyMCE JS plugin Web page for possible options.
30
     * @see http://www.tinymce.com/wiki.php/Configuration
31
     */
32
    public $clientOptions = [];
33
    /**
34
     * @var bool whether to set the on change event for the editor. This is required to be able to validate data.
35
     * @see https://github.com/2amigos/yii2-tinymce-widget/issues/7
36
     */
37
    public $triggerSaveOnBeforeValidateForm = true;
38
39
    /**
40
     * @inheritdoc
41
     */
42 3
    public function init()
43
    {
44 3
        parent::init();
45 2
46 2
        if (empty($this->language)) {
47 1
            $this->language = \Yii::$app->language;
48
        }
49 3
50 3
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 3
    public function run()
56
    {
57 3
        if ($this->hasModel()) {
58 3
            echo Html::activeTextarea($this->model, $this->attribute, $this->options);
59
        } else {
60 3
            echo Html::textarea($this->name, $this->value, $this->options);
61
        }
62 3
        $this->registerClientScript();
63
    }
64 3
65
    /**
66
     * Registers tinyMCE js plugin
67
     */
68
    protected function registerClientScript()
69
    {
70
        $js = [];
71
        $view = $this->getView();
72
73
        TinyMceAsset::register($view);
74 3
75
        $id = $this->options['id'];
76 3
77 3
        $this->clientOptions['selector'] = "#$id";
78 3
        // @codeCoverageIgnoreStart
79 3
        if ($this->language !== null && $this->language !== 'en') {
80 3
            $langFile = "langs/{$this->language}.js";
81 3
            $langAssetBundle = TinyMceLangAsset::register($view);
82
            $langAssetBundle->js[] = $langFile;
83
            $this->clientOptions['language_url'] = $langAssetBundle->baseUrl . "/{$langFile}";
84
        }
85
        // @codeCoverageIgnoreEnd
86
87
        $options = Json::encode($this->clientOptions);
88
89
        $js[] = "tinymce.init($options);";
90
        if ($this->triggerSaveOnBeforeValidateForm) {
91
            $js[] = "$('#{$id}').parents('form').on('beforeValidate', function() { tinymce.triggerSave(); });";
92
        }
93
        $view->registerJs(implode("\n", $js));
94
    }
95
}
96