Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | Rev 7094 | 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]),
5780 efrain 172
                'save_reaction_recommended_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid, 'reaction' => ContentReaction::REACTION_RECOMMENDED]),
173
                'save_reaction_support_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid, 'reaction' => ContentReaction::REACTION_SUPPORT]),
174
                'save_reaction_love_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid, 'reaction' => ContentReaction::REACTION_LOVE]),
175
                'save_reaction_interest_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid, 'reaction' => ContentReaction::REACTION_INTEREST]),
176
                'save_reaction_fun_url' => $this->url()->fromRoute('post/save-reaction', ['id' => $post->uuid, 'reaction' => ContentReaction::REACTION_FUN]),
177
                'delete_reaction_url' =>  $this->url()->fromRoute('post/delete-reaction', ['id' => $post->uuid]),
5765 efrain 178
                'my_reaction' => $reaction ? $reaction->reaction : '',
179
                'reactions' =>  $reactions,
5775 efrain 180
                //'is_liked' => $contentReaction ? 1 : 0,
5765 efrain 181
                //'like_url' => $this->url()->fromRoute('post/like', ['id' => $post->uuid]),
182
                //'unlike_url' => $this->url()->fromRoute('post/unlike', ['id' => $post->uuid]),
5575 anderson 183
 
4808 efrain 184
            ]);
185
            return $viewModel;
186
        } else {
187
            $response = [
188
                'success' => false,
189
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
190
            ];
5575 anderson 191
 
4808 efrain 192
            return new JsonModel($response);
193
        }
194
    }
5575 anderson 195
 
4808 efrain 196
    public function commentsAddAction()
197
    {
198
        $id = $this->params()->fromRoute('id');
5575 anderson 199
 
4808 efrain 200
        $request = $this->getRequest();
5575 anderson 201
        if ($request->isPost()) {
4808 efrain 202
 
5575 anderson 203
 
204
 
4808 efrain 205
            $postMapper = PostMapper::getInstance($this->adapter);
6388 efrain 206
            $now = $postMapper->getDatebaseNow();
207
 
4808 efrain 208
            $post = $postMapper->fetchOneByUuid($id);
5575 anderson 209
            if (!$post) {
4808 efrain 210
                $response = [
211
                    'success' => false,
212
                    'data' => 'ERROR_POST_NOT_FOUND'
213
                ];
214
                return new JsonModel($response);
215
            }
5575 anderson 216
 
4808 efrain 217
            $dataPost = $request->getPost()->toArray();
218
            $form = new CommentForm();
219
            $form->setData($dataPost);
5575 anderson 220
 
221
            if ($form->isValid()) {
222
 
6388 efrain 223
 
4808 efrain 224
                $currentUserPlugin = $this->plugin('currentUserPlugin');
225
                $currentUser = $currentUserPlugin->getUser();
5575 anderson 226
 
4808 efrain 227
                $dataPost = (array) $form->getData();
5575 anderson 228
 
229
 
230
 
4808 efrain 231
                $comment = new Comment();
232
                $comment->network_id = $currentUser->network_id;
233
                $comment->comment = $dataPost['comment'];
234
                $comment->user_id = $currentUser->id;
235
                $comment->post_id = $post->id;
236
                $comment->relational = Comment::RELATIONAL_POST;
5575 anderson 237
 
4808 efrain 238
                $commentMapper = CommentMapper::getInstance($this->adapter);
5575 anderson 239
                if ($commentMapper->insert($comment)) {
240
 
4808 efrain 241
                    $total_comments = $commentMapper->fetchCountCommentByPostId($comment->post_id);
5575 anderson 242
 
4808 efrain 243
                    $post->total_comments = $total_comments;
244
                    $postMapper->update($post);
5575 anderson 245
 
4808 efrain 246
                    $response = [
247
                        'success'   => true,
248
                        'data'   => $this->renderComment($comment->id, $now),
249
                        'total_comments' => $total_comments
250
                    ];
5575 anderson 251
 
4808 efrain 252
                    return new JsonModel($response);
253
                } else {
5575 anderson 254
 
4808 efrain 255
                    $response = [
256
                        'success'   => false,
257
                        'data'   => $commentMapper->getError()
258
                    ];
5575 anderson 259
 
4808 efrain 260
                    return new JsonModel($response);
261
                }
262
            } else {
263
                $message = '';;
264
                $form_messages = (array) $form->getMessages();
5575 anderson 265
                foreach ($form_messages  as $fieldname => $field_messages) {
266
                    foreach ($field_messages as $key => $value) {
4808 efrain 267
                        $message = $value;
268
                    }
269
                }
5575 anderson 270
 
4808 efrain 271
                $response = [
272
                    'success'   => false,
273
                    'data'   => $message
274
                ];
5575 anderson 275
 
4808 efrain 276
                return new JsonModel($response);
277
            }
278
        } else {
279
            $response = [
280
                'success' => false,
281
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
282
            ];
5575 anderson 283
 
4808 efrain 284
            return new JsonModel($response);
285
        }
286
    }
5575 anderson 287
 
288
 
289
 
4808 efrain 290
    public function commentsDeleteAction()
291
    {
5576 anderson 292
        $request = $this->getRequest();
293
        if ($request->isPost()) {
294
            $currentUserPlugin = $this->plugin('currentUserPlugin');
295
            $currentUser = $currentUserPlugin->getUser();
5575 anderson 296
 
5576 anderson 297
            $post_id = $this->params()->fromRoute('id');
298
            $comment = $this->params()->fromRoute('comment');
5575 anderson 299
 
5576 anderson 300
            $postMapper = PostMapper::getInstance($this->adapter);
301
            $post = $postMapper->fetchOneByUuidAndNetworkId($post_id, $currentUser->network_id);
5575 anderson 302
 
5576 anderson 303
            if ($post) {
5575 anderson 304
 
5576 anderson 305
                $commentMapper = CommentMapper::getInstance($this->adapter);
306
                $comment = $commentMapper->fetchOneByUuid($comment);
5575 anderson 307
 
5576 anderson 308
                if ($comment && $comment->post_id == $post->id && $comment->user_id == $currentUser->id) {
5575 anderson 309
 
5576 anderson 310
                    $comment->status = Comment::STATUS_DELETED;
5575 anderson 311
 
5576 anderson 312
                    if ($commentMapper->update($comment)) {
5575 anderson 313
 
5576 anderson 314
                        $total_comments = $commentMapper->fetchCountCommentByPostId($comment->post_id);
5575 anderson 315
 
316
 
5576 anderson 317
                        $postMapper = PostMapper::getInstance($this->adapter);
318
                        $post = $postMapper->fetchOne($comment->post_id);
319
                        $post->total_comments = $total_comments;
320
                        $postMapper->update($post);
5575 anderson 321
 
322
 
323
 
324
 
325
 
5576 anderson 326
                        $response = [
327
                            'success' => true,
328
                            'data' => 'LABEL_COMMENT_WAS_DELETED',
329
                            'total_comments' => $total_comments
330
                        ];
331
                    } else {
332
                        $response = [
333
                            'success' => false,
334
                            'data' => $commentMapper->getError()
335
                        ];
336
                    }
337
                } else {
338
                    $response = [
339
                        'success' => false,
340
                        'data' => 'ERROR_COMMENT_NOT_FOUND'
341
                    ];
342
                }
343
            } else {
344
                $response = [
345
                    'success' => false,
346
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
347
                ];
348
            }
349
        } else {
350
            $response = [
351
                'success' => false,
352
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
353
            ];
354
        }
5575 anderson 355
 
5576 anderson 356
        return new JsonModel($response);
4808 efrain 357
    }
5575 anderson 358
 
5765 efrain 359
    /*
4808 efrain 360
    public function likeAction()
361
    {
5732 anderson 362
        $id = $this->params()->fromRoute('id');
5575 anderson 363
 
5732 anderson 364
        $request = $this->getRequest();
365
        if ($request->isPost()) {
366
            $currentUserPlugin = $this->plugin('currentUserPlugin');
367
            $currentUser = $currentUserPlugin->getUser();
4808 efrain 368
 
5575 anderson 369
 
5732 anderson 370
            $postMapper = PostMapper::getInstance($this->adapter);
371
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
372
            if (!$post) {
373
                $response = [
374
                    'success' => false,
375
                    'data' => 'ERROR_POST_NOT_FOUND'
376
                ];
377
                return new JsonModel($response);
378
            }
5575 anderson 379
 
5765 efrain 380
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
5775 efrain 381
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
5575 anderson 382
 
5775 efrain 383
            if ($contentReaction) {
5732 anderson 384
                $response = [
385
                    'success' => false,
386
                    'data' => 'ERROR_DUPLICATE_ACTION'
387
                ];
388
                return new JsonModel($response);
389
            }
5575 anderson 390
 
5775 efrain 391
            $contentReaction = new Like();
392
            $contentReaction->user_id = $currentUser->id;
393
            $contentReaction->post_id = $post->id;
394
            $contentReaction->relational = Like::RELATIONAL_POST;
5575 anderson 395
 
5775 efrain 396
            if ($contentReactionMapper->insert($contentReaction)) {
5575 anderson 397
 
5775 efrain 398
                $contentReactions = $contentReactionMapper->fetchCountLikeByPostId($post->id);
5732 anderson 399
                $response = [
400
                    'success' => true,
401
                    'data' => [
5775 efrain 402
                        'likes' => $contentReactions
5732 anderson 403
                    ]
404
                ];
405
            } else {
406
                $response = [
407
                    'success' => false,
5765 efrain 408
                    'data' => $contentReactionMapper->getError()
5732 anderson 409
                ];
410
            }
411
            return new JsonModel($response);
412
        }
5575 anderson 413
 
5732 anderson 414
        $response = [
415
            'success' => false,
416
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
417
        ];
418
        return new JsonModel($response);
4808 efrain 419
    }
5575 anderson 420
 
4808 efrain 421
    public function unlikeAction()
422
    {
423
        $id = $this->params()->fromRoute('id');
5575 anderson 424
 
4808 efrain 425
        $request = $this->getRequest();
5575 anderson 426
        if ($request->isPost()) {
4808 efrain 427
            $currentUserPlugin = $this->plugin('currentUserPlugin');
428
            $currentUser = $currentUserPlugin->getUser();
429
 
430
            $postMapper = PostMapper::getInstance($this->adapter);
431
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5575 anderson 432
            if (!$post) {
4808 efrain 433
                $response = [
434
                    'success' => false,
435
                    'data' => 'ERROR_POST_NOT_FOUND'
436
                ];
437
                return new JsonModel($response);
438
            }
5575 anderson 439
 
5765 efrain 440
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
5775 efrain 441
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
5575 anderson 442
 
5775 efrain 443
            if (!$contentReaction) {
4808 efrain 444
                $response = [
445
                    'success' => false,
446
                    'data' => 'ERROR_DUPLICATE_ACTION'
447
                ];
448
                return new JsonModel($response);
449
            }
5575 anderson 450
 
5765 efrain 451
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
5775 efrain 452
                $contentReactions = $contentReactionMapper->fetchCountLikeByPostId($post->id);
5575 anderson 453
 
454
 
4808 efrain 455
                $response = [
456
                    'success' => true,
457
                    'data' => [
5775 efrain 458
                        'likes' => $contentReactions
4808 efrain 459
                    ]
460
                ];
461
            } else {
462
                $response = [
463
                    'success' => false,
5765 efrain 464
                    'data' => $contentReactionMapper->getError()
4808 efrain 465
                ];
466
            }
467
            return new JsonModel($response);
468
        }
5575 anderson 469
 
4808 efrain 470
        $response = [
471
            'success' => false,
472
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
473
        ];
474
        return new JsonModel($response);
5765 efrain 475
    }*/
476
 
5775 efrain 477
    public function saveReactionAction()
5765 efrain 478
    {
479
        $id = $this->params()->fromRoute('id');
480
        $reaction  = $this->params()->fromRoute('reaction');
481
 
482
        $request = $this->getRequest();
483
        if ($request->isPost()) {
484
            $currentUserPlugin = $this->plugin('currentUserPlugin');
485
            $currentUser = $currentUserPlugin->getUser();
486
 
487
 
488
            $postMapper = PostMapper::getInstance($this->adapter);
489
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
490
            if (!$post) {
491
                $response = [
492
                    'success' => false,
493
                    'data' => 'ERROR_POST_NOT_FOUND'
494
                ];
495
                return new JsonModel($response);
496
            }
497
 
498
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
499
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
500
 
501
            if ($contentReaction) {
5775 efrain 502
                $contentReaction->reaction = $reaction;
503
 
504
                $result = $contentReactionMapper->update($contentReaction);
505
            } else {
506
                $contentReaction = new ContentReaction();
507
                $contentReaction->user_id = $currentUser->id;
508
                $contentReaction->post_id = $post->id;
509
                $contentReaction->relational = ContentReaction::RELATIONAL_POST;
510
                $contentReaction->reaction = $reaction;
511
 
512
                $result = $contentReactionMapper->insert($contentReaction);
5765 efrain 513
            }
514
 
5775 efrain 515
 
5765 efrain 516
 
5775 efrain 517
            if ($result) {
5765 efrain 518
 
6521 efrain 519
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
5765 efrain 520
                $response = [
521
                    'success' => true,
522
                    'data' => [
5775 efrain 523
                        'reactions' => $reactions
5765 efrain 524
                    ]
525
                ];
526
            } else {
527
                $response = [
528
                    'success' => false,
529
                    'data' => $contentReactionMapper->getError()
530
                ];
531
            }
532
            return new JsonModel($response);
533
        }
534
 
535
        $response = [
536
            'success' => false,
537
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
538
        ];
539
        return new JsonModel($response);
4808 efrain 540
    }
5765 efrain 541
 
5775 efrain 542
    public function deleteReactionAction()
5765 efrain 543
    {
544
        $id = $this->params()->fromRoute('id');
545
 
546
        $request = $this->getRequest();
547
        if ($request->isPost()) {
548
            $currentUserPlugin = $this->plugin('currentUserPlugin');
549
            $currentUser = $currentUserPlugin->getUser();
550
 
551
            $postMapper = PostMapper::getInstance($this->adapter);
552
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
553
            if (!$post) {
554
                $response = [
555
                    'success' => false,
556
                    'data' => 'ERROR_POST_NOT_FOUND'
557
                ];
558
                return new JsonModel($response);
559
            }
560
 
561
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
5775 efrain 562
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
5765 efrain 563
 
5775 efrain 564
            if (!$contentReaction) {
5765 efrain 565
                $response = [
566
                    'success' => false,
567
                    'data' => 'ERROR_DUPLICATE_ACTION'
568
                ];
569
                return new JsonModel($response);
570
            }
571
 
572
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
6521 efrain 573
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
5765 efrain 574
 
575
 
576
                $response = [
577
                    'success' => true,
578
                    'data' => [
579
                        'reactions' => $reactions
580
                    ]
581
                ];
582
            } else {
583
                $response = [
584
                    'success' => false,
585
                    'data' => $contentReactionMapper->getError()
586
                ];
587
            }
588
            return new JsonModel($response);
589
        }
590
 
591
        $response = [
592
            'success' => false,
593
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
594
        ];
595
        return new JsonModel($response);
596
    }
5575 anderson 597
 
4808 efrain 598
    public function commentsAction()
599
    {
600
        $id = $this->params()->fromRoute('id');
5575 anderson 601
 
4808 efrain 602
        $request = $this->getRequest();
5575 anderson 603
        if ($request->isGet()) {
6388 efrain 604
 
605
 
5575 anderson 606
 
4808 efrain 607
            $currentUserPlugin = $this->plugin('currentUserPlugin');
608
            $currentUser = $currentUserPlugin->getUser();
5575 anderson 609
 
4808 efrain 610
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
611
            $currentNetwork = $currentNetworkPlugin->getNetwork();
5575 anderson 612
 
613
 
4808 efrain 614
            $id = $this->params()->fromRoute('id');
615
            $postMapper = PostMapper::getInstance($this->adapter);
6388 efrain 616
            $now = $postMapper->getDatebaseNow();
617
 
4808 efrain 618
            $post = $postMapper->fetchOneByUuidAndNetworkId($id,  $currentNetwork->id);
5575 anderson 619
            if (!$post) {
4808 efrain 620
                $data = [
621
                    'success' => false,
622
                    'data' => 'ERROR_UNAUTHORIZED',
623
                ];
5575 anderson 624
 
4808 efrain 625
                return new JsonModel($data);
626
            }
5575 anderson 627
 
4808 efrain 628
            $commentMapper = CommentMapper::getInstance($this->adapter);
629
            $records = $commentMapper->fetchAllPublishedByPostId($post->id);
5575 anderson 630
 
4808 efrain 631
            $comments = [];
5575 anderson 632
            foreach ($records as $record) {
4808 efrain 633
                $comment = $this->renderComment($record->id, $now);
634
                array_push($comments, $comment);
635
            }
5575 anderson 636
 
4808 efrain 637
            $response = [
638
                'success' => true,
639
                'data' => $comments
640
            ];
5575 anderson 641
 
4808 efrain 642
            return new JsonModel($response);
643
        } else {
644
 
5575 anderson 645
 
646
 
4808 efrain 647
            $response = [
648
                'success' => false,
649
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
650
            ];
5575 anderson 651
 
652
 
4808 efrain 653
            return new JsonModel($response);
654
        }
655
    }
5575 anderson 656
 
657
 
658
 
4808 efrain 659
    /**
660
     *
661
     * @param int $comment_id
662
     * @return array
663
     */
664
    private function renderComment($comment_id, $now)
665
    {
666
        $item = [];
5575 anderson 667
 
4808 efrain 668
        $commentMapper = CommentMapper::getInstance($this->adapter);
669
        $record = $commentMapper->fetchOne($comment_id);
5575 anderson 670
 
4808 efrain 671
        $postMapper = PostMapper::getInstance($this->adapter);
672
        $post = $postMapper->fetchOne($record->post_id);
5575 anderson 673
 
674
        if ($record) {
4808 efrain 675
            $userMapper = UserMapper::getInstance($this->adapter);
5575 anderson 676
 
4808 efrain 677
            $user = $userMapper->fetchOne($record->user_id);
5575 anderson 678
 
4808 efrain 679
            $item['unique'] = uniqid();
5575 anderson 680
            $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image]);
4894 stevensc 681
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
4808 efrain 682
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
683
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
684
            $item['comment'] = $record->comment;
5575 anderson 685
            $item['link_delete'] = $this->url()->fromRoute('post/comments/delete', ['id' => $post->uuid, 'comment' => $record->uuid]);
4808 efrain 686
        }
687
        return $item;
688
    }
689
}