Proyectos de Subversion LeadersLinked - Services

Rev

Rev 242 | Rev 283 | 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';
243 efrain 231
                    } else if(in_array($extension, ['wav', 'mp3'] )) {
234 efrain 232
                        $type = 'audio';
243 efrain 233
                    } else if(in_array($extension, ['pdf'] )) {
234
                        $type = 'document';
234 efrain 235
                    }
236
                }
1 efrain 237
 
234 efrain 238
            }
239
 
240 efrain 240
 
241
 
242
 
1 efrain 243
 
234 efrain 244
 
245
 
246
 
1 efrain 247
                return new JsonModel([
248
                    'success' => true,
249
                    'data' => [
250
                        'post' => $post,
251
                        'id' => $post->id,
252
                        'uuid' => $post->uuid,
253
                        'title' => $post->title,
254
                        'description' => $post->description,
255
                        'url' => $post->url,
256
                        'date' => $post->date,
234 efrain 257
                        'type' => $post->type,
1 efrain 258
                        'status' => $post->status,
140 efrain 259
                        'image' =>   $this->url()->fromRoute('storage',['type' => 'post', 'code' => $post->uuid, 'filename' => $post->image], ['force_canonical' => true]),
260
                        'file' => $post->file ? $this->url()->fromRoute('storage',['type' => 'post', 'code' => $post->uuid, 'filename' => $post->file], ['force_canonical' => true]) : '',
234 efrain 261
                        'type' => $type,
1 efrain 262
                        'added_on' => $post->added_on,
263
                        'share_external_url' =>  $share_external_url,
264
                        'total_share_external' => $post->total_external_shared,
265
                        'share_increment_external_counter_url' => $share_increment_external_counter_url,
242 efrain 266
                        'comments_url' => $this->url()->fromRoute('post/comments', ['id' => $post->uuid],['force_canonical' => true]),
267
                        'comments_add_url' => $this->url()->fromRoute('post/comments/add', ['id' => $post->uuid],['force_canonical' => true]),
268
                        'save_reaction_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid],['force_canonical' => true]),
269
                        'delete_reaction_url' =>  $this->url()->fromRoute('post/delete-reaction', ['id' => $post->uuid],['force_canonical' => true]),
270
                        'reactions_url' => $this->url()->fromRoute('post/reactions', ['id' => $post->uuid],['force_canonical' => true]),
1 efrain 271
                        'my_reaction' => $reaction ? $reaction->reaction : '',
272
                        'reactions' =>  $reactions,
273
                        //'is_liked' => $contentReaction ? 1 : 0,
274
                        //'like_url' => $this->url()->fromRoute('post/like', ['id' => $post->uuid]),
275
                        //'unlike_url' => $this->url()->fromRoute('post/unlike', ['id' => $post->uuid]),
276
 
277
                    ]
278
                ]);
279
        } else {
280
            $response = [
281
                'success' => false,
282
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
283
            ];
284
            return new JsonModel($response);
285
        }
286
    }
287
 
288
    public function commentsAddAction()
289
    {
290
        $id = $this->params()->fromRoute('id');
291
 
292
        $request = $this->getRequest();
293
        if ($request->isPost()) {
294
 
295
 
296
 
297
            $postMapper = PostMapper::getInstance($this->adapter);
298
            $now = $postMapper->getDatebaseNow();
299
 
300
            $post = $postMapper->fetchOneByUuid($id);
301
            if (!$post) {
302
                $response = [
303
                    'success' => false,
304
                    'data' => 'ERROR_POST_NOT_FOUND'
305
                ];
306
                return new JsonModel($response);
307
            }
308
 
309
            $dataPost = $request->getPost()->toArray();
310
            $form = new CommentForm();
311
            $form->setData($dataPost);
312
 
313
            if ($form->isValid()) {
314
 
315
 
316
                $currentUserPlugin = $this->plugin('currentUserPlugin');
317
                $currentUser = $currentUserPlugin->getUser();
318
 
319
                $dataPost = (array) $form->getData();
320
 
321
 
322
 
323
                $comment = new Comment();
324
                $comment->network_id = $currentUser->network_id;
325
                $comment->comment = $dataPost['comment'];
326
                $comment->user_id = $currentUser->id;
327
                $comment->post_id = $post->id;
328
                $comment->relational = Comment::RELATIONAL_POST;
329
 
330
                $commentMapper = CommentMapper::getInstance($this->adapter);
331
                if ($commentMapper->insert($comment)) {
332
 
333
                    $total_comments = $commentMapper->fetchCountCommentByPostId($comment->post_id);
334
 
335
                    $post->total_comments = $total_comments;
336
                    $postMapper->update($post);
337
 
338
                    $response = [
339
                        'success'   => true,
340
                        'data'   => $this->renderComment($comment->id, $now),
341
                        'total_comments' => $total_comments
342
                    ];
343
 
344
                    return new JsonModel($response);
345
                } else {
346
 
347
                    $response = [
348
                        'success'   => false,
349
                        'data'   => $commentMapper->getError()
350
                    ];
351
 
352
                    return new JsonModel($response);
353
                }
354
            } else {
355
                $message = '';;
356
                $form_messages = (array) $form->getMessages();
357
                foreach ($form_messages  as $fieldname => $field_messages) {
358
                    foreach ($field_messages as $key => $value) {
359
                        $message = $value;
360
                    }
361
                }
362
 
363
                $response = [
364
                    'success'   => false,
365
                    'data'   => $message
366
                ];
367
 
368
                return new JsonModel($response);
369
            }
370
        } else {
371
            $response = [
372
                'success' => false,
373
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
374
            ];
375
 
376
            return new JsonModel($response);
377
        }
378
    }
379
 
380
 
381
 
382
    public function commentsDeleteAction()
383
    {
384
        $request = $this->getRequest();
385
        if ($request->isPost()) {
386
            $currentUserPlugin = $this->plugin('currentUserPlugin');
387
            $currentUser = $currentUserPlugin->getUser();
388
 
389
            $post_id = $this->params()->fromRoute('id');
390
            $comment = $this->params()->fromRoute('comment');
391
 
392
            $postMapper = PostMapper::getInstance($this->adapter);
393
            $post = $postMapper->fetchOneByUuidAndNetworkId($post_id, $currentUser->network_id);
394
 
395
            if ($post) {
396
 
397
                $commentMapper = CommentMapper::getInstance($this->adapter);
398
                $comment = $commentMapper->fetchOneByUuid($comment);
399
 
400
                if ($comment && $comment->post_id == $post->id && $comment->user_id == $currentUser->id) {
401
 
402
                    $comment->status = Comment::STATUS_DELETED;
403
 
404
                    if ($commentMapper->update($comment)) {
405
 
406
                        $total_comments = $commentMapper->fetchCountCommentByPostId($comment->post_id);
407
 
408
 
409
                        $postMapper = PostMapper::getInstance($this->adapter);
410
                        $post = $postMapper->fetchOne($comment->post_id);
411
                        $post->total_comments = $total_comments;
412
                        $postMapper->update($post);
413
 
414
 
415
 
416
 
417
 
418
                        $response = [
419
                            'success' => true,
420
                            'data' => 'LABEL_COMMENT_WAS_DELETED',
421
                            'total_comments' => $total_comments
422
                        ];
423
                    } else {
424
                        $response = [
425
                            'success' => false,
426
                            'data' => $commentMapper->getError()
427
                        ];
428
                    }
429
                } else {
430
                    $response = [
431
                        'success' => false,
432
                        'data' => 'ERROR_COMMENT_NOT_FOUND'
433
                    ];
434
                }
435
            } else {
436
                $response = [
437
                    'success' => false,
438
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
439
                ];
440
            }
441
        } else {
442
            $response = [
443
                'success' => false,
444
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
445
            ];
446
        }
447
 
448
        return new JsonModel($response);
449
    }
450
 
451
    /*
452
    public function likeAction()
453
    {
454
        $id = $this->params()->fromRoute('id');
455
 
456
        $request = $this->getRequest();
457
        if ($request->isPost()) {
458
            $currentUserPlugin = $this->plugin('currentUserPlugin');
459
            $currentUser = $currentUserPlugin->getUser();
460
 
461
 
462
            $postMapper = PostMapper::getInstance($this->adapter);
463
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
464
            if (!$post) {
465
                $response = [
466
                    'success' => false,
467
                    'data' => 'ERROR_POST_NOT_FOUND'
468
                ];
469
                return new JsonModel($response);
470
            }
471
 
472
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
473
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
474
 
475
            if ($contentReaction) {
476
                $response = [
477
                    'success' => false,
478
                    'data' => 'ERROR_DUPLICATE_ACTION'
479
                ];
480
                return new JsonModel($response);
481
            }
482
 
483
            $contentReaction = new Like();
484
            $contentReaction->user_id = $currentUser->id;
485
            $contentReaction->post_id = $post->id;
486
            $contentReaction->relational = Like::RELATIONAL_POST;
487
 
488
            if ($contentReactionMapper->insert($contentReaction)) {
489
 
490
                $contentReactions = $contentReactionMapper->fetchCountLikeByPostId($post->id);
491
                $response = [
492
                    'success' => true,
493
                    'data' => [
494
                        'likes' => $contentReactions
495
                    ]
496
                ];
497
            } else {
498
                $response = [
499
                    'success' => false,
500
                    'data' => $contentReactionMapper->getError()
501
                ];
502
            }
503
            return new JsonModel($response);
504
        }
505
 
506
        $response = [
507
            'success' => false,
508
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
509
        ];
510
        return new JsonModel($response);
511
    }
512
 
513
    public function unlikeAction()
514
    {
515
        $id = $this->params()->fromRoute('id');
516
 
517
        $request = $this->getRequest();
518
        if ($request->isPost()) {
519
            $currentUserPlugin = $this->plugin('currentUserPlugin');
520
            $currentUser = $currentUserPlugin->getUser();
521
 
522
            $postMapper = PostMapper::getInstance($this->adapter);
523
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
524
            if (!$post) {
525
                $response = [
526
                    'success' => false,
527
                    'data' => 'ERROR_POST_NOT_FOUND'
528
                ];
529
                return new JsonModel($response);
530
            }
531
 
532
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
533
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
534
 
535
            if (!$contentReaction) {
536
                $response = [
537
                    'success' => false,
538
                    'data' => 'ERROR_DUPLICATE_ACTION'
539
                ];
540
                return new JsonModel($response);
541
            }
542
 
543
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
544
                $contentReactions = $contentReactionMapper->fetchCountLikeByPostId($post->id);
545
 
546
 
547
                $response = [
548
                    'success' => true,
549
                    'data' => [
550
                        'likes' => $contentReactions
551
                    ]
552
                ];
553
            } else {
554
                $response = [
555
                    'success' => false,
556
                    'data' => $contentReactionMapper->getError()
557
                ];
558
            }
559
            return new JsonModel($response);
560
        }
561
 
562
        $response = [
563
            'success' => false,
564
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
565
        ];
566
        return new JsonModel($response);
567
    }*/
568
 
569
    public function saveReactionAction()
570
    {
571
        $id = $this->params()->fromRoute('id');
572
        $reaction  = $this->params()->fromPost('reaction');
573
 
574
        $request = $this->getRequest();
575
        if ($request->isPost()) {
576
            $currentUserPlugin = $this->plugin('currentUserPlugin');
577
            $currentUser = $currentUserPlugin->getUser();
578
 
579
 
580
            $postMapper = PostMapper::getInstance($this->adapter);
581
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
582
            if (!$post) {
583
                $response = [
584
                    'success' => false,
585
                    'data' => 'ERROR_POST_NOT_FOUND'
586
                ];
587
                return new JsonModel($response);
588
            }
589
 
590
            $reactions = [
591
                ContentReaction::REACTION_RECOMMENDED,
592
                ContentReaction::REACTION_SUPPORT,
593
                ContentReaction::REACTION_LOVE,
594
                ContentReaction::REACTION_INTEREST,
595
                ContentReaction::REACTION_FUN
596
 
597
            ];
598
            if(!in_array($reaction, $reactions)) {
599
                $response = [
600
                    'success' => false,
601
                    'data' => 'ERROR_REACTION_NOT_FOUND'
602
                ];
603
                return new JsonModel($response);
604
            }
605
 
606
 
607
 
608
 
609
 
610
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
611
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
612
 
613
            if ($contentReaction) {
614
                $contentReaction->reaction = $reaction;
615
 
616
                $result = $contentReactionMapper->update($contentReaction);
617
            } else {
618
                $contentReaction = new ContentReaction();
619
                $contentReaction->user_id = $currentUser->id;
620
                $contentReaction->post_id = $post->id;
621
                $contentReaction->relational = ContentReaction::RELATIONAL_POST;
622
                $contentReaction->reaction = $reaction;
623
 
624
                $result = $contentReactionMapper->insert($contentReaction);
625
            }
626
 
627
 
628
 
629
            if ($result) {
630
 
631
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
632
                $response = [
633
                    'success' => true,
634
                    'data' => [
635
                        'reactions' => $reactions
636
                    ]
637
                ];
638
            } else {
639
                $response = [
640
                    'success' => false,
641
                    'data' => $contentReactionMapper->getError()
642
                ];
643
            }
644
            return new JsonModel($response);
645
        }
646
 
647
        $response = [
648
            'success' => false,
649
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
650
        ];
651
        return new JsonModel($response);
652
    }
653
 
654
    public function deleteReactionAction()
655
    {
656
        $id = $this->params()->fromRoute('id');
657
 
658
        $request = $this->getRequest();
659
        if ($request->isPost()) {
660
            $currentUserPlugin = $this->plugin('currentUserPlugin');
661
            $currentUser = $currentUserPlugin->getUser();
662
 
663
            $postMapper = PostMapper::getInstance($this->adapter);
664
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
665
            if (!$post) {
666
                $response = [
667
                    'success' => false,
668
                    'data' => 'ERROR_POST_NOT_FOUND'
669
                ];
670
                return new JsonModel($response);
671
            }
672
 
673
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
674
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
675
 
676
            if (!$contentReaction) {
677
                $response = [
678
                    'success' => false,
679
                    'data' => 'ERROR_DUPLICATE_ACTION'
680
                ];
681
                return new JsonModel($response);
682
            }
683
 
684
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
685
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
686
 
687
 
688
                $response = [
689
                    'success' => true,
690
                    'data' => [
691
                        'reactions' => $reactions
692
                    ]
693
                ];
694
            } else {
695
                $response = [
696
                    'success' => false,
697
                    'data' => $contentReactionMapper->getError()
698
                ];
699
            }
700
            return new JsonModel($response);
701
        }
702
 
703
        $response = [
704
            'success' => false,
705
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
706
        ];
707
        return new JsonModel($response);
708
    }
709
 
710
    public function commentsAction()
711
    {
712
        $id = $this->params()->fromRoute('id');
713
 
714
        $request = $this->getRequest();
715
        if ($request->isGet()) {
716
 
717
 
718
 
719
            $currentUserPlugin = $this->plugin('currentUserPlugin');
720
            $currentUser = $currentUserPlugin->getUser();
721
 
722
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
723
            $currentNetwork = $currentNetworkPlugin->getNetwork();
724
 
725
 
726
            $id = $this->params()->fromRoute('id');
727
            $postMapper = PostMapper::getInstance($this->adapter);
728
            $now = $postMapper->getDatebaseNow();
729
 
730
            $post = $postMapper->fetchOneByUuidAndNetworkId($id,  $currentNetwork->id);
731
            if (!$post) {
732
                $data = [
733
                    'success' => false,
734
                    'data' => 'ERROR_UNAUTHORIZED',
735
                ];
736
 
737
                return new JsonModel($data);
738
            }
739
 
740
            $commentMapper = CommentMapper::getInstance($this->adapter);
741
            $records = $commentMapper->fetchAllPublishedByPostId($post->id);
742
 
743
            $comments = [];
744
            foreach ($records as $record) {
745
                $comment = $this->renderComment($record->id, $now);
746
                array_push($comments, $comment);
747
            }
748
 
749
            $response = [
750
                'success' => true,
751
                'data' => $comments
752
            ];
753
 
754
            return new JsonModel($response);
755
        } else {
756
 
757
 
758
 
759
            $response = [
760
                'success' => false,
761
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
762
            ];
763
 
764
 
765
            return new JsonModel($response);
766
        }
767
    }
768
 
769
 
770
 
771
    /**
772
     *
773
     * @param int $comment_id
774
     * @return array
775
     */
776
    private function renderComment($comment_id, $now)
777
    {
243 efrain 778
 
779
        $currentUserPlugin = $this->plugin('currentUserPlugin');
780
        $currentUser = $currentUserPlugin->getUser();
781
 
1 efrain 782
        $item = [];
783
 
784
        $commentMapper = CommentMapper::getInstance($this->adapter);
785
        $record = $commentMapper->fetchOne($comment_id);
786
 
787
        $postMapper = PostMapper::getInstance($this->adapter);
788
        $post = $postMapper->fetchOne($record->post_id);
789
 
790
        if ($record) {
791
            $userMapper = UserMapper::getInstance($this->adapter);
792
 
793
            $user = $userMapper->fetchOne($record->user_id);
794
 
795
            $item['unique'] = uniqid();
60 efrain 796
            $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image],['force_canonical' => true]);
1 efrain 797
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
798
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
799
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
800
            $item['comment'] = $record->comment;
243 efrain 801
            $item['link_delete'] = $currentUser->id == $record->user_id ? $this->url()->fromRoute('post/comments/delete', ['id' => $post->uuid, 'comment' => $record->uuid]) : '';
1 efrain 802
        }
803
        return $item;
804
    }
805
}