Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Ir a la última revisión | | 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
        }
279
 
280
        return $user_ids;
281
    }
282
 
283
    /**
284
     *
285
     * @param int $user_id
286
     * @return int[]
287
     */
288
    function fetchAllSecondDegreeConnectionsForUserIdReturnIds($user_id)
289
    {
290
        $user_ids = [];
291
        $connected_user_ids = $this->fetchAllConnectionsByUserReturnIds($user_id);
292
 
293
 
294
        if($connected_user_ids) {
295
            $select = $this->sql->select(self::_TABLE);
296
            $select->where->notEqualTo('request_from', $user_id)
297
            ->and->notEqualTo('request_to', $user_id)
298
            ->and->equalTo('status', Connection::STATUS_ACCEPTED)
299
            ->and->NEST->in('request_from', $connected_user_ids)->or->in('request_to', $connected_user_ids)->UNNEST;
300
 
301
 
302
            $connections = $this->executeFetchAllArray($select);
303
            foreach($connections as $connection)
304
            {
305
                $request_from   = $connection['request_from'];
306
                $request_to     = $connection['request_to'];
307
                if (in_array($request_from, $connected_user_ids)) {
308
                    $intermediate_user_id = $request_to;
309
                } else {
310
                    $intermediate_user_id = $request_from;
311
                }
312
 
313
                $checkIfRequestSent = $this->fetchOneByUserId1AndUserId2($intermediate_user_id, $user_id);
314
                if(!$checkIfRequestSent) {
315
 
316
                    if(!in_array($intermediate_user_id, $user_ids)) {
317
                        array_push($user_ids, $intermediate_user_id);
318
                    }
319
                }
320
            }
321
        }
322
        return $user_ids;
323
    }
324
 
325
    /**
326
     *
327
     * @param int $first_user_id
328
     * @param int $second_user_id
329
     * @return int[]
330
     */
331
    function fetchAllCommonConnectionsUserIdByUser1ReturnIds($first_user_id, $second_user_id)
332
    {
333
        $first_user_connections     = $this->fetchAllConnectionsByUserReturnIds($first_user_id);
334
        $second_user_connections    = $this->fetchAllConnectionsByUserReturnIds($second_user_id);
335
 
336
        return array_values(array_intersect($first_user_connections, $second_user_connections));
337
    }
338
 
339
    /**
340
     *
341
     * @param int $user_id
342
     * @return int[]
343
     */
344
    public function fetchAllSentsUserIdForUser($user_id)
345
    {
346
        $prototype = new Connection();
347
 
348
        $select = $this->sql->select(self::_TABLE);
349
        $select->where->equalTo('request_to', $user_id);
350
        $select->where->equalTo('status', Connection::STATUS_SENT);
351
 
352
        $connections = $this->executeFetchAllObject($select, $prototype);
353
 
354
 
355
 
356
        $user_ids = [];
357
        foreach($connections as $connection)
358
        {
359
            array_push($user_ids, $connection->request_from);
360
        }
361
 
362
        return $user_ids;
363
    }
364
 
365
    /**
366
     *
367
     * @param int $user_id
368
     * @return int[]
369
     */
370
    public function fetchAllReceiveInvitationsReturnIds($user_id)
371
    {
372
        $select = $this->sql->select(self::_TABLE);
373
        $select->columns(['request_from']);
374
        $select->where->equalTo('request_to', $user_id)->and->equalTo('status', Connection::STATUS_SENT);
375
 
376
        //echo $select->getSqlString($this->adapter->platform); exit;
377
 
378
        $user_ids  = [];
379
        $records = $this->executeFetchAllArray($select);
380
        foreach($records as $record)
381
        {
382
            array_push($user_ids, (int)  $record['request_from']);
383
        }
384
        return $user_ids;
385
    }
386
 
387
 
388
    /**
389
     *
390
     * @param int $user_id
391
     * @return int[]
392
     */
393
    public function fetchAllSentInvitationsReturnIds($user_id)
394
    {
395
        $select = $this->sql->select(self::_TABLE);
396
        $select->columns(['request_to']);
397
        $select->where->equalTo('request_from', $user_id)->and->equalTo('status', Connection::STATUS_SENT);
398
 
399
        $user_ids  = [];
400
        $records = $this->executeFetchAllArray($select);
401
        foreach($records as $record)
402
        {
403
            array_push($user_ids, (int)  $record['request_to']);
404
        }
405
        return $user_ids;
406
    }
407
 
408
    /**
409
     *
410
     * @param int $user_id
411
     * @return int
412
     */
413
    public function fetchConnectionRequestCount($user_id)
414
    {
415
        $select = $this->sql->select(self::_TABLE);
416
        $select->columns(['total' => new Expression('COUNT(*)')]);
417
        $select->where->equalTo('request_to', $user_id)->and->equalTo('status', Connection::STATUS_SENT);
418
 
419
        $record = $this->executeFetchOneArray($select);
420
        return $record['total'];
421
    }
422
 
423
    /**
424
     *
425
     * @param Connection $connection
426
     * @return boolean
427
     */
428
    public function approve($connection)
429
    {
430
        $update = $this->sql->update(self::_TABLE);
431
        $update->set(['status' => Connection::STATUS_ACCEPTED]);
432
        $update->where->equalTo('id', $connection->id);
433
 
434
        return $this->executeUpdate($update);
435
    }
436
 
437
 
438
    /**
439
     *
440
     * @param Connection $connection
441
     * @return boolean
442
     */
443
    public function reject($connection)
444
    {
445
        $update = $this->sql->update(self::_TABLE);
446
        $update->set(['status' => Connection::STATUS_REJECTED]);
447
        $update->where->equalTo('id', $connection->id);
448
 
449
        return $this->executeUpdate($update);
450
    }
451
 
452
    /**
453
     *
454
     * @param Connection $connection
455
     * @return boolean
456
     */
457
    public function cancel($connection)
458
    {
459
        $update = $this->sql->update(self::_TABLE);
460
        $update->set(['status' => Connection::STATUS_CANCELLED]);
461
        $update->where->equalTo('id', $connection->id);
462
 
463
        return $this->executeUpdate($update);
464
 
465
    }
466
 
467
 
468
    /**
469
     *
470
     * @param Connection $connection
471
     * @return boolean
472
     */
473
    public function delete($connection)
474
    {
475
        $delete = $this->sql->delete(self::_TABLE);
476
        $delete->where->equalTo('id', $connection->id);
477
 
478
        return $this->executeDelete($delete);
479
 
480
    }
481
 
482
 
483
    /**
484
     *
485
     * @param Connection $connection
486
     * @return boolean
487
     */
488
    public function update($connection)
489
    {
490
        $hydrator = new ObjectPropertyHydrator();
491
        $values = $hydrator->extract($connection);
492
        $values = $this->removeEmpty($values);
493
 
494
        $update = $this->sql->update(self::_TABLE);
495
        $update->set($values);
496
        $update->where->equalTo('id', $connection->id);
497
 
498
        return $this->executeUpdate($update);
499
 
500
    }
501
 
502
    /**
503
     *
504
     * @param int $user_id
505
     * @return int[]
506
     */
507
    public function getSecondDegreeConnectionsReturnIds($user_id)
508
    {
509
        $connected_uses_ids_array = $this->fetchAllConnectionsByUserReturnIds($user_id);
510
 
511
        $user_ids_array = [];
512
        if ($connected_uses_ids_array) {
513
 
514
            $queryMapper = QueryMapper::getInstance($this->adapter);
515
            $select = $queryMapper->getSql()->select(ConnectionMapper::_TABLE);
516
            $select->columns(['request_from', 'request_to']);
517
            $select->where->equalTo('status', Connection::STATUS_ACCEPTED)
518
            ->and->notEqualTo('request_from', $user_id)
519
            ->and->notEqualTo('request_to', $user_id)
520
            ->and->nest()->in('request_from',$connected_uses_ids_array)->or->in('request_to',$connected_uses_ids_array)->unnest();
521
 
522
 
523
            $connections = $queryMapper->fetchAll($select);
524
            if ($connections) {
525
                foreach ($connections as $connection) {
526
                    $request_from   = $connection['request_from'];
527
                    $request_to     = $connection['request_to'];
528
                    if (in_array($request_from, $connected_uses_ids_array)) {
529
                        $intermediate_user_id = $request_to;
530
                    } else {
531
                        $intermediate_user_id = $request_from;
532
                    }
533
                    if (!in_array($intermediate_user_id, $user_ids_array)) {
534
                        $select = $queryMapper->getSql()->select(ConnectionMapper::_TABLE);
535
                         $select->columns(['request_from', 'request_to']);
536
                         $select->where->nest()->equalTo('request_from', $user_id)->and->equalTo('request_to', $intermediate_user_id)->unnest()
537
                         ->or->nest()->equalTo('request_from', $intermediate_user_id)->and->equalTo('request_to', $user_id)->unnest();
538
 
539
                         $checkIfRequestSent = $queryMapper->fetchOne($select);
540
                         if (!$checkIfRequestSent) {
541
                             array_push($user_ids_array, $intermediate_user_id);
542
                        }
543
                    }
544
                }
545
            }
546
        }
547
        return $user_ids_array;
548
    }
549
 
550
    /**
551
     *
552
     * @param Connection $connection
553
     * @return boolean
554
     */
555
    public function insert($connection)
556
    {
557
        $hydrator = new ObjectPropertyHydrator();
558
        $values = $hydrator->extract($connection);
559
        $values = $this->removeEmpty($values);
560
 
561
        $insert = $this->sql->insert(self::_TABLE);
562
        $insert->values($values);
563
 
564
        $response = $this->executeInsert($insert);
565
        if($response) {
566
            $connection->id = $this->lastInsertId;
567
        }
568
 
569
        return $response;
570
    }
571
 
572
 
573
 
574
}