Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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