Completed
Push — develop ( 0d3736...607e7f )
by Nate
09:29
created

EditUserAssociation::getTriggerHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 0
cts 45
cp 0
rs 9.1127
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://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\elements\actions;
10
11
use Craft;
12
use craft\base\ElementAction;
13
use craft\helpers\Json;
14
use flipbox\organizations\objects\OrganizationMutatorTrait;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class EditUserAssociation extends ElementAction
21
{
22
    use OrganizationMutatorTrait;
23
24
    /**
25
     * @return array
26
     */
27
    public function settingsAttributes(): array
28
    {
29
        return array_merge(
30
            parent::settingsAttributes(),
31
            [
32
                'organization'
33
            ]
34
        );
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function getTriggerLabel(): string
41
    {
42
        return Craft::t('organizations', 'Edit');
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function getTriggerHtml()
49
    {
50
        $type = Json::encode(static::class);
51
        $organizationId = $this->getOrganizationId();
52
53
        $js = <<<EOT
54
(function()
55
{
56
57
    var trigger = new Craft.NestedIndexElementActionTrigger(
58
        index_nested_index_organization_users,
59
        {
60
            type: {$type},
61
            batch: false,
62
            validateSelection: function(\$selectedItems)
63
            {
64
                return Garnish.hasAttr(\$selectedItems.find('.element'), 'data-editable');
65
            },
66
            activate: function(\$selectedItems)
67
            {
68
                var \$element = \$selectedItems.find('.element:first');
69
    
70
                if (index_nested_index_organization_users.viewMode === 'table') {
71
                    new Craft.UserAssociationEditor(\$element, {
72
                        params: {
73
                            organization: {$organizationId},
74
                            includeTableAttributesForSource: 'organizations:' + 
75
                                index_nested_index_organization_users.sourceKey
76
                        },
77
                        onSaveElement: $.proxy(function(response) {
78
                            if (response.tableAttributes) {
79
                                index_nested_index_organization_users.view._updateTableAttributes(
80
                                    \$element, 
81
                                    response.tableAttributes
82
                                );
83
                            }
84
                        }, this)
85
                    });
86
                } else {
87
                    new Craft.UserAssociationEditor(\$element);
88
                }
89
            }
90
        }
91
    );
92
})();
93
EOT;
94
95
        Craft::$app->getView()->registerJs($js);
96
    }
97
}
98