Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6239 | Rev 6388 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
5500 anderson 2
 
1 www 3
/**
4
 *
5
 * Controlador: Mis Perfiles
6
 *
7
 */
5500 anderson 8
 
1 www 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
 
19
use LeadersLinked\Library\Functions;
20
use LeadersLinked\Mapper\CompanyUserMapper;
21
use LeadersLinked\Form\Feed\CreateForm;
22
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
23
use LeadersLinked\Model\Feed;
24
use LeadersLinked\Mapper\FeedMapper;
25
use LeadersLinked\Library\Image;
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
use Laminas\Paginator\Paginator;
33
use Laminas\Paginator\Adapter\DbSelect;
5765 efrain 34
use LeadersLinked\Mapper\ContentReactionMapper;
35
use LeadersLinked\Model\ContentReaction;
1 www 36
use LeadersLinked\Form\Feed\CommentForm;
37
use LeadersLinked\Model\Comment;
38
use LeadersLinked\Form\Feed\CreateGroupCompanyForm;
39
use LeadersLinked\Model\CompanyUser;
40
use LeadersLinked\Model\VideoConvert;
41
use LeadersLinked\Mapper\VideoConvertMapper;
42
use LeadersLinked\Mapper\CompanyMapper;
43
use LeadersLinked\Model\Company;
44
use LeadersLinked\Model\Group;
45
use LeadersLinked\Mapper\GroupMemberMapper;
46
use LeadersLinked\Model\GroupMember;
47
use LeadersLinked\Model\Notification;
48
use LeadersLinked\Mapper\NotificationMapper;
49
use LeadersLinked\Mapper\UserNotificationSettingMapper;
50
use LeadersLinked\Mapper\EmailTemplateMapper;
51
use LeadersLinked\Model\EmailTemplate;
52
use LeadersLinked\Library\QueueEmail;
3138 efrain 53
use LeadersLinked\Mapper\UtilMapper;
1 www 54
 
55
 
56
class FeedController extends AbstractActionController
57
{
58
    /**
59
     *
60
     * @var AdapterInterface
61
     */
62
    private $adapter;
5500 anderson 63
 
64
 
1 www 65
    /**
66
     *
67
     * @var AbstractAdapter
68
     */
69
    private $cache;
5500 anderson 70
 
1 www 71
    /**
72
     *
73
     * @var  LoggerInterface
74
     */
75
    private $logger;
76
 
5500 anderson 77
 
1 www 78
    /**
79
     *
80
     * @var array
81
     */
82
    private $config;
5500 anderson 83
 
1 www 84
    /**
85
     *
86
     * @param AdapterInterface $adapter
87
     * @param AbstractAdapter $cache
88
     * @param LoggerInterface $logger
89
     * @param array $config
90
     */
5500 anderson 91
    public function __construct($adapter, $cache, $logger,  $config)
1 www 92
    {
93
        $this->adapter      = $adapter;
94
        $this->cache        = $cache;
95
        $this->logger       = $logger;
96
        $this->config       = $config;
5500 anderson 97
    }
1 www 98
 
99
    /**
100
     *
101
     * Generación del listado de perfiles
102
     * {@inheritDoc}
103
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
104
     */
105
    public function indexAction()
106
    {
5500 anderson 107
 
1 www 108
        return new JsonModel([
109
            'success' => false,
110
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
111
        ]);
112
    }
5500 anderson 113
 
5560 anderson 114
    public function commentAction()
115
    {
116
        $id = $this->params()->fromRoute('id');
5500 anderson 117
 
5560 anderson 118
        $request = $this->getRequest();
119
        if ($request->isPost()) {
3639 efrain 120
 
5500 anderson 121
 
122
 
5560 anderson 123
            $feedMapper = FeedMapper::getInstance($this->adapter);
124
            $feed = $feedMapper->fetchOneByUuid($id);
125
            if (!$feed) {
126
                $response = [
127
                    'success' => false,
128
                    'data' => 'ERROR_POST_NOT_FOUND'
129
                ];
130
                return new JsonModel($response);
131
            }
5500 anderson 132
 
5560 anderson 133
            $dataPost = $request->getPost()->toArray();
134
            $form = new CommentForm();
135
            $form->setData($dataPost);
5500 anderson 136
 
5560 anderson 137
            if ($form->isValid()) {
138
                $utilMapper = UtilMapper::getInstance($this->adapter);
139
                $now = $utilMapper->getDatebaseNow();
5500 anderson 140
 
5560 anderson 141
                $currentUserPlugin = $this->plugin('currentUserPlugin');
142
                $currentUser = $currentUserPlugin->getUser();
5500 anderson 143
 
5560 anderson 144
                $dataPost = (array) $form->getData();
5500 anderson 145
 
146
 
147
 
5560 anderson 148
                $comment = new Comment();
149
                $comment->network_id = $currentUser->network_id;
150
                $comment->comment = $dataPost['comment'];
151
                $comment->user_id = $currentUser->id;
152
                $comment->feed_id = $feed->id;
153
                $comment->relational = Comment::RELATIONAL_FEED;
5500 anderson 154
 
5560 anderson 155
                $commentMapper = CommentMapper::getInstance($this->adapter);
156
                if ($commentMapper->insert($comment)) {
5500 anderson 157
 
158
 
5560 anderson 159
                    if ($feed->type == Feed::TYPE_UPDATE) {
160
                        $userMapper = UserMapper::getInstance($this->adapter);
161
                        $ownerFeed = $userMapper->fetchOne($feed->user_id);
5500 anderson 162
 
5560 anderson 163
                        $notification = new Notification();
164
                        $notification->type = Notification::TYPE_COMMENT_MY_FEED;
165
                        $notification->feed_id = $feed->id;
166
                        $notification->read     = Notification::NO;
167
                        $notification->user_id  = $ownerFeed->id;
168
                        $notification->message  = 'LABEL_COMMENT_MY_FEED';
169
                        $notification->url      = $this->url()->fromRoute('dashboard', ['feed' => $feed->uuid]);
5500 anderson 170
 
5560 anderson 171
                        $notificationMapper = NotificationMapper::getInstance($this->adapter);
172
                        $notificationMapper->insert($notification);
5500 anderson 173
 
5560 anderson 174
                        $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
175
                        $userNotification = $userNotificationMapper->fetchOne($ownerFeed->id);
5500 anderson 176
 
5560 anderson 177
                        if ($userNotification && $userNotification->comment_my_feed) {
178
                            $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
179
                            $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_COMMENT_MY_FEED,  $currentUser->network_id);
5500 anderson 180
 
5560 anderson 181
                            if ($emailTemplate) {
182
                                $arrayCont = [
183
                                    'firstname'             => $currentUser->first_name,
184
                                    'lastname'              => $currentUser->last_name,
185
                                    'other_user_firstname'  => $ownerFeed->first_name,
186
                                    'other_user_lastname'   => $ownerFeed->last_name,
187
                                    'company_name'          => '',
188
                                    'group_name'            => '',
189
                                    'content'               => '',
190
                                    'code'                  => '',
191
                                    'link'                  => $this->url()->fromRoute('dashboard', ['feed' => $feed->uuid], ['force_canonical' => true])
192
                                ];
5500 anderson 193
 
5560 anderson 194
                                $email = new QueueEmail($this->adapter);
195
                                $email->processEmailTemplate($emailTemplate, $arrayCont, $ownerFeed->email, trim($ownerFeed->first_name . ' ' . $ownerFeed->last_name));
196
                            }
197
                        }
198
                    }
5500 anderson 199
 
5560 anderson 200
                    $total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
5500 anderson 201
 
5560 anderson 202
                    $feed->total_comments = $total_comments;
203
                    $feedMapper->update($feed);
5500 anderson 204
 
5560 anderson 205
                    $response = [
206
                        'success'   => true,
207
                        'data'   => $this->renderComment($comment->id, $now),
208
                        'total_comments' => $total_comments
209
                    ];
5500 anderson 210
 
5560 anderson 211
                    return new JsonModel($response);
212
                } else {
5500 anderson 213
 
5560 anderson 214
                    $response = [
215
                        'success'   => false,
216
                        'data'   => $commentMapper->getError()
217
                    ];
5500 anderson 218
 
5560 anderson 219
                    return new JsonModel($response);
220
                }
221
            } else {
222
                $message = '';;
223
                $form_messages = (array) $form->getMessages();
224
                foreach ($form_messages  as $fieldname => $field_messages) {
225
                    foreach ($field_messages as $key => $value) {
226
                        $message = $value;
227
                    }
228
                }
5500 anderson 229
 
5560 anderson 230
                $response = [
231
                    'success'   => false,
232
                    'data'   => $message
233
                ];
5500 anderson 234
 
5560 anderson 235
                return new JsonModel($response);
236
            }
237
        } else {
238
            $response = [
239
                'success' => false,
240
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
241
            ];
5500 anderson 242
 
5560 anderson 243
            return new JsonModel($response);
244
        }
245
    }
5500 anderson 246
 
5560 anderson 247
    public function shareAction()
248
    {
249
        $id = $this->params()->fromRoute('id');
5500 anderson 250
 
5560 anderson 251
        $request = $this->getRequest();
252
        if ($request->isPost()) {
5500 anderson 253
 
5560 anderson 254
            $currentUserPlugin = $this->plugin('currentUserPlugin');
255
            $currentUser = $currentUserPlugin->getUser();
5500 anderson 256
 
257
 
5560 anderson 258
            $feedMapper = FeedMapper::getInstance($this->adapter);
259
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
260
            if (!$feed) {
261
                $response = [
262
                    'success' => false,
263
                    'data' => 'ERROR_POST_NOT_FOUND'
264
                ];
265
                return new JsonModel($response);
266
            }
5500 anderson 267
 
5560 anderson 268
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
269
            $dataPost['company_id'] = $this->params()->fromRoute('company_id');
270
            $dataPost['group_id'] = $this->params()->fromRoute('group_id');
271
            $dataPost['shared_feed_id'] = $feed->id;
1 www 272
 
5500 anderson 273
 
274
 
275
 
5560 anderson 276
            $response = $this->postOrShareFeed($dataPost);
277
            if ($response['success']) {
278
                $feedMapper->incTotalShared($feed->id);
279
                $total_shared = $feedMapper->fetchTotalShared($feed->id);
5500 anderson 280
 
5560 anderson 281
                $response['total_shared'] = $total_shared;
282
            }
5500 anderson 283
 
284
 
5560 anderson 285
            return new JsonModel($response);
286
        } else {
287
            $response = [
288
                'success' => false,
289
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
290
            ];
291
        }
5500 anderson 292
 
5560 anderson 293
        return new JsonModel($response);
294
    }
5500 anderson 295
 
1 www 296
    public function commentDeleteAction()
297
    {
298
        $request = $this->getRequest();
5500 anderson 299
        if ($request->isPost()) {
1 www 300
            $currentUserPlugin = $this->plugin('currentUserPlugin');
301
            $currentUser = $currentUserPlugin->getUser();
5500 anderson 302
 
1 www 303
            $feed_id = $this->params()->fromRoute('id');
304
            $comment = $this->params()->fromRoute('comment');
5500 anderson 305
 
1 www 306
            $feedMapper = FeedMapper::getInstance($this->adapter);
3639 efrain 307
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($feed_id, $currentUser->network_id);
5500 anderson 308
 
309
            if ($feed) {
310
 
1 www 311
                $commentMapper = CommentMapper::getInstance($this->adapter);
312
                $comment = $commentMapper->fetchOneByUuid($comment);
5500 anderson 313
 
314
                if ($comment && $comment->feed_id == $feed->id && $comment->user_id == $currentUser->id) {
315
 
1 www 316
                    $comment->status = Comment::STATUS_DELETED;
5500 anderson 317
 
318
                    if ($commentMapper->update($comment)) {
319
 
1 www 320
                        $total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
5500 anderson 321
 
322
 
1 www 323
                        $feedMapper = FeedMapper::getInstance($this->adapter);
324
                        $feed = $feedMapper->fetchOne($comment->feed_id);
325
                        $feed->total_comments = $total_comments;
326
                        $feedMapper->update($feed);
5500 anderson 327
 
328
 
329
 
330
 
331
 
1 www 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
        }
5500 anderson 361
 
1 www 362
        return new JsonModel($response);
363
    }
5500 anderson 364
 
1 www 365
    public function deleteAction()
366
    {
5588 anderson 367
        $request = $this->getRequest();
368
        if ($request->isPost()) {
369
            $currentUserPlugin = $this->plugin('currentUserPlugin');
370
            $currentUser = $currentUserPlugin->getUser();
5500 anderson 371
 
5588 anderson 372
            $feed_id =  $this->params()->fromRoute('id');
5500 anderson 373
 
5588 anderson 374
            $feedMapper = FeedMapper::getInstance($this->adapter);
375
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($feed_id, $currentUser->network_id);
5500 anderson 376
 
5588 anderson 377
            if ($feed) {
5500 anderson 378
 
5588 anderson 379
                if ($feed->user_id && $feed->user_id == $currentUser->id) {
5500 anderson 380
 
5588 anderson 381
                    return new JsonModel($this->deleteFeed($feed));
5604 anderson 382
                } else if ($feed->group_id) {
383
 
384
                    $groupMapper = GroupMapper::getInstance($this->adapter);
385
                    $group = $groupMapper->fetchOne($feed->group_id);
386
                    if ($group && $group->user_id == $currentUser->id) {
387
                        return new JsonModel($this->deleteFeed($feed));
388
                    }
389
                } else if ($feed->company_id) {
390
                    $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
391
                    $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($feed->company_id, $currentUser->id);
392
                    if ($companyUser) {
393
                        if (in_array($companyUser->type, [CompanyUser::TYPE_OWNER, CompanyUser::TYPE_ADMINISTRATOR, CompanyUser::TYPE_MANAGER])) {
394
                            return new JsonModel($this->deleteFeed($feed));
395
                        }
396
                    }
5588 anderson 397
                }
5500 anderson 398
 
5602 anderson 399
                $response = [
400
                    'success' => false,
401
                    'data' => ' ERROR_UNAUTHORIZED'
402
                ];
5591 anderson 403
            } else {
5590 anderson 404
                $response = [
405
                    'success' => false,
5591 anderson 406
                    'data' => 'ERROR_FEED_NOT_FOUND'
5590 anderson 407
                ];
5588 anderson 408
            }
5500 anderson 409
 
5588 anderson 410
            return new JsonModel($response);
411
        } else {
5591 anderson 412
            $response = [
413
                'success' => false,
414
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
415
            ];
5588 anderson 416
        }
5500 anderson 417
 
5588 anderson 418
        return new JsonModel($response);
1 www 419
    }
5500 anderson 420
 
5560 anderson 421
    public function addAction()
422
    {
423
        $request = $this->getRequest();
424
        if ($request->isPost()) {
425
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
426
            $dataPost['company_id'] = $this->params()->fromRoute('company_id');
427
            $dataPost['group_id'] = $this->params()->fromRoute('group_id');
428
            $response = $this->postOrShareFeed($dataPost);
5500 anderson 429
 
430
 
5560 anderson 431
            return new JsonModel($response);
432
        } else {
433
            $response = [
434
                'success' => false,
435
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
436
            ];
437
        }
5500 anderson 438
 
5560 anderson 439
        return new JsonModel($response);
440
    }
1 www 441
 
5560 anderson 442
    public function postOrShareFeed($dataPost)
443
    {
444
        $utilMapper = UtilMapper::getInstance($this->adapter);
445
        $now = $utilMapper->getDatebaseNow();
5500 anderson 446
 
5560 anderson 447
        $currentUserPlugin = $this->plugin('currentUserPlugin');
448
        $currentUser = $currentUserPlugin->getUser();
5500 anderson 449
 
5560 anderson 450
        $companyId  = empty($dataPost['company_id']) ? '' : $dataPost['company_id'];
451
        $groupId    = empty($dataPost['group_id']) ? '' : $dataPost['group_id'];
5500 anderson 452
 
453
 
5560 anderson 454
        if ($groupId || $companyId) {
455
            $form = new  CreateGroupCompanyForm($this->adapter);
456
        } else {
457
            $form = new  CreateForm($this->adapter);
458
        }
459
        $form->setData($dataPost);
460
        if ($form->isValid()) {
1 www 461
 
462
 
5500 anderson 463
 
464
 
5560 anderson 465
            $hydrator = new ObjectPropertyHydrator();
466
            $feed = new Feed();
467
            $hydrator->hydrate($dataPost, $feed);
1 www 468
 
5560 anderson 469
            $company = null;
470
            $group = null;
1 www 471
 
5560 anderson 472
            if ($companyId) {
473
                $companyMapper = CompanyMapper::getInstance($this->adapter);
474
                $company = $companyMapper->fetchOneByUuidAndNetworkId($companyId, $currentUser->network_id);
5500 anderson 475
 
476
 
5560 anderson 477
                if (!$company) {
478
                    return [
479
                        'success'   => false,
480
                        'data'   => 'ERROR_COMPANY_NOT_FOUND'
481
                    ];
482
                }
5500 anderson 483
 
5560 anderson 484
                if ($company->status != Company::STATUS_ACTIVE) {
485
                    return [
486
                        'success'   => false,
487
                        'data'   => 'ERROR_COMPANY_IS_NOT_ACTIVE'
488
                    ];
489
                }
5500 anderson 490
 
5560 anderson 491
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
492
                $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
5500 anderson 493
 
5560 anderson 494
                //print_r($companyUser); exit;
5500 anderson 495
 
496
 
5560 anderson 497
                if (!$companyUser || $companyUser->status != CompanyUser::STATUS_ACCEPTED) {
498
                    return [
499
                        'success'   => false,
500
                        'data'   => 'ERROR_UNAUTHORIZED'
501
                    ];
502
                }
503
            }
5500 anderson 504
 
5560 anderson 505
            if ($groupId) {
506
                $groupMapper = GroupMapper::getInstance($this->adapter);
507
                $group = $groupMapper->fetchOneByUuidAndNetworkId($groupId, $currentUser->network_id);
5500 anderson 508
 
5560 anderson 509
                if (!$group) {
510
                    return [
511
                        'success'   => false,
512
                        'data'   => 'ERROR_GROUP_NOT_FOUND'
513
                    ];
514
                }
5500 anderson 515
 
5560 anderson 516
                if ($group->status != Group::STATUS_ACTIVE) {
517
                    return [
518
                        'success'   => false,
519
                        'data'   => 'ERROR_GROUP_IS_NOT_ACTIVE'
520
                    ];
521
                }
5500 anderson 522
 
5560 anderson 523
                $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
524
                $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
525
                if (!$groupMember || !in_array($groupMember->status, [GroupMember::STATUS_ACCEPTED, GroupMember::STATUS_AUTO_JOIN])) {
526
                    return [
527
                        'success'   => false,
528
                        'data'   => 'ERROR_UNAUTHORIZED'
529
                    ];
530
                }
531
            }
5500 anderson 532
 
5560 anderson 533
            $feed->network_id   = $currentUser->network_id;
534
            $feed->company_id   = $company ? $company->id : null;
535
            $feed->group_id     = $group ? $group->id : null;
536
            $feed->user_id      = $currentUser->id;
5500 anderson 537
 
5560 anderson 538
            if ($companyId) {
539
                $feed->type = Feed::TYPE_COMPANY;
540
            } else if ($groupId) {
541
                $feed->type = Feed::TYPE_GROUP;
542
            } else {
543
                $feed->type = Feed::TYPE_UPDATE;
544
            }
5500 anderson 545
 
5560 anderson 546
            $posted_or_shared   = $dataPost['posted_or_shared'];
547
            if ($posted_or_shared != Feed::POSTED && $posted_or_shared != Feed::SHARED) {
548
                return [
549
                    'success'   => false,
550
                    'data'      => 'ERROR_SUPPLY_VALID_SHARING_STATUS'
551
                ];
552
            }
553
            $feed->posted_or_shared = $posted_or_shared;
554
            $feedMapper = FeedMapper::getInstance($this->adapter);
5500 anderson 555
 
5560 anderson 556
            $shared_with = isset($dataPost['shared_with']) ? $dataPost['shared_with'] : Feed::SHARE_WITH_PUBLIC;
557
            if ($shared_with != Feed::SHARE_WITH_PUBLIC && $shared_with != Feed::SHARE_WITH_CONNECTIONS) {
558
                return [
559
                    'status' => false,
560
                    'error' => 'ERROR_SUPPLY_VALID_SHARING_WITH_TYPE',
561
                ];
562
            }
5500 anderson 563
 
5560 anderson 564
            $feed->shared_with = $shared_with;
565
            if (Feed::SHARED == $feed->posted_or_shared) {
566
                $shared_feed_id     = empty($dataPost['shared_feed_id']) ? 0 : $dataPost['shared_feed_id'];
5500 anderson 567
 
568
 
5560 anderson 569
                $sharedFeed = $feedMapper->fetchOneByIdAndNetworkId($shared_feed_id, $currentUser->network_id);
570
                if (!$sharedFeed) {
571
                    return [
572
                        'status' => false,
573
                        'error' => 'ERROR_POST_SHARE',
574
                    ];
575
                }
5500 anderson 576
 
5560 anderson 577
                $feed->shared_feed_id       = $sharedFeed->id;
578
                $feed->shared_company_id    = $sharedFeed->shared_company_id;
579
                $feed->shared_group_id      = $sharedFeed->shared_group_id;
580
                $feed->shared_job_id        = $sharedFeed->shared_job_id;
581
            } else {
582
                $sharedFeed = null;
583
            }
414 geraldo 584
 
5560 anderson 585
            $feed->status = Feed::STATUS_PUBLISHED;
586
            $feed->total_comments    = 0;
587
            $feed->total_shared     = 0;
5500 anderson 588
 
589
 
5560 anderson 590
            $files = $this->getRequest()->getFiles()->toArray();
591
            $file_type = '';
5500 anderson 592
 
5560 anderson 593
            $encoding = $this->params()->fromRoute('encoding');
594
            if ($encoding == 'base64') {
5500 anderson 595
 
596
 
5560 anderson 597
                $fileBase64Name = $this->params()->fromPost('file_base64_name');
598
                $fileBase64Content = $this->params()->fromPost('file_base64_content');
5500 anderson 599
 
5560 anderson 600
                if ($fileBase64Name && $fileBase64Content) {
5500 anderson 601
 
5560 anderson 602
                    try {
603
                        $fileBase64Content = base64_decode($fileBase64Content);
604
                        if ($fileBase64Content) {
605
                            $feed_filename      = $this->normalizeString($fileBase64Name);
606
                            $feed_tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp';
607
                            if (!file_exists($feed_tmp_filename)) {
608
                                mkdir($feed_tmp_filename, 0755, true);
609
                            }
415 geraldo 610
 
5500 anderson 611
 
612
 
5560 anderson 613
                            $feed_tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $feed_filename;
614
                            file_put_contents($feed_tmp_filename, $fileBase64Content);
5500 anderson 615
 
616
 
617
 
5560 anderson 618
                            $mime_type = mime_content_type($feed_tmp_filename);
619
                            //echo '$mime_type = '. $mime_type; exit;
5500 anderson 620
 
621
 
5560 anderson 622
                            if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
623
                                $file_type = Feed::FILE_TYPE_IMAGE;
624
                            } else if ($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
625
                                $file_type = Feed::FILE_TYPE_VIDEO;
626
                            } else if ($mime_type == 'application/pdf') {
627
                                $file_type = Feed::FILE_TYPE_DOCUMENT;
628
                            }
629
                        }
630
                    } catch (\Exception $e) {
631
                    }
632
                }
633
            } else {
415 geraldo 634
 
5560 anderson 635
                if (isset($files['file']) && empty($files['file']['error'])) {
636
                    $feed_tmp_filename  = $files['file']['tmp_name'];
637
                    $feed_filename      = $this->normalizeString($files['file']['name']);
5500 anderson 638
 
5560 anderson 639
                    $mime_type = mime_content_type($feed_tmp_filename);
5500 anderson 640
 
641
 
5560 anderson 642
                    if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
643
                        $file_type = Feed::FILE_TYPE_IMAGE;
644
                    } else if ($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
645
                        $file_type = Feed::FILE_TYPE_VIDEO;
646
                    } else if ($mime_type == 'application/pdf') {
647
                        $file_type = Feed::FILE_TYPE_DOCUMENT;
648
                    }
649
                }
650
            }
424 geraldo 651
 
5500 anderson 652
 
5560 anderson 653
            $result = $feedMapper->insert($feed);
654
            if ($result) {
5500 anderson 655
 
5560 anderson 656
                if ($sharedFeed && $sharedFeed->type == Feed::TYPE_UPDATE) {
5500 anderson 657
 
658
 
5560 anderson 659
                    $userMapper = UserMapper::getInstance($this->adapter);
660
                    $ownerFeed = $userMapper->fetchOne($sharedFeed->user_id);
5500 anderson 661
 
5560 anderson 662
                    $notification = new Notification();
663
                    $notification->type     = Notification::TYPE_SHARE_MY_FEED;
664
                    $notification->read     = Notification::NO;
665
                    $notification->user_id  = $ownerFeed->id;
666
                    $notification->feed_id = $feed->id;
667
                    $notification->message  = 'LABEL_SHARE_MY_FEED';
668
                    $notification->url      = $this->url()->fromRoute('dashboard', ['feed' => $sharedFeed->uuid]);
5500 anderson 669
 
5560 anderson 670
                    $notificationMapper = NotificationMapper::getInstance($this->adapter);
671
                    $notificationMapper->insert($notification);
5500 anderson 672
 
5560 anderson 673
                    $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
674
                    $userNotification = $userNotificationMapper->fetchOne($ownerFeed->id);
5500 anderson 675
 
5560 anderson 676
                    if ($userNotification && $userNotification->share_my_feed) {
677
                        $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
678
                        $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_SHARE_MY_FEED, $currentUser->network_id);
5500 anderson 679
 
5560 anderson 680
                        if ($emailTemplate) {
681
                            $arrayCont = [
682
                                'firstname'             => $currentUser->first_name,
683
                                'lastname'              => $currentUser->last_name,
684
                                'other_user_firstname'  => $ownerFeed->first_name,
685
                                'other_user_lastname'   => $ownerFeed->last_name,
686
                                'company_name'          => '',
687
                                'group_name'            => '',
688
                                'content'               => '',
689
                                'code'                  => '',
690
                                'link'                  => $this->url()->fromRoute('dashboard', ['feed' => $feed->uuid], ['force_canonical' => true])
691
                            ];
5500 anderson 692
 
5560 anderson 693
                            $email = new QueueEmail($this->adapter);
694
                            $email->processEmailTemplate($emailTemplate, $arrayCont, $ownerFeed->email, trim($ownerFeed->first_name . ' ' . $ownerFeed->last_name));
695
                        }
696
                    }
697
                }
5500 anderson 698
 
699
 
5560 anderson 700
                $feed = $feedMapper->fetchOne($feed->id);
5500 anderson 701
 
5560 anderson 702
                if ($file_type == Feed::FILE_TYPE_DOCUMENT) {
703
                    try {
704
                        $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
705
                        if (!file_exists($target_path)) {
706
                            mkdir($target_path, 0755);
707
                        }
5500 anderson 708
 
5560 anderson 709
                        $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
5500 anderson 710
 
5560 anderson 711
                        if ($encoding == 'base64') {
712
                            $resultMoveOrRename = rename($feed_tmp_filename, $full_filename);
713
                        } else {
714
                            $resultMoveOrRename = move_uploaded_file($feed_tmp_filename, $full_filename);
715
                        }
5500 anderson 716
 
5560 anderson 717
                        if ($resultMoveOrRename) {
5500 anderson 718
 
5560 anderson 719
                            $feed->file_type = $file_type;
720
                            $feed->file_name = basename($feed_filename);
721
                            $feedMapper->update($feed);
722
                        } else {
723
                            error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename);
724
                        }
725
                    } catch (\Throwable $e) {
726
                        error_log($e->getTraceAsString());
727
                    }
728
                }
729
                if ($file_type == Feed::FILE_TYPE_IMAGE) {
730
                    try {
731
                        $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
732
                        if (!file_exists($target_path)) {
733
                            mkdir($target_path, 0755);
734
                        }
5500 anderson 735
 
5560 anderson 736
                        list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
424 geraldo 737
 
5560 anderson 738
                        $feed_filename = substr($feed_filename, 0, strrpos($feed_filename, '.'))  . '.png';
739
                        //$crop_to_dimensions = false;
740
                        //if(Image::uploadImage($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height, $crop_to_dimensions )) {
741
                        if (Image::uploadImageWithoutCompletationSize($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height)) {
5500 anderson 742
 
5560 anderson 743
                            $feed->file_type = $file_type;
744
                            $feed->file_name = basename($feed_filename);
745
                            $feedMapper->update($feed);
746
                        }
747
                    } catch (\Throwable $e) {
748
                        error_log($e->getTraceAsString());
749
                    }
750
                }
751
                if ($file_type == Feed::FILE_TYPE_VIDEO) {
752
                    try {
753
                        $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
754
                        if (!file_exists($target_path)) {
755
                            mkdir($target_path, 0755);
756
                        }
5500 anderson 757
 
5560 anderson 758
                        $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
424 geraldo 759
 
5560 anderson 760
                        if ($encoding == 'base64') {
761
                            $resultMoveOrRename = rename($feed_tmp_filename, $full_filename);
762
                        } else {
763
                            $resultMoveOrRename = move_uploaded_file($feed_tmp_filename, $full_filename);
764
                        }
3477 efrain 765
 
5560 anderson 766
                        if ($resultMoveOrRename) {
5500 anderson 767
 
768
 
5560 anderson 769
                            // $getFromSecound = 2;
5500 anderson 770
 
5560 anderson 771
                            //extracción del cover
772
                            $generateFileName = substr($feed_filename, 0, strrpos($feed_filename, '.'));
773
                            $generateFile =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.jpg';
3477 efrain 774
 
5560 anderson 775
                            $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $full_filename";
776
                            //error_log($cmd);
5500 anderson 777
 
5560 anderson 778
                            $response   = trim(shell_exec($cmd));
5500 anderson 779
 
5560 anderson 780
                            $source_duration = 0;
3477 efrain 781
 
5500 anderson 782
 
5560 anderson 783
                            $lines = explode("\n", $response);
784
                            foreach ($lines as $line) {
785
                                $line = trim(strtolower($line));
786
                                if (strpos($line, 'duration') !== false) {
787
                                    $values = explode('=', $line);
788
                                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
789
                                }
790
                            }
5500 anderson 791
 
792
 
5560 anderson 793
                            if ($source_duration == 0) {
794
                                $second_extract = '00:00:02';
795
                            } else {
796
                                if ($source_duration > 10) {
797
                                    $second_extract = '00:00:10';
798
                                } else {
799
                                    $second_extract = '00:00:02';
800
                                }
801
                            }
5500 anderson 802
 
5560 anderson 803
                            $imageSize = $this->config['leaderslinked.image_sizes.feed_image_size'];
5500 anderson 804
 
5560 anderson 805
                            //$cmd = "/usr/bin/ffmpeg -y -i $full_filename  -pix_fmt yuvj422p -deinterlace -an -ss $second_extract -f mjpeg -t 1 -r 1 -y -s $imageSize $generateFile";
806
                            $cmd = "/usr/bin/ffmpeg -y -i $full_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y -s $imageSize $generateFile";
807
                            //error_log($cmd);
3477 efrain 808
 
5500 anderson 809
 
5560 anderson 810
                            exec($cmd);
5500 anderson 811
 
812
 
5560 anderson 813
                            $feed->file_type = $file_type;
814
                            $feed->file_name = basename($feed_filename);
815
                            $feed->file_image_preview = basename($generateFile);
816
                            $feedMapper->update($feed);
5500 anderson 817
 
5560 anderson 818
                            $videoConvert = new VideoConvert();
819
                            $videoConvert->filename = $full_filename;
820
                            $videoConvert->type = VideoConvert::TYPE_FEED;
5500 anderson 821
 
5560 anderson 822
                            $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
823
                            $videoConvertMapper->insert($videoConvert);
824
                        } else {
825
                            error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename);
826
                        }
827
                    } catch (\Throwable $e) {
828
                        error_log($e->getTraceAsString());
829
                    }
830
                }
424 geraldo 831
 
832
 
5560 anderson 833
                return [
834
                    'success'   => true,
835
                    'data'   => $this->renderFeed($feed->id, $now)
836
                ];
837
            } else {
838
                return [
839
                    'success'   => false,
840
                    'data'   => $feedMapper->getError()
841
                ];
842
            }
843
        } else {
844
            $messages = [];
845
            $form_messages = (array) $form->getMessages();
846
            foreach ($form_messages  as $fieldname => $field_messages) {
847
                $messages[$fieldname] = array_values($field_messages);
848
            }
5500 anderson 849
 
5560 anderson 850
            return [
851
                'success'   => false,
852
                'data'   => $messages
853
            ];
854
        }
855
    }
5956 anderson 856
 
5765 efrain 857
    public function saveReactionAction()
858
    {
859
        $id = $this->params()->fromRoute('id');
860
        $reaction  = $this->params()->fromRoute('reaction');
5956 anderson 861
 
5765 efrain 862
        $request = $this->getRequest();
863
        if ($request->isPost()) {
864
            $currentUserPlugin = $this->plugin('currentUserPlugin');
865
            $currentUser = $currentUserPlugin->getUser();
5956 anderson 866
 
867
 
5765 efrain 868
            $feedMapper = FeedMapper::getInstance($this->adapter);
869
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
870
            if (!$feed) {
871
                $response = [
872
                    'success' => false,
873
                    'data' => 'ERROR_POST_NOT_FOUND'
874
                ];
875
                return new JsonModel($response);
876
            }
877
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
878
            $contentReaction = $contentReactionMapper->fetchOneByFeedIdAndUserId($feed->id, $currentUser->id);
5956 anderson 879
 
5765 efrain 880
            if ($contentReaction) {
5775 efrain 881
                $contentReaction->reaction = $reaction;
882
                $result = $contentReactionMapper->update($contentReaction);
883
            } else {
884
                $contentReaction = new ContentReaction();
885
                $contentReaction->user_id = $currentUser->id;
886
                $contentReaction->feed_id = $feed->id;
887
                $contentReaction->relational = ContentReaction::RELATIONAL_FEED;
888
                $contentReaction->reaction = $reaction;
5956 anderson 889
 
5775 efrain 890
                $result = $contentReactionMapper->insert($contentReaction);
5765 efrain 891
            }
5956 anderson 892
 
893
 
894
 
5775 efrain 895
            if ($result) {
5956 anderson 896
 
5765 efrain 897
                $reactions = $contentReactionMapper->fetchCountContentReactionsByFeedId($feed->id);
898
                $response = [
899
                    'success' => true,
900
                    'data' => [
5775 efrain 901
                        'reactions' => $reactions
5765 efrain 902
                    ]
903
                ];
904
            } else {
905
                $response = [
906
                    'success' => false,
907
                    'data' => $contentReactionMapper->getError()
908
                ];
909
            }
910
            return new JsonModel($response);
911
        }
5956 anderson 912
 
5765 efrain 913
        $response = [
914
            'success' => false,
915
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
916
        ];
917
        return new JsonModel($response);
918
    }
5956 anderson 919
 
5765 efrain 920
    public function deleteReactionAction()
921
    {
922
        $id = $this->params()->fromRoute('id');
5956 anderson 923
 
5765 efrain 924
        $request = $this->getRequest();
925
        if ($request->isPost()) {
926
            $currentUserPlugin = $this->plugin('currentUserPlugin');
927
            $currentUser = $currentUserPlugin->getUser();
5956 anderson 928
 
5765 efrain 929
            $feedMapper = FeedMapper::getInstance($this->adapter);
930
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
931
            if (!$feed) {
932
                $response = [
933
                    'success' => false,
934
                    'data' => 'ERROR_POST_NOT_FOUND'
935
                ];
936
                return new JsonModel($response);
937
            }
5956 anderson 938
 
5765 efrain 939
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
5780 efrain 940
            $contentReaction = $contentReactionMapper->fetchOneByFeedIdAndUserId($feed->id, $currentUser->id);
5956 anderson 941
 
5780 efrain 942
            if (!$contentReaction) {
5765 efrain 943
                $response = [
944
                    'success' => false,
945
                    'data' => 'ERROR_DUPLICATE_ACTION'
946
                ];
947
                return new JsonModel($response);
948
            }
5956 anderson 949
 
5765 efrain 950
            if ($contentReactionMapper->deleteByFeedIdAndUserId($feed->id, $currentUser->id)) {
5780 efrain 951
                $reactions = $contentReactionMapper->fetchCountContentReactionsByFeedId($feed->id);
5956 anderson 952
 
953
 
5765 efrain 954
                $response = [
955
                    'success' => true,
956
                    'data' => [
957
                        'reactions' => $reactions
958
                    ]
959
                ];
960
            } else {
961
                $response = [
962
                    'success' => false,
963
                    'data' => $contentReactionMapper->getError()
964
                ];
965
            }
966
            return new JsonModel($response);
967
        }
5956 anderson 968
 
5765 efrain 969
        $response = [
970
            'success' => false,
971
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
972
        ];
973
        return new JsonModel($response);
974
    }
5500 anderson 975
 
5765 efrain 976
    /*
5560 anderson 977
    public function likeAction()
978
    {
979
        $id = $this->params()->fromRoute('id');
5500 anderson 980
 
5560 anderson 981
        $request = $this->getRequest();
982
        if ($request->isPost()) {
983
            $currentUserPlugin = $this->plugin('currentUserPlugin');
984
            $currentUser = $currentUserPlugin->getUser();
3639 efrain 985
 
5500 anderson 986
 
5560 anderson 987
            $feedMapper = FeedMapper::getInstance($this->adapter);
988
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
989
            if (!$feed) {
990
                $response = [
991
                    'success' => false,
992
                    'data' => 'ERROR_POST_NOT_FOUND'
993
                ];
994
                return new JsonModel($response);
995
            }
5500 anderson 996
 
5765 efrain 997
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
998
            $contentReaction =  $contentReactionMapper->fetchOneByFeedIdAndUserId($feed->id, $currentUser->id);
5500 anderson 999
 
5765 efrain 1000
            if ($contentReaction) {
5560 anderson 1001
                $response = [
1002
                    'success' => false,
1003
                    'data' => 'ERROR_DUPLICATE_ACTION'
1004
                ];
1005
                return new JsonModel($response);
1006
            }
5500 anderson 1007
 
5780 efrain 1008
            $contentReaction = new Like();
1009
            $contentReaction->user_id = $currentUser->id;
1010
            $contentReaction->feed_id = $feed->id;
1011
            $contentReaction->relational = Like::RELATIONAL_FEED;
5500 anderson 1012
 
5780 efrain 1013
            if ($contentReactionMapper->insert($contentReaction)) {
5500 anderson 1014
 
5780 efrain 1015
                $contentReactions = $contentReactionMapper->fetchCountLikeByFeedId($feed->id);
5500 anderson 1016
 
1017
 
1018
 
5560 anderson 1019
                if ($feed->type == Feed::TYPE_UPDATE) {
5500 anderson 1020
 
1021
 
5560 anderson 1022
                    $userMapper = UserMapper::getInstance($this->adapter);
1023
                    $ownerFeed = $userMapper->fetchOne($feed->user_id);
5500 anderson 1024
 
5560 anderson 1025
                    $notification = new Notification();
1026
                    $notification->type     = Notification::TYPE_LIKE_MY_FEED;
1027
                    $notification->read     = Notification::NO;
1028
                    $notification->user_id  = $feed->user_id;
1029
                    $notification->feed_id  = $feed->id;
1030
                    $notification->message  = 'LABEL_LIKE_MY_FEED';
1031
                    $notification->url      = $this->url()->fromRoute('dashboard', ['feed' => $feed->uuid]);
5500 anderson 1032
 
5560 anderson 1033
                    $notificationMapper = NotificationMapper::getInstance($this->adapter);
1034
                    $notificationMapper->insert($notification);
5500 anderson 1035
 
5560 anderson 1036
                    $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
1037
                    $userNotification = $userNotificationMapper->fetchOne($ownerFeed->id);
5500 anderson 1038
 
5560 anderson 1039
                    if ($userNotification && $userNotification->like_my_feed) {
1040
                        $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
1041
                        $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_LIKE_MY_FEED, $currentUser->network_id);
5500 anderson 1042
 
5560 anderson 1043
                        if ($emailTemplate) {
1044
                            $arrayCont = [
1045
                                'firstname'             => $currentUser->first_name,
1046
                                'lastname'              => $currentUser->last_name,
1047
                                'other_user_firstname'  => $ownerFeed->first_name,
1048
                                'other_user_lastname'   => $ownerFeed->last_name,
1049
                                'company_name'          => '',
1050
                                'group_name'            => '',
1051
                                'content'               => '',
1052
                                'code'                  => '',
1053
                                'link'                  => $this->url()->fromRoute('dashboard', ['feed' => $feed->uuid], ['force_canonical' => true])
1054
                            ];
5500 anderson 1055
 
5560 anderson 1056
                            $email = new QueueEmail($this->adapter);
1057
                            $email->processEmailTemplate($emailTemplate, $arrayCont, $ownerFeed->email, trim($ownerFeed->first_name . ' ' . $ownerFeed->last_name));
1058
                        }
1059
                    }
1060
                }
1 www 1061
 
5500 anderson 1062
 
1063
 
1064
 
1065
 
1066
 
1067
 
1068
 
5560 anderson 1069
                $response = [
1070
                    'success' => true,
1071
                    'data' => [
5780 efrain 1072
                        'likes' => $contentReactions
5560 anderson 1073
                    ]
1074
                ];
1075
            } else {
1076
                $response = [
1077
                    'success' => false,
5780 efrain 1078
                    'data' => $contentReactionMapper->getError()
5560 anderson 1079
                ];
1080
            }
1081
            return new JsonModel($response);
1082
        }
5500 anderson 1083
 
5560 anderson 1084
        $response = [
1085
            'success' => false,
1086
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1087
        ];
1088
        return new JsonModel($response);
1089
    }
5500 anderson 1090
 
5560 anderson 1091
    public function unlikeAction()
1092
    {
1093
        $id = $this->params()->fromRoute('id');
5500 anderson 1094
 
5560 anderson 1095
        $request = $this->getRequest();
1096
        if ($request->isPost()) {
1097
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1098
            $currentUser = $currentUserPlugin->getUser();
3639 efrain 1099
 
5560 anderson 1100
            $feedMapper = FeedMapper::getInstance($this->adapter);
1101
            $feed = $feedMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1102
            if (!$feed) {
1103
                $response = [
1104
                    'success' => false,
1105
                    'data' => 'ERROR_POST_NOT_FOUND'
1106
                ];
1107
                return new JsonModel($response);
1108
            }
5500 anderson 1109
 
5780 efrain 1110
            $contentReactionMapper = LikeMapper::getInstance($this->adapter);
1111
            $contentReaction = $contentReactionMapper->fetchOneByFeedIdAndUserId($feed->id, $currentUser->id);
5500 anderson 1112
 
5780 efrain 1113
            if (!$contentReaction) {
5560 anderson 1114
                $response = [
1115
                    'success' => false,
1116
                    'data' => 'ERROR_DUPLICATE_ACTION'
1117
                ];
1118
                return new JsonModel($response);
1119
            }
5500 anderson 1120
 
5780 efrain 1121
            if ($contentReactionMapper->deleteByFeedIdAndUserId($feed->id, $currentUser->id)) {
1122
                $contentReactions = $contentReactionMapper->fetchCountLikeByFeedId($feed->id);
5500 anderson 1123
 
1124
 
5560 anderson 1125
                $response = [
1126
                    'success' => true,
1127
                    'data' => [
5780 efrain 1128
                        'likes' => $contentReactions
5560 anderson 1129
                    ]
1130
                ];
1131
            } else {
1132
                $response = [
1133
                    'success' => false,
5780 efrain 1134
                    'data' => $contentReactionMapper->getError()
5560 anderson 1135
                ];
1136
            }
1137
            return new JsonModel($response);
1138
        }
5500 anderson 1139
 
5560 anderson 1140
        $response = [
1141
            'success' => false,
1142
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1143
        ];
1144
        return new JsonModel($response);
5765 efrain 1145
    }*/
3639 efrain 1146
 
5502 anderson 1147
    public function timelineAction()
1148
    {
1149
        $utilMapper = UtilMapper::getInstance($this->adapter);
1150
        $now = $utilMapper->getDatebaseNow();
5517 anderson 1151
 
1152
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1153
        $currentUser = $currentUserPlugin->getUser();
1154
 
1155
 
5518 anderson 1156
        $type   = $this->params()->fromRoute('type');
5520 anderson 1157
        if ($type == 'user') {
1158
            $id = $currentUser->id;
5526 anderson 1159
        } else {
1160
            $id     = $this->params()->fromRoute('id');
5520 anderson 1161
        }
5517 anderson 1162
 
5518 anderson 1163
        $feedHighlighted = 0;
1164
        $feedUuid = $this->params()->fromRoute('feed');
1165
 
1166
 
1167
 
1168
        if ($feedUuid) {
1169
            $feedMapper = FeedMapper::getInstance($this->adapter);
1170
            $feed = $feedMapper->fetchOneByUuidAndNetworkIdAnyStatus($feedUuid, $currentUser->network_id);
1171
 
1172
            if ($feed) {
1173
                $feedHighlighted = $feed->id;
1174
            }
1175
        }
1176
 
1177
 
1178
 
1179
        $request = $this->getRequest();
5531 anderson 1180
        if ($request->isGet()) {
5518 anderson 1181
 
5531 anderson 1182
            $page = (int) $this->params()->fromQuery('page');
5545 anderson 1183
            if ($type == 'user') {
5532 anderson 1184
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
1185
                $records = $connectionMapper->fetchAllConnectionsByUser($id);
1186
                $connectionIds = [0];
1187
                foreach ($records as $record) {
1188
                    if ($currentUser->id == $record->request_from) {
1189
                        array_push($connectionIds, $record->request_to);
1190
                    } else {
1191
                        array_push($connectionIds, $record->request_from);
1192
                    }
1193
                }
1194
                array_push($connectionIds, 1);
5518 anderson 1195
 
5532 anderson 1196
                $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
1197
                $records = $companyFollowerMapper->fetchAllByFollowerId($currentUser->id);
5518 anderson 1198
 
5532 anderson 1199
                $companyIds = [0];
1200
                foreach ($records as $record) {
1201
                    if (!in_array($record->company_id, $companyIds)) {
1202
                        array_push($companyIds, $record->company_id);
1203
                    }
1204
                }
5518 anderson 1205
 
5532 anderson 1206
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1207
                $companyUsers = $companyUserMapper->fetchAllByUserId($currentUser->id);
5518 anderson 1208
 
5532 anderson 1209
                foreach ($companyUsers as $companyUser) {
1210
                    if ($companyUser->status == CompanyUser::STATUS_ACCEPTED) {
1211
                        if (!in_array($companyUser->company_id, $companyIds)) {
1212
                            array_push($companyIds, $companyUser->company_id);
1213
                        }
1214
                    }
1215
                }
5518 anderson 1216
 
5560 anderson 1217
 
5547 anderson 1218
                $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1219
                $groupMembers = $groupMemberMapper->fetchAllByUserId($currentUser->id);
5554 anderson 1220
 
5555 anderson 1221
                $groupIds = [0];
5554 anderson 1222
 
5552 anderson 1223
                foreach ($groupMembers as $groupMember) {
1224
                    if ($groupMember->status == GroupMember::STATUS_ACCEPTED) {
5555 anderson 1225
                        if (!in_array($groupMember->group_id, $groupIds)) {
1226
                            array_push($groupIds, $groupMember->group_id);
5552 anderson 1227
                        }
1228
                    }
1229
                }
5546 anderson 1230
 
5534 anderson 1231
                $queryMapper = QueryMapper::getInstance($this->adapter);
1232
                $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
1233
                $select->columns(['id']);
1234
                $select->where->equalTo('network_id', $currentUser->network_id);
1235
                $select->where->equalTo('status', Feed::STATUS_PUBLISHED)
1236
                    ->AND->NEST
1237
                    ->OR->NEST->equalTo('user_id', $id)->and->equalTo('type', Feed::TYPE_UPDATE)->UNNEST
1238
                    ->OR->NEST->and->equalTo('type', Feed::TYPE_UPDATE)->and->equalTo('shared_with', Feed::SHARE_WITH_PUBLIC)->UNNEST
1239
                    ->OR->NEST->in('user_id', $connectionIds)->and->equalTo('type', Feed::TYPE_UPDATE)->and->equalTo('shared_with', Feed::SHARE_WITH_CONNECTIONS)->UNNEST
5535 anderson 1240
                    ->OR->NEST->or->in('company_id', $companyIds)->and->equalTo('type', Feed::TYPE_COMPANY)->UNNEST
5555 anderson 1241
                    ->OR->NEST->or->in('group_id', $groupIds)->and->equalTo('type', Feed::TYPE_GROUP)->UNNEST
5534 anderson 1242
                    ->UNNEST;
1243
                $select->order('added_on desc');
5532 anderson 1244
            }
5531 anderson 1245
            if ($type == 'company') {
1246
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1247
                $company = $companyMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5518 anderson 1248
 
5531 anderson 1249
                if (!$company || $company->status != Company::STATUS_ACTIVE) {
1250
                    $id = 0;
5517 anderson 1251
                } else {
5531 anderson 1252
                    $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
1253
                    $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
1254
                    if ($companyFollower) {
1255
                        $id = $company->id;
1256
                    } else {
5518 anderson 1257
 
5531 anderson 1258
                        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1259
                        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
5518 anderson 1260
 
5531 anderson 1261
                        if (!$companyUser || $companyUser->status != CompanyUser::STATUS_ACCEPTED) {
1262
                            $id = 0;
1263
                        } else {
1264
                            $id = $company->id;
1265
                        }
5518 anderson 1266
                    }
5517 anderson 1267
                }
5518 anderson 1268
 
1269
 
5531 anderson 1270
                $queryMapper = QueryMapper::getInstance($this->adapter);
1271
                $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
1272
                $select->columns(['id']);
1273
                $select->where->equalTo('network_id', $currentUser->network_id);
1274
                $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
1275
                $select->where->equalTo('company_id', $id);
1276
                $select->where->and->equalTo('type', Feed::TYPE_COMPANY);
1277
                $select->order('added_on desc');
1278
            }
5958 anderson 1279
            if ($type == 'group') {
1280
                $groupMapper = GroupMapper::getInstance($this->adapter);
1281
                $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5518 anderson 1282
 
5958 anderson 1283
                $id = 0;
5518 anderson 1284
 
5958 anderson 1285
                if ($group && $group->status == Group::STATUS_ACTIVE) {
1286
                    $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1287
                    $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
1288
                    if ($groupMember) {
5518 anderson 1289
 
1290
 
5958 anderson 1291
                        if (
1292
                            $groupMember->status == GroupMember::STATUS_ACCEPTED ||
1293
                            $groupMember->status == GroupMember::STATUS_AUTO_JOIN
1294
                        ) {
5518 anderson 1295
 
5958 anderson 1296
                            $id =  $groupMember->group_id;
1297
                        }
1298
                    }
1299
                }
5531 anderson 1300
 
5958 anderson 1301
                $queryMapper = QueryMapper::getInstance($this->adapter);
1302
                $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
1303
                $select->columns(['id']);
1304
                $select->where->equalTo('network_id', $currentUser->network_id);
5963 anderson 1305
                $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
1306
                $select->where->equalTo('group_id', $id);
1307
                $select->where->and->equalTo('type', Feed::TYPE_GROUP);
1308
                $select->where->or->equalTo('type', Feed::TYPE_UPDATE);
5958 anderson 1309
                $select->order('added_on desc');
1310
            }
5518 anderson 1311
 
5531 anderson 1312
            $dbSelect = new DbSelect($select, $this->adapter);
1313
            $paginator = new Paginator($dbSelect);
1314
            $paginator->setCurrentPageNumber($page ? $page : 1);
1315
            $paginator->setItemCountPerPage(10);
5518 anderson 1316
 
5531 anderson 1317
            $items = [];
5518 anderson 1318
 
1319
 
5531 anderson 1320
            if ($feedHighlighted) {
5518 anderson 1321
 
1322
 
5531 anderson 1323
                $items[] = $this->renderFeed($feedHighlighted, $now, $feedHighlighted);
1324
            }
5518 anderson 1325
 
5531 anderson 1326
            $feeds = $paginator->getCurrentItems();
1327
            foreach ($feeds as $feed) {
1328
                $items[] = $this->renderFeed($feed->id, $now, $feedHighlighted);
1329
            }
5518 anderson 1330
 
5531 anderson 1331
            $response = [
1332
                'success' => true,
1333
                'data' => [
1334
                    'total' => [
1335
                        'count' => $paginator->getTotalItemCount(),
1336
                        'pages' => $paginator->getPages()->pageCount,
1337
                    ],
1338
                    'current' => [
1339
                        'items'    => $items,
1340
                        'page'     => $paginator->getCurrentPageNumber(),
1341
                        'count'    => $paginator->getCurrentItemCount(),
1342
                    ]
5518 anderson 1343
                ]
5531 anderson 1344
            ];
5518 anderson 1345
 
1346
 
1347
 
5531 anderson 1348
            /*
5518 anderson 1349
            if($feedHighlighted) {
1350
 
1351
 
1352
                $items[] = $this->renderFeed($feedHighlighted, $now, $feedHighlighted);
1353
 
1354
                $response = [
1355
                    'success' => true,
1356
                    'data' => [
1357
                        'total' => [
1358
                            'count' => 1,
1359
                            'pages' => 1,
1360
                        ],
1361
                        'current' => [
1362
                            'items'    => $items,
1363
                            'page'     => 1,
1364
                            'count'    => 1,
1365
                        ]
1366
                    ]
1367
                ];
1368
            } else {
1369
 
1370
 
1371
 
1372
                $feeds = $paginator->getCurrentItems();
1373
                foreach($feeds as $feed)
1374
                {
1375
                    $items[] = $this->renderFeed($feed->id, $now, $feedHighlighted);
1376
                }
1377
 
1378
                $response = [
1379
                    'success' => true,
1380
                    'data' => [
1381
                        'total' => [
1382
                            'count' => $paginator->getTotalItemCount(),
1383
                            'pages' => $paginator->getPages()->pageCount,
1384
                        ],
1385
                        'current' => [
1386
                            'items'    => $items,
1387
                            'page'     => $paginator->getCurrentPageNumber(),
1388
                            'count'    => $paginator->getCurrentItemCount(),
1389
                        ]
1390
                    ]
1391
                ];
1392
            }*/
1393
 
1394
 
1395
 
5531 anderson 1396
            return new JsonModel($response);
1397
        }
5518 anderson 1398
 
1399
        $response = [
1400
            'success' => false,
1401
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1402
        ];
1403
 
1404
 
1405
        return new JsonModel($response);
5502 anderson 1406
    }
5500 anderson 1407
 
1 www 1408
    /**
1409
     *
1410
     * @param string $str
1411
     * @return string
1412
     */
5500 anderson 1413
    private function normalizeString($str = '')
1 www 1414
    {
1415
        $basename  = substr($str, 0, strrpos($str, '.'));
1416
        $basename  = str_replace('.', '-', $basename);
5500 anderson 1417
 
1 www 1418
        $extension  = substr($str, strrpos($str, '.'));
5500 anderson 1419
 
1 www 1420
        $str = $basename . $extension;
5500 anderson 1421
 
1 www 1422
        $str = strip_tags($str);
1423
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1424
        $str = preg_replace('/[\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1425
        $str = strtolower($str);
5500 anderson 1426
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1 www 1427
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1428
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1429
        $str = str_replace(' ', '-', $str);
1430
        $str = rawurlencode($str);
1431
        $str = str_replace('%', '-', $str);
1432
        return trim(strtolower($str));
1433
    }
5500 anderson 1434
 
1435
 
1436
 
1 www 1437
    /**
1438
     *
1439
     * @param int $feed_id
1440
     * @return array
1441
     */
3146 efrain 1442
    private function renderFeed($feed_id, $now, $feedHighlighted = 0)
1 www 1443
    {
1444
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1445
        $currentUser = $currentUserPlugin->getUser();
6049 anderson 1446
 
6117 anderson 1447
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1448
        $network = $currentNetworkPlugin->getNetwork();
1449
 
6128 anderson 1450
        //$currentCompany = $currentUserPlugin->getCompany();
6123 anderson 1451
 
3639 efrain 1452
        //$acl = $this->getEvent()->getViewModel()->getVariable('acl');
1 www 1453
 
1454
        $userMapper = UserMapper::getInstance($this->adapter);
1455
        $feedMapper = FeedMapper::getInstance($this->adapter);
3146 efrain 1456
        $feed = $feedMapper->fetchOneAnyStatus($feed_id);
5500 anderson 1457
 
1458
 
1459
 
1460
 
5765 efrain 1461
        /*
5780 efrain 1462
        $contentReactionMapper = LikeMapper::getInstance($this->adapter);
1463
        $contentReaction = $contentReactionMapper->fetchOneByFeedIdAndUserId($feed->id, $currentUser->id);
1464
        $contentReactions = $contentReactionMapper->fetchCountLikeByFeedId($feed->id);
5765 efrain 1465
        */
1466
        $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1467
        $reactions = $contentReactionMapper->fetchCountContentReactionsByFeedId($feed->id);
1468
        $reaction = $contentReactionMapper->fetchOneByFeedIdAndUserId($feed->id, $currentUser->id);
5956 anderson 1469
 
1470
 
1 www 1471
        $params = [
1472
            'id' => $feed->uuid
1473
        ];
5500 anderson 1474
        if ($feed->company_id) {
1 www 1475
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1476
            $company = $companyMapper->fetchOne($feed->company_id);
5500 anderson 1477
 
1 www 1478
            $params['company_id'] = $company->uuid;
1479
        } else {
1480
            $company = null;
1481
        }
5500 anderson 1482
        if ($feed->group_id) {
1 www 1483
            $groupMapper = GroupMapper::getInstance($this->adapter);
1484
            $group = $groupMapper->fetchOne($feed->group_id);
5500 anderson 1485
 
1 www 1486
            $params['group_id'] = $group->uuid;
1487
        } else {
1488
            $group = null;
1489
        }
5500 anderson 1490
 
1491
 
3364 efrain 1492
        $timestamp = time();
5500 anderson 1493
 
3364 efrain 1494
        list($usec, $sec) = explode(' ', microtime());
1495
        $seed = intval($sec + ((float) $usec * 100000));
1496
        mt_srand($seed, MT_RAND_MT19937);
1497
        $rand =  mt_rand();
5500 anderson 1498
 
1499
 
1500
 
1501
        $password  = md5('user-' . $currentUser->uuid . '-feed-' . $feed->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key);
1502
 
1503
 
3380 efrain 1504
        $share_params = [
5500 anderson 1505
            'type' => 'feed',
3379 efrain 1506
            'code' => $feed->uuid,
1507
            'user' => $currentUser->uuid,
1508
            'timestamp' => $timestamp,
1509
            'rand' => $rand,
1510
            'password' => $password,
3364 efrain 1511
        ];
3379 efrain 1512
 
5500 anderson 1513
 
1514
        $share_increment_external_counter_url = $this->url()->fromRoute('share/increment-external-counter', $share_params, ['force_canonical' => true]);
1515
 
1516
 
1517
        $share_external_url = $this->url()->fromRoute('shorter/generate', ['code' => $feed->uuid, 'type' => 'feed'], ['force_canonical' => true]);
1518
 
5956 anderson 1519
 
1520
 
5765 efrain 1521
        $params_reaction = array_merge($params, ['reaction' => ContentReaction::REACTION_RECOMMENDED]);
1522
        $feed_save_reaction_recommended = $this->url()->fromRoute('feed/save-reaction', $params_reaction);
5956 anderson 1523
 
5765 efrain 1524
        $params_reaction = array_merge($params, ['reaction' => ContentReaction::REACTION_SUPPORT]);
1525
        $feed_save_reaction_support = $this->url()->fromRoute('feed/save-reaction', $params_reaction);
5956 anderson 1526
 
5765 efrain 1527
        $params_reaction = array_merge($params, ['reaction' => ContentReaction::REACTION_LOVE]);
1528
        $feed_save_reaction_love = $this->url()->fromRoute('feed/save-reaction', $params_reaction);
5956 anderson 1529
 
5765 efrain 1530
        $params_reaction = array_merge($params, ['reaction' => ContentReaction::REACTION_INTEREST]);
1531
        $feed_save_reaction_interest = $this->url()->fromRoute('feed/save-reaction', $params_reaction);
5956 anderson 1532
 
5765 efrain 1533
        $params_reaction = array_merge($params, ['reaction' => ContentReaction::REACTION_FUN]);
1534
        $feed_save_reaction_fun = $this->url()->fromRoute('feed/save-reaction', $params_reaction);
5956 anderson 1535
 
1536
 
1537
 
1 www 1538
        $item = [
1539
            'feed_unique' => uniqid(),
1540
            'feed_uuid' => $feed->uuid,
5765 efrain 1541
            'feed_my_reaction' => $reaction ? $reaction->reaction : '',
1542
            'feed_save_reaction_recommended_url' => $feed_save_reaction_recommended,
1543
            'feed_save_reaction_support_url' => $feed_save_reaction_support,
1544
            'feed_save_reaction_love_url' => $feed_save_reaction_love,
1545
            'feed_save_reaction_interest_url' => $feed_save_reaction_interest,
1546
            'feed_save_reaction_fun_url' => $feed_save_reaction_fun,
1547
            'feed_delete_reaction_url' =>  $this->url()->fromRoute('feed/delete-reaction',  $params),
1 www 1548
            'feed_share_url' => $this->url()->fromRoute('feed/share',  $params),
3364 efrain 1549
            'feed_share_external_url' => $share_external_url,
4778 efrain 1550
            'feed_increment_external_counter_url' => $share_increment_external_counter_url,
1 www 1551
            'feed_delete_url' =>  '',
1552
            'feed_content_type' => $feed->file_type ? $feed->file_type : '',
5765 efrain 1553
            'feed_reactions' =>  $reactions,
3146 efrain 1554
            'feed_highlighted' => $feed->id == $feedHighlighted ? 1 : 0,
5780 efrain 1555
            //'is_liked' => $contentReaction ? 1 : 0,
5765 efrain 1556
            //'like_url' => $this->url()->fromRoute('post/like', ['id' => $post->uuid]),
1557
            //'unlike_url' => $this->url()->fromRoute('post/unlike', ['id' => $post->uuid]),
5500 anderson 1558
 
1 www 1559
        ];
5500 anderson 1560
 
1561
        if ($feed->posted_or_shared == Feed::POSTED) {
1562
 
1563
 
1564
            if ($feed->type == Feed::TYPE_COMPANY) {
1 www 1565
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1566
                $company = $companyMapper->fetchOne($feed->company_id);
1567
                $item['owner_url'] = $this->url()->fromRoute('company/view', ['id' => $company->uuid]);
1568
                $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $company->uuid, 'type' => 'company', 'filename' => $company->image]);
1569
                $item['owner_name'] = $company->name;
1570
                /*
1571
 
1572
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1573
                $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
1574
 
1575
 
1576
                if( $companyUser && $acl->isAllowed( $currentUser->usertype_id, 'my-company/feed' ) ) {
1577
                    $item['feed_delete_url'] = $this->url()->fromRoute('feed/delete',  $params);
1578
                }
1579
                */
5500 anderson 1580
            } else if ($feed->type == Feed::TYPE_GROUP) {
1 www 1581
                $groupMapper = GroupMapper::getInstance($this->adapter);
1582
                $group = $groupMapper->fetchOne($feed->group_id);
1583
                $item['owner_url'] = '';
1584
                $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $group->uuid, 'type' => 'group', 'filename' => $group->image]);
1585
                $item['owner_name'] = $group->name;
5500 anderson 1586
 
1587
                if ($group->user_id == $currentUser->id) {
1 www 1588
                    $item['feed_delete_url'] = $this->url()->fromRoute('feed/delete',  $params);
1589
                }
1590
            } else {
1591
                $userMapper = UserMapper::getInstance($this->adapter);
1592
                $user = $userMapper->fetchOne($feed->user_id);
5500 anderson 1593
 
1 www 1594
                $item['owner_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
1595
                $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $user->uuid, 'type' => 'user', 'filename' => $user->image]);
1596
                $item['owner_name'] = $user->first_name . ' ' . $user->last_name;
5500 anderson 1597
 
1598
                if ($feed->user_id == $currentUser->id) {
1 www 1599
                    $item['feed_delete_url'] = $this->url()->fromRoute('feed/delete',  $params);
1600
                }
5500 anderson 1601
            }
1 www 1602
        } else {
5500 anderson 1603
            if ($feed->user_id == $currentUser->id) {
1 www 1604
                $item['feed_delete_url'] = $this->url()->fromRoute('feed/delete',  $params);
1605
            }
1606
        }
5500 anderson 1607
 
1 www 1608
        $userMapper = UserMapper::getInstance($this->adapter);
1609
        $user = $userMapper->fetchOne($feed->user_id);
5500 anderson 1610
 
1611
        if ($feed->posted_or_shared == Feed::SHARED) {
1 www 1612
            $item['owner_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
1613
            $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $user->uuid, 'type' => 'user', 'filename' => $user->image]);
1614
            $item['owner_name'] = $user->first_name . ' ' . $user->last_name;
1615
        }
5500 anderson 1616
 
1 www 1617
        $item['owner_description'] = $feed->description;
5696 anderson 1618
        $item['owner_feed_type'] = $feed->type;
5500 anderson 1619
 
1 www 1620
        $item['owner_shared'] = $feed->total_shared;
4778 efrain 1621
        $item['owner_external_shared'] = $feed->total_external_shared;
1 www 1622
        $item['owner_comments'] = $feed->total_comments;
5500 anderson 1623
 
1624
 
3138 efrain 1625
        $item['owner_time_elapse'] = Functions::timeAgo($feed->added_on, $now);
5500 anderson 1626
 
1627
        if ($feed->file_type == Feed::FILE_TYPE_IMAGE) {
1 www 1628
            $item['owner_file_image'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1629
        }
5500 anderson 1630
        if ($feed->file_type == Feed::FILE_TYPE_DOCUMENT) {
1 www 1631
            $item['owner_file_document'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1632
        }
5500 anderson 1633
        if ($feed->file_type == Feed::FILE_TYPE_VIDEO) {
1 www 1634
            $item['owner_file_image_preview'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_image_preview]);
1635
            $item['owner_file_video'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1636
        }
5500 anderson 1637
 
1638
        if ($feed->posted_or_shared == Feed::SHARED) {
1639
 
1640
 
3143 efrain 1641
            $sharedFeed = $feedMapper->fetchOneAnyStatus($feed->shared_feed_id);
5500 anderson 1642
 
1643
 
1644
 
1645
 
1 www 1646
            $item['shared_description'] = $sharedFeed->description;
5500 anderson 1647
            $item['shared_time_elapse'] = Functions::timeAgo($sharedFeed->added_on, $now);
1648
 
1649
            if ($sharedFeed->file_type == Feed::FILE_TYPE_IMAGE) {
1 www 1650
                $item['shared_file_image'] = $this->url()->fromRoute('storage', ['code' => $sharedFeed->uuid, 'type' => 'feed', 'filename' => $sharedFeed->file_name]);
1651
            }
5500 anderson 1652
            if ($sharedFeed->file_type == Feed::FILE_TYPE_DOCUMENT) {
1 www 1653
                $item['shared_file_document'] = $this->url()->fromRoute('storage', ['code' => $sharedFeed->uuid, 'type' => 'feed', 'filename' => $sharedFeed->file_name]);
1654
            }
5500 anderson 1655
            if ($sharedFeed->file_type == Feed::FILE_TYPE_VIDEO) {
1 www 1656
                $item['shared_file_image_preview'] = $this->url()->fromRoute('storage', ['code' => $sharedFeed->uuid, 'type' => 'feed', 'filename' => $sharedFeed->file_image_preview]);
1657
                $item['shared_file_video'] = $this->url()->fromRoute('storage', ['code' => $sharedFeed->uuid, 'type' => 'feed', 'filename' => $sharedFeed->file_name]);
1658
            }
5500 anderson 1659
 
1 www 1660
            $item['shared_content_type'] = $sharedFeed->file_type ? $sharedFeed->file_type : '';
5500 anderson 1661
 
1662
            if ($feed->company_id) {
1663
 
1 www 1664
                $companyMapper = CompanyMapper::getInstance($this->adapter);
1665
                $sharedCompany = $companyMapper->fetchOne($feed->company_id);
1666
                $item['shared_url'] = $this->url()->fromRoute('company/view', ['id' => $sharedCompany->uuid]);
1667
                $item['shared_image'] = $this->url()->fromRoute('storage', ['code' => $sharedCompany->uuid, 'type' => 'company', 'filename' => $sharedCompany->image]);
1668
                $item['shared_name'] = $sharedCompany->name;
5500 anderson 1669
            } else if ($feed->group_id) {
1 www 1670
                $groupMapper = GroupMapper::getInstance($this->adapter);
1671
                $sharedGroup = $groupMapper->fetchOne($feed->group_id);
5500 anderson 1672
                $item['shared_url'] = '';
1 www 1673
                $item['shared_image'] = $this->url()->fromRoute('storage', ['code' => $sharedGroup->uuid, 'type' => 'group', 'filename' =>  $sharedGroup->image]);
1674
                $item['shared_name'] = $sharedGroup->name;
1675
            } else {
5500 anderson 1676
 
1 www 1677
                $sharedUser = $userMapper->fetchOne($sharedFeed->user_id);
1678
                $item['shared_url'] = $this->url()->fromRoute('profile/view', ['id' => $sharedUser->uuid]);
1679
                $item['shared_image'] = $this->url()->fromRoute('storage', ['code' => $sharedUser->uuid, 'type' => 'user', 'filename' => $sharedUser->image]);
1680
                $item['shared_name'] = $sharedUser->first_name . ' ' . $sharedUser->last_name;
1681
            }
1682
        }
5500 anderson 1683
 
6161 anderson 1684
        $companyMapper = CompanyMapper::getInstance($this->adapter);
6196 anderson 1685
 
6161 anderson 1686
        $companies = [];
1 www 1687
        $commentMapper = CommentMapper::getInstance($this->adapter);
1688
        $records = $commentMapper->fetchAllPublishedByFeedId($feed->id);
5500 anderson 1689
 
6225 anderson 1690
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
6234 anderson 1691
        $owner = $companyUserMapper->fetchOwnerByCompanyId($feed->company_id);
6225 anderson 1692
 
1 www 1693
        $comments = [];
5500 anderson 1694
        foreach ($records as $record) {
1 www 1695
            $user = $userMapper->fetchOne($record->user_id);
6230 anderson 1696
            $company = $companyMapper->fetchOne($feed->company_id);
6225 anderson 1697
 
6228 anderson 1698
            if ($record->user_id == $currentUser->id) {
1699
                $link_delete = $this->url()->fromRoute('feed/comment/delete', ['id' => $feed->uuid, 'comment' => $record->uuid]);
6238 anderson 1700
                array_push($comments, [
1701
                    'unique' => uniqid(),
1702
                    'user_url' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
1703
                    'user_name' => $user->first_name . ' ' . $user->last_name,
1704
                    'company' => $owner,
1705
                    'user_image' => $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image]),
1706
                    'time_elapsed' => Functions::timeAgo($record->added_on, $now),
1707
                    'comment' => $record->comment,
1708
                    'link_delete' => $link_delete
1709
                ]);
6224 anderson 1710
            } else {
6238 anderson 1711
                array_push($comments, [
1712
                    'unique' => uniqid(),
1713
                    'user_url' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
1714
                    'user_name' => $user->first_name . ' ' . $user->last_name,
1715
                    'user_image' => $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image]),
1716
                    'time_elapsed' => Functions::timeAgo($record->added_on, $now),
1717
                    'comment' => $record->comment,
6239 anderson 1718
                    //'link_delete' => $link_delete
6238 anderson 1719
                ]);
6228 anderson 1720
                $link_delete = '';
1721
            }
1 www 1722
        }
1723
        $item['comment_add_url'] = $this->url()->fromRoute('feed/comment', ['id' => $feed->uuid]);
1724
        $item['comments'] = $comments;
5500 anderson 1725
 
1 www 1726
        return $item;
1727
    }
5500 anderson 1728
 
1 www 1729
    /**
1730
     *
1731
     * @param int $comment_id
1732
     * @return array
1733
     */
3138 efrain 1734
    private function renderComment($comment_id, $now)
1 www 1735
    {
1736
        $item = [];
5500 anderson 1737
 
1 www 1738
        $commentMapper = CommentMapper::getInstance($this->adapter);
1739
        $record = $commentMapper->fetchOne($comment_id);
5500 anderson 1740
 
1 www 1741
        $feedMapper = FeedMapper::getInstance($this->adapter);
1742
        $feed = $feedMapper->fetchOne($record->feed_id);
5500 anderson 1743
 
1744
        if ($record) {
1 www 1745
            $userMapper = UserMapper::getInstance($this->adapter);
5500 anderson 1746
 
1 www 1747
            $user = $userMapper->fetchOne($record->user_id);
5500 anderson 1748
 
1 www 1749
            $item['unique'] = uniqid();
6176 anderson 1750
            //$item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image]);
1 www 1751
            $item['user_url'] = $this->url()->fromRoute('profile', ['id' => $user->uuid]);
1752
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
3138 efrain 1753
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
1 www 1754
            $item['comment'] = $record->comment;
5500 anderson 1755
            $item['link_delete'] = $this->url()->fromRoute('feed/comment/delete', ['id' => $feed->uuid, 'comment' => $record->uuid]);
1 www 1756
        }
1757
        return $item;
1758
    }
5500 anderson 1759
 
1 www 1760
    /**
1761
     *
1762
     * @param string $path
1763
     * @return boolean
1764
     */
1765
    private function deletePath($path)
1766
    {
1767
        try {
5500 anderson 1768
            if (is_dir($path)) {
1 www 1769
                if ($dh = opendir($path)) {
5500 anderson 1770
                    while (($file = readdir($dh)) !== false) {
1771
                        if ($file == '.' || $file == '..') {
1 www 1772
                            continue;
1773
                        }
1774
                        unlink($path . DIRECTORY_SEPARATOR . $file);
1775
                    }
1776
                    closedir($dh);
1777
                }
5500 anderson 1778
 
1 www 1779
                rmdir($path);
1780
            }
1781
            return true;
5500 anderson 1782
        } catch (\Throwable $e) {
1 www 1783
            error_log($e->getTraceAsString());
1784
            return false;
1785
        }
1786
    }
5500 anderson 1787
 
1788
 
1 www 1789
    /**
1790
     *
1791
     * @param Feed $feed
1792
     * @return array
1793
     */
5500 anderson 1794
    private function deleteFeed($feed)
1 www 1795
    {
1796
        $feedMapper = FeedMapper::getInstance($this->adapter);
1797
        $feed->status = Feed::STATUS_DELETED;
5500 anderson 1798
        if ($feedMapper->update($feed)) {
1 www 1799
            $response = [
1800
                'success' => true,
1801
                'data' => 'LABEL_FEED_WAS_DELETED'
1802
            ];
1803
        } else {
1804
            $response = [
1805
                'success' => false,
1806
                'data' => $feedMapper->getError()
1807
            ];
1808
        }
5500 anderson 1809
 
1810
 
1 www 1811
        /*$commentMapper = CommentMapper::getInstance($this->adapter);
1812
        if($commentMapper->deleteAllByFeedId($feed->id)) {
1813
            if($feedMapper->delete($feed->id)) {
1814
                $target_path = $this->config['leaderslinked.fullpath.feed'] . DIRECTORY_SEPARATOR . $feed->id;
1815
                if(file_exists($target_path)) {
1816
                    $this->deletePath($target_path);
1817
                }
1818
 
1819
                $response = [
1820
                    'success' => true,
1821
                    'data' => 'LABEL_FEED_WAS_DELETED'
1822
                ];
1823
 
1824
            } else {
1825
                $response = [
1826
                    'success' => false,
1827
                    'data' => $feedMapper->getError()
1828
                ];
1829
            }
1830
        } else {
1831
            $response = [
1832
                'success' => false,
1833
                'data' => $commentMapper->getError()
1834
            ];
1835
 
1836
        }*/
5500 anderson 1837
 
1 www 1838
        return $response;
1839
    }
1840
}