Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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