Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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