Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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