UserIndexesController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 118
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 55 1
A elementResponseData() 0 33 2
A includeActions() 0 4 1
A getOrganizationFromRequest() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\cp\controllers;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\controllers\ElementIndexesController;
14
use craft\elements\User;
15
use craft\events\RegisterElementHtmlAttributesEvent;
16
use flipbox\organizations\events\handlers\RegisterOrganizationUserElementActions;
17
use flipbox\organizations\events\handlers\RegisterOrganizationUserElementDefaultTableAttributes;
18
use flipbox\organizations\events\handlers\RegisterOrganizationUserElementTableAttributes;
19
use flipbox\organizations\events\handlers\SetOrganizationUserElementTableAttributeHtml;
20
use yii\base\Event;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class UserIndexesController extends ElementIndexesController
27
{
28
    /**
29
     * @inheritdoc
30
     */
31
    public function init()
32
    {
33
        Event::on(
34
            User::class,
35
            User::EVENT_REGISTER_ACTIONS,
36
            [
37
                RegisterOrganizationUserElementActions::class,
38
                'handle'
39
            ]
40
        );
41
42
        // Add attributes the user index
43
        Event::on(
44
            User::class,
45
            User::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES,
46
            [
47
                RegisterOrganizationUserElementDefaultTableAttributes::class,
48
                'handle'
49
            ]
50
        );
51
52
        // Add attributes the user index
53
        Event::on(
54
            User::class,
55
            User::EVENT_REGISTER_TABLE_ATTRIBUTES,
56
            [
57
                RegisterOrganizationUserElementTableAttributes::class,
58
                'handle'
59
            ]
60
        );
61
62
        // Add 'organizations' on the user html element
63
        Event::on(
64
            User::class,
65
            User::EVENT_SET_TABLE_ATTRIBUTE_HTML,
66
            [
67
                SetOrganizationUserElementTableAttributeHtml::class,
68
                'handle'
69
            ]
70
        );
71
72
        // Add 'organizations' on the user html element
73
        Event::on(
74
            User::class,
75
            User::EVENT_REGISTER_HTML_ATTRIBUTES,
76
            function (RegisterElementHtmlAttributesEvent $event) {
77
                $event->htmlAttributes['data-organization'] = $event->data['organization'] ?? null;
78
            },
79
            [
80
                'organization' => $this->getOrganizationFromRequest()
81
            ]
82
        );
83
84
        parent::init();
85
    }
86
87
    /**
88
     * Returns the element data to be returned to the client.
89
     *
90
     * @param bool $includeContainer Whether the element container should be included in the response data
91
     * @param bool $includeActions Whether info about the available actions should be included in the response data
92
     * @return array
93
     */
94
    protected function elementResponseData(bool $includeContainer, bool $includeActions): array
95
    {
96
        $responseData = [];
97
98
        $view = $this->getView();
99
100
        // Get the action head/foot HTML before any more is added to it from the element HTML
101
        if ($includeActions) {
102
            $responseData['actions'] = $this->actionData();
103
            $responseData['actionsHeadHtml'] = $view->getHeadHtml();
104
            $responseData['actionsFootHtml'] = $view->getBodyHtml();
105
        }
106
107
        $disabledElementIds = Craft::$app->getRequest()->getParam('disabledElementIds', []);
108
        $showCheckboxes = !empty($this->actions);
109
        /** @var string|ElementInterface $elementType */
110
        $elementType = $this->elementType;
111
112
        $responseData['html'] = $elementType::indexHtml(
113
            $this->elementQuery,
114
            $disabledElementIds,
115
            $this->viewState,
116
            'organizations', // Changed this
117
            'index', // Changed this
118
            $includeContainer,
119
            $showCheckboxes
120
        );
121
122
        $responseData['headHtml'] = $view->getHeadHtml();
123
        $responseData['footHtml'] = $view->getBodyHtml();
124
125
        return $responseData;
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131
    protected function includeActions(): bool
132
    {
133
        return true;
134
    }
135
136
    /**
137
     * @return mixed
138
     */
139
    private function getOrganizationFromRequest()
140
    {
141
        return Craft::$app->getRequest()->getParam('organization');
142
    }
143
}
144