Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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