Completed
Push — master ( 64cb63...78a26b )
by vistart
09:14
created

SetUpForm::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
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\user\User;
16
use rhosocial\organization\Organization;
17
use Yii;
18
use yii\base\InvalidConfigException;
19
use yii\base\Model;
20
21
/**
22
 * @version 1.0
23
 * @author vistart <[email protected]>
24
 */
25
class SetUpForm extends Model
26
{
27
    public $name;
28
    public $nickname = '';
29
    public $gravatar_type = 0;
30
    public $gravatar = '';
31
    public $timezone;
32
    public $description = '';
33
    /**
34
     * @var Organization 
35
     */
36
    private $_parent;
37
    /**
38
     * @var User 
39
     */
40
    private $_user;
41
42
    /**
43
     * Finds user.
44
     *
45
     * @return User|null
46
     * @throws InvalidConfigException
47
     */
48
    public function getUser()
49
    {
50
        if ($this->_user instanceof Yii::$app->user->identityClass) {
51
            return $this->_user;
52
        }
53
        throw new InvalidConfigException('User Class Invalid.');
54
    }
55
56
    /**
57
     * Set user.
58
     * @param User $user
59
     * @return boolean
60
     */
61
    public function setUser($user)
62
    {
63
        if ($user instanceof Yii::$app->user->identityClass) {
64
            $this->_user = $user;
65
            return true;
66
        }
67
        $this->_user = null;
68
        return false;
69
    }
70
71
    /**
72
     * Get parent organization or department.
73
     * @return Organization
74
     */
75
    public function getParent()
76
    {
77
        if ($this->_parent instanceof Organization) {
78
            return $this->_parent;
79
        }
80
        return null;
81
    }
82
83
    /**
84
     * Set parent organization or department.
85
     * If you want to set up organization, please set it null.
86
     * @param Organization|string|integer $parent
87
     * @return boolean
88
     */
89
    public function setParent($parent)
90
    {
91
        if (is_numeric($parent) || is_int($parent)) {
92
            $class = $this->getUser()->organizationClass;
93
            $parent = $class::find()->id($parent)->one();
94
        }
95
        if ($parent instanceof Organization) {
96
            $this->_parent = $parent;
97
            return true;
98
        }
99
        $this->_parent = null;
100
        return false;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function attributeLabels()
107
    {
108
        return [
109
            'name' => Yii::t('organization', 'Name'),
110
            'nickname' => Yii::t('user', 'Nickname'),
111
            'gravatar_type' => Yii::t('user', 'Gravatar Type'),
112
            'gravatar' => Yii::t('user', 'Gravatar'),
113
            'timezone' => Yii::t('user', 'Timezone'),
114
            'description' => Yii::t('organization', 'Description'),
115
        ];
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function rules()
122
    {
123
        return [
124
            ['name', 'required'],
125
            [['nickname', 'gravatar', 'description'], 'default', 'value' => ''],
126
            ['gravatar_type', 'default', 'value' => 0],
127
            ['timezone', 'default', 'value' => Yii::$app->timeZone],
128
            [['name', 'nickname', 'gravatar', 'timezone'], 'string', 'max' => 255],
129
            ['description', 'string', 'max' => 65535],
130
            ['gravatar_type', 'integer'],
131
        ];
132
    }
133
134
    /**
135
     * Set up organization.
136
     * You need to make sure that the user who want to set up the organization
137
     * who has the `orgCreator` permission.
138
     * @return boolean
139
     */
140
    public function setUpOrganization()
141
    {
142
        return $this->getUser()->setUpOrganization($this->name, $this->nickname, $this->gravatar_type, $this->gravatar, $this->timezone, $this->description);
143
    }
144
145
    /**
146
     * Set up department.
147
     * You need to make sure that the user who want to set up the department who
148
     * is the creator or administrator of parent one.
149
     * @return boolean
150
     */
151
    public function setUpDepartment()
152
    {
153
        return $this->getUser()->setUpDepartment($this->name, $this->getParent(), $this->nickname, $this->gravatar_type, $this->gravatar, $this->timezone, $this->description);
154
    }
155
156
    /**
157
     *
158
     */
159
    public function init()
160
    {
161
        if(!isset($this->timezone)) {
162
            $this->timezone = Yii::$app->timeZone;
163
        }
164
    }
165
}
166