Proyectos de Subversion LeadersLinked - Services

Rev

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