AddMemberActionColumn::initDefaultButtons()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 14
nc 12
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\grid;
14
15
use rhosocial\user\User;
16
use rhosocial\user\grid\ActionColumn;
17
use rhosocial\organization\Organization;
18
use Yii;
19
use yii\helpers\Url;
20
21
/**
22
 * Class AddMemberActionColumn
23
 * This class is used for GridView, and insert action column in it.
24
 * This class contains only one action: Add Member.
25
 * By default, if the user in the list are already member, the "Add" button will not be displayed.
26
 *
27
 * Typical usage:
28
 * ```php
29
 * echo GridView::widget([
30
 *     'columns' => [
31
 *         ...
32
 *         [
33
 *             'class' => AddMemberActionColumn::class,
34
 *             'organization' => <Organization Instance>, // You must specify organization.
35
 *         ],
36
 *     ],
37
 * ]);
38
 * ```
39
 *
40
 * @package rhosocial\organization\grid
41
 * @version 1.0
42
 * @author vistart <[email protected]>
43
 */
44
class AddMemberActionColumn extends ActionColumn
45
{
46
    /**
47
     * @var string
48
     */
49
    public $template = '{add}';
50
51
    /**
52
     * @var array This array should contain two elements:
53
     * - iconName
54
     * - buttonAddOptions
55
     * @see initDefaultButton()
56
     */
57
    public $addButtonConfig;
58
59
    /**
60
     * @var bool If you want to confirm before adding, you should set true.
61
     */
62
    public $addConfirm = false;
63
64
    /**
65
     * @var string If you want to modify confirmation text, please set it with your own.
66
     */
67
    public $addConfirmText;
68
69
    /**
70
     * @var Organization The organization which the user tend to join in.
71
     */
72
    public $organization;
73
74
    /**
75
     * Initializes the default button rendering callbacks.
76
     */
77
    protected function initDefaultButtons()
78
    {
79
        $buttonAddOptions = [
80
            'data-method' => 'post',
81
            'title' => Yii::t('organization', 'Add'),
82
            'aria-label' => Yii::t('organization', 'Add'),
83
        ];
84
        if ($this->addConfirm) {
85
            if (!isset($this->addConfirmText)) {
86
                $this->addConfirmText = Yii::t('organization', 'Are you sure to add this user to the organization / department?');
87
            }
88
            $buttonAddOptions['data-confirm'] = $this->addConfirmText;
89
        }
90
        if (!isset($this->addButtonConfig['iconName'])) {
91
            $this->addButtonConfig['iconName'] = false;
92
        }
93
        if (!isset($this->addButtonConfig['buttonAddOptions'])) {
94
            $this->addButtonConfig['buttonAddOptions'] = $buttonAddOptions;
95
        }
96
        $this->initDefaultButton('add', $this->addButtonConfig['iconName'], $this->addButtonConfig['buttonAddOptions']);
97
    }
98
99
    public function init()
100
    {
101
        parent::init();
102
        if (!isset($this->header)) {
103
            $this->header = Yii::t('user', 'Action');
104
        }
105
        $this->initUrlCreator();
106
        $this->initVisibleButtons();
107
    }
108
109
    protected function initUrlCreator()
110
    {
111
        if (isset($this->urlCreator)) {
112
            return;
113
        }
114
        $this->urlCreator = function ($action, $model, $key, $index, AddMemberActionColumn $column) {
115
            /* @var $model User */
116
            if ($action == 'add') {
117
                return Url::to(['add-member', 'org' => $column->organization->getID(), 'u' => $model->id]);
118
            }
119
            return '#';
120
        };
121
    }
122
123
    protected function initVisibleButtons()
124
    {
125
        if (!empty($this->visibleButtons)) {
126
            return;
127
        }
128
        $this->visibleButtons = [
129
            'add' => function ($model, $key, $index) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
                return !$this->organization->hasMember($model->id);
131
            }
132
        ];
133
    }
134
}
135