1 <?php
2
3 namespace PLus\Orders\Repositories\Entities;
4
5
6 use PLus\Orders\Repositories\Security;
7
8
9 10 11 12 13
14 class UserRepository extends \PLus\Orders\Repositories\Repository {
15
16 17 18 19 20 21
22 public function findByName($username){
23 return $this->getTable()
24 ->select('user.*, :unit_user.unit.name AS jednotka, :unit_user.unit.department.name AS oddeleni')
25 ->select(':unit_user.unit.department.branch.name AS obor')
26 ->where('username', $username)
27 ->fetch();
28 }
29
30 31 32 33 34 35
36 public function setPassword($id, $password) {
37 $this->getTable()->where(array('id' => $id))->update(array('password' => Security\UserManager::calculateHash($password)));
38 }
39
40
41 public function setEmail($id, $email) {
42 $this->getTable()->where(array('id' => $id))->update(array('email' => $email));
43 }
44
45
46 function getPairData(){
47 return $this->getTable()->select('id, username')->order('name ASC')->fetchPairs('id', 'username');
48 }
49
50
51 function getGridSelection(){
52 return $this->getTable()
53 ->select('user.id, user.username, CASE WHEN user.isunit=1 THEN :unit_user.unit.name ELSE CONCAT_WS(\' \',user.firstname, '
54 . 'user.lastname) END AS jname, user.email, user.isunit, user.ischief');
55 }
56
57 58 59 60 61 62
63 public function findById($id) {
64 return $this->getTable()
65 ->select('user.*')->wherePrimary($id)->fetch();
66 }
67
68 69 70 71 72 73
74 public function findUserDepartmentById($id) {
75 return $this->getTable()
76 ->select('user.id, :unit_user.unit.department.*')->get($id);
77 }
78
79
80 81 82 83 84 85
86 public function findUserUnitById($id) {
87 return $this->getTable()
88 ->select('user.id, :unit_user.unit.*')->get($id);
89 }
90
91 92 93 94 95 96 97
98 public function findUserByRole($roles, $user_id) {
99 return $this->getTable()->select('user.id, CONCAT_WS(\' \',user.lastname,user.firstname) AS uname')
100 ->where(':role_user.role.code', $roles)
101 ->where('NOT user.id',$user_id)
102 ->order('uname')
103 ->fetchPairs('id', 'uname');
104 }
105
106
107 108 109 110 111 112
113 public function findUserByEmail($email) {
114 return $this->getTable()->select('user.id, user.username')
115 ->where('email',$email)
116 ->where('isunit',0)
117 ->fetch();
118 }
119
120 121 122 123 124 125
126 public function getCountUserByEmail($email) {
127 return $this->getTable()->select('COUNT(id) AS cnt')
128 ->where('email',$email)
129 ->where('isunit',0)
130 ->fetch();
131 }
132
133 134 135 136 137 138
139 public function getUserByToken($token) {
140 return $this->getTable()->select('id, expires')
141 ->where('token',$token)
142 ->fetch();
143 }
144
145
146 147 148 149 150 151 152
153 function assignArea($area_id, $user_id) {
154 try {
155 $this->connection->table('area_user')->insert(array('area_id' => $area_id, 'user_id' => $user_id));
156 }
157 catch ( \PDOException $e) {
158 if ($e->getCode() == '23000')
159 $this->connection->table('area_user')->where(array('area_id' => $area_id, 'user_id' => $user_id))
160 ->update(array('area_id' => $area_id, 'user_id' => $user_id));
161 else
162 throw $e;
163 }
164 }
165
166 167 168 169 170 171
172 function removeAssignedArea($area_id, $user_id) {
173 $this->connection->table('area_user')->where(array('area_id' => $area_id, 'user_id' => $user_id))->delete();
174 }
175
176
177 178 179 180 181 182
183 function removeAllAssignedArea($user_id) {
184 $this->connection->table('area_user')->where(array('user_id' => $user_id))->delete();
185 }
186
187 188 189 190 191 192
193 function getUserAreaById($id) {
194 return $this->getTable()->select(':area_user.area.name')
195 ->where('user.id', $id);
196 }
197
198
199
200
201 202 203 204 205 206 207
208 function assignRole($role_code, $user_id) {
209 try {
210 $this->connection->table('role_user')->insert(array('role_code' => $role_code, 'user_id' => $user_id));
211 }
212 catch ( \PDOException $e) {
213 if ($e->getCode() == '23000')
214 $this->connection->table('role_user')->where(array('role_code' => $role_code, 'user_id' => $user_id))
215 ->update(array('role_code' => $role_code, 'user_id' => $user_id));
216 else
217 throw $e;
218 }
219 }
220
221 222 223 224 225 226
227 function removeAssignedRole($role_code, $user_id) {
228 $this->connection->table('role_user')->where(array('role_code' => $role_code, 'user_id' => $user_id))->delete();
229 }
230
231
232 233 234 235 236 237 238
239 function assignDepartment($department_id, $user_id) {
240 try {
241 $this->connection->table('department_user')->insert(array('department_id' => $department_id, 'user_id' => $user_id));
242 } catch (\PDOException $e) {
243 if ($e->getCode() == '23000')
244 $this->connection->table('department_user')->where(array('user_id' => $user_id))
245 ->update(array('department_id' => $department_id));
246 else
247 throw $e;
248 }
249 }
250
251 252 253 254 255
256 function removeDepartment($user_id) {
257 $this->connection->table('department_user')->where(array('user_id' => $user_id))->delete();
258 }
259
260
261 262 263 264 265 266
267 public function getUserByArea($area_id) {
268 return $this->getTable()->select('user.id, CONCAT_WS(\' \',user.firstname, user.lastname, \'(\', :area_user.area.name ,\')\') AS uname')
269 ->where(':area_user.area.id', $area_id)
270 ->fetchPairs('id', 'uname');
271
272 }
273
274 275 276 277 278 279 280
281 public function getUserByAreaOrderLastname($area_id, $user_id) {
282 return $this->getTable()->select('user.id, CONCAT_WS(\' \',user.lastname, user.firstname, \'(\', :area_user.area.name ,\')\') AS uname')
283 ->where(':area_user.area.id', $area_id)
284 ->where('NOT user.id', $user_id)
285 ->order('uname')
286 ->fetchPairs('id', 'uname');
287
288 }
289
290 291 292 293 294
295 public function getManagerEmail() {
296 return $this->getTable()->select('user.id,user.email')
297 ->where(':role_user.role.code', 'manager')
298 ->fetchPairs('id','email');
299 }
300
301
302 303 304 305 306
307 public function getTraderEmail() {
308 return $this->getTable()->select('user.id,user.email')
309 ->where(':role_user.role.code', 'trader')
310 ->fetchPairs('id','email');
311 }
312
313
314 315 316 317 318 319
320 public function getUnitEmail($user_id) {
321 return $this->getTable()->select('user.id, user.email')
322 ->where('user.id', $user_id)
323 ->fetchPairs('id', 'email');
324 }
325
326 327 328 329 330 331
332 public function getChiefEmail($department_id) {
333 return $this->getTable()->select('user.id, user.email')
334 ->where(':department_user.department.id', $department_id)
335 ->fetchPairs('id', 'email');
336 }
337
338
339 340 341 342 343 344
345 public function getExpertEmail($area_id) {
346 return $this->getTable()->select('DISTINCT user.id, user.email')
347 ->where(':area_user.area.id', $area_id)
348 ->where(':role_user.role.code','expert')
349 ->fetchPairs('id', 'email');
350 }
351
352
353 354 355 356 357 358
359 public function getImplementerEmail($user_id) {
360 return $this->getTable()->select('user.id,user.email')
361 ->where('user.id', $user_id)
362 ->fetchPairs('id','email');
363 }
364
365
366 367 368 369 370 371
372 public function getUserEmail($user_id) {
373 return $this->getTable()->select('user.id, user.email')
374 ->where('user.id', $user_id)
375 ->fetchPairs('id','email');
376 }
377 }
378