Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | | 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 LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use LeadersLinked\Mapper\Common\MapperCommon;
11
use LeadersLinked\Model\Connection;
12
 
13
class ConnectionMapper extends MapperCommon
14
{
15
    const _TABLE = 'tbl_connections';
16
 
17
 
18
    /**
19
     *
20
     * @var ConnectionMapper
21
     */
22
    private static $_instance;
23
 
24
    /**
25
     *
26
     * @param AdapterInterface $adapter
27
     */
28
    private function __construct($adapter)
29
    {
30
        parent::__construct($adapter);
31
    }
32
 
33
    /**
34
     *
35
     * @param AdapterInterface $adapter
36
     * @return ConnectionMapper
37
     */
38
    public static function getInstance($adapter)
39
    {
40
        if(self::$_instance == null) {
41
            self::$_instance = new ConnectionMapper($adapter);
42
        }
43
        return self::$_instance;
44
    }
45
 
46
 
47
    /*
48
    public function fetchAllConnectionByUserIdReturnIds(int $user_id, $return_full_array = false, int $currentpage = 1, int $limit = 10, string $status = Connection::STATUS_ACCEPTED)
49
    {
50
        $connections_array = [];
51
 
52
        $select = $this->sql->select();
53
        $select->columns(['request_from', 'request_to']);
54
        $select->from(self::_TABLE);
55
        $select->where->equalTo('status',  $status);
56
        $select->where->and->nest()
57
            ->equalTo('request_from', $user_id)
58
            ->or->equalTo('request_to', $user_id)
59
            ->unnest();
60
 
61
 
62
 
63
        $connections = $this->executeFetchAllArray($select);
64
        if ($connections) {
65
            for ($i = 0, $max = count($connections); $i < $max; $i++)
66
            {
67
                $request_from = $connections[$i]['request_from'];
68
                $request_to = $connections[$i]['request_to'];
69
                if ($request_from == $user_id) {
70
                    array_push($connections_array, $request_to);
71
                } else {
72
                    array_push($connections_array, $request_from);
73
                }
74
            }
75
        }
76
        if ($return_full_array) {
77
            $offset = ( $currentpage - 1 ) * $limit;
78
            $connections_array = array_slice($connections_array, $offset, $limit);
79
        }
80
        return $connections_array;
81
    }
82
 
83
    function fetchAllConnectionByUserIdsReturnIds(array $userIds, int $user_id, $return_full_array = false, int $currentpage = 1, int $limit = 10, string $status = Connection::STATUS_ACCEPTED)
84
    {
85
        $select = $this->sql->select();
86
        $select->columns(['request_from', 'request_to']);
87
        $select->from(self::_TABLE);
88
        $select->where->equalTo('status',  $status);
89
        $select->where->and->nest()
90
            ->nest()
91
            ->equalTo('request_from', $user_id)
92
            ->and->in('request_to', $userIds)
93
            ->unnest()
94
            ->or->nest()
95
            ->equalTo('request_to', $user_id)
96
            ->and->in('request_from', $userIds)
97
            ->unnest()
98
       ->unnest();
99
 
100
       $connections_array = [];
101
       $connections = $this->executeFetchAllArray($select);
102
       if ($connections) {
103
           for ($i = 0, $max = count($connections); $i < $max; $i++)
104
           {
105
               $request_from = $connections[$i]['request_from'];
106
               $request_to = $connections[$i]['request_to'];
107
               if ($request_from == $user_id) {
108
                   array_push($connections_array, $request_to);
109
               } else {
110
                   array_push($connections_array, $request_from);
111
               }
112
           }
113
       }
114
       if ($return_full_array) {
115
           $offset = ( $currentpage - 1 ) * $limit;
116
           $connections_array = array_slice($connections_array, $offset, $limit);
117
       }
118
       return $connections_array;
119
    }
120
 
121
 
122
    public function fetchAllPendingInvitationsByIdUser(int $user_id, bool $return_full_array = false, int $currentpage = 1, int $limit = 10, string $status = Connection::STATUS_ACCEPTED)
123
    {
124
 
125
        $select = $this->sql->select(self::_TABLE);
126
        $select->columns(['request_from']);
127
        $select->where->equalTo('request_to', $user_id)->and->equalTo('status',  $status);
128
 
129
        $invitations_array = [];
130
        $invitations = $this->executeFetchAllArray($select);
131
        if ($invitations) {
132
            for ($i = 0 , $max = count($invitations) ; $i < $max ; $i++)
133
            {
134
                $request_from = $invitations[$i]['request_from'];
135
                array_push($invitations_array, $request_from);
136
            }
137
        }
138
        if ($return_full_array) {
139
            $offset = ( $currentpage - 1 ) * $limit;
140
            $invitations_array = array_slice($invitations_array, $offset, $limit);
141
        }
142
        return $invitations_array;
143
    }
144
 
145
 
146
    function fetchAllSentInvitationsByUserIds(int $user_id, bool $return_full_array = false, int $currentpage = 1, int $limit = 10, string $status = Connection::STATUS_ACCEPTED)
147
    {
148
        $select = $this->sql->select(self::_TABLE);
149
        $select->columns(['request_from']);
150
        $select->where->equalTo('request_from', $user_id)->and->equalTo('status',  $status);
151
 
152
        $invitations_array = [];
153
        $invitations = $this->executeFetchAllArray($select);
154
        if ($invitations) {
155
            for ($i = 0 , $max = count($invitations) ; $i < $max ; $i++)
156
            {
157
                $request_from = $invitations[$i]['request_to'];
158
                array_push($invitations_array, $request_from);
159
            }
160
        }
161
        if ($return_full_array) {
162
            $offset = ( $currentpage - 1 ) * $limit;
163
            $invitations_array = array_slice($invitations_array, $offset, $limit);
164
        }
165
        return $invitations_array;
166
 
167
    }
168
 
169
    /**
170
     * @param int $user1
171
     * @param int $user2
172
     * @return Connection
173
     */
174
    public function fetchOneByUserId1AndUserId2($user1, $user2)
175
    {
176
        $prototype = new Connection();
177
        $select = $this->sql->select(self::_TABLE);
178
        $select->where->and->nest()
179
        ->nest()
180
        ->equalTo('request_from', $user1)
181
        ->and->equalTo('request_to', $user2)
182
        ->unnest()
183
        ->or->nest()
184
        ->equalTo('request_to', $user1)
185
        ->and->equalTo('request_from', $user2)
186
        ->unnest()
187
        ->unnest();
188
 
189
 
190
        return $this->executeFetchOneObject($select, $prototype);
191
    }
192
 
193
 
194
    /**
195
     *
196
     * @param int $user_id
197
     * @return int
198
     */
199
    public function fetchTotalConnectionByUser($user_id)
200
    {
201
        $select1 = $this->sql->select(self::_TABLE);
202
        $select1->columns(['total' => new Expression('COUNT(*)')]);
203
        $select1->where->equalTo('request_from', $user_id);
204
        $select1->where->equalTo('status', Connection::STATUS_ACCEPTED);
205
 
206
        $total = 0;
207
        $record = $this->executeFetchOneArray($select1);
208
        $total += $record['total'];
209
 
210
        $select2 = $this->sql->select(self::_TABLE);
211
        $select2->columns(['total' => new Expression('COUNT(*)')]);
212
        $select2->where->equalTo('request_to', $user_id);
213
        $select2->where->equalTo('status', Connection::STATUS_ACCEPTED);
214
        $record = $this->executeFetchOneArray($select2);
215
        $total += $record['total'];
216
 
217
        return  $total;
218
    }
219
 
220
    /**
221
     *
222
     * @param int $user_id
223
     * @return Connection[]
224
     */
225
    public function fetchAllConnectionsByUser($user_id)
226
    {
227
        $prototype = new Connection();
228
 
229
        $select1 = $this->sql->select(self::_TABLE);
230
        $select1->where->equalTo('request_from', $user_id);
231
        $select1->where->equalTo('status', Connection::STATUS_ACCEPTED);
232
 
233
        $connections1 = $this->executeFetchAllObject($select1, $prototype);
234
 
235
 
236
 
237
        $select2 = $this->sql->select(self::_TABLE);
238
        $select2->columns(['user_id' => 'request_from']);
239
        $select2->where->equalTo('request_to', $user_id);
240
        $select2->where->equalTo('status', Connection::STATUS_ACCEPTED);
241
 
242
        $connections2 = $this->executeFetchAllObject($select2, $prototype);
243
 
244
        return array_merge($connections1, $connections2);
245
    }
246
 
247
    /**
248
     *
249
     * @param int $user_id
250
     * @return int[]
251
     */
252
    public function fetchAllConnectionsByUserReturnIds($user_id)
253
    {
254
        $user_ids = [];
255
 
256
        $select1 = $this->sql->select(self::_TABLE);
257
        $select1->columns(['request_to']);
258
        $select1->where->equalTo('request_from', $user_id);
259
        $select1->where->equalTo('status', Connection::STATUS_ACCEPTED);
260
 
261
        $connections = $this->executeFetchAllArray($select1);
262
        foreach($connections as $connection)
263
        {
264
            array_push($user_ids, $connection['request_to']);
265
        }
266
 
267
 
268
 
269
        $select2 = $this->sql->select(self::_TABLE);
270
        $select2->columns(['user_id' => 'request_from']);
271
        $select2->where->equalTo('request_to', $user_id);
272
        $select2->where->equalTo('status', Connection::STATUS_ACCEPTED);
273
 
274
        $connections = $this->executeFetchAllArray($select2);
275
        foreach($connections as $connection)
276
        {
277
            array_push($user_ids, $connection['user_id']);
278
        }
6012 efrain 279
 
280
 
281
 
1 www 282
 
283
        return $user_ids;
284
    }
285
 
286
    /**
287
     *
288
     * @param int $user_id
289
     * @return int[]
290
     */
291
    function fetchAllSecondDegreeConnectionsForUserIdReturnIds($user_id)
292
    {
293
        $user_ids = [];
294
        $connected_user_ids = $this->fetchAllConnectionsByUserReturnIds($user_id);
295
 
296
 
297
        if($connected_user_ids) {
298
            $select = $this->sql->select(self::_TABLE);
299
            $select->where->notEqualTo('request_from', $user_id)
300
            ->and->notEqualTo('request_to', $user_id)
301
            ->and->equalTo('status', Connection::STATUS_ACCEPTED)
302
            ->and->NEST->in('request_from', $connected_user_ids)->or->in('request_to', $connected_user_ids)->UNNEST;
303
 
304
 
305
            $connections = $this->executeFetchAllArray($select);
306
            foreach($connections as $connection)
307
            {
308
                $request_from   = $connection['request_from'];
309
                $request_to     = $connection['request_to'];
310
                if (in_array($request_from, $connected_user_ids)) {
311
                    $intermediate_user_id = $request_to;
312
                } else {
313
                    $intermediate_user_id = $request_from;
314
                }
315
 
316
                $checkIfRequestSent = $this->fetchOneByUserId1AndUserId2($intermediate_user_id, $user_id);
317
                if(!$checkIfRequestSent) {
318
 
319
                    if(!in_array($intermediate_user_id, $user_ids)) {
320
                        array_push($user_ids, $intermediate_user_id);
321
                    }
322
                }
323
            }
324
        }
325
        return $user_ids;
326
    }
327
 
328
    /**
329
     *
330
     * @param int $first_user_id
331
     * @param int $second_user_id
332
     * @return int[]
333
     */
334
    function fetchAllCommonConnectionsUserIdByUser1ReturnIds($first_user_id, $second_user_id)
335
    {
336
        $first_user_connections     = $this->fetchAllConnectionsByUserReturnIds($first_user_id);
337
        $second_user_connections    = $this->fetchAllConnectionsByUserReturnIds($second_user_id);
338
 
339
        return array_values(array_intersect($first_user_connections, $second_user_connections));
340
    }
341
 
342
    /**
343
     *
344
     * @param int $user_id
345
     * @return int[]
346
     */
347
    public function fetchAllSentsUserIdForUser($user_id)
348
    {
349
        $prototype = new Connection();
350
 
351
        $select = $this->sql->select(self::_TABLE);
352
        $select->where->equalTo('request_to', $user_id);
353
        $select->where->equalTo('status', Connection::STATUS_SENT);
354
 
355
        $connections = $this->executeFetchAllObject($select, $prototype);
356
 
357
 
358
 
359
        $user_ids = [];
360
        foreach($connections as $connection)
361
        {
362
            array_push($user_ids, $connection->request_from);
363
        }
364
 
365
        return $user_ids;
366
    }
367
 
368
    /**
369
     *
370
     * @param int $user_id
371
     * @return int[]
372
     */
373
    public function fetchAllReceiveInvitationsReturnIds($user_id)
374
    {
375
        $select = $this->sql->select(self::_TABLE);
376
        $select->columns(['request_from']);
377
        $select->where->equalTo('request_to', $user_id)->and->equalTo('status', Connection::STATUS_SENT);
378
 
379
        //echo $select->getSqlString($this->adapter->platform); exit;
380
 
381
        $user_ids  = [];
382
        $records = $this->executeFetchAllArray($select);
383
        foreach($records as $record)
384
        {
385
            array_push($user_ids, (int)  $record['request_from']);
386
        }
387
        return $user_ids;
388
    }
389
 
390
 
391
    /**
392
     *
393
     * @param int $user_id
394
     * @return int[]
395
     */
396
    public function fetchAllSentInvitationsReturnIds($user_id)
397
    {
398
        $select = $this->sql->select(self::_TABLE);
399
        $select->columns(['request_to']);
400
        $select->where->equalTo('request_from', $user_id)->and->equalTo('status', Connection::STATUS_SENT);
401
 
402
        $user_ids  = [];
403
        $records = $this->executeFetchAllArray($select);
404
        foreach($records as $record)
405
        {
406
            array_push($user_ids, (int)  $record['request_to']);
407
        }
408
        return $user_ids;
409
    }
410
 
411
    /**
412
     *
413
     * @param int $user_id
414
     * @return int
415
     */
416
    public function fetchConnectionRequestCount($user_id)
417
    {
418
        $select = $this->sql->select(self::_TABLE);
419
        $select->columns(['total' => new Expression('COUNT(*)')]);
420
        $select->where->equalTo('request_to', $user_id)->and->equalTo('status', Connection::STATUS_SENT);
421
 
422
        $record = $this->executeFetchOneArray($select);
423
        return $record['total'];
424
    }
425
 
426
    /**
427
     *
428
     * @param Connection $connection
429
     * @return boolean
430
     */
431
    public function approve($connection)
432
    {
433
        $update = $this->sql->update(self::_TABLE);
434
        $update->set(['status' => Connection::STATUS_ACCEPTED]);
435
        $update->where->equalTo('id', $connection->id);
436
 
437
        return $this->executeUpdate($update);
438
    }
439
 
440
 
441
    /**
442
     *
443
     * @param Connection $connection
444
     * @return boolean
445
     */
446
    public function reject($connection)
447
    {
448
        $update = $this->sql->update(self::_TABLE);
449
        $update->set(['status' => Connection::STATUS_REJECTED]);
450
        $update->where->equalTo('id', $connection->id);
451
 
452
        return $this->executeUpdate($update);
453
    }
454
 
455
    /**
456
     *
457
     * @param Connection $connection
458
     * @return boolean
459
     */
460
    public function cancel($connection)
461
    {
462
        $update = $this->sql->update(self::_TABLE);
463
        $update->set(['status' => Connection::STATUS_CANCELLED]);
464
        $update->where->equalTo('id', $connection->id);
465
 
466
        return $this->executeUpdate($update);
467
 
468
    }
469
 
470
 
471
    /**
472
     *
473
     * @param Connection $connection
474
     * @return boolean
475
     */
476
    public function delete($connection)
477
    {
478
        $delete = $this->sql->delete(self::_TABLE);
479
        $delete->where->equalTo('id', $connection->id);
480
 
481
        return $this->executeDelete($delete);
482
 
483
    }
484
 
485
 
486
    /**
487
     *
488
     * @param Connection $connection
489
     * @return boolean
490
     */
491
    public function update($connection)
492
    {
493
        $hydrator = new ObjectPropertyHydrator();
494
        $values = $hydrator->extract($connection);
495
        $values = $this->removeEmpty($values);
496
 
497
        $update = $this->sql->update(self::_TABLE);
498
        $update->set($values);
499
        $update->where->equalTo('id', $connection->id);
500
 
501
        return $this->executeUpdate($update);
502
 
503
    }
504
 
505
    /**
506
     *
507
     * @param int $user_id
508
     * @return int[]
509
     */
510
    public function getSecondDegreeConnectionsReturnIds($user_id)
511
    {
512
        $connected_uses_ids_array = $this->fetchAllConnectionsByUserReturnIds($user_id);
513
 
514
        $user_ids_array = [];
515
        if ($connected_uses_ids_array) {
516
 
517
            $queryMapper = QueryMapper::getInstance($this->adapter);
518
            $select = $queryMapper->getSql()->select(ConnectionMapper::_TABLE);
519
            $select->columns(['request_from', 'request_to']);
520
            $select->where->equalTo('status', Connection::STATUS_ACCEPTED)
521
            ->and->notEqualTo('request_from', $user_id)
522
            ->and->notEqualTo('request_to', $user_id)
523
            ->and->nest()->in('request_from',$connected_uses_ids_array)->or->in('request_to',$connected_uses_ids_array)->unnest();
524
 
525
 
526
            $connections = $queryMapper->fetchAll($select);
527
            if ($connections) {
528
                foreach ($connections as $connection) {
529
                    $request_from   = $connection['request_from'];
530
                    $request_to     = $connection['request_to'];
531
                    if (in_array($request_from, $connected_uses_ids_array)) {
532
                        $intermediate_user_id = $request_to;
533
                    } else {
534
                        $intermediate_user_id = $request_from;
535
                    }
536
                    if (!in_array($intermediate_user_id, $user_ids_array)) {
537
                        $select = $queryMapper->getSql()->select(ConnectionMapper::_TABLE);
538
                         $select->columns(['request_from', 'request_to']);
539
                         $select->where->nest()->equalTo('request_from', $user_id)->and->equalTo('request_to', $intermediate_user_id)->unnest()
540
                         ->or->nest()->equalTo('request_from', $intermediate_user_id)->and->equalTo('request_to', $user_id)->unnest();
541
 
542
                         $checkIfRequestSent = $queryMapper->fetchOne($select);
543
                         if (!$checkIfRequestSent) {
544
                             array_push($user_ids_array, $intermediate_user_id);
545
                        }
546
                    }
547
                }
548
            }
549
        }
550
        return $user_ids_array;
551
    }
552
 
553
    /**
554
     *
555
     * @param Connection $connection
556
     * @return boolean
557
     */
558
    public function insert($connection)
559
    {
560
        $hydrator = new ObjectPropertyHydrator();
561
        $values = $hydrator->extract($connection);
562
        $values = $this->removeEmpty($values);
563
 
564
        $insert = $this->sql->insert(self::_TABLE);
565
        $insert->values($values);
566
 
567
        $response = $this->executeInsert($insert);
568
        if($response) {
569
            $connection->id = $this->lastInsertId;
570
        }
571
 
572
        return $response;
573
    }
574
 
575
 
576
 
577
}