Proyectos de Subversion LeadersLinked - Services

Rev

Rev 60 | Ir a la última revisión | | Ultima modificación | Ver Log |

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