ElementIndexesController::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 33
cp 0
rs 9.344
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\controllers;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\events\RegisterElementActionsEvent;
14
use craft\events\RegisterElementSortOptionsEvent;
15
use flipbox\craft\element\lists\ElementList;
16
use flipbox\craft\element\lists\elements\actions\DissociateFromElementAction;
17
use flipbox\craft\element\lists\records\Association;
18
use yii\base\Event;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class ElementIndexesController extends \craft\controllers\ElementIndexesController
25
{
26
    /**
27
     * @inheritdoc
28
     */
29
    public function init()
30
    {
31
        Event::on(
32
            Element::class,
33
            Element::EVENT_REGISTER_ACTIONS,
34
            function (RegisterElementActionsEvent $event) {
35
                $event->actions = [
36
                    [
37
                        'type' => DissociateFromElementAction::class,
38
                        'sourceId' => $event->data['sourceId'] ?? null,
39
                        'fieldId' => $event->data['fieldId'] ?? null
40
                    ]
41
                ];
42
            },
43
            [
44
                'sourceId' => $this->sourceId(),
45
                'fieldId' => $this->fieldId()
46
            ]
47
        );
48
49
        Event::on(
50
            Element::class,
51
            Element::EVENT_REGISTER_SORT_OPTIONS,
52
            function (RegisterElementSortOptionsEvent $event) {
53
                $event->sortOptions[] = [
54
                    'label' => ElementList::t('Field Order'),
55
                    'attribute' => 'field',
56
                    'orderBy' => Association::TABLE_ALIAS . '.sortOrder'
57
                ];
58
            }
59
        );
60
61
        $this->normalizeDisabledElementIds();
62
63
        parent::init();
64
    }
65
66
    /**
67
     * When working with large sets of elements, this may be sent as a comma delimited string
68
     */
69
    protected function normalizeDisabledElementIds()
70
    {
71
        $disabledElementIds = Craft::$app->getRequest()->getBodyParam('disabledElementIds', []);
72
73
        if (is_string($disabledElementIds)) {
74
            $disabledElementIds = explode(",", $disabledElementIds);
75
76
            Craft::$app->getRequest()->setBodyParams(array_merge(
77
                Craft::$app->getRequest()->getBodyParams(),
78
                [
79
                    'disabledElementIds' => $disabledElementIds
80
                ]
81
            ));
82
        }
83
    }
84
85
    /**
86
     * @return mixed
87
     */
88
    protected function sourceId()
89
    {
90
        return Craft::$app->getRequest()->getParam('sourceId');
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    protected function fieldId()
97
    {
98
        return Craft::$app->getRequest()->getParam('fieldId');
99
    }
100
}
101