Completed
Push — master ( b96e2f...be9b65 )
by vistart
19:04
created

SettingsForm   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 3
B loadSettings() 0 10 7
C submit() 0 23 12
A attributeLabels() 0 9 1
A attributeHints() 0 11 3
A rules() 0 6 1
A scenarios() 0 7 1
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
    const SCENARIO_ORGANIZATION = 'organization';
50
    const SCENARIO_DEPARTMENT = 'department';
51
52
    /**
53
     * @var Organization
54
     */
55
    public $organization;
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function init()
61
    {
62
        if (!$this->organization) {
63
            throw new InvalidConfigException('Invalid Organization Model.');
64
        }
65
        $this->scenario = $this->organization->isOrganization() ? static::SCENARIO_ORGANIZATION : static::SCENARIO_DEPARTMENT;
66
        $this->loadSettings();
67
    }
68
69
    /**
70
     * Load settings.
71
     */
72
    protected function loadSettings()
73
    {
74
        if ($this->organization->isOrganization()) {
75
            $this->exclude_other_members = $this->organization->isExcludeOtherMembers ? '1' : '0';
76
            $this->disallow_member_join_other = $this->organization->isDisallowMemberJoinOther ? '1' : '0';
77
        } elseif ($this->organization->isDepartment()) {
78
            $this->only_accept_current_org_member = $this->organization->isOnlyAcceptCurrentOrgMember ? '1' : '0';
79
            $this->only_accept_superior_org_member = $this->organization->isOnlyAcceptSuperiorOrgMember ? '1' : '0';
80
        }
81
    }
82
83
    /**
84
     * Submit settings.
85
     */
86
    public function submit()
87
    {
88
        try {
89
            if ($this->organization->isOrganization()) {
90
                if ($this->exclude_other_members != ($this->organization->isExcludeOtherMembers ? '1' : '0')) {
91
                    $this->organization->isExcludeOtherMembers = ($this->exclude_other_members == '1');
92
                }
93
                if ($this->disallow_member_join_other != ($this->organization->isDisallowMemberJoinOther ? '1' : '0')) {
94
                    $this->organization->isDisallowMemberJoinOther = ($this->disallow_member_join_other == '1');
95
                }
96
            } elseif ($this->organization->isDepartment()) {
97
                if ($this->only_accept_current_org_member != ($this->organization->isOnlyAcceptCurrentOrgMember ? '1' : '0')) {
98
                    $this->organization->isOnlyAcceptCurrentOrgMember = ($this->only_accept_current_org_member == '1');
99
                }
100
                if ($this->only_accept_superior_org_member != ($this->organization->isOnlyAcceptSuperiorOrgMember ? '1' : '0')) {
101
                    $this->organization->isOnlyAcceptSuperiorOrgMember = ($this->only_accept_superior_org_member == '1');
102
                }
103
            }
104
        } catch (\Exception $ex) {
105
            throw new ServerErrorHttpException($ex->getMessage());
106
        }
107
        return true;
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function attributeLabels()
114
    {
115
        return [
116
            'exclude_other_members' => Yii::t('organization', 'Exclude other members'),
117
            'disallow_member_join_other' => Yii::t('organization', 'Disallow members to join other'),
118
            'only_accept_current_org_member' => Yii::t('organization', 'Only accept organization members'),
119
            'only_accept_superior_org_member' => Yii::t('organization', 'Only accept superior members'),
120
        ];
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function attributeHints()
127
    {
128
        $topName = $this->organization->isDepartment() ? $this->organization->topOrganization->profile->name . ' (' . $this->organization->topOrganization->getID() . ')' : '';
129
        $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...
130
        return [
131
            '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.'),
132
            '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.'),
133
            '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]),
134
            '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]),
135
        ];
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function rules()
142
    {
143
        return [
144
            [['exclude_other_members', 'disallow_member_join_other', 'only_accept_current_org_member', 'only_accept_superior_org_member'], 'boolean', 'trueValue' => '1', 'falseValue' => '0'],
145
        ];
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function scenarios()
152
    {
153
        return [
154
            static::SCENARIO_ORGANIZATION => ['exclude_other_members', 'disallow_member_join_other'],
155
            static::SCENARIO_DEPARTMENT => ['only_accept_current_org_member', 'only_accept_superior_org_member'],
156
        ];
157
    }
158
}
159