Passed
Push — master ( 21ec97...663af3 )
by vistart
20:03
created

SettingsForm::init()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
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\forms;
14
15
use rhosocial\organization\Organization;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\base\Model;
19
use yii\web\ServerErrorHttpException;
20
21
/**
22
 * Class SettingsForm
23
 * @package rhosocial\organization\forms
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class SettingsForm extends Model
28
{
29
    /**
30
     * @var string
31
     */
32
    public $exclude_other_members;
33
34
    /**
35
     * @var string
36
     */
37
    public $disallow_member_join_other;
38
39
    /**
40
     * @var string
41
     */
42
    public $only_accept_current_org_member;
43
44
    /**
45
     * @var string
46
     */
47
    public $only_accept_superior_org_member;
48
49
    /**
50
     * @var string
51
     */
52
    public $join_password;
53
54
    /**
55
     * @var string
56
     */
57
    public $join_ip_address;
58
59
    /**
60
     * @var string
61
     */
62
    public $join_entrance_url;
63
64
    const SCENARIO_ORGANIZATION = 'organization';
65
    const SCENARIO_DEPARTMENT = 'department';
66
67
    /**
68
     * @var Organization
69
     */
70
    public $organization;
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function init()
76
    {
77
        if (!$this->organization) {
78
            throw new InvalidConfigException('Invalid Organization Model.');
79
        }
80
        $this->scenario = $this->organization->isOrganization() ? static::SCENARIO_ORGANIZATION : static::SCENARIO_DEPARTMENT;
81
        $this->loadSettings();
82
    }
83
84
    /**
85
     * Load settings.
86
     */
87
    protected function loadSettings()
88
    {
89
        if ($this->organization->isOrganization()) {
90
            $this->exclude_other_members = $this->organization->isExcludeOtherMembers ? '1' : '0';
91
            $this->disallow_member_join_other = $this->organization->isDisallowMemberJoinOther ? '1' : '0';
92
        } elseif ($this->organization->isDepartment()) {
93
            $this->only_accept_current_org_member = $this->organization->isOnlyAcceptCurrentOrgMember ? '1' : '0';
94
            $this->only_accept_superior_org_member = $this->organization->isOnlyAcceptSuperiorOrgMember ? '1' : '0';
95
        }
96
        $this->join_password = $this->organization->joinPassword;
97
        $this->join_ip_address = $this->organization->joinIpAddress;
98
        $this->join_entrance_url = $this->organization->joinEntranceUrl;
99
    }
100
101
    /**
102
     * Submit settings.
103
     */
104
    public function submit()
105
    {
106
        try {
107
            if ($this->organization->isOrganization()) {
108
                if ($this->exclude_other_members != ($this->organization->isExcludeOtherMembers ? '1' : '0')) {
109
                    $this->organization->isExcludeOtherMembers = ($this->exclude_other_members == '1');
110
                }
111
                if ($this->disallow_member_join_other != ($this->organization->isDisallowMemberJoinOther ? '1' : '0')) {
112
                    $this->organization->isDisallowMemberJoinOther = ($this->disallow_member_join_other == '1');
113
                }
114
            } elseif ($this->organization->isDepartment()) {
115
                if ($this->only_accept_current_org_member != ($this->organization->isOnlyAcceptCurrentOrgMember ? '1' : '0')) {
116
                    $this->organization->isOnlyAcceptCurrentOrgMember = ($this->only_accept_current_org_member == '1');
117
                }
118
                if ($this->only_accept_superior_org_member != ($this->organization->isOnlyAcceptSuperiorOrgMember ? '1' : '0')) {
119
                    $this->organization->isOnlyAcceptSuperiorOrgMember = ($this->only_accept_superior_org_member == '1');
120
                }
121
            }
122
            if ($this->join_password != $this->organization->joinPassword) {
123
                $this->organization->joinPassword = $this->join_password;
124
            }
125
            if ($this->join_ip_address != $this->organization->joinIpAddress) {
126
                $this->organization->joinIpAddress = $this->join_ip_address;
127
            }
128
            if ($this->join_entrance_url != $this->organization->joinEntranceUrl) {
129
                $this->organization->joinEntranceUrl = $this->join_entrance_url;
130
            }
131
        } catch (\Exception $ex) {
132
            throw new ServerErrorHttpException($ex->getMessage());
133
        }
134
        return true;
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function attributeLabels()
141
    {
142
        return [
143
            'exclude_other_members' => Yii::t('organization', 'Exclude other members'),
144
            'disallow_member_join_other' => Yii::t('organization', 'Disallow members to join other'),
145
            'only_accept_current_org_member' => Yii::t('organization', 'Only accept organization members'),
146
            'only_accept_superior_org_member' => Yii::t('organization', 'Only accept superior members'),
147
            'join_password' => Yii::t('organization', 'Password'),
148
            'join_ip_address' => Yii::t('organization', 'IP Address'),
149
            'join_entrance_url' => Yii::t('organization', 'Entrance URL'),
150
        ];
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function attributeHints()
157
    {
158
        $topName = $this->organization->isDepartment() ? $this->organization->topOrganization->profile->name . ' (' . $this->organization->topOrganization->getID() . ')' : '';
159
        $parentName = $this->organization->isDepartment() ? $this->organization->parent->profile->name . ' (' . $this->organization->parent->getID() . ')' : '';
0 ignored issues
show
Bug introduced by
The property profile does not seem to exist in rhosocial\base\models\traits\SelfBlameableTrait.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like getID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
160
        return [
161
            'exclude_other_members' => Yii::t('organization', 'This organization does not allow other organizations and their subordinates\' members to join.') . "\n" . Yii::t('organization', 'All members of the other organizations (including their subordinates) who have joined this organization (including subordinate departments) are not affected.'),
162
            'disallow_member_join_other' => Yii::t('organization', 'This organization does not allow the organization and its subordinates\' members to join other organizations or their subordinates.') . "\n" . Yii::t('organization', 'All members of this organization (including subordinate departments) who have joined other organizations (including their subordinates) are not affected.') . "\n" . Yii::t('organization', 'If this option is enabled, all members of the organization (including subordinate departments) who have the "Set Up Organization" permission will not be able to set up a new organization.'),
163
            'only_accept_current_org_member' => Yii::t('organization', 'This department is only accepted by members of the organization.') . "\n" . Yii::t('organization', 'That is to say, only the members of {name} are accepted.', ['name' => $topName]),
164
            'only_accept_superior_org_member' => Yii::t('organization', 'This department only accepts members of the parent organization or department.') . "\n" . Yii::t('organization', 'That is to say, only the members of {name} are accepted.', ['name' => $parentName]),
165
            'join_entrance_url' => Yii::t('organization', 'Only the users through the above entrance URL can join this organization / department proactively.') . Yii::t('organization', 'This condition needs to be unique and can not be the same as the entrance URL for other organizations / departments.'),
166
            'join_password' => Yii::t('organization', 'Only the users by entering the above password can join this organization / department proactively.'),
167
            'join_ip_address' => Yii::t('organization', 'Only the users from the above IP address (segment) can join the organization / department proactively.'),
168
        ];
169
    }
170
171
    /**
172
     * @inheritdoc
173
     */
174
    public function rules()
175
    {
176
        $orgClass = get_class($this->organization);
177
        return [
178
            [['exclude_other_members', 'disallow_member_join_other', 'only_accept_current_org_member', 'only_accept_superior_org_member'], 'boolean', 'trueValue' => '1', 'falseValue' => '0'],
179
            [['join_password', 'join_entrance_url'], 'string', 'max' => 255],
180
            ['join_ip_address', 'ip', 'subnet' => null, 'normalize' => true],
181
            ['join_entrance_url', 'setting_unique', 'skipOnError' => false, 'params' => ['item' => $orgClass::SETTING_ITEM_JOIN_ENTRANCE_URL]],
182
        ];
183
    }
184
185
    /**
186
     * Check whether the setting is unique.
187
     * @param string $attribute the attribute currently being validated
188
     * @param mixed $params the value of the "params" given in the rule
189
     * @param \yii\validators\InlineValidator related InlineValidator instance.
190
     */
191
    public function setting_unique($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $validator 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...
192
    {
193
        $value = (string)$this->$attribute;
194
        if (empty($value)) {
195
            return;
196
        }
197
198
        $class = $this->organization->organizationSettingClass;
199
        if (empty($class) || !is_string($class)) {
200
            return;
201
        }
202
203
        $result = $class::find()->andWhere([
204
            $this->organization->getNoInitOrganizationSetting()->idAttribute => $params['item'],
205
            $this->organization->getNoInitOrganizationSetting()->contentAttribute => $value,
206
        ])->exists();
207
        if ($result) {
208
            $this->addError($attribute, Yii::t('organization', "{value} already exists.", ['value' => $value]));
209
        }
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215
    public function scenarios()
216
    {
217
        return [
218
            static::SCENARIO_ORGANIZATION => ['exclude_other_members', 'disallow_member_join_other', 'join_password', 'join_entrance_url', 'join_ip_address'],
219
            static::SCENARIO_DEPARTMENT => ['only_accept_current_org_member', 'only_accept_superior_org_member', 'join_password', 'join_entrance_url', 'join_ip_address'],
220
        ];
221
    }
222
}
223