Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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