ChangeOrganizationStatus::isDestructive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\organization\elements\actions;
10
11
use Craft;
12
use craft\base\ElementAction;
13
use craft\elements\db\ElementQueryInterface;
14
use craft\helpers\Json;
15
use flipbox\organization\elements\Organization;
16
use flipbox\organization\Organization as OrganizationPlugin;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class ChangeOrganizationStatus extends ElementAction
23
{
24
25
    public $status;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function getTriggerLabel(): string
31
    {
32
        return Craft::t('organization', 'Change Status');
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public static function isDestructive(): bool
39
    {
40
        return true;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getTriggerHtml()
47
    {
48
        $type = Json::encode(static::class);
49
        $statuses = Json::encode(
50
            OrganizationPlugin::getInstance()->getOrganization()->getStatuses()
51
        );
52
        $redirect = Json::encode(Craft::$app->getSecurity()->hashData('organization'));
53
54
        $js = <<<EOD
55
(function()
56
{
57
    var trigger = new Craft.ElementActionTrigger({
58
        type: {$type},
59
        batch: true,
60
        activate: function(\$selectedItems)
61
        {
62
            var modal = new Craft.ChangeOrganizationStatusModal(Craft.elementIndex.getSelectedElementIds(), {
63
                onSubmit: function()
64
                {
65
                    Craft.elementIndex.submitAction({$type}, Garnish.getPostData(modal.\$container));
66
                    modal.hide();
67
68
                    return false;
69
                },
70
                redirect: {$redirect},
71
                statuses: {$statuses}
72
            });
73
        }
74
    });
75
})();
76
EOD;
77
78
        Craft::$app->getView()->registerJs($js);
79
    }
80
81
    /**
82
     * Performs the action on any elements that match the given criteria.
83
     *
84
     * @param ElementQueryInterface $query The element query defining which elements the action should affect.
85
     *
86
     * @return bool Whether the action was performed successfully.
87
     * @throws Exception
88
     */
89
    public function performAction(ElementQueryInterface $query): bool
90
    {
91
        /** @var Organization[] $organizations */
92
        $organizations = $query->all();
93
94
        $successful = true;
95
96
        // Transfer the users
97
        foreach ($organizations as $organization) {
98
            if (!OrganizationPlugin::getInstance()->getOrganization()->changeStatus(
99
                $organization,
100
                $this->status
101
            )) {
102
                $successful = false;
103
            }
104
        }
105
106
        if ($successful) {
107
            $this->setMessage(Craft::t('organization', 'Organization statuses were changed.'));
108
            return true;
109
        }
110
111
        $this->setMessage(Craft::t('organization', 'Unable to change srganization statuses.'));
112
        return false;
113
    }
114
}
115