Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6866 | 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]),
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');
7094 efrain 480
        $reaction  = $this->params()->fromPost('reaction');
5765 efrain 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
 
7094 efrain 498
            $reactions = [
499
                ContentReaction::REACTION_RECOMMENDED,
500
                ContentReaction::REACTION_SUPPORT,
501
                ContentReaction::REACTION_LOVE,
502
                ContentReaction::REACTION_INTEREST,
503
                ContentReaction::REACTION_FUN
504
 
505
            ];
506
            if(in_array($reaction, $reactions)) {
507
                $response = [
508
                    'success' => false,
509
                    'data' => 'ERROR_REACTION_NOT_FOUND'
510
                ];
511
                return new JsonModel($response);
512
            }
513
 
514
 
515
 
516
 
517
 
5765 efrain 518
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
519
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
520
 
521
            if ($contentReaction) {
5775 efrain 522
                $contentReaction->reaction = $reaction;
523
 
524
                $result = $contentReactionMapper->update($contentReaction);
525
            } else {
526
                $contentReaction = new ContentReaction();
527
                $contentReaction->user_id = $currentUser->id;
528
                $contentReaction->post_id = $post->id;
529
                $contentReaction->relational = ContentReaction::RELATIONAL_POST;
530
                $contentReaction->reaction = $reaction;
531
 
532
                $result = $contentReactionMapper->insert($contentReaction);
5765 efrain 533
            }
534
 
5775 efrain 535
 
5765 efrain 536
 
5775 efrain 537
            if ($result) {
5765 efrain 538
 
6521 efrain 539
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
5765 efrain 540
                $response = [
541
                    'success' => true,
542
                    'data' => [
5775 efrain 543
                        'reactions' => $reactions
5765 efrain 544
                    ]
545
                ];
546
            } else {
547
                $response = [
548
                    'success' => false,
549
                    'data' => $contentReactionMapper->getError()
550
                ];
551
            }
552
            return new JsonModel($response);
553
        }
554
 
555
        $response = [
556
            'success' => false,
557
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
558
        ];
559
        return new JsonModel($response);
4808 efrain 560
    }
5765 efrain 561
 
5775 efrain 562
    public function deleteReactionAction()
5765 efrain 563
    {
564
        $id = $this->params()->fromRoute('id');
565
 
566
        $request = $this->getRequest();
567
        if ($request->isPost()) {
568
            $currentUserPlugin = $this->plugin('currentUserPlugin');
569
            $currentUser = $currentUserPlugin->getUser();
570
 
571
            $postMapper = PostMapper::getInstance($this->adapter);
572
            $post = $postMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
573
            if (!$post) {
574
                $response = [
575
                    'success' => false,
576
                    'data' => 'ERROR_POST_NOT_FOUND'
577
                ];
578
                return new JsonModel($response);
579
            }
580
 
581
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
5775 efrain 582
            $contentReaction = $contentReactionMapper->fetchOneByPostIdAndUserId($post->id, $currentUser->id);
5765 efrain 583
 
5775 efrain 584
            if (!$contentReaction) {
5765 efrain 585
                $response = [
586
                    'success' => false,
587
                    'data' => 'ERROR_DUPLICATE_ACTION'
588
                ];
589
                return new JsonModel($response);
590
            }
591
 
592
            if ($contentReactionMapper->deleteByPostIdAndUserId($post->id, $currentUser->id)) {
6521 efrain 593
                $reactions = $contentReactionMapper->fetchCountByPostId($post->id);
5765 efrain 594
 
595
 
596
                $response = [
597
                    'success' => true,
598
                    'data' => [
599
                        'reactions' => $reactions
600
                    ]
601
                ];
602
            } else {
603
                $response = [
604
                    'success' => false,
605
                    'data' => $contentReactionMapper->getError()
606
                ];
607
            }
608
            return new JsonModel($response);
609
        }
610
 
611
        $response = [
612
            'success' => false,
613
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
614
        ];
615
        return new JsonModel($response);
616
    }
5575 anderson 617
 
4808 efrain 618
    public function commentsAction()
619
    {
620
        $id = $this->params()->fromRoute('id');
5575 anderson 621
 
4808 efrain 622
        $request = $this->getRequest();
5575 anderson 623
        if ($request->isGet()) {
6388 efrain 624
 
625
 
5575 anderson 626
 
4808 efrain 627
            $currentUserPlugin = $this->plugin('currentUserPlugin');
628
            $currentUser = $currentUserPlugin->getUser();
5575 anderson 629
 
4808 efrain 630
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
631
            $currentNetwork = $currentNetworkPlugin->getNetwork();
5575 anderson 632
 
633
 
4808 efrain 634
            $id = $this->params()->fromRoute('id');
635
            $postMapper = PostMapper::getInstance($this->adapter);
6388 efrain 636
            $now = $postMapper->getDatebaseNow();
637
 
4808 efrain 638
            $post = $postMapper->fetchOneByUuidAndNetworkId($id,  $currentNetwork->id);
5575 anderson 639
            if (!$post) {
4808 efrain 640
                $data = [
641
                    'success' => false,
642
                    'data' => 'ERROR_UNAUTHORIZED',
643
                ];
5575 anderson 644
 
4808 efrain 645
                return new JsonModel($data);
646
            }
5575 anderson 647
 
4808 efrain 648
            $commentMapper = CommentMapper::getInstance($this->adapter);
649
            $records = $commentMapper->fetchAllPublishedByPostId($post->id);
5575 anderson 650
 
4808 efrain 651
            $comments = [];
5575 anderson 652
            foreach ($records as $record) {
4808 efrain 653
                $comment = $this->renderComment($record->id, $now);
654
                array_push($comments, $comment);
655
            }
5575 anderson 656
 
4808 efrain 657
            $response = [
658
                'success' => true,
659
                'data' => $comments
660
            ];
5575 anderson 661
 
4808 efrain 662
            return new JsonModel($response);
663
        } else {
664
 
5575 anderson 665
 
666
 
4808 efrain 667
            $response = [
668
                'success' => false,
669
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
670
            ];
5575 anderson 671
 
672
 
4808 efrain 673
            return new JsonModel($response);
674
        }
675
    }
5575 anderson 676
 
677
 
678
 
4808 efrain 679
    /**
680
     *
681
     * @param int $comment_id
682
     * @return array
683
     */
684
    private function renderComment($comment_id, $now)
685
    {
686
        $item = [];
5575 anderson 687
 
4808 efrain 688
        $commentMapper = CommentMapper::getInstance($this->adapter);
689
        $record = $commentMapper->fetchOne($comment_id);
5575 anderson 690
 
4808 efrain 691
        $postMapper = PostMapper::getInstance($this->adapter);
692
        $post = $postMapper->fetchOne($record->post_id);
5575 anderson 693
 
694
        if ($record) {
4808 efrain 695
            $userMapper = UserMapper::getInstance($this->adapter);
5575 anderson 696
 
4808 efrain 697
            $user = $userMapper->fetchOne($record->user_id);
5575 anderson 698
 
4808 efrain 699
            $item['unique'] = uniqid();
5575 anderson 700
            $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image]);
4894 stevensc 701
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
4808 efrain 702
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
703
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
704
            $item['comment'] = $record->comment;
5575 anderson 705
            $item['link_delete'] = $this->url()->fromRoute('post/comments/delete', ['id' => $post->uuid, 'comment' => $record->uuid]);
4808 efrain 706
        }
707
        return $item;
708
    }
709
}