Completed
Push — master ( c01525...9b654e )
by Alexey
05:15
created

User::beforeSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
/**
4
 * User
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Users;
13
14
class User extends \Model
15
{
16
    public static $cur;
17
    public static $objectName = "Пользователь";
18
    public static $labels = [
19
        'login' => 'Логин',
20
        'mail' => 'E-Mail',
21
        'pass' => 'Пароль',
22
        'parent_id' => 'Пригласивший',
23
        'group_id' => 'Группа',
24
        'role_id' => 'Роль',
25
        'admin_text' => 'Комментарий администратора',
26
        'activation' => 'Код активации',
27
        'blocked' => 'Заблокирован',
28
        'date_last_active' => 'Последняя активность',
29
        'date_create' => 'Дата регистрации',
30
    ];
31
    public static $cols = [
32
        'login' => ['type' => 'text'],
33
        'mail' => ['type' => 'email'],
34
        'pass' => ['type' => 'password'],
35
        'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent'],
36
        'group_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'group'],
37
        'role_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'role'],
38
        'admin_text' => ['type' => 'html'],
39
        'activation' => ['type' => 'text'],
40
        'blocked' => ['type' => 'bool',],
41
        'date_last_active' => ['type' => 'dateTime'],
42
        'date_create' => ['type' => 'dateTime']
43
    ];
44
    public static $dataManagers = [
45
        'manager' => [
46
            'options' => [
47
                'access' => [
48
                    'groups' => [
49
                        3
50
                    ]
51
                ]
52
            ],
53
            'cols' => [
54
                'mail',
55
                'info:first_name',
56
                'info:last_name',
57
                'group_id',
58
                'role_id',
59
                'date_last_active',
60
                'date_create',
61
            ],
62
            'sortable' => [
63
                'mail',
64
                'info:first_name',
65
                'info:last_name',
66
                'group_id',
67
                'role_id',
68
                'date_last_active',
69
                'date_create'
70
            ],
71
            'filters' => [
72
                'mail',
73
                'info:first_name',
74
                'info:last_name',
75
                'role_id',
76
                'date_last_active',
77
                'date_create',
78
            ],
79
            'searchableCols' => ['mail']
80
        ],
81
    ];
82
    public static $forms = [
83
        'manager' => [
84
            'inputs' => [
85
                'userSearch' => [
86
                    'type' => 'search',
87
                    'source' => 'relation',
88
                    'relation' => 'parent',
89
                    'label' => 'Пригласивший',
90
                    'cols' => [
91
                        'info:first_name',
92
                        'info:last_name',
93
                        'info:middle_name',
94
                        'mail'
95
                    ],
96
                    'col' => 'parent_id',
97
                ],
98
                'pass' => [
99
                    'type' => 'changePassword'
100
                ]
101
            ],
102
            'map' => [
103
                ['login', 'mail',],
104
                ['group_id', 'role_id'],
105
                ['userSearch', 'blocked'],
106
                ['pass'],
107
                ['activation'],
108
                ['admin_text'],
109
                ['form:info:manager']
110
            ]
111
        ],
112
        'profile' => [
113
            'options' => [
114
                'access' => [
115
                    'groups' => [
116
                        3
117
                    ],
118
                    'self' => true
119
                ]
120
            ],
121
            'inputs' => [
122
                'pass' => [
123
                    'type' => 'changePassword'
124
                ]
125
            ],
126
            'map' => [
127
                ['pass'],
128
                ['form:info:profile']
129
            ]
130
        ]
131
    ];
132
133
    public static function relations()
134
    {
135
        return [
136
            'group' => [
137
                'model' => 'Users\Group',
138
                'col' => 'group_id'
139
            ],
140
            'role' => [
141
                'model' => 'Users\Role',
142
                'col' => 'role_id'
143
            ],
144
            'info' => [
145
                'type' => 'one',
146
                'model' => 'Users\User\Info',
147
                'col' => 'user_id'
148
            ],
149
            'inventory' => [
150
                'type' => 'one',
151
                'model' => 'Users\User\Inventory',
152
                'col' => 'user_id'
153
            ],
154
            'socials' => [
155
                'type' => 'many',
156
                'model' => 'Users\User\Social',
157
                'col' => 'user_id'
158
            ],
159
            'parent' => [
160
                'model' => 'Users\User',
161
                'col' => 'parent_id'
162
            ],
163
            'users' => [
164
                'type' => 'many',
165
                'model' => 'Users\User',
166
                'col' => 'parent_id'
167
            ]
168
        ];
169
    }
170
171
    public function name()
172
    {
173
        if ($this->info) {
174
            return trim($this->info->name());
175
        } else {
176
            $this->id;
177
        }
178
    }
179
180
    public function isAdmin()
181
    {
182
        if ($this->group_id == 3) {
183
            return true;
184
        }
185
        return false;
186
    }
187
188
    public function beforeSave()
189
    {
190
        if (isset($this->_changedParams['user_parent_id'])) {
191
            $parenthistory = new User\ParentHistory([
192
                'user_id' => $this->id,
193
                'old' => $this->_changedParams['user_parent_id'],
194
                'new' => $this->parent_id
195
            ]);
196
            $parenthistory->save();
197
        }
198
    }
199
200
    public function beforeDelete()
201
    {
202
        if ($this->info) {
203
            $this->info->delete();
204
        }
205
        $sessions = Session::getList(['where' => ['user_id', $this->id]]);
206
        foreach ($sessions as $session) {
207
            $session->delete();
208
        }
209
        foreach ($this->socials as $social) {
210
            $social->delete();
211
        }
212
    }
213
214
}
215