TreeWidget::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace devgroup\JsTreeWidget\widgets;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\base\Widget;
8
use yii\helpers\Html;
9
use yii\helpers\Json;
10
use yii\helpers\Url;
11
use yii\web\JsExpression;
12
use yii\helpers\ArrayHelper;
13
use yii\web\View;
14
15
/**
16
 * JsTree widget for Yii Framework 2
17
 */
18
class TreeWidget extends Widget
19
{
20
21
    const TREE_TYPE_ADJACENCY = 'adjacency';
22
    const TREE_TYPE_NESTED_SET = 'nested-set';
23
24
    public $treeType = self::TREE_TYPE_ADJACENCY;
25
    /**
26
     * @var array Enabled jsTree plugins
27
     * @see http://www.jstree.com/plugins/
28
     */
29
    public $plugins = [
30
        'wholerow',
31
        'contextmenu',
32
        'dnd',
33
        'types',
34
        'state',
35
    ];
36
37
    /**
38
     * @var array Configuration for types plugin
39
     * @see http://www.jstree.com/api/#/?f=$.jstree.defaults.types
40
     */
41
    public $types = [
42
        'show' => [
43
            'icon' => 'fa fa-file-o',
44
        ],
45
        'list' => [
46
            'icon' => 'fa fa-list',
47
        ],
48
    ];
49
50
    /**
51
     * Context menu actions configuration.
52
     * @var array
53
     */
54
    public $contextMenuItems = [];
55
56
    /**
57
     * Various options for jsTree plugin. Will be merged with default options.
58
     * @var array
59
     */
60
    public $options = [];
61
62
    /**
63
     * Route to action which returns json data for tree
64
     * @var array
65
     */
66
    public $treeDataRoute = null;
67
68
    /**
69
     * Translation category for Yii::t() which will be applied to labels.
70
     * If translation is not needed - use false.
71
     */
72
    public $menuLabelsTranslationCategory = false;
73
74
    /**
75
     * JsExpression for action(callback function) on double click. You can use JsExpression or make custom expression.
76
     * Warning! Callback function differs from native jsTree function - it consumes only one attribute - node(similar to contextmenu action).
77
     * Use false if no action needed.
78
     * @var bool|JsExpression
79
     */
80
    public $doubleClickAction = false;
81
82
    /** @var bool|array route to change parent action (applicable to Adjacency List only) */
83
    public $changeParentAction = false;
84
85
    /** @var bool|array route to reorder action */
86
    public $reorderAction = false;
87
88
    /** @var bool plugin config option for allow multiple nodes selections or not */
89
    public $multiSelect = false;
90
91
    /** @var string Default labels translation category */
92
    private $defaultTranslationCategory = 'jstw.defaults';
93
94
    /** @var array selected nodes */
95
    public $selectedNodes = [];
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function init()
101
    {
102
        if (false === $this->menuLabelsTranslationCategory) {
103
            $this->menuLabelsTranslationCategory = $this->defaultTranslationCategory;
0 ignored issues
show
Documentation Bug introduced by
The property $menuLabelsTranslationCategory was declared of type boolean, but $this->defaultTranslationCategory is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
104
        }
105
        self::registerTranslations();
106
        parent::init();
107
    }
108
109
    public static function registerTranslations()
110
    {
111
        Yii::$app->i18n->translations['jstw*'] = [
112
            'class' => 'yii\i18n\PhpMessageSource',
113
            'basePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'messages',
114
        ];
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function run()
121
    {
122
        if (!is_array($this->treeDataRoute)) {
123
            throw new InvalidConfigException("Attribute treeDataRoute is required to use TreeWidget.");
124
        }
125
126
        if (count($this->selectedNodes) > 0) {
127
            $this->treeDataRoute['selected'] = $this->selectedNodes;
128
        }
129
130
        $options = [
131
            'plugins' => $this->plugins,
132
            'core' => [
133
                'check_callback' => true,
134
                'multiple' => $this->multiSelect,
135
                'data' => [
136
                    'url' => new JsExpression(
137
                        "function (node) {
138
                            return " . Json::encode(Url::to($this->treeDataRoute)) . ";
139
                        }"
140
                    ),
141
                    'success' => new JsExpression(
142
                        "function (node) {
143
                            return { 'id' : node.id };
144
                        }"
145
                    ),
146
                    'data' => new JsExpression(
147
                        "function (node) {
148
                        return { 'id' : node.id };
149
                        }"
150
                    ),
151
                    'error' => new JsExpression(
152
                        "function ( o, textStatus, errorThrown ) {
153
                            alert(o.responseText);
154
                        }"
155
                    )
156
                ]
157
            ]
158
        ];
159
160
        // merge with attribute-provided options
161
        $options = ArrayHelper::merge($options, $this->options);
162
        if (false === empty($this->contextMenuItems)) {
163
            if (!in_array('contextmenu', $this->plugins)) {
164
                // add missing contextmenu plugin
165
                $options['plugins'] = ['contextmenu'];
166
            }
167
            $functionName = $this->getId() . 'ContextMenu';
168
            $options['contextmenu'] = ['items' => new JsExpression($functionName), 'select_node' => false];
169
            $this->contextMenuOptions($functionName);
170
        }
171
        $options = Json::encode($options);
172
        $this->getView()->registerAssetBundle('devgroup\JsTreeWidget\widgets\JsTreeAssetBundle');
173
174
        $doubleClick = '';
175
        if ($this->doubleClickAction !== false) {
176
            $doubleClick = "
177
            jsTree_{$this->getId()}.on('dblclick.jstree', function (e) {
178
                var node = $(e.target).closest('.jstree-node').children('.jstree-anchor');
179
                var callback = " . $this->doubleClickAction . ";
180
                callback(node);
181
                return false;
182
            });\n";
183
        }
184
        $treeJs = $this->prepareJs();
185
        $this->getView()->registerJs("
186
        var jsTree_{$this->getId()} = \$('#{$this->getId()}').jstree($options);
187
        $doubleClick $treeJs", View::POS_READY);
188
        return Html::tag('div', '', ['id' => $this->getId()]);
189
    }
190
191
    /**
192
     * @param $functionName
193
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
194
     */
195
    private function contextMenuOptions($functionName)
196
    {
197
        $items = [];
198
        $conditionItems = "";
199
        foreach ($this->contextMenuItems as $index => $item) {
200
            $item['label'] = Yii::t($this->menuLabelsTranslationCategory, $item['label']);
0 ignored issues
show
Documentation introduced by
$this->menuLabelsTranslationCategory is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
201
            if (false === empty($item['showWhen'])) {
202
                if (true === is_array($item['showWhen'])) {
203
                    $condition = [];
204
                    foreach ($item['showWhen'] as $key => $value) {
205
                        $key = (false !== strpos($key, 'data-')) ? $key : 'data-' . $key;
206
                        $condition[] = "node.hasOwnProperty('a_attr') && node.a_attr['$key'] == {$value}";
207
                    }
208
                    $condition = implode(' && ', $condition);
209
                } else {
210
                    $condition = $item['showWhen'];
211
                }
212
                unset($item['showWhen']);
213
                $item = Json::encode($item);
214
                $conditionItems .= new JsExpression("
215
                if ({$condition}) {
216
                    items.{$index} = $item;
217
                }
218
                ");
219
            } else {
220
                $items[$index] = $item;
221
            }
222
        }
223
        $items = Json::encode($items);
224
        $js = <<<JS
225
            function $functionName(node) {
226
                var items = $items;
227
                $conditionItems
228
                return items;
229
            }
230
JS;
231
        $this->view->registerJs($js, View::POS_HEAD);
232
    }
233
234
    /**
235
     * Prepares js according to given tree type
236
     *
237
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
238
     */
239
    private function prepareJs()
240
    {
241
        switch ($this->treeType) {
242
            case self::TREE_TYPE_ADJACENCY :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
243
                return $this->adjacencyJs();
244
            case self::TREE_TYPE_NESTED_SET :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
245
                return $this->nestedSetJs();
246
        }
247
    }
248
249
    /**
250
     * @return string
251
     */
252
    private function adjacencyJs()
253
    {
254
        $changeParentJs = '';
255
        if ($this->changeParentAction !== false) {
256
            $changeParentUrl = is_array($this->changeParentAction) ? Url::to($this->changeParentAction) : $this->changeParentAction;
257
            $changeParentJs = <<<JS
258
             jsTree_{$this->getId()}.on('move_node.jstree', function(e, data) {
259
                var \$this = $(this);
260
                $.get('$changeParentUrl', {
261
                        'id': data.node.id,
262
                        'parent_id': data.parent
263
                    }, "json")
264
                    .done(function (data) {
265
                        if ('undefined' !== typeof(data.error)) {
266
                            alert(data.error);
267
                        }
268
                        \$this.jstree('refresh');
269
                    })
270
                    .fail(function ( o, textStatus, errorThrown ) {
271
                        alert(o.responseText);
272
                    });
273
                return false;
274
            });
275
JS;
276
        }
277
278
        $reorderJs = '';
279
        if ($this->reorderAction !== false) {
280
            $reorderUrl = is_array($this->reorderAction) ? Url::to($this->reorderAction) : $this->reorderAction;
281
            $reorderJs = <<<JS
282
            jsTree_{$this->getId()}.on('move_node.jstree', function(e, data) {
283
                var params = [];
284
                var \$this = $(this);
285
                $('.jstree-node').each(function(i, e) {
286
                    params[e.id] = i;
287
                });
288
                $.post('$reorderUrl', {
289
                    'order':params,
290
                    'id': data.node.id
291
                     },
292
                      "json")
293
                    .done(function (data) {
294
                        if ('undefined' !== typeof(data.error)) {
295
                            alert(data.error);
296
                        }
297
                        \$this.jstree('refresh');
298
                    })
299
                    .fail(function ( o, textStatus, errorThrown ) {
300
                        alert(o.responseText);
301
                    });
302
                return false;
303
            });
304
JS;
305
        }
306
        return $changeParentJs . "\n" . $reorderJs . "\n";
307
    }
308
309
    /**
310
     * @return string
311
     */
312
    private function nestedSetJs()
313
    {
314
        $js = "";
315
        if (false !== $this->reorderAction || false !== $this->changeParentAction) {
316
            $action = $this->reorderAction ?: $this->changeParentAction;
317
            $url = is_array($action) ? Url::to($action) : $action;
318
            $js = <<<JS
319
                jsTree_{$this->getId()}.on('move_node.jstree', function(e, data) {
320
                    var \$this = $(this),
321
                        \$parent = \$this.jstree(true).get_node(data.parent),
322
                        \$oldParent = \$this.jstree(true).get_node(data.old_parent),
323
                        siblings = \$parent.children || {};
324
                    $.post('$url', {
325
                            'node_id' : data.node.id,
326
                            'parent': data.parent,
327
                            'position': data.position,
328
                            'old_parent': data.old_parent,
329
                            'old_position': data.old_position,
330
                            'is_multi': data.is_multi,
331
                            'siblings': siblings
332
                         }, "json")
333
                         .done(function (data) {
334
                            if ('undefined' !== typeof(data.error)) {
335
                                alert(data.error);
336
                            }
337
                            \$this.jstree('refresh');
338
                         })
339
                         .fail(function ( o, textStatus, errorThrown ) {
340
                            alert(o.responseText);
341
                         });
342
                    return false;
343
                });
344
JS;
345
        }
346
        return $js . "\n";
347
    }
348
}
349