Blame view

common/components/ckeditor/CKEditor.php 5.46 KB
c7f222e2   Artem   first
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  <?php
  /**
   * Date: 17.01.14
   * Time: 1:18
   */
  
  namespace common\components\ckeditor;
  
  
  use yii\helpers\ArrayHelper;
  use yii\helpers\Html;
  use yii\web\JsExpression;
  use yii\web\View;
  use yii\helpers\Json;
  use yii\widgets\InputWidget;
  
  class CKEditor extends InputWidget{
      public $editorOptions = [];
      public $containerOptions = [];
      private $_inline = false;
  
      public function init()
      {
          parent::init();
  
          if (array_key_exists('inline', $this->editorOptions)) {
              $this->_inline = $this->editorOptions['inline'];
              unset($this->editorOptions['inline']);
          }
  
          if (array_key_exists('preset', $this->editorOptions)) {
              if($this->editorOptions['preset'] == 'basic'){
                  $this->presetBasic();
              }elseif($this->editorOptions['preset'] == 'standard'){
                  $this->presetStandard();
              }elseif($this->editorOptions['preset'] == 'full'){
                  $this->presetFull();
              }
              unset($this->editorOptions['preset']);
          }
  
          if($this->_inline && !isset($this->editorOptions['height']))
              $this->editorOptions['height'] = 100;
  
          if($this->_inline && !isset($this->containerOptions['id']))
              $this->containerOptions['id'] = $this->id.'_inline';
      }
  
      private function presetBasic(){
          $options['height'] = 100;
  
          $options['toolbarGroups'] = [
              ['name' => 'undo'],
              ['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
              ['name' => 'colors'],
              ['name' => 'links', 'groups' => ['links', 'insert']],
              ['name' => 'others','groups' => ['others', 'about']],
          ];
          $options['removeButtons'] = 'Subscript,Superscript,Flash,Table,HorizontalRule,Smiley,SpecialChar,PageBreak,Iframe';
          $options['removePlugins'] = 'elementspath';
          $options['resize_enabled'] = false;
  
  
          $this->editorOptions = ArrayHelper::merge($options, $this->editorOptions);
      }
  
      private function presetStandard(){
          $options['height'] = 300;
  
          $options['toolbarGroups'] = [
              ['name' => 'clipboard', 'groups' => ['mode','undo', 'selection', 'clipboard','doctools']],
              ['name' => 'editing', 'groups' => ['tools', 'about']],
              '/',
              ['name' => 'paragraph', 'groups' => ['templates', 'list', 'indent', 'align']],
              ['name' => 'insert'],
              '/',
              ['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
              ['name' => 'colors'],
              ['name' => 'links'],
              ['name' => 'others'],
          ];
  
          $options['removeButtons'] = 'Smiley,Iframe';
  
          if($this->_inline){
              $options['extraPlugins'] = 'sourcedialog';
              $options['removePlugins'] = 'sourcearea';
          }
  
          $this->editorOptions = ArrayHelper::merge($options, $this->editorOptions);
      }
  
  
  
      private function presetFull(){
          $options['height'] = 400;
  
          $options['toolbarGroups'] = [
              ['name' => 'clipboard', 'groups' => ['mode','undo', 'selection', 'clipboard', 'doctools']],
              ['name' => 'editing', 'groups' => ['find', 'spellchecker', 'tools', 'about']],
              '/',
              ['name' => 'paragraph', 'groups' => ['templates', 'list', 'indent', 'align']],
              ['name' => 'forms'],
              '/',
              ['name' => 'styles'],
              ['name' => 'blocks'],
              '/',
              ['name' => 'basicstyles', 'groups' => ['basicstyles', 'colors','cleanup']],
              ['name' => 'links', 'groups' => ['links', 'insert']],
              ['name' => 'others'],
          ];
  
          if($this->_inline){
              $options['extraPlugins'] = 'sourcedialog';
              $options['removePlugins'] = 'sourcearea';
          }
  
          $this->editorOptions = ArrayHelper::merge($options, $this->editorOptions);
      }
  
      public function run()
      {
          Assets::register($this->getView());
  
          echo Html::beginTag('div', $this->containerOptions);
          if ($this->hasModel()) {
              echo Html::activeTextarea($this->model, $this->attribute, $this->options);
          } else {
              echo Html::textarea($this->name, $this->value, $this->options);
          }
  
          echo Html::endTag('div');
  		$js = [
  			'mihaildev.ckEditor.registerOnChange('.Json::encode($this->options['id']).');'
  		];
  
  		if(isset($this->editorOptions['filebrowserUploadUrl']))
  			$js[] = "mihaildev.ckEditor.registerCsrf();";
  
  		if(!isset($this->editorOptions['on']['instanceReady']))
  			$this->editorOptions['on']['instanceReady'] = new JsExpression("function( ev ){".implode(' ', $js)."}");
  
          if($this->_inline){
              $JavaScript = "CKEDITOR.inline(";
              $JavaScript .= Json::encode($this->options['id']);
              $JavaScript .= empty($this->editorOptions) ? '' : ', '.Json::encode($this->editorOptions);
              $JavaScript .= ");";
  
              $this->getView()->registerJs($JavaScript, View::POS_END);
              $this->getView()->registerCss('#'.$this->containerOptions['id'].', #'.$this->containerOptions['id'].' .cke_textarea_inline{height: '.$this->editorOptions['height'].'px;}');
          }else{
              $JavaScript = "CKEDITOR.replace(";
              $JavaScript .= Json::encode($this->options['id']);
              $JavaScript .= empty($this->editorOptions) ? '' : ', '.Json::encode($this->editorOptions);
              $JavaScript .= ");";
  
              $this->getView()->registerJs($JavaScript, View::POS_END);
          }
      }
  
  }