Proyectos de Subversion LeadersLinked - Services

Rev

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