Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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