Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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