Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6328 | Rev 6749 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Db\Sql\Expression;
9
use Laminas\Log\LoggerInterface;
10
 
11
use LeadersLinked\Model\User;
12
use LeadersLinked\Mapper\Common\MapperCommon;
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use Laminas\Hydrator\ArraySerializableHydrator;
15
use Laminas\Paginator\Paginator;
16
use Laminas\Paginator\Adapter\DbSelect;
17
use Laminas\Db\ResultSet\HydratingResultSet;
18
use LeadersLinked\Model\UserType;
19
use LeadersLinked\Model\CompanyUser;
20
use LeadersLinked\Model\CompanyMicrolearningCapsuleUser;
2083 nelberth 21
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
1 www 22
 
23
 
1665 nelberth 24
 
2083 nelberth 25
 
1 www 26
class UserMapper extends MapperCommon
27
{
28
    const _TABLE = 'tbl_users';
29
 
30
    /**
31
     *
32
     * @var UserMapper
33
     */
34
    private static $_instance;
5973 anderson 35
 
1 www 36
    /**
37
     *
38
     * @param AdapterInterface $adapter
39
     */
40
    private function __construct($adapter)
41
    {
42
        parent::__construct($adapter);
43
    }
5973 anderson 44
 
1 www 45
    /**
46
     *
47
     * @param AdapterInterface $adapter
48
     * @return UserMapper
49
     */
50
    public static function getInstance($adapter)
51
    {
5973 anderson 52
        if (self::$_instance == null) {
1 www 53
            self::$_instance = new UserMapper($adapter);
54
        }
55
        return self::$_instance;
56
    }
5973 anderson 57
 
1 www 58
    /**
59
     *
60
     * @param string $uuid
61
     * @return User
62
     */
63
    public function fetchOneByUuid($uuid)
64
    {
65
        $prototype = new User();
66
        $select = $this->sql->select(self::_TABLE);
67
        $select->where->equalTo('uuid', $uuid);
5973 anderson 68
 
69
 
1 www 70
        return $this->executeFetchOneObject($select, $prototype);
71
    }
5973 anderson 72
 
73
 
1 www 74
    /**
3639 efrain 75
     *
4733 efrain 76
     * @return User
77
     */
78
    public function fetchOneDefaultForConnection()
79
    {
80
        $prototype = new User();
81
        $select = $this->sql->select(self::_TABLE);
82
        $select->where->equalTo('default_for_connection', User::DEFAULT_FOR_CONNECTION_YES);
5973 anderson 83
 
84
 
4733 efrain 85
        return $this->executeFetchOneObject($select, $prototype);
86
    }
5973 anderson 87
 
88
 
89
 
4733 efrain 90
    /**
91
     *
3639 efrain 92
     * @param string $uuid
93
     * @param int $network_id
94
     * @return User
95
     */
96
    public function fetchOneByUuidAndNetworkId($uuid, $network_id)
97
    {
98
        $prototype = new User();
99
        $select = $this->sql->select(self::_TABLE);
100
        $select->where->equalTo('uuid', $uuid);
101
        $select->where->equalTo('network_id', $network_id);
5973 anderson 102
 
103
 
3639 efrain 104
        return $this->executeFetchOneObject($select, $prototype);
105
    }
5973 anderson 106
 
3639 efrain 107
    /**
1 www 108
     *
109
     * @param int $id
110
     * @return User
111
     */
112
    public function fetchOne($id)
113
    {
114
        $prototype = new User();
115
        $select = $this->sql->select(self::_TABLE);
116
        $select->where->equalTo('id', $id);
6329 anderson 117
 
1 www 118
        return $this->executeFetchOneObject($select, $prototype);
119
    }
5973 anderson 120
 
1271 nelberth 121
    public function fetchOneRelation($id)
122
    {
123
        $prototype = new User();
124
        $select = $this->sql->select(self::_TABLE);
125
        $select->where->equalTo('id', $id);
5973 anderson 126
 
1271 nelberth 127
        return $this->executeFetchOneObject($select, $prototype);
128
    }
129
 
5973 anderson 130
 
1 www 131
    /**
132
     *
133
     * @param string $email
134
     * @return void|User
135
     */
136
    public function fetchOneByEmail($email)
137
    {
138
        $prototype = new User();
139
        $select = $this->sql->select(self::_TABLE);
140
        $select->where->equalTo('email', $email);
5973 anderson 141
 
1 www 142
        //echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 143
 
1 www 144
        return $this->executeFetchOneObject($select, $prototype);
145
    }
5973 anderson 146
 
1 www 147
    /**
3639 efrain 148
     *
149
     * @param string $email
150
     * @param int $network_id
151
     * @return void|User
152
     */
153
    public function fetchOneByEmailAndNetworkId($email, $network_id)
154
    {
155
        $prototype = new User();
156
        $select = $this->sql->select(self::_TABLE);
157
        $select->where->equalTo('email', $email);
158
        $select->where->equalTo('network_id', $network_id);
5973 anderson 159
 
3639 efrain 160
        //echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 161
 
3639 efrain 162
        return $this->executeFetchOneObject($select, $prototype);
163
    }
5973 anderson 164
 
3639 efrain 165
    /**
5050 efrain 166
     *
167
     * @param string $email
168
     * @param int $network_id
169
     * @return void|User
170
     */
171
    public function fetchOneActiveByEmailAndNetworkId($email, $network_id)
172
    {
173
        $prototype = new User();
174
        $select = $this->sql->select(self::_TABLE);
175
        $select->where->equalTo('email', $email);
176
        $select->where->equalTo('network_id', $network_id);
177
        $select->where->equalTo('status', User::STATUS_ACTIVE);
178
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 179
 
5050 efrain 180
        //echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 181
 
5050 efrain 182
        return $this->executeFetchOneObject($select, $prototype);
183
    }
5973 anderson 184
 
185
 
186
 
5050 efrain 187
    /**
1 www 188
     *
189
     * @param string $password_reset_key
190
     * @return void|User
191
     */
192
    public function fetchOneByPasswordResetKey($password_reset_key)
193
    {
194
        $prototype = new User();
195
        $select = $this->sql->select(self::_TABLE);
196
        $select->where->equalTo('password_reset_key', $password_reset_key);
5973 anderson 197
 
1 www 198
        return $this->executeFetchOneObject($select, $prototype);
199
    }
5973 anderson 200
 
201
 
1 www 202
    /**
3649 efrain 203
     *
204
     * @param string $password_reset_key
205
     * @param int $network_id
206
     * @return void|User
207
     */
208
    public function fetchOneByPasswordResetKeyAndNetworkId($password_reset_key, $network_id)
209
    {
210
        $prototype = new User();
211
        $select = $this->sql->select(self::_TABLE);
212
        $select->where->equalTo('password_reset_key', $password_reset_key);
213
        $select->where->equalTo('network_id', $network_id);
5973 anderson 214
 
3649 efrain 215
        return $this->executeFetchOneObject($select, $prototype);
216
    }
5973 anderson 217
 
3649 efrain 218
    /**
1 www 219
     *
220
     * @param string $activation_key
221
     * @return void|User
222
     */
223
    public function fetchOneByActivationKey($activation_key)
224
    {
225
        $prototype = new User();
226
        $select = $this->sql->select(self::_TABLE);
227
        $select->where->equalTo('activation_key', $activation_key);
5973 anderson 228
 
1 www 229
        return $this->executeFetchOneObject($select, $prototype);
230
    }
5973 anderson 231
 
3649 efrain 232
    /**
233
     *
234
     * @param string $activation_key
235
     * @param int $network_id
236
     * @return void|User
237
     */
3759 efrain 238
    public function fetchOneByActivationKeyAndNetworkId($activation_key, $network_id)
3649 efrain 239
    {
240
        $prototype = new User();
241
        $select = $this->sql->select(self::_TABLE);
242
        $select->where->equalTo('activation_key', $activation_key);
243
        $select->where->equalTo('network_id', $network_id);
3671 efrain 244
 
5973 anderson 245
 
246
 
3649 efrain 247
        return $this->executeFetchOneObject($select, $prototype);
248
    }
5973 anderson 249
 
250
 
3639 efrain 251
    /**
252
     *
253
     * @param int $network_id
254
     * @return int
255
     */
256
    public function fetchCountByNetworkId($network_id)
257
    {
5973 anderson 258
 
3639 efrain 259
        $select = $this->sql->select(self::_TABLE);
260
        $select->columns(['total' => new Expression('COUNT(*)')]);
261
        $select->where->equalTo('network_id', $network_id);
5973 anderson 262
 
3639 efrain 263
        $record = $this->executeFetchOneArray($select);
5973 anderson 264
 
3639 efrain 265
        return $record['total'];
266
    }
5973 anderson 267
 
4842 efrain 268
    /**
269
     *
270
     * @param int $network_id
271
     * @return int
272
     */
273
    public function fetchCountActiveByDefaultNetworkId($network_id)
274
    {
5973 anderson 275
 
4842 efrain 276
        $select = $this->sql->select(self::_TABLE);
277
        $select->columns(['total' => new Expression('COUNT(*)')]);
278
        $select->where->equalTo('network_id', $network_id);
279
        $select->where->in('usertype_id', [UserType::USER, UserType::ADMIN]);
280
        $select->where->equalTo('status', User::STATUS_ACTIVE);
5973 anderson 281
 
4842 efrain 282
        $record = $this->executeFetchOneArray($select);
5973 anderson 283
 
4842 efrain 284
        return $record['total'];
285
    }
5973 anderson 286
 
4842 efrain 287
    /**
288
     *
289
     * @param int $network_id
290
     * @return int
291
     */
292
    public function fetchCountActiveByOtherNetworkId($network_id)
293
    {
5973 anderson 294
 
4842 efrain 295
        $select = $this->sql->select(self::_TABLE);
296
        $select->columns(['total' => new Expression('COUNT(*)')]);
297
        $select->where->equalTo('network_id', $network_id);
298
        $select->where->in('usertype_id', [UserType::USER, UserType::ADMIN]);
299
        $select->where->equalTo('status', User::STATUS_ACTIVE);
300
        $select->where->equalTo('request_access', User::REQUEST_ACCESS_APPROVED);
301
 
5973 anderson 302
 
4842 efrain 303
        $record = $this->executeFetchOneArray($select);
5973 anderson 304
 
4842 efrain 305
        return $record['total'];
306
    }
1 www 307
 
5973 anderson 308
 
309
 
1 www 310
    /**
311
     *
312
     * @param string $keyword
313
     * @param int $current_user
314
     * @return User[]
315
     */
316
    public function fetchAllByKeyword($keyword, $current_user = 0)
317
    {
5973 anderson 318
 
1 www 319
        $prototype = new User();
320
 
321
        $select = $this->sql->select(self::_TABLE);
5973 anderson 322
 
1 www 323
        $select->where->equalTo('status', User::STATUS_ACTIVE);
324
        $select->where->notEqualTo('id', $current_user);
325
        $select->where->and->nest()
326
            ->like('first_name', '%' . $keyword . '%')
327
            ->or->like('last_name', '%' . $keyword . '%')
5973 anderson 328
            ->or->like(new Expression("CONCAT(first_name,' ',last_name)"), '%' . $keyword . '%')
1 www 329
            ->unnest();
5973 anderson 330
 
331
 
1 www 332
        return $this->executeFetchAllObject($select, $prototype);
333
    }
5973 anderson 334
 
1 www 335
    /**
336
     *
337
     * @param int[] $ids
338
     * @param int $current_user
339
     * @param string $status
340
     * @return User[]
341
     */
342
    public function fetchAllByIds($ids, $current_user = 0, $status = User::STATUS_ACTIVE)
343
    {
5973 anderson 344
 
1 www 345
        $prototype = new User();
346
        $select = $this->sql->select(self::_TABLE);
5973 anderson 347
 
348
 
1 www 349
        $select->where->equalTo('status', User::STATUS_ACTIVE);
5973 anderson 350
        $select->where->and->in('id', $ids);
351
 
352
        if ($current_user) {
1 www 353
            $select->where->and->notEqualTo('id ', $current_user);
354
        }
630 efrain 355
        $select->order(['first_name ASC', 'last_name ASC', 'email ASC']);
5973 anderson 356
 
1 www 357
        return $this->executeFetchAllObject($select, $prototype);
358
    }
5973 anderson 359
 
4857 efrain 360
    /**
361
     *
362
     * @param int[] $ids
363
     * @param string $search
364
     * @param int $current_user
365
     * @param string $status
366
     * @return User[]
367
     */
368
    public function fetchAllByIdsAndSearch($ids, $search, $current_user = 0, $status = User::STATUS_ACTIVE)
369
    {
5973 anderson 370
 
4857 efrain 371
        $prototype = new User();
372
        $select = $this->sql->select(self::_TABLE);
5973 anderson 373
 
374
 
4857 efrain 375
        $select->where->equalTo('status', User::STATUS_ACTIVE);
5973 anderson 376
        $select->where->and->in('id', $ids);
4857 efrain 377
        $select->where->nest()
5973 anderson 378
            ->like('first_name', '%' . $search . '%')
379
            ->or->like('last_name', '%' . $search . '%')
6031 anderson 380
            ->or->like(new Expression("CONCAT(first_name,' ',last_name)"), '%' . $search . '%')
381
            ->or->like(new Expression("CONCAT(last_name,' ',first_name)"), '%' . $search . '%')
5973 anderson 382
            ->or->like('email', '%' . $search . '%')->unnest();
383
 
384
        if ($current_user) {
4857 efrain 385
            $select->where->and->notEqualTo('id ', $current_user);
386
        }
387
        $select->order(['first_name ASC', 'last_name ASC', 'email ASC']);
5973 anderson 388
 
4857 efrain 389
        return $this->executeFetchAllObject($select, $prototype);
390
    }
391
 
5973 anderson 392
 
6023 anderson 393
 
6011 efrain 394
    /**
395
     *
396
     * @param int $network_id
397
     * @param int[] $ids
398
     * @param string $search
399
     * @param int $current_user
400
     * @param string $status
401
     * @return User[]
402
     */
403
    public function fetchAllByNetworkIdAndIdsAndSearch($network_id, $ids, $search, $current_user = 0, $status = User::STATUS_ACTIVE)
404
    {
6023 anderson 405
 
6011 efrain 406
        $prototype = new User();
407
        $select = $this->sql->select(self::_TABLE);
6023 anderson 408
 
409
 
6011 efrain 410
        $select->where->equalTo('status', User::STATUS_ACTIVE);
411
        $select->where->equalTo('network_id', $network_id);
412
        $select->where->and->in('id', $ids);
413
        $select->where->nest()
6023 anderson 414
            ->like('first_name', '%' . $search . '%')
415
            ->or->like('last_name', '%' . $search . '%')
416
            ->or->like('email', '%' . $search . '%')->unnest();
417
 
6011 efrain 418
        if ($current_user) {
419
            $select->where->and->notEqualTo('id ', $current_user);
420
        }
421
        $select->order(['first_name ASC', 'last_name ASC', 'email ASC']);
6023 anderson 422
 
6011 efrain 423
        return $this->executeFetchAllObject($select, $prototype);
424
    }
5973 anderson 425
 
426
 
427
 
428
 
1 www 429
    /**
430
     *
431
     * @return User[]
432
     */
433
    public function fetchAllByActives()
434
    {
5973 anderson 435
 
1 www 436
        $prototype = new User();
437
        $select = $this->sql->select(self::_TABLE);
5973 anderson 438
 
439
 
1 www 440
        $select->where->equalTo('status', User::STATUS_ACTIVE);
5973 anderson 441
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
442
 
646 efrain 443
        $select->order(['first_name DESC, last_name DESC, email_DESC']);
1 www 444
 
5973 anderson 445
 
1 www 446
        return $this->executeFetchAllObject($select, $prototype);
447
    }
5973 anderson 448
 
1 www 449
    /**
4857 efrain 450
     *
451
     * @param int $network_id
452
     * @param int $current_user_id;
453
     * @return int[]
454
     */
455
    public function fetchAllIdsByDefaultNetworkId($network_id, $current_user_id = 0)
456
    {
457
 
5973 anderson 458
 
4857 efrain 459
        $select = $this->sql->select(self::_TABLE);
460
        $select->columns(['id']);
461
        $select->where->equalTo('network_id', $network_id);
462
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
463
        $select->where->equalTo('status', User::STATUS_ACTIVE);
6011 efrain 464
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 465
 
466
        if ($current_user_id) {
4857 efrain 467
            $select->where->notEqualTo('id', $current_user_id);
468
        }
5973 anderson 469
 
4857 efrain 470
        $ids = [];
5973 anderson 471
 
4857 efrain 472
        $records = $this->executeFetchAllArray($select);
5973 anderson 473
        foreach ($records as $record) {
4857 efrain 474
            array_push($ids, $record['id']);
475
        }
5973 anderson 476
 
4857 efrain 477
        return $ids;
478
    }
5973 anderson 479
 
4857 efrain 480
    /**
481
     *
482
     * @param int $network_id
483
     * @param string $search
484
     * @param int $current_user_id;
485
     * @return int[]
486
     */
487
    public function fetchAllIdsByDefaultNetworkIdAndSearch($network_id, $search, $current_user_id = 0)
488
    {
5973 anderson 489
 
490
 
4857 efrain 491
        $select = $this->sql->select(self::_TABLE);
492
        $select->columns(['id']);
493
        $select->where->equalTo('network_id', $network_id);
494
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
495
        $select->where->equalTo('status', User::STATUS_ACTIVE);
496
        $select->where->nest()
5973 anderson 497
            ->like('first_name', '%' . $search . '%')
498
            ->or->like('last_name', '%' . $search . '%')
499
            ->or->like('email', '%' . $search . '%')->unnest();
500
 
501
        if ($current_user_id) {
4857 efrain 502
            $select->where->notEqualTo('id', $current_user_id);
503
        }
5973 anderson 504
 
4857 efrain 505
        $ids = [];
5973 anderson 506
 
4857 efrain 507
        $records = $this->executeFetchAllArray($select);
5973 anderson 508
        foreach ($records as $record) {
4857 efrain 509
            array_push($ids, $record['id']);
510
        }
5973 anderson 511
 
4857 efrain 512
        return $ids;
513
    }
5973 anderson 514
 
4857 efrain 515
    /**
516
     *
517
     * @param int $network_id
518
     * @param int $current_user_id;
519
     * @return int[]
520
     */
521
    public function fetchAllIdsByNonDefaultNetworkId($network_id, $current_user_id = 0)
522
    {
5973 anderson 523
 
524
 
4857 efrain 525
        $select = $this->sql->select(self::_TABLE);
526
        $select->columns(['id']);
527
        $select->where->equalTo('network_id', $network_id);
528
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
529
        $select->where->equalTo('status', User::STATUS_ACTIVE);
6011 efrain 530
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
4857 efrain 531
        $select->where->equalTo('request_access', User::REQUEST_ACCESS_APPROVED);
5973 anderson 532
 
533
        if ($current_user_id) {
4857 efrain 534
            $select->where->notEqualTo('id', $current_user_id);
535
        }
5973 anderson 536
 
4857 efrain 537
        $ids = [];
5973 anderson 538
 
4857 efrain 539
        $records = $this->executeFetchAllArray($select);
5973 anderson 540
        foreach ($records as $record) {
4857 efrain 541
            array_push($ids, $record['id']);
542
        }
5973 anderson 543
 
4857 efrain 544
        return $ids;
545
    }
5973 anderson 546
 
547
 
4857 efrain 548
    /**
549
     *
550
     * @param int $network_id
551
     * @param string $search
552
     * @param int $current_user_id;
553
     * @return int[]
554
     */
555
    public function fetchAllIdsByNonDefaultNetworkIdAndSearch($network_id, $search, $current_user_id = 0)
556
    {
5973 anderson 557
 
558
 
4857 efrain 559
        $select = $this->sql->select(self::_TABLE);
560
        $select->columns(['id']);
561
        $select->where->equalTo('network_id', $network_id);
562
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
563
        $select->where->equalTo('status', User::STATUS_ACTIVE);
564
        $select->where->equalTo('request_access', User::REQUEST_ACCESS_APPROVED);
565
        $select->where->nest()
5973 anderson 566
            ->like('first_name', '%' . $search . '%')
567
            ->or->like('last_name', '%' . $search . '%')
568
            ->or->like('email', '%' . $search . '%')->unnest();
569
 
570
        if ($current_user_id) {
4857 efrain 571
            $select->where->notEqualTo('id', $current_user_id);
572
        }
5973 anderson 573
 
574
 
4857 efrain 575
        $ids = [];
5973 anderson 576
 
4857 efrain 577
        $records = $this->executeFetchAllArray($select);
5973 anderson 578
        foreach ($records as $record) {
4857 efrain 579
            array_push($ids, $record['id']);
580
        }
5973 anderson 581
 
4857 efrain 582
        return $ids;
583
    }
5973 anderson 584
 
585
 
4857 efrain 586
    /**
1 www 587
     *
646 efrain 588
     * @param int $company_id
5751 efrain 589
     * @return User[]
646 efrain 590
     */
591
    public function fetchAllByCompanyId($company_id)
592
    {
5973 anderson 593
 
646 efrain 594
        $prototype = new User();
595
        $select = $this->sql->select();
596
        $select->from(['tb1' => self::_TABLE]);
597
        $select->join(['tb2' => CompanyUserMapper::_TABLE], 'tb1.id = tb2.user_id', []);
5973 anderson 598
 
599
 
646 efrain 600
        $select->where->equalTo('tb2.company_id', $company_id);
601
        $select->where->in('tb2.status', [CompanyUser::STATUS_ACCEPTED, CompanyUser::STATUS_ADMIN_WILL_ADD]);
5973 anderson 602
 
646 efrain 603
        $select->where->equalTo('tb1.status', User::STATUS_ACTIVE);
5973 anderson 604
        $select->where->equalTo('tb1.email_verified', User::EMAIL_VERIFIED_YES);
605
 
646 efrain 606
        $select->order(['first_name DESC, last_name DESC, email_DESC']);
5973 anderson 607
 
608
 
609
 
646 efrain 610
        return $this->executeFetchAllObject($select, $prototype);
611
    }
5973 anderson 612
 
646 efrain 613
    /**
614
     *
1 www 615
     * @param int $user_id
3639 efrain 616
     * @return boolean
617
     */
618
    public function leaveImpersonate($user_id)
619
    {
620
        $values = [
621
            'impersonate_user_id' => new Expression('NULL')
622
        ];
5973 anderson 623
 
3639 efrain 624
        $update = $this->sql->update(self::_TABLE);
625
        $update->set($values);
626
        $update->where->equalTo('id', $user_id);
5973 anderson 627
 
3639 efrain 628
        return $this->executeUpdate($update);
629
    }
5973 anderson 630
 
3639 efrain 631
    /**
632
     *
633
     * @param int $user_id
634
     * @param int $impersonate_user_id
635
     * @return boolean
636
     */
637
    public function startImpersonate($user_id, $impersonate_user_id)
638
    {
639
        $values = [
640
            'impersonate_user_id' => $impersonate_user_id
641
        ];
5973 anderson 642
 
3639 efrain 643
        $update = $this->sql->update(self::_TABLE);
644
        $update->set($values);
645
        $update->where->equalTo('id', $user_id);
5973 anderson 646
 
3639 efrain 647
        return $this->executeUpdate($update);
648
    }
5973 anderson 649
 
3639 efrain 650
    /**
651
     *
652
     * @param int $user_id
1 www 653
     * @param string $password_reset_key
654
     * @return boolean
655
     */
656
    public function updatePasswordResetKey($user_id, $password_reset_key)
657
    {
658
        $values = [
659
            'password_reset_key' => $password_reset_key,
660
            'password_generated_on' => date('Y-m-d H:i:s'),
661
            'updated_on' => new Expression('NOW()')
662
        ];
663
 
664
        $update = $this->sql->update(self::_TABLE);
665
        $update->set($values);
666
        $update->where->equalTo('id', $user_id);
5973 anderson 667
 
1 www 668
        return $this->executeUpdate($update);
669
    }
5973 anderson 670
 
671
 
1979 efrain 672
    /**
673
     *
674
     * @param int $user_id
675
     * @param string $delete_account_key
676
     * @return boolean
677
     */
678
    public function updateDeleteAccountKey($user_id, $delete_account_key)
679
    {
680
        $values = [
681
            'delete_account_key' => $delete_account_key,
682
            'delete_account_generated_on' => date('Y-m-d H:i:s'),
683
            'updated_on' => new Expression('NOW()')
684
        ];
5973 anderson 685
 
1979 efrain 686
        $update = $this->sql->update(self::_TABLE);
687
        $update->set($values);
688
        $update->where->equalTo('id', $user_id);
5973 anderson 689
 
1979 efrain 690
        return $this->executeUpdate($update);
691
    }
1 www 692
 
5973 anderson 693
 
694
 
1 www 695
    /**
696
     *
697
     * @param User $user
698
     * @param string $password_hash
699
     * @return boolean
700
     */
701
    public function updatePassword($user, $password_hash)
702
    {
703
        $values = [
704
            'password'              => $password_hash,
705
            'password_reset_key'    => '',
706
            'password_updated_on'   => date('Y-m-d H:i:s'),
707
            'login_attempt'         => 0,
708
            'blocked'               => User::BLOCKED_NO,
709
            'updated_on'            => new Expression('NOW()')
710
        ];
5973 anderson 711
 
1 www 712
        $update = $this->sql->update(self::_TABLE);
713
        $update->set($values);
714
        $update->where->equalTo('id', $user->id);
5973 anderson 715
 
1 www 716
        return $this->executeUpdate($update);
717
    }
5973 anderson 718
 
719
 
720
 
1 www 721
    /**
722
     *
723
     * @param User $user
5935 efrain 724
     * @param int $usertype_id
1 www 725
     * @return boolean
726
     */
5935 efrain 727
    public function updateUserTypeId($user, $usertype_id)
728
    {
729
        $values = [
730
            'usertype_id'   => $usertype_id,
731
            'updated_on'    => new Expression('NOW()')
732
        ];
5973 anderson 733
 
5935 efrain 734
        $update = $this->sql->update(self::_TABLE);
735
        $update->set($values);
736
        $update->where->equalTo('id', $user->id);
5973 anderson 737
 
5935 efrain 738
        return $this->executeUpdate($update);
739
    }
5973 anderson 740
 
741
 
5935 efrain 742
    /**
743
     *
744
     * @param User $user
745
     * @return boolean
746
     */
1 www 747
    public function unblock($user)
748
    {
749
        $values = [
750
            'login_attempt'         => 0,
751
            'blocked'               => User::BLOCKED_NO,
752
            'updated_on'            => new Expression('NOW()')
753
        ];
5973 anderson 754
 
1 www 755
        $update = $this->sql->update(self::_TABLE);
756
        $update->set($values);
757
        $update->where->equalTo('id', $user->id);
5973 anderson 758
 
1 www 759
        return $this->executeUpdate($update);
760
    }
5973 anderson 761
 
762
 
763
 
764
 
1 www 765
    /**
766
     *
767
     * @param User $user
768
     * @param string $one_time_password
769
     * @return boolean
770
     */
771
    public function updateOneTimePassword($user, $one_time_password)
772
    {
773
        $values = [
774
            'one_time_password' => $one_time_password,
775
            'updated_on'        => new Expression('NOW()')
776
        ];
5973 anderson 777
 
1 www 778
        $update = $this->sql->update(self::_TABLE);
779
        $update->set($values);
780
        $update->where->equalTo('id', $user->id);
5973 anderson 781
 
1 www 782
        return $this->executeUpdate($update);
783
    }
5973 anderson 784
 
785
 
1 www 786
    /**
787
     *
788
     * @param User $user
789
     * @return boolean
790
     */
791
    public function update($user)
792
    {
793
        $hydrator = new ObjectPropertyHydrator();
794
        $values = $hydrator->extract($user);
1979 efrain 795
        //$values = $this->removeEmpty($values);
796
 
797
 
798
        unset($values['added_on']);
5973 anderson 799
        $values['updated_on'] = new Expression('NOW()');
800
 
1 www 801
        $update = $this->sql->update(self::_TABLE);
802
        $update->set($values);
5973 anderson 803
        $update->where->equalTo('id', $user->id);
804
 
1979 efrain 805
        //echo $update->getSqlString($this->adapter->platform); exit;
5973 anderson 806
 
1 www 807
        return $this->executeUpdate($update);
808
    }
5973 anderson 809
 
1 www 810
    /**
811
     *
812
     * @param User $user
813
     * @return boolean
814
     */
815
    public function updatePrivacy($user)
816
    {
817
        $values = [
818
            'show_in_search' => $user->show_in_search,
819
            'updated_on' => new Expression('NOW()')
820
        ];
5973 anderson 821
 
1 www 822
        $update = $this->sql->update(self::_TABLE);
823
        $update->set($values);
5973 anderson 824
        $update->where->equalTo('id', $user->id);
825
 
1 www 826
        return $this->executeUpdate($update);
827
    }
5973 anderson 828
 
1 www 829
    /**
830
     *
831
     * @param User $user
832
     * @return boolean
833
     */
834
    public function updateBasic($user)
835
    {
836
        $values = [
837
            'first_name' => $user->first_name,
838
            'last_name' => $user->last_name,
839
            'phone' => $user->phone,
840
            'gender' => $user->gender,
4113 efrain 841
            'timezone' => $user->timezone,
4416 efrain 842
            'is_adult' => $user->is_adult,
1 www 843
            'updated_on' => new Expression('NOW()')
844
        ];
5973 anderson 845
 
1 www 846
        $update = $this->sql->update(self::_TABLE);
847
        $update->set($values);
5973 anderson 848
        $update->where->equalTo('id', $user->id);
849
 
1 www 850
        return $this->executeUpdate($update);
851
    }
5973 anderson 852
 
853
 
1 www 854
    /**
855
     *
856
     * @param User $user
857
     * @return boolean
858
     */
859
    public function updateImage($user)
860
    {
861
        $values = [
862
            'image' => $user->image,
863
            'updated_on' => new Expression('NOW()')
864
        ];
5973 anderson 865
 
1 www 866
        $update = $this->sql->update(self::_TABLE);
867
        $update->set($values);
5973 anderson 868
        $update->where->equalTo('id', $user->id);
869
 
1 www 870
        return $this->executeUpdate($update);
871
    }
5973 anderson 872
 
1 www 873
    /**
874
     *
875
     * @param User $user
876
     * @return boolean
877
     */
878
    public function updateLocation($user)
879
    {
880
        $values = [
881
            'location_id' => $user->location_id,
882
            'updated_on' => new Expression('NOW()')
883
        ];
5973 anderson 884
 
1 www 885
        $update = $this->sql->update(self::_TABLE);
886
        $update->set($values);
5973 anderson 887
        $update->where->equalTo('id', $user->id);
888
 
1 www 889
        return $this->executeUpdate($update);
890
    }
5973 anderson 891
 
1 www 892
    /**
893
     *
894
     * @param int $user_id
895
     * @return boolean
896
     */
897
    public function activateAccount($user_id)
898
    {
899
        $values = [
900
            'email_verified'    => User::EMAIL_VERIFIED_YES,
901
            'status'            => User::STATUS_ACTIVE,
902
            'activation_key'    => '',
903
            'updated_on' => new Expression('NOW()')
904
        ];
5973 anderson 905
 
1 www 906
        $update = $this->sql->update(self::_TABLE);
907
        $update->set($values);
908
        $update->where->equalTo('id', $user_id);
5973 anderson 909
 
1 www 910
        return $this->executeUpdate($update);
911
    }
5973 anderson 912
 
1 www 913
    /**
914
     *
915
     * @param User $user
916
     * @return boolean
917
     */
918
    public function insert($user)
919
    {
5973 anderson 920
 
1 www 921
        $hydrator = new ObjectPropertyHydrator();
922
        $values = $hydrator->extract($user);
923
        $values = $this->removeEmpty($values);
5973 anderson 924
 
1 www 925
        $insert = $this->sql->insert(self::_TABLE);
926
        $insert->values($values);
5973 anderson 927
 
1 www 928
        $response = $this->executeInsert($insert);
5973 anderson 929
        if ($response) {
1 www 930
            $user->id = $this->lastInsertId;
931
        }
5973 anderson 932
 
1 www 933
        return $response;
934
    }
5973 anderson 935
 
936
 
1 www 937
    /**
938
     *
939
     * @return boolean
940
     */
941
    public function truncate()
942
    {
943
        $sql = sprintf('TRUNCATE TABLE `%s` ', self::_TABLE);
944
        return $this->executeSentenceWithParameters($sql);
945
    }
5973 anderson 946
 
1 www 947
    /**
948
     *
949
     * @param int $company_id
950
     * @param string $status
951
     * @param string $search
952
     * @param int $page
953
     * @param int $records_per_page
954
     * @param string $order_field
955
     * @param string $order_direction
956
     * @return Paginator
957
     */
5973 anderson 958
    public function fetchAllDataTableByCompanyId($company_id, $status = '', $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
1 www 959
    {
5973 anderson 960
        $select = $this->sql->select();
1 www 961
        $select->columns(['status', 'backend', 'creator']);
962
        $select->from(['tb1' => CompanyUserMapper::_TABLE]);
5973 anderson 963
        $select->join(['tb2' => self::_TABLE], 'tb1.user_id = tb2.id', [
1 www 964
            'id', 'uuid', 'first_name', 'last_name', 'email', 'last_activity_on', 'image',
965
            'blocked', 'login_attempt', 'email_verified'
5973 anderson 966
 
1 www 967
        ]);
5973 anderson 968
 
1 www 969
        $select->where->equalTo('tb1.company_id', $company_id);
970
        $select->where->equalTo('tb1.owner', CompanyUser::OWNER_NO);
5973 anderson 971
 
972
        if ($status) {
973
            $select->where->equalTo('tb1.status', $status);
974
        }
975
 
976
        if ($search) {
1 www 977
            $select->where->nest()
5973 anderson 978
                ->like('first_name', '%' . $search . '%')
979
                ->or->like('last_name', '%' . $search . '%')
980
                ->or->like('email', '%' . $search . '%')
981
                ->unnest();
1 www 982
        }
5973 anderson 983
 
984
 
1 www 985
        $select->order($order_field . ' ' . $order_direction);
5973 anderson 986
 
1 www 987
        $hydrator   = new ArraySerializableHydrator();
988
        $resultset  = new HydratingResultSet($hydrator);
5973 anderson 989
 
1 www 990
        $adapter = new DbSelect($select, $this->sql, $resultset);
991
        $paginator = new Paginator($adapter);
992
        $paginator->setItemCountPerPage($records_per_page);
993
        $paginator->setCurrentPageNumber($page);
5973 anderson 994
 
995
 
1 www 996
        return $paginator;
997
    }
5973 anderson 998
 
999
 
1 www 1000
    /**
1001
     *
1002
     * @param int $company_id
1003
     * @param string $search
1004
     * @param int $page
1005
     * @param int $records_per_page
1006
     * @param string $order_field
1007
     * @param string $order_direction
1008
     * @return Paginator
1009
     */
5973 anderson 1010
    public function fetchAllDataTableStudensByCompanyId($company_id,  $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
1 www 1011
    {
1012
        $date = date('Y-m-d');
5973 anderson 1013
 
1 www 1014
        $selectCapsuleUser = $this->getSql()->select();
5973 anderson 1015
        $selectCapsuleUser->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
1016
        $selectCapsuleUser->from(CompanyMicrolearningCapsuleUserMapper::_TABLE);
1 www 1017
        $selectCapsuleUser->where->equalTo('company_id', $company_id);
1018
        $selectCapsuleUser->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
5973 anderson 1019
            ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
1020
            ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
1021
            ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date)->unnest()->unnest();
1022
 
1 www 1023
        //echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1024
 
1 www 1025
        $select = $this->sql->select();
5973 anderson 1026
        $select->columns(['id', 'uuid', 'first_name', 'last_name', 'email', 'blocked']);
1 www 1027
        $select->from(UserMapper::_TABLE);
1028
        $select->where->in('id', $selectCapsuleUser);
1029
 
5973 anderson 1030
 
1031
        if ($search) {
1 www 1032
            $select->where->nest()
5973 anderson 1033
                ->like('first_name', '%' . $search . '%')
1034
                ->or->like('last_name', '%' . $search . '%')
1035
                ->or->like('email', '%' . $search . '%')
1036
                ->unnest();
1 www 1037
        }
5973 anderson 1038
 
1039
 
1 www 1040
        $select->order($order_field . ' ' . $order_direction);
5973 anderson 1041
 
1 www 1042
        $hydrator   = new ArraySerializableHydrator();
1043
        $resultset  = new HydratingResultSet($hydrator);
5973 anderson 1044
 
1 www 1045
        $adapter = new DbSelect($select, $this->sql, $resultset);
1046
        $paginator = new Paginator($adapter);
1047
        $paginator->setItemCountPerPage($records_per_page);
1048
        $paginator->setCurrentPageNumber($page);
5973 anderson 1049
 
1050
 
1 www 1051
        return $paginator;
1052
    }
2381 nelberth 1053
 
5973 anderson 1054
 
1 www 1055
    /**
1056
     *
2381 nelberth 1057
     * @param int $company_id
1 www 1058
     * @param string $search
2381 nelberth 1059
     * @param int $page
1060
     * @param int $records_per_page
1061
     * @param string $order_field
1062
     * @param string $order_direction
1063
     * @return Paginator
1064
     */
1065
    public function fetchAllStudensByCompanyId($company_id)
1066
    {
5973 anderson 1067
 
2381 nelberth 1068
        $date = date('Y-m-d');
5973 anderson 1069
 
2381 nelberth 1070
        $selectCapsuleUser = $this->getSql()->select();
5973 anderson 1071
        $selectCapsuleUser->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
1072
        $selectCapsuleUser->from(CompanyMicrolearningCapsuleUserMapper::_TABLE);
2381 nelberth 1073
        $selectCapsuleUser->where->equalTo('company_id', $company_id);
1074
        $selectCapsuleUser->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
5973 anderson 1075
            ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
1076
            ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
1077
            ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date)->unnest()->unnest();
1078
 
2381 nelberth 1079
        //echo $select->getSqlString($this->adapter->platform); exit;
1080
        $prototype = new User();
1081
        $select = $this->sql->select();
5973 anderson 1082
        $select->columns(['id', 'uuid', 'first_name', 'last_name', 'email', 'blocked']);
2381 nelberth 1083
        $select->from(UserMapper::_TABLE);
1084
        $select->where->in('id', $selectCapsuleUser);
1085
        return $this->executeFetchAllObject($select, $prototype);
1086
    }
1087
 
1088
    /**
1089
     *
1090
     * @param string $search
1 www 1091
     * @return User[]
1092
     */
1093
    public function fetchAllSuggest($search)
1094
    {
1095
 
1096
        $select = $this->sql->select();
1097
        $select->from(self::_TABLE);
1098
        $select->where->equalTo('status', User::STATUS_ACTIVE);
1099
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 1100
 
1101
        if ($search) {
1 www 1102
            $select->where->nest()
5973 anderson 1103
                ->like('first_name', '%' . $search . '%')
1104
                ->or->like('last_name', '%' . $search . '%')
1105
                ->or->like('email', '%' . $search . '%')
1106
                ->unnest();
1 www 1107
        }
5973 anderson 1108
 
1 www 1109
        $select->order(['first_name', 'last_name']);
5973 anderson 1110
 
1 www 1111
        // echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1112
 
1 www 1113
        $prototype = new User();
5973 anderson 1114
 
1 www 1115
        return $this->executeFetchAllObject($select, $prototype);
1116
    }
5973 anderson 1117
 
3639 efrain 1118
    /**
1119
     *
1120
     * @param int $network_id
1121
     * @param string $search
1122
     * @return User[]
1123
     */
1124
    public function fetchAllSuggestByNetworkIdAndSearch($network_id, $search)
1125
    {
5973 anderson 1126
 
3639 efrain 1127
        $select = $this->sql->select();
1128
        $select->from(self::_TABLE);
1129
        $select->where->equalTo('network_id', $network_id);
1130
        $select->where->equalTo('status', User::STATUS_ACTIVE);
1131
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 1132
 
1133
        if ($search) {
3639 efrain 1134
            $select->where->nest()
5984 anderson 1135
                ->like('first_name', '%' . $search . '%')
5987 anderson 1136
                ->or->like('last_name', '%' . $search . '%')
5993 anderson 1137
                ->or->like(new Expression("CONCAT(first_name,' ',last_name)"), '%' . $search . '%')
5994 anderson 1138
                ->or->like(new Expression("CONCAT(last_name,' ',first_name)"), '%' . $search . '%')
5985 anderson 1139
                ->or->like('email', '%' . $search . '%')
5973 anderson 1140
                ->unnest();
3639 efrain 1141
        }
5973 anderson 1142
 
3639 efrain 1143
        $select->order(['first_name', 'last_name']);
5973 anderson 1144
 
3639 efrain 1145
        // echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1146
 
3639 efrain 1147
        $prototype = new User();
5973 anderson 1148
 
3639 efrain 1149
        return $this->executeFetchAllObject($select, $prototype);
1150
    }
5973 anderson 1151
 
1 www 1152
    /**
1153
     *
1154
     * @param int $company_id
3639 efrain 1155
     * @param int $network_id
1 www 1156
     * @param string $search
1157
     * @return User[]
1158
     */
3639 efrain 1159
    public function fetchAllSuggestForInvitationByCompanyIdAndNetworkIdAndSearch($company_id, $network_id, $search)
1160
    {
1161
        $selectCompanyUsers = $this->sql->select(CompanyUserMapper::_TABLE);
1162
        $selectCompanyUsers->columns(['user_id']);
1163
        $selectCompanyUsers->where->equalTo('company_id', $company_id);
1164
        $selectCompanyUsers->where->in('status', [
1165
            CompanyUser::STATUS_ACCEPTED,
1166
            CompanyUser::STATUS_PENDING,
1167
            CompanyUser::STATUS_SENT,
5973 anderson 1168
 
3639 efrain 1169
        ]);
5973 anderson 1170
 
3639 efrain 1171
        //echo $selectCompanyUsers->getSqlString($this->adapter->platform); exit;
5973 anderson 1172
 
3639 efrain 1173
        $select = $this->sql->select();
1174
        $select->from(self::_TABLE);
1175
        $select->where->notIn('id', $selectCompanyUsers);
1176
        $select->where->equalTo('network_id', $network_id);
1177
        $select->where->equalTo('status', User::STATUS_ACTIVE);
1178
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 1179
 
1180
        if ($search) {
3639 efrain 1181
            $select->where->nest()
5973 anderson 1182
                ->like('first_name', '%' . $search . '%')
1183
                ->or->like('last_name', '%' . $search . '%')
1184
                ->or->like('email', '%' . $search . '%')
1185
                ->unnest();
3639 efrain 1186
        }
5973 anderson 1187
 
3639 efrain 1188
        $select->order(['first_name', 'last_name']);
5973 anderson 1189
 
3639 efrain 1190
        // echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1191
 
3639 efrain 1192
        $prototype = new User();
5973 anderson 1193
 
3639 efrain 1194
        return $this->executeFetchAllObject($select, $prototype);
1195
    }
5973 anderson 1196
 
3639 efrain 1197
    /**
1198
     *
1199
     * @param int $company_id
1200
     * @param string $search
1201
     * @return User[]
1202
     */
1 www 1203
    public function fetchAllSuggestForInvitationByCompanyId($company_id, $search)
1204
    {
1205
        $selectCompanyUsers = $this->sql->select(CompanyUserMapper::_TABLE);
1206
        $selectCompanyUsers->columns(['user_id']);
1207
        $selectCompanyUsers->where->equalTo('company_id', $company_id);
5973 anderson 1208
        $selectCompanyUsers->where->in('status', [
1 www 1209
            CompanyUser::STATUS_ACCEPTED,
1210
            CompanyUser::STATUS_PENDING,
1211
            CompanyUser::STATUS_SENT,
1212
 
1213
        ]);
5973 anderson 1214
 
1 www 1215
        //echo $selectCompanyUsers->getSqlString($this->adapter->platform); exit;
5973 anderson 1216
 
1 www 1217
        $select = $this->sql->select();
1218
        $select->from(self::_TABLE);
1219
        $select->where->notIn('id', $selectCompanyUsers);
1220
        $select->where->equalTo('status', User::STATUS_ACTIVE);
1221
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 1222
 
1223
        if ($search) {
1 www 1224
            $select->where->nest()
5973 anderson 1225
                ->like('first_name', '%' . $search . '%')
1226
                ->or->like('last_name', '%' . $search . '%')
1227
                ->or->like('email', '%' . $search . '%')
1228
                ->unnest();
1 www 1229
        }
5973 anderson 1230
 
1 www 1231
        $select->order(['first_name', 'last_name']);
5973 anderson 1232
 
1233
        // echo $select->getSqlString($this->adapter->platform); exit;
1234
 
1235
        $prototype = new User();
1236
 
1 www 1237
        return $this->executeFetchAllObject($select, $prototype);
1238
    }
5973 anderson 1239
 
1 www 1240
    /**
1241
     *
1242
     * @param int $group_id
3639 efrain 1243
     * @param int $network_id
1 www 1244
     * @param string $search
1245
     * @return User[]
1246
     */
3639 efrain 1247
    public function fetchAllSuggestForInvitationByGroupIdAndNetworkIdAndSearch($group_id, $network_id, $search)
1248
    {
1249
        $selectGroupMembers = $this->sql->select(GroupMemberMapper::_TABLE);
1250
        $selectGroupMembers->columns(['user_id']);
1251
        $selectGroupMembers->where->equalTo('group_id', $group_id);
1252
        $selectGroupMembers->where->in('status', [
1253
            CompanyUser::STATUS_ACCEPTED,
1254
        ]);
5973 anderson 1255
 
3639 efrain 1256
        //echo $selectGroupMembers->getSqlString($this->adapter->platform); exit;
5973 anderson 1257
 
3639 efrain 1258
        $select = $this->sql->select();
1259
        $select->from(self::_TABLE);
1260
        $select->where->equalTo('network_id', $network_id);
1261
        $select->where->notIn('id', $selectGroupMembers);
1262
        $select->where->equalTo('status', User::STATUS_ACTIVE);
1263
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 1264
 
1265
        if ($search) {
3639 efrain 1266
            $select->where->nest()
5973 anderson 1267
                ->like('first_name', '%' . $search . '%')
1268
                ->or->like('last_name', '%' . $search . '%')
1269
                ->or->like('email', '%' . $search . '%')
1270
                ->unnest();
3639 efrain 1271
        }
5973 anderson 1272
 
3639 efrain 1273
        $select->order(['first_name', 'last_name']);
5973 anderson 1274
 
3639 efrain 1275
        // echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1276
 
3639 efrain 1277
        $prototype = new User();
5973 anderson 1278
 
3639 efrain 1279
        return $this->executeFetchAllObject($select, $prototype);
1280
    }
5973 anderson 1281
 
3639 efrain 1282
    /**
1283
     *
1284
     * @param int $group_id
1285
     * @param string $search
1286
     * @return User[]
1287
     */
1 www 1288
    public function fetchAllSuggestForInvitationByGroupId($group_id, $search)
1289
    {
1290
        $selectGroupMembers = $this->sql->select(GroupMemberMapper::_TABLE);
1291
        $selectGroupMembers->columns(['user_id']);
1292
        $selectGroupMembers->where->equalTo('group_id', $group_id);
1293
        $selectGroupMembers->where->in('status', [
5973 anderson 1294
            CompanyUser::STATUS_ACCEPTED,
1 www 1295
        ]);
5973 anderson 1296
 
1 www 1297
        //echo $selectGroupMembers->getSqlString($this->adapter->platform); exit;
5973 anderson 1298
 
1 www 1299
        $select = $this->sql->select();
1300
        $select->from(self::_TABLE);
1301
        $select->where->notIn('id', $selectGroupMembers);
1302
        $select->where->equalTo('status', User::STATUS_ACTIVE);
1303
        $select->where->equalTo('email_verified', User::EMAIL_VERIFIED_YES);
5973 anderson 1304
 
1305
        if ($search) {
1 www 1306
            $select->where->nest()
5973 anderson 1307
                ->like('first_name', '%' . $search . '%')
1308
                ->or->like('last_name', '%' . $search . '%')
1309
                ->or->like('email', '%' . $search . '%')
1310
                ->unnest();
1 www 1311
        }
5973 anderson 1312
 
1 www 1313
        $select->order(['first_name', 'last_name']);
5973 anderson 1314
 
1 www 1315
        // echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1316
 
1 www 1317
        $prototype = new User();
5973 anderson 1318
 
1 www 1319
        return $this->executeFetchAllObject($select, $prototype);
1320
    }
1665 nelberth 1321
 
5973 anderson 1322
 
1323
 
1324
    public function fetchAllSuggestForInvitationByHptgId($group_id, $company_id, $search)
1665 nelberth 1325
    {
1326
        $selectGroupMembers = $this->sql->select(HighPerformanceTeamsGroupsMembersMapper::_TABLE);
1327
        $selectGroupMembers->columns(['user_id']);
1328
        $selectGroupMembers->where->equalTo('group_id', $group_id);
1329
        $selectGroupMembers->where->in('status', [
2082 nelberth 1330
            HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED,
1331
            HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN,
1332
            HighPerformanceTeamsGroupsMembers::STATUS_INVITED,
1665 nelberth 1333
        ]);
5973 anderson 1334
 
1665 nelberth 1335
        //echo $selectGroupMembers->getSqlString($this->adapter->platform); exit;
5973 anderson 1336
 
1665 nelberth 1337
        $select = $this->sql->select();
2092 nelberth 1338
        $select->from(['u' => self::_TABLE]);
1339
        $select->join(['cu' => CompanyUserMapper::_TABLE], 'cu.user_id = u.id', []);
2091 nelberth 1340
        $select->where->notIn('u.id', $selectGroupMembers);
2090 nelberth 1341
        $select->where->equalTo('u.status', User::STATUS_ACTIVE);
1342
        $select->where->equalTo('u.email_verified', User::EMAIL_VERIFIED_YES);
1343
        $select->where->equalTo('cu.company_id', $company_id);
1344
 
5973 anderson 1345
 
1346
        if ($search) {
1665 nelberth 1347
            $select->where->nest()
5973 anderson 1348
                ->like('u.first_name', '%' . $search . '%')
1349
                ->or->like('u.last_name', '%' . $search . '%')
1350
                ->or->like('u.email', '%' . $search . '%')
1351
                ->unnest();
1665 nelberth 1352
        }
5973 anderson 1353
 
2090 nelberth 1354
        $select->order(['u.first_name', 'u.last_name']);
5973 anderson 1355
 
1665 nelberth 1356
        // echo $select->getSqlString($this->adapter->platform); exit;
5973 anderson 1357
 
1665 nelberth 1358
        $prototype = new User();
5973 anderson 1359
 
1665 nelberth 1360
        return $this->executeFetchAllObject($select, $prototype);
1361
    }
5973 anderson 1362
 
1 www 1363
    /**
1364
     *
1365
     * @param string $search
1366
     * @param int $page
1367
     * @param int $records_per_page
1368
     * @param string $order_field
1369
     * @param string $order_direction
1370
     * @return Paginator
1371
     */
5973 anderson 1372
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
1 www 1373
    {
1374
        $prototype = new User();
1375
        $select = $this->sql->select(self::_TABLE);
1376
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
5973 anderson 1377
 
1378
 
1379
        if ($search) {
1 www 1380
            $select->where->nest()
5973 anderson 1381
                ->like('first_name', '%' . $search . '%')
1382
                ->or->like('last_name', '%' . $search . '%')
1383
                ->or->like('email', '%' . $search . '%')
1384
                ->unnest();
1 www 1385
        }
1386
 
5973 anderson 1387
 
1 www 1388
        $select->order($order_field . ' ' . $order_direction);
5973 anderson 1389
 
1 www 1390
        $hydrator   = new ObjectPropertyHydrator();
1391
        $resultset  = new HydratingResultSet($hydrator, $prototype);
5973 anderson 1392
 
1 www 1393
        $adapter = new DbSelect($select, $this->sql, $resultset);
1394
        $paginator = new Paginator($adapter);
1395
        $paginator->setItemCountPerPage($records_per_page);
1396
        $paginator->setCurrentPageNumber($page);
5973 anderson 1397
 
1398
 
1 www 1399
        return $paginator;
1400
    }
5973 anderson 1401
 
1 www 1402
    /**
4398 efrain 1403
     *
4751 efrain 1404
     * @param int $network_id
1405
     * @param string $search
1406
     * @param int $page
1407
     * @param int $records_per_page
1408
     * @param string $order_field
1409
     * @param string $order_direction
1410
     * @return Paginator
1411
     */
5973 anderson 1412
    public function fetchAllDataTableByNetworkId($network_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
4751 efrain 1413
    {
1414
        $prototype = new User();
1415
        $select = $this->sql->select(self::_TABLE);
1416
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
4776 efrain 1417
        $select->where->equalTo('network_id', $network_id);
1418
        $select->where->equalTo('request_access', User::REQUEST_ACCESS_APPROVED);
5973 anderson 1419
 
1420
 
1421
        if ($search) {
4751 efrain 1422
            $select->where->nest()
5973 anderson 1423
                ->like('first_name', '%' . $search . '%')
1424
                ->or->like('last_name', '%' . $search . '%')
1425
                ->or->like('email', '%' . $search . '%')
1426
                ->unnest();
4751 efrain 1427
        }
5973 anderson 1428
 
1429
 
4751 efrain 1430
        $select->order($order_field . ' ' . $order_direction);
5973 anderson 1431
 
4751 efrain 1432
        $hydrator   = new ObjectPropertyHydrator();
1433
        $resultset  = new HydratingResultSet($hydrator, $prototype);
5973 anderson 1434
 
4751 efrain 1435
        $adapter = new DbSelect($select, $this->sql, $resultset);
1436
        $paginator = new Paginator($adapter);
1437
        $paginator->setItemCountPerPage($records_per_page);
1438
        $paginator->setCurrentPageNumber($page);
5973 anderson 1439
 
1440
 
4751 efrain 1441
        return $paginator;
1442
    }
5973 anderson 1443
 
1444
 
1445
 
1446
 
4776 efrain 1447
    /**
1448
     *
1449
     * @param int $network_id
1450
     * @param string $search
1451
     * @param int $page
1452
     * @param int $records_per_page
1453
     * @param string $order_field
1454
     * @param string $order_direction
1455
     * @return Paginator
1456
     */
5973 anderson 1457
    public function fetchAllDataTableRequestAccessPendingByNetworkId($network_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
4776 efrain 1458
    {
1459
        $prototype = new User();
1460
        $select = $this->sql->select(self::_TABLE);
1461
        $select->where->in('usertype_id', [UserType::ADMIN, UserType::USER]);
1462
        $select->where->equalTo('network_id', $network_id);
1463
        $select->where->equalTo('request_access', User::REQUEST_ACCESS_PENDING);
5973 anderson 1464
 
1465
        if ($search) {
4776 efrain 1466
            $select->where->nest()
5973 anderson 1467
                ->like('first_name', '%' . $search . '%')
1468
                ->or->like('last_name', '%' . $search . '%')
1469
                ->or->like('email', '%' . $search . '%')
1470
                ->unnest();
4776 efrain 1471
        }
5973 anderson 1472
 
1473
 
4776 efrain 1474
        $select->order($order_field . ' ' . $order_direction);
5973 anderson 1475
 
4776 efrain 1476
        $hydrator   = new ObjectPropertyHydrator();
1477
        $resultset  = new HydratingResultSet($hydrator, $prototype);
5973 anderson 1478
 
4776 efrain 1479
        $adapter = new DbSelect($select, $this->sql, $resultset);
1480
        $paginator = new Paginator($adapter);
1481
        $paginator->setItemCountPerPage($records_per_page);
1482
        $paginator->setCurrentPageNumber($page);
5973 anderson 1483
 
1484
 
4776 efrain 1485
        return $paginator;
1486
    }
4751 efrain 1487
 
5973 anderson 1488
 
4751 efrain 1489
    /**
1490
     *
4398 efrain 1491
     * @param int $id
1492
     * @return boolean
1493
     */
1494
    public function markIsAdult($id)
1495
    {
1496
        $update = $this->sql->update(self::_TABLE);
1497
        $update->set([
1498
            'id_adult' => User::IS_ADULT_YES,
1499
        ]);
1500
        $update->where->equalTo('id', $id);
5973 anderson 1501
 
4398 efrain 1502
        return $this->executeUpdate($update);
1503
    }
5973 anderson 1504
 
4398 efrain 1505
    /**
1506
     *
1507
     * @param int $id
1508
     * @return boolean
1509
     */
1510
    public function markIsNotAdult($id)
1511
    {
1512
        $update = $this->sql->update(self::_TABLE);
1513
        $update->set([
1514
            'id_adult' => User::IS_ADULT_NO,
1515
        ]);
1516
        $update->where->equalTo('id', $id);
5973 anderson 1517
 
4398 efrain 1518
        return $this->executeUpdate($update);
1519
    }
5973 anderson 1520
 
4398 efrain 1521
    /**
1 www 1522
     *
1523
     * @param int $id
1524
     * @return boolean
1525
     */
1526
    public function updateChatOnlineStatus($id)
1527
    {
1528
        $update = $this->sql->update(self::_TABLE);
1529
        $update->set([
5973 anderson 1530
            'online' => 1,
1 www 1531
        ]);
1532
        $update->where->equalTo('id', $id);
5973 anderson 1533
 
1 www 1534
        return $this->executeUpdate($update);
1535
    }
3086 efrain 1536
 
5973 anderson 1537
 
1538
 
1 www 1539
    /**
1540
     *
1541
     * @param int $id
1542
     * @return boolean
1543
     */
1544
    public function updateLastActivity($id)
1545
    {
1546
        $update = $this->sql->update(self::_TABLE);
1547
        $update->set([
1548
            'last_activity_on' => new Expression('NOW()'),
3086 efrain 1549
            'online' => 1,
1 www 1550
        ]);
1551
        $update->where->equalTo('id', $id);
5973 anderson 1552
 
1 www 1553
        return $this->executeUpdate($update);
1554
    }
5973 anderson 1555
 
1556
 
1 www 1557
    /**
1558
     *
1559
     * @param int $id
1560
     * @return boolean
1561
     */
1562
    public function updateLastHeartBeat($id)
1563
    {
1564
        $update = $this->sql->update(self::_TABLE);
1565
        $update->set([
1566
            'last_heart_beat_at' => new Expression('NOW()'),
242 efrain 1567
            'online' => 1,
1568
 
1 www 1569
        ]);
1570
        $update->where->equalTo('id', $id);
5973 anderson 1571
 
1 www 1572
        return $this->executeUpdate($update);
1573
    }
5973 anderson 1574
 
242 efrain 1575
    /**
1576
     *
1577
     * @param int $id
1578
     * @return boolean
1579
     */
1580
    public function emailVerifyAndActive($id)
1581
    {
1582
        $update = $this->sql->update(self::_TABLE);
1583
        $update->set([
1584
            'email_verified' => User::EMAIL_VERIFIED_YES,
5973 anderson 1585
            'status' => User::STATUS_ACTIVE,
242 efrain 1586
        ]);
1587
        $update->where->equalTo('id', $id);
5973 anderson 1588
 
242 efrain 1589
        return $this->executeUpdate($update);
1590
    }
5973 anderson 1591
 
1592
 
630 efrain 1593
    /**
1594
     *
1595
     * @param int $company_id
1596
     * @return User[]
1597
     */
1598
    public function fetchAllVerifyAndActiveByCompanyId($company_id)
1599
    {
1600
        $select = $this->sql->select();
1601
        $select->columns(['status', 'company_id']);
1602
        $select->from(['tb1' => CompanyUserMapper::_TABLE]);
5973 anderson 1603
        $select->join(['tb2' => self::_TABLE], 'tb1.user_id = tb2.id');
630 efrain 1604
 
5973 anderson 1605
 
1606
 
630 efrain 1607
        $select->where->equalTo('tb1.company_id', $company_id);
1608
        $select->where->equalTo('tb1.owner', CompanyUser::OWNER_NO);
1609
        $select->where->in('tb1.status', [CompanyUser::STATUS_ACCEPTED, CompanyUser::STATUS_ADMIN_WILL_ADD]);
1610
        $select->where->equalTo('tb2.email_verified', User::EMAIL_VERIFIED_YES);
1611
        $select->where->equalTo('tb2.status', User::STATUS_ACTIVE);
5973 anderson 1612
 
630 efrain 1613
        $select->order('first_name', 'last_name', 'email');
1614
 
1615
        $prototype = User();
5973 anderson 1616
 
630 efrain 1617
        return $this->executeFetchAllObject($select, $prototype);
1618
    }
1 www 1619
}