Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\JsonModel;
11
use Laminas\View\Model\ViewModel;
12
 
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Model\Post;
15
use LeadersLinked\Mapper\PostMapper;
16
use LeadersLinked\Mapper\UserMapper;
17
use LeadersLinked\Mapper\CommentMapper;
18
use LeadersLinked\Model\ContentReaction;
19
use LeadersLinked\Model\Comment;
20
use LeadersLinked\Form\Post\CommentForm;
21
use LeadersLinked\Mapper\ContentReactionMapper;
242 efrain 22
use LeadersLinked\Model\User;
1 efrain 23
 
24
 
25
 
26
class PostController extends AbstractActionController
27
{
28
    /**
29
     *
30
     * @var \Laminas\Db\Adapter\AdapterInterface
31
     */
32
    private $adapter;
33
 
34
    /**
35
     *
36
     * @var \LeadersLinked\Cache\CacheInterface
37
     */
38
    private $cache;
39
 
40
 
41
    /**
42
     *
43
     * @var \Laminas\Log\LoggerInterface
44
     */
45
    private $logger;
46
 
47
    /**
48
     *
49
     * @var array
50
     */
51
    private $config;
52
 
53
 
54
    /**
55
     *
56
     * @var \Laminas\Mvc\I18n\Translator
57
     */
58
    private $translator;
59
 
60
 
61
    /**
62
     *
63
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
64
     * @param \LeadersLinked\Cache\CacheInterface $cache
65
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
66
     * @param array $config
67
     * @param \Laminas\Mvc\I18n\Translator $translator
68
     */
69
    public function __construct($adapter, $cache, $logger, $config, $translator)
70
    {
71
        $this->adapter      = $adapter;
72
        $this->cache        = $cache;
73
        $this->logger       = $logger;
74
        $this->config       = $config;
75
        $this->translator   = $translator;
76
    }
77
 
78
    /**
79
     *
80
     * Generación del listado de perfiles
81
     * {@inheritDoc}
82
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
83
     */
84
    public function indexAction()
85
    {
86
 
87
        return new JsonModel([
88
            'success' => false,
89
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
90
        ]);
91
    }
242 efrain 92
 
93
    public function reactionsAction()
94
    {
95
        $id = $this->params()->fromRoute('id');
96
 
97
        $request = $this->getRequest();
98
        $request = $this->getRequest();
99
        if ($request->isGet()) {
100
 
101
            $currentUserPlugin = $this->plugin('currentUserPlugin');
102
            $currentUser = $currentUserPlugin->getUser();
103
 
104
            $postMapper = PostMapper::getInstance($this->adapter);
105
            $post = $postMapper->fetchOneByUuid($id);
106
 
107
 
108
            if (!$post || $post->status != Post::STATUS_ACTIVE) {
109
                return new JsonModel([
110
                    'success' => false,
111
                    'data' => 'ERROR_POST_NOT_AVAILABLE'
112
                ]);
113
            }
114
 
115
 
116
            $userMapper = UserMapper::getInstance($this->adapter);
117
 
118
            $items = [];
119
 
120
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
121
            $records = $contentReactionMapper->fetchAllByPostId($post->id);
122
 
123
            foreach($records as $record)
124
            {
125
                $user = $userMapper->fetchOne($record->user_id);
126
                if($user && $user->status == User::STATUS_ACTIVE) {
127
 
128
                    array_push($items, [
129
                        'first_name' => $user->first_name,
130
                        'last_name' => $user->last_name,
131
                        'email' => $user->email,
132
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image], ['force_canonical' => true]),
133
                        'reaction' => $record->reaction,
134
                    ]);
135
                }
136
            }
137
 
138
            $response = [
139
                'success' => true,
140
                'data' => $items
141
            ];
142
 
143
            return new JsonModel($response);
144
 
145
 
146
 
147
 
148
        } else {
149
            $response = [
150
                'success' => false,
151
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
152
            ];
153
 
154
            return new JsonModel($response);
155
        }
156
    }
1 efrain 157
 
158
    public function viewAction()
159
    {
160
        $request = $this->getRequest();
161
        if ($request->isGet()) {
162
            $currentUserPlugin = $this->plugin('currentUserPlugin');
163
            $currentUser = $currentUserPlugin->getUser();
164
 
165
 
166
            $id = $this->params()->fromRoute('id');
167
 
168
            $postMapper = PostMapper::getInstance($this->adapter);
169
            $post = $postMapper->fetchOneByUuid($id);
170
 
171
 
172
                if (!$post || $post->status != Post::STATUS_ACTIVE) {
173
                    return new JsonModel([
174
                        'success' => false,
175
                        'data' => 'ERROR_POST_NOT_AVAILABLE'
176
                    ]);
177
                }
178
 
179
 
180
            $timestamp = time();
181
 
182
            list($usec, $sec) = explode(' ', microtime());
183
            $seed = intval($sec + ((float) $usec * 100000));
184
            mt_srand($seed, MT_RAND_MT19937);
185
            $rand =  mt_rand();
186
 
187
 
188
 
189
            $password  = md5('user-' . $currentUser->uuid . '-post-' . $post->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key);
190
 
191
 
192
            $share_params = [
193
                'type' => 'post',
194
                'code' => $post->uuid,
195
                'user' => $currentUser->uuid,
196
                'timestamp' => $timestamp,
197
                'rand' => $rand,
198
                'password' => $password,
199
            ];
200
            $share_increment_external_counter_url = $this->url()->fromRoute('share/increment-external-counter', $share_params, ['force_canonical' => true]);
201
 
202
 
203
            $share_external_url = $this->url()->fromRoute('shorter/generate', ['code' => $post->uuid, 'type' => 'post'], ['force_canonical' => true]);
204
 
205
            //$contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
206
            //$contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
207
 
208
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
209
            $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
210
            $reaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
234 efrain 211
 
240 efrain 212
 
213
 
214
 
234 efrain 215
            $type = '';
240 efrain 216
 
234 efrain 217
            if($post->file) {
240 efrain 218
                $filename = trim(strtolower(basename($post->file)));
219
                $parts = explode('.', $filename);
220
 
221
 
222
                $max = is_array($parts) ? count($parts) : 0;
223
 
224
 
225
                if($max > 1) {
226
                    $extension = $parts[ $max -1 ];
234 efrain 227
                    if(in_array($extension, ['mov', 'webm','mp4','mpeg'] )) {
228
                        $type = 'video';
229
                    } else if(in_array($extension, ['jpg','jpeg','png'] )) {
230
                        $type = 'image';
231
                    } else if(in_array($extension, ['wav', 'mp3', 'pdf'] )) {
232
                        $type = 'audio';
233
                    }
234
                }
1 efrain 235
 
234 efrain 236
            }
237
 
240 efrain 238
 
239
 
240
 
1 efrain 241
 
234 efrain 242
 
243
 
244
 
1 efrain 245
                return new JsonModel([
246
                    'success' => true,
247
                    'data' => [
248
                        'post' => $post,
249
                        'id' => $post->id,
250
                        'uuid' => $post->uuid,
251
                        'title' => $post->title,
252
                        'description' => $post->description,
253
                        'url' => $post->url,
254
                        'date' => $post->date,
234 efrain 255
                        'type' => $post->type,
1 efrain 256
                        'status' => $post->status,
140 efrain 257
                        'image' =>   $this->url()->fromRoute('storage',['type' => 'post', 'code' => $post->uuid, 'filename' => $post->image], ['force_canonical' => true]),
258
                        'file' => $post->file ? $this->url()->fromRoute('storage',['type' => 'post', 'code' => $post->uuid, 'filename' => $post->file], ['force_canonical' => true]) : '',
234 efrain 259
                        'type' => $type,
1 efrain 260
                        'added_on' => $post->added_on,
261
                        'share_external_url' =>  $share_external_url,
262
                        'total_share_external' => $post->total_external_shared,
263
                        'share_increment_external_counter_url' => $share_increment_external_counter_url,
242 efrain 264
                        'comments_url' => $this->url()->fromRoute('post/comments', ['id' => $post->uuid],['force_canonical' => true]),
265
                        'comments_add_url' => $this->url()->fromRoute('post/comments/add', ['id' => $post->uuid],['force_canonical' => true]),
266
                        'save_reaction_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid],['force_canonical' => true]),
267
                        'delete_reaction_url' =>  $this->url()->fromRoute('post/delete-reaction', ['id' => $post->uuid],['force_canonical' => true]),
268
                        'reactions_url' => $this->url()->fromRoute('post/reactions', ['id' => $post->uuid],['force_canonical' => true]),
1 efrain 269
                        'my_reaction' => $reaction ? $reaction->reaction : '',
270
                        'reactions' =>  $reactions,
271
                        //'is_liked' => $contentReaction ? 1 : 0,
272
                        //'like_url' => $this->url()->fromRoute('post/like', ['id' => $post->uuid]),
273
                        //'unlike_url' => $this->url()->fromRoute('post/unlike', ['id' => $post->uuid]),
274
 
275
                    ]
276
                ]);
277
        } else {
278
            $response = [
279
                'success' => false,
280
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
281
            ];
282
            return new JsonModel($response);
283
        }
284
    }
285
 
286
    public function commentsAddAction()
287
    {
288
        $id = $this->params()->fromRoute('id');
289
 
290
        $request = $this->getRequest();
291
        if ($request->isPost()) {
292
 
293
 
294
 
295
            $postMapper = PostMapper::getInstance($this->adapter);
296
            $now = $postMapper->getDatebaseNow();
297
 
298
            $post = $postMapper->fetchOneByUuid($id);
299
            if (!$post) {
300
                $response = [
301
                    'success' => false,
302
                    'data' => 'ERROR_POST_NOT_FOUND'
303
                ];
304
                return new JsonModel($response);
305
            }
306
 
307
            $dataPost = $request->getPost()->toArray();
308
            $form = new CommentForm();
309
            $form->setData($dataPost);
310
 
311
            if ($form->isValid()) {
312
 
313
 
314
                $currentUserPlugin = $this->plugin('currentUserPlugin');
315
                $currentUser = $currentUserPlugin->getUser();
316
 
317
                $dataPost = (array) $form->getData();
318
 
319
 
320
 
321
                $comment = new Comment();
322
                $comment->network_id = $currentUser->network_id;
323
                $comment->comment = $dataPost['comment'];
324
                $comment->user_id = $currentUser->id;
325
                $comment->post_id = $post->id;
326
                $comment->relational = Comment::RELATIONAL_POST;
327
 
328
                $commentMapper = CommentMapper::getInstance($this->adapter);
329
                if ($commentMapper->insert($comment)) {
330
 
331
                    $total_comments = $commentMapper->fetchCountCommentByPostId($comment->post_id);
332
 
333
                    $post->total_comments = $total_comments;
334
                    $postMapper->update($post);
335
 
336
                    $response = [
337
                        'success'   => true,
338
                        'data'   => $this->renderComment($comment->id, $now),
339
                        'total_comments' => $total_comments
340
                    ];
341
 
342
                    return new JsonModel($response);
343
                } else {
344
 
345
                    $response = [
346
                        'success'   => false,
347
                        'data'   => $commentMapper->getError()
348
                    ];
349
 
350
                    return new JsonModel($response);
351
                }
352
            } else {
353
                $message = '';;
354
                $form_messages = (array) $form->getMessages();
355
                foreach ($form_messages  as $fieldname => $field_messages) {
356
                    foreach ($field_messages as $key => $value) {
357
                        $message = $value;
358
                    }
359
                }
360
 
361
                $response = [
362
                    'success'   => false,
363
                    'data'   => $message
364
                ];
365
 
366
                return new JsonModel($response);
367
            }
368
        } else {
369
            $response = [
370
                'success' => false,
371
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
372
            ];
373
 
374
            return new JsonModel($response);
375
        }
376
    }
377
 
378
 
379
 
380
    public function commentsDeleteAction()
381
    {
382
        $request = $this->getRequest();
383
        if ($request->isPost()) {
384
            $currentUserPlugin = $this->plugin('currentUserPlugin');
385
            $currentUser = $currentUserPlugin->getUser();
386
 
387
            $post_id = $this->params()->fromRoute('id');
388
            $comment = $this->params()->fromRoute('comment');
389
 
390
            $postMapper = PostMapper::getInstance($this->adapter);
391
            $post = $postMapper->fetchOneByUuidAndNetworkId($post_id, $currentUser->network_id);
392
 
393
            if ($post) {
394
 
395
                $commentMapper = CommentMapper::getInstance($this->adapter);
396
                $comment = $commentMapper->fetchOneByUuid($comment);
397
 
398
                if ($comment && $comment->post_id == $post->id && $comment->user_id == $currentUser->id) {
399
 
400
                    $comment->status = Comment::STATUS_DELETED;
401
 
402
                    if ($commentMapper->update($comment)) {
403
 
404
                        $total_comments = $commentMapper->fetchCountCommentByPostId($comment->post_id);
405
 
406
 
407
                        $postMapper = PostMapper::getInstance($this->adapter);
408
                        $post = $postMapper->fetchOne($comment->post_id);
409
                        $post->total_comments = $total_comments;
410
                        $postMapper->update($post);
411
 
412
 
413
 
414
 
415
 
416
                        $response = [
417
                            'success' => true,
418
                            'data' => 'LABEL_COMMENT_WAS_DELETED',
419
                            'total_comments' => $total_comments
420
                        ];
421
                    } else {
422
                        $response = [
423
                            'success' => false,
424
                            'data' => $commentMapper->getError()
425
                        ];
426
                    }
427
                } else {
428
                    $response = [
429
                        'success' => false,
430
                        'data' => 'ERROR_COMMENT_NOT_FOUND'
431
                    ];
432
                }
433
            } else {
434
                $response = [
435
                    'success' => false,
436
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
437
                ];
438
            }
439
        } else {
440
            $response = [
441
                'success' => false,
442
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
443
            ];
444
        }
445
 
446
        return new JsonModel($response);
447
    }
448
 
449
    /*
450
    public function likeAction()
451
    {
452
        $id = $this->params()->fromRoute('id');
453
 
454
        $request = $this->getRequest();
455
        if ($request->isPost()) {
456
            $currentUserPlugin = $this->plugin('currentUserPlugin');
457
            $currentUser = $currentUserPlugin->getUser();
458
 
459
 
460
            $postMapper = PostMapper::getInstance($this->adapter);
461
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
462
            if (!$post) {
463
                $response = [
464
                    'success' => false,
465
                    'data' => 'ERROR_POST_NOT_FOUND'
466
                ];
467
                return new JsonModel($response);
468
            }
469
 
470
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
471
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
472
 
473
            if ($contentReaction) {
474
                $response = [
475
                    'success' => false,
476
                    'data' => 'ERROR_DUPLICATE_ACTION'
477
                ];
478
                return new JsonModel($response);
479
            }
480
 
481
            $contentReaction = new Like();
482
            $contentReaction->user_id = $currentUser->id;
483
            $contentReaction->post_id = $post->id;
484
            $contentReaction->relational = Like::RELATIONAL_POST;
485
 
486
            if ($contentReactionMapper->insert($contentReaction)) {
487
 
488
                $contentReactions = $contentReactionMapper->fetchCountLikeByPostId($post->id);
489
                $response = [
490
                    'success' => true,
491
                    'data' => [
492
                        'likes' => $contentReactions
493
                    ]
494
                ];
495
            } else {
496
                $response = [
497
                    'success' => false,
498
                    'data' => $contentReactionMapper->getError()
499
                ];
500
            }
501
            return new JsonModel($response);
502
        }
503
 
504
        $response = [
505
            'success' => false,
506
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
507
        ];
508
        return new JsonModel($response);
509
    }
510
 
511
    public function unlikeAction()
512
    {
513
        $id = $this->params()->fromRoute('id');
514
 
515
        $request = $this->getRequest();
516
        if ($request->isPost()) {
517
            $currentUserPlugin = $this->plugin('currentUserPlugin');
518
            $currentUser = $currentUserPlugin->getUser();
519
 
520
            $postMapper = PostMapper::getInstance($this->adapter);
521
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
522
            if (!$post) {
523
                $response = [
524
                    'success' => false,
525
                    'data' => 'ERROR_POST_NOT_FOUND'
526
                ];
527
                return new JsonModel($response);
528
            }
529
 
530
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
531
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
532
 
533
            if (!$contentReaction) {
534
                $response = [
535
                    'success' => false,
536
                    'data' => 'ERROR_DUPLICATE_ACTION'
537
                ];
538
                return new JsonModel($response);
539
            }
540
 
541
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
542
                $contentReactions = $contentReactionMapper->fetchCountLikeByPostId($post->id);
543
 
544
 
545
                $response = [
546
                    'success' => true,
547
                    'data' => [
548
                        'likes' => $contentReactions
549
                    ]
550
                ];
551
            } else {
552
                $response = [
553
                    'success' => false,
554
                    'data' => $contentReactionMapper->getError()
555
                ];
556
            }
557
            return new JsonModel($response);
558
        }
559
 
560
        $response = [
561
            'success' => false,
562
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
563
        ];
564
        return new JsonModel($response);
565
    }*/
566
 
567
    public function saveReactionAction()
568
    {
569
        $id = $this->params()->fromRoute('id');
570
        $reaction  = $this->params()->fromPost('reaction');
571
 
572
        $request = $this->getRequest();
573
        if ($request->isPost()) {
574
            $currentUserPlugin = $this->plugin('currentUserPlugin');
575
            $currentUser = $currentUserPlugin->getUser();
576
 
577
 
578
            $postMapper = PostMapper::getInstance($this->adapter);
579
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
580
            if (!$post) {
581
                $response = [
582
                    'success' => false,
583
                    'data' => 'ERROR_POST_NOT_FOUND'
584
                ];
585
                return new JsonModel($response);
586
            }
587
 
588
            $reactions = [
589
                ContentReaction::REACTION_RECOMMENDED,
590
                ContentReaction::REACTION_SUPPORT,
591
                ContentReaction::REACTION_LOVE,
592
                ContentReaction::REACTION_INTEREST,
593
                ContentReaction::REACTION_FUN
594
 
595
            ];
596
            if(!in_array($reaction, $reactions)) {
597
                $response = [
598
                    'success' => false,
599
                    'data' => 'ERROR_REACTION_NOT_FOUND'
600
                ];
601
                return new JsonModel($response);
602
            }
603
 
604
 
605
 
606
 
607
 
608
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
609
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
610
 
611
            if ($contentReaction) {
612
                $contentReaction->reaction = $reaction;
613
 
614
                $result = $contentReactionMapper->update($contentReaction);
615
            } else {
616
                $contentReaction = new ContentReaction();
617
                $contentReaction->user_id = $currentUser->id;
618
                $contentReaction->post_id = $post->id;
619
                $contentReaction->relational = ContentReaction::RELATIONAL_POST;
620
                $contentReaction->reaction = $reaction;
621
 
622
                $result = $contentReactionMapper->insert($contentReaction);
623
            }
624
 
625
 
626
 
627
            if ($result) {
628
 
629
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
630
                $response = [
631
                    'success' => true,
632
                    'data' => [
633
                        'reactions' => $reactions
634
                    ]
635
                ];
636
            } else {
637
                $response = [
638
                    'success' => false,
639
                    'data' => $contentReactionMapper->getError()
640
                ];
641
            }
642
            return new JsonModel($response);
643
        }
644
 
645
        $response = [
646
            'success' => false,
647
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
648
        ];
649
        return new JsonModel($response);
650
    }
651
 
652
    public function deleteReactionAction()
653
    {
654
        $id = $this->params()->fromRoute('id');
655
 
656
        $request = $this->getRequest();
657
        if ($request->isPost()) {
658
            $currentUserPlugin = $this->plugin('currentUserPlugin');
659
            $currentUser = $currentUserPlugin->getUser();
660
 
661
            $postMapper = PostMapper::getInstance($this->adapter);
662
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
663
            if (!$post) {
664
                $response = [
665
                    'success' => false,
666
                    'data' => 'ERROR_POST_NOT_FOUND'
667
                ];
668
                return new JsonModel($response);
669
            }
670
 
671
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
672
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
673
 
674
            if (!$contentReaction) {
675
                $response = [
676
                    'success' => false,
677
                    'data' => 'ERROR_DUPLICATE_ACTION'
678
                ];
679
                return new JsonModel($response);
680
            }
681
 
682
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
683
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
684
 
685
 
686
                $response = [
687
                    'success' => true,
688
                    'data' => [
689
                        'reactions' => $reactions
690
                    ]
691
                ];
692
            } else {
693
                $response = [
694
                    'success' => false,
695
                    'data' => $contentReactionMapper->getError()
696
                ];
697
            }
698
            return new JsonModel($response);
699
        }
700
 
701
        $response = [
702
            'success' => false,
703
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
704
        ];
705
        return new JsonModel($response);
706
    }
707
 
708
    public function commentsAction()
709
    {
710
        $id = $this->params()->fromRoute('id');
711
 
712
        $request = $this->getRequest();
713
        if ($request->isGet()) {
714
 
715
 
716
 
717
            $currentUserPlugin = $this->plugin('currentUserPlugin');
718
            $currentUser = $currentUserPlugin->getUser();
719
 
720
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
721
            $currentNetwork = $currentNetworkPlugin->getNetwork();
722
 
723
 
724
            $id = $this->params()->fromRoute('id');
725
            $postMapper = PostMapper::getInstance($this->adapter);
726
            $now = $postMapper->getDatebaseNow();
727
 
728
            $post = $postMapper->fetchOneByUuidAndNetworkId($id,  $currentNetwork->id);
729
            if (!$post) {
730
                $data = [
731
                    'success' => false,
732
                    'data' => 'ERROR_UNAUTHORIZED',
733
                ];
734
 
735
                return new JsonModel($data);
736
            }
737
 
738
            $commentMapper = CommentMapper::getInstance($this->adapter);
739
            $records = $commentMapper->fetchAllPublishedByPostId($post->id);
740
 
741
            $comments = [];
742
            foreach ($records as $record) {
743
                $comment = $this->renderComment($record->id, $now);
744
                array_push($comments, $comment);
745
            }
746
 
747
            $response = [
748
                'success' => true,
749
                'data' => $comments
750
            ];
751
 
752
            return new JsonModel($response);
753
        } else {
754
 
755
 
756
 
757
            $response = [
758
                'success' => false,
759
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
760
            ];
761
 
762
 
763
            return new JsonModel($response);
764
        }
765
    }
766
 
767
 
768
 
769
    /**
770
     *
771
     * @param int $comment_id
772
     * @return array
773
     */
774
    private function renderComment($comment_id, $now)
775
    {
776
        $item = [];
777
 
778
        $commentMapper = CommentMapper::getInstance($this->adapter);
779
        $record = $commentMapper->fetchOne($comment_id);
780
 
781
        $postMapper = PostMapper::getInstance($this->adapter);
782
        $post = $postMapper->fetchOne($record->post_id);
783
 
784
        if ($record) {
785
            $userMapper = UserMapper::getInstance($this->adapter);
786
 
787
            $user = $userMapper->fetchOne($record->user_id);
788
 
789
            $item['unique'] = uniqid();
60 efrain 790
            $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image],['force_canonical' => true]);
1 efrain 791
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
792
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
793
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
794
            $item['comment'] = $record->comment;
795
            $item['link_delete'] = $this->url()->fromRoute('post/comments/delete', ['id' => $post->uuid, 'comment' => $record->uuid]);
796
        }
797
        return $item;
798
    }
799
}