Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 10972 | Rev 10985 | 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
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
 
18
use LeadersLinked\Mapper\CompanyMapper;
19
use LeadersLinked\Mapper\CompanyUserMapper;
20
use LeadersLinked\Form\CreateFeedForm;
21
use LeadersLinked\Form\CommentForm;
8402 nelberth 22
use LeadersLinked\Form\CommentAnswerForm;
1 www 23
use LeadersLinked\Model\Comment;
24
use LeadersLinked\Mapper\CommentMapper;
25
use LeadersLinked\Mapper\FeedMapper;
26
use LeadersLinked\Mapper\GroupMapper;
27
use LeadersLinked\Model\Feed;
28
use LeadersLinked\Mapper\QueryMapper;
29
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
30
use LeadersLinked\Library\Image;
31
use LeadersLinked\Model\VideoConvert;
32
use LeadersLinked\Mapper\VideoConvertMapper;
33
use LeadersLinked\Mapper\LikeMapper;
34
use Laminas\Paginator\Adapter\DbSelect;
35
use Laminas\Paginator\Paginator;
36
use LeadersLinked\Mapper\UserMapper;
37
use LeadersLinked\Library\Functions;
38
use LeadersLinked\Model\Company;
39
use LeadersLinked\Model\UserType;
40
 
9537 nelberth 41
use LeadersLinked\Mapper\TopicMapper;
7381 nelberth 42
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
43
use LeadersLinked\Model\HighPerformanceTeamsGroups;
44
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
45
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
46
 
1 www 47
class FeedController extends AbstractActionController
48
{
49
    /**
50
     *
51
     * @var AdapterInterface
52
     */
53
    private $adapter;
54
 
55
 
56
    /**
57
     *
58
     * @var AbstractAdapter
59
     */
60
    private $cache;
61
 
62
    /**
63
     *
64
     * @var  LoggerInterface
65
     */
66
    private $logger;
67
 
68
 
69
    /**
70
     *
71
     * @var array
72
     */
73
    private $config;
74
 
75
    /**
76
     *
77
     * @param AdapterInterface $adapter
78
     * @param AbstractAdapter $cache
79
     * @param LoggerInterface $logger
80
     * @param array $config
81
     */
82
    public function __construct($adapter, $cache , $logger,  $config)
83
    {
84
        $this->adapter      = $adapter;
85
        $this->cache        = $cache;
86
        $this->logger       = $logger;
87
        $this->config       = $config;
88
 
89
    }
90
 
91
    /**
92
     *
93
     * Generación del listado de perfiles
94
     * {@inheritDoc}
95
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
96
     */
97
    public function indexAction()
98
    {
99
        $currentUserPlugin = $this->plugin('currentUserPlugin');
100
        $currentUser = $currentUserPlugin->getUser();
101
        $currentCompany = $currentUserPlugin->getCompany();
102
 
103
 
104
 
105
        $request = $this->getRequest();
106
        if($request->isGet()) {
107
            $headers  = $request->getHeaders();
108
 
109
            $isJson = false;
110
            if($headers->has('Accept')) {
111
                $accept = $headers->get('Accept');
112
 
113
                $prioritized = $accept->getPrioritized();
114
 
115
                foreach($prioritized as $key => $value) {
116
                    $raw = trim($value->getRaw());
117
 
118
                    if(!$isJson) {
119
                        $isJson = strpos($raw, 'json');
120
                    }
121
 
122
                }
123
            }
124
 
7645 nelberth 125
            $formFeed = new CreateFeedForm($this->adapter);
1 www 126
 
127
            $this->layout()->setTemplate('layout/layout-backend');
128
            $viewModel = new ViewModel();
129
            $viewModel->setTemplate('leaders-linked/feeds/index.phtml');
130
            $viewModel->setVariables([
131
                'formFeed'      => $formFeed,
132
            ]);
133
            return $viewModel ;
134
 
135
 
136
        } else {
137
            return new JsonModel([
138
                'success' => false,
139
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
140
            ]);
141
        }
142
    }
143
 
144
 
145
    public function commentAction()
146
    {
8396 nelberth 147
 
8394 nelberth 148
 
1 www 149
        $currentUserPlugin = $this->plugin('currentUserPlugin');
150
        $currentUser = $currentUserPlugin->getUser();
151
        $currentCompany = $currentUserPlugin->getCompany();
152
 
153
 
154
        $id = $this->params()->fromRoute('id');
155
 
156
        $request = $this->getRequest();
157
        if($request->isPost()) {
158
            $feedMapper = FeedMapper::getInstance($this->adapter);
159
            $feed = $feedMapper->fetchOneByUuid($id);
160
            if(!$feed) {
161
                $response = [
162
                    'success' => false,
163
                    'data' => 'ERROR_POST_NOT_FOUND'
164
                ];
165
                return new JsonModel($response);
166
            }
8398 nelberth 167
 
1 www 168
            if($feed->company_id != $currentCompany->id) {
169
                $response = [
170
                    'success' => false,
171
                    'data' => 'ERROR_UNAUTHORIZED'
172
                ];
173
                return new JsonModel($response);
174
            }
175
 
176
            $dataPost = $request->getPost()->toArray();
177
            $form = new CommentForm();
178
            $form->setData($dataPost);
8399 nelberth 179
 
1 www 180
            if($form->isValid()) {
8400 nelberth 181
 
1 www 182
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
183
                $owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
184
 
185
                $dataPost = (array) $form->getData();
186
                $comment = new Comment();
187
                $comment->comment = $dataPost['comment'];
188
                $comment->feed_id = $feed->id;
189
 
8232 nelberth 190
                if($feed->type=='hptg'){
191
                    $comment->user_id = $currentUser->id;
192
 
193
                }else{
194
                    $comment->user_id = $owner->user_id;
195
 
196
                }
197
 
1 www 198
                $commentMapper = CommentMapper::getInstance($this->adapter);
199
                if($commentMapper->insert($comment)) {
200
 
201
                    $total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
202
 
203
                    $feed->total_comments = $total_comments;
204
                    $feedMapper->update($feed);
205
 
206
                    $response = [
207
                        'success'   => true,
208
                        'data'   => $this->renderComment($comment->id),
209
                        'total_comments' => $total_comments
210
                    ];
211
 
212
                    return new JsonModel($response);
213
 
214
                } else {
215
 
216
                    $response = [
217
                        'success'   => false,
218
                        'data'   => $commentMapper->getError()
219
                    ];
220
 
221
                    return new JsonModel($response);
222
                }
223
 
224
            } else {
225
                $message = '';;
226
                $form_messages = (array) $form->getMessages();
227
                foreach($form_messages  as $fieldname => $field_messages)
228
                {
229
                    foreach( $field_messages as $key => $value)
230
                    {
231
                        $message = $value;
232
                    }
233
                }
234
 
235
                $response = [
236
                    'success'   => false,
237
                    'data'   => $message
238
                ];
239
 
240
                return new JsonModel($response);
241
            }
242
 
243
 
244
        } else {
245
            $response = [
246
                'success' => false,
247
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
248
            ];
249
 
250
            return new JsonModel($response);
251
        }
252
 
253
 
254
    }
8390 nelberth 255
 
8402 nelberth 256
    public function answerAction()
257
    {
1 www 258
 
8404 nelberth 259
 
8402 nelberth 260
        $currentUserPlugin = $this->plugin('currentUserPlugin');
261
        $currentUser = $currentUserPlugin->getUser();
262
        $currentCompany = $currentUserPlugin->getCompany();
263
 
264
 
265
        $id = $this->params()->fromRoute('id');
266
        $comment_uuid = $this->params()->fromRoute('comment');
8405 nelberth 267
 
8402 nelberth 268
        $request = $this->getRequest();
269
        if($request->isPost()) {
270
            $feedMapper = FeedMapper::getInstance($this->adapter);
271
            $feed = $feedMapper->fetchOneByUuid($id);
272
            if(!$feed) {
273
                $response = [
274
                    'success' => false,
275
                    'data' => 'ERROR_POST_NOT_FOUND'
276
                ];
277
                return new JsonModel($response);
278
            }
279
 
280
            if($feed->company_id != $currentCompany->id) {
281
                $response = [
282
                    'success' => false,
283
                    'data' => 'ERROR_UNAUTHORIZED'
284
                ];
285
                return new JsonModel($response);
286
            }
8406 nelberth 287
 
8402 nelberth 288
            $dataPost = $request->getPost()->toArray();
289
            $form = new CommentAnswerForm();
290
            $form->setData($dataPost);
291
 
292
            if($form->isValid()) {
8407 nelberth 293
 
8402 nelberth 294
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
295
                $owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
296
 
297
                $dataPost = (array) $form->getData();
8408 nelberth 298
                $answer = new Comment();
299
                $answer->comment = $dataPost['answer'];
300
                $answer->feed_id = $feed->id;
8402 nelberth 301
 
302
                if($feed->type=='hptg'){
8408 nelberth 303
                    $answer->user_id = $currentUser->id;
8402 nelberth 304
 
305
                }else{
8408 nelberth 306
                    $answer->user_id = $owner->user_id;
8402 nelberth 307
 
308
                }
8408 nelberth 309
 
310
                $commentMapper = CommentMapper::getInstance($this->adapter);
311
                $comment=$commentMapper->fetchOneByUuid($comment_uuid);
312
                $answer->parent_id = $comment->id;
8402 nelberth 313
 
8408 nelberth 314
                if($commentMapper->insert($answer)) {
8402 nelberth 315
 
316
                    $total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
317
 
318
                    $feed->total_comments = $total_comments;
319
                    $feedMapper->update($feed);
320
 
321
                    $response = [
322
                        'success'   => true,
8408 nelberth 323
                        'data'   => $this->renderComment($answer->id),
8402 nelberth 324
                        'total_comments' => $total_comments
325
                    ];
326
 
327
                    return new JsonModel($response);
328
 
329
                } else {
330
 
331
                    $response = [
332
                        'success'   => false,
333
                        'data'   => $commentMapper->getError()
334
                    ];
335
 
336
                    return new JsonModel($response);
337
                }
338
 
339
            } else {
340
                $message = '';;
341
                $form_messages = (array) $form->getMessages();
342
                foreach($form_messages  as $fieldname => $field_messages)
343
                {
344
                    foreach( $field_messages as $key => $value)
345
                    {
346
                        $message = $value;
347
                    }
348
                }
349
 
350
                $response = [
351
                    'success'   => false,
352
                    'data'   => $message
353
                ];
354
 
355
                return new JsonModel($response);
356
            }
357
 
358
 
359
        } else {
360
            $response = [
361
                'success' => false,
362
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
363
            ];
364
 
365
            return new JsonModel($response);
366
        }
367
 
368
 
369
    }
370
 
1 www 371
 
8402 nelberth 372
 
1 www 373
    public function commentDeleteAction()
374
    {
375
        $currentUserPlugin = $this->plugin('currentUserPlugin');
376
        $currentUser = $currentUserPlugin->getUser();
377
        $currentCompany = $currentUserPlugin->getCompany();
378
 
379
        $request = $this->getRequest();
380
        if($request->isPost()) {
381
            $currentUserPlugin = $this->plugin('currentUserPlugin');
382
            $currentUser = $currentUserPlugin->getUser();
383
 
384
            $id = $this->params()->fromRoute('id');
385
            $comment = $this->params()->fromRoute('comment');
386
 
387
            $feedMapper = FeedMapper::getInstance($this->adapter);
388
            $feed = $feedMapper->fetchOneByUuid($id);
389
            if(!$feed) {
390
                $response = [
391
                    'success' => false,
392
                    'data' => 'ERROR_POST_NOT_FOUND'
393
                ];
394
                return new JsonModel($response);
395
            }
396
 
397
            if($feed->company_id != $currentCompany->id) {
398
                $response = [
399
                    'success' => false,
400
                    'data' => 'ERROR_UNAUTHORIZED'
401
                ];
402
                return new JsonModel($response);
403
            }
404
 
405
            $commentMapper = CommentMapper::getInstance($this->adapter);
406
            $comment = $commentMapper->fetchOneByUuid($comment);
407
 
408
            if($comment && $comment->feed_id == $feed->id) {
409
                $comment->status = Comment::STATUS_DELETED;
410
                if($commentMapper->update($comment)) {
411
                    $total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
412
 
413
                    $feed = $feedMapper->fetchOne($comment->feed_id);
414
                    $feed->total_comments = $total_comments;
415
                    $feedMapper->update($feed);
416
 
417
 
418
 
419
 
420
 
421
                    $response = [
422
                        'success' => true,
423
                        'data' => [
424
                            'message' => 'LABEL_COMMENT_WAS_DELETED',
425
                            'total_comments' => $total_comments
426
                        ]
427
                   ];
428
                } else {
429
                    $response = [
430
                        'success' => false,
431
                        'data' => $commentMapper->getError()
432
                    ];
433
                }
434
 
435
            } else {
436
                $response = [
437
                    'success' => false,
438
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
439
                ];
440
            }
441
 
442
        } else {
443
            $response = [
444
                'success' => false,
445
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
446
            ];
447
        }
448
 
449
        return new JsonModel($response);
450
    }
451
 
452
    public function deleteAction()
453
    {
454
        $currentUserPlugin = $this->plugin('currentUserPlugin');
455
        $currentUser = $currentUserPlugin->getUser();
456
        $currentCompany = $currentUserPlugin->getCompany();
457
 
458
        $request = $this->getRequest();
459
        if($request->isPost()) {
460
 
461
 
462
            $id =  $this->params()->fromRoute('id');
463
 
464
            $feedMapper = FeedMapper::getInstance($this->adapter);
465
            $feed = $feedMapper->fetchOneByUuid($id);
466
            if(!$feed) {
467
                $response = [
468
                    'success' => false,
469
                    'data' => 'ERROR_POST_NOT_FOUND'
470
                ];
471
                return new JsonModel($response);
472
            }
473
 
474
            if($feed->company_id != $currentCompany->id) {
475
                $response = [
476
                    'success' => false,
477
                    'data' => 'ERROR_UNAUTHORIZED'
478
                ];
479
                return new JsonModel($response);
480
            }
481
 
482
            $feedMapper = FeedMapper::getInstance($this->adapter);
483
            $feed->status = Feed::STATUS_DELETED;
484
            if($feedMapper->update($feed)) {
485
                $response = [
486
                    'success' => true,
487
                    'data' => 'LABEL_FEED_WAS_DELETED'
488
                ];
489
 
490
            } else {
491
                $response = [
492
                    'success' => false,
493
                    'data' => $feedMapper->getError()
494
                ];
495
            }
496
 
497
 
498
            return new JsonModel($response);
499
 
500
        } else {
501
            $response = [
502
                'success' => false,
503
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
504
            ];
505
        }
506
 
507
        return new JsonModel($response);
508
    }
509
 
510
    public function addAction()
511
    {
512
        $currentUserPlugin = $this->plugin('currentUserPlugin');
513
        $currentUser = $currentUserPlugin->getUser();
514
        $currentCompany = $currentUserPlugin->getCompany();
10970 eleazar 515
        $myt_id =  $this->params()->fromRoute('myt_id');
7403 nelberth 516
 
517
        $request = $this->getRequest();
518
        if($request->isPost()) {
7653 nelberth 519
 
520
 
521
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
522
 
523
            $form = new CreateFeedForm($this->adapter);
7654 nelberth 524
 
1 www 525
            $form->setData($dataPost);
7673 nelberth 526
 
1 www 527
            if($form->isValid()) {
528
 
7680 nelberth 529
 
1 www 530
                $companyMapper = CompanyMapper::getInstance($this->adapter);
531
                $company = $companyMapper->fetchOne($currentCompany->id);
532
 
533
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
534
                $owner = $companyUserMapper->fetchOwnerByCompanyId($company->id);
535
 
9049 nelberth 536
                $dataPost['priority'] =  isset($dataPost['priority']) ? Feed::PRIORITY_URGENT  : '';
9050 nelberth 537
 
7408 nelberth 538
 
1 www 539
                $hydrator = new ObjectPropertyHydrator();
540
                $feed = new Feed();
541
                $hydrator->hydrate($dataPost, $feed);
10970 eleazar 542
                /*if($feed->type=='hptg'){
7407 nelberth 543
 
10418 nelberth 544
                    $group_id =  $this->params()->fromRoute('group_id');
8612 nelberth 545
 
7409 nelberth 546
                    $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
10418 nelberth 547
                    $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_id);
7414 nelberth 548
 
7409 nelberth 549
                    if (!$highPerformanceTeamsGroups) {
550
                        $data = [
551
                            'success' => false,
7414 nelberth 552
                            'data' => 'ERROR_RECORD_NOT_FOUND'
7409 nelberth 553
                        ];
7407 nelberth 554
 
7409 nelberth 555
                        return new JsonModel($data);
556
                    }
557
 
558
                    if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
559
 
560
                        return new JsonModel([
561
                            'success' => false,
7414 nelberth 562
                            'data' => 'ERROR_UNAUTHORIZED'
7409 nelberth 563
                        ]);
564
 
565
                    }
566
 
567
                    $highPerformanceTeamsGroupsMemberMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
568
                    $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMemberMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
569
                    if($highPerformanceTeamsGroupsMember) {
7413 nelberth 570
                        if($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN ){
571
                            $id =  $highPerformanceTeamsGroups->id;
572
                        }else{
573
 
574
                            return new JsonModel([
575
                                'success' => false,
576
                                'data' => 'ERROR_UNAUTHORIZED'
577
                            ]);
578
                        }
7409 nelberth 579
 
580
                    } else {
581
 
582
                        return new JsonModel([
583
                            'success' => false,
7414 nelberth 584
                            'data' => 'ERROR_RECORD_NOT_FOUND'
7409 nelberth 585
                        ]);
586
                    }
9541 nelberth 587
 
9569 nelberth 588
                    if(isset($dataPost['topic_id'])){
9567 nelberth 589
 
9539 nelberth 590
                        $topicMapper = TopicMapper::getInstance($this->adapter);
9569 nelberth 591
                        $topic = $topicMapper->fetchOneByUuid($feed->topic_id);
9537 nelberth 592
                        if($topic){
9569 nelberth 593
                            $feed->topic_id = $topic->id;
7644 nelberth 594
                        }else{
595
                            return new JsonModel([
596
                                'success' => false,
597
                                'data' => 'ERROR_RECORD_NOT_FOUND'
598
                            ]);
599
                        }
9572 nelberth 600
 
7644 nelberth 601
                    }
9544 nelberth 602
 
7409 nelberth 603
 
604
                    $feed->company_id       = $currentCompany->id;
605
                    $feed->group_id         = null;
606
                    $feed->high_performance_group_id = $id;
8222 nelberth 607
                    $feed->user_id          = $currentUser->id;
7409 nelberth 608
                    $feed->type             = Feed::TYPE_HPTG;
609
                    $feed->posted_or_shared = Feed::POSTED;
610
                    $feed->shared_with      = Feed::SHARE_WITH_CONNECTIONS;
611
 
10970 eleazar 612
                } else*/
10972 eleazar 613
                if($myt_id){
10601 eleazar 614
                    $feed->company_id       = $currentCompany->id;
10981 eleazar 615
                    $feed->group_id         = null;
10601 eleazar 616
                    $feed->user_id          = $owner->user_id;
10970 eleazar 617
                    $feed->myt_id           = $myt_id;
618
                    $feed->related_id       = $myt_id;
10601 eleazar 619
                    $feed->type             = Feed::TYPE_MYT;
620
                    $feed->posted_or_shared = Feed::POSTED;
10680 eleazar 621
                    $feed->shared_with      = Feed::SHARE_WITH_PUBLIC;
7407 nelberth 622
                }else{
623
                    $feed->company_id       = $currentCompany->id;
624
                    $feed->group_id         = null;
625
                    $feed->user_id          = $owner->user_id;
626
                    $feed->type             = Feed::TYPE_COMPANY;
627
                    $feed->posted_or_shared = Feed::POSTED;
628
                    $feed->shared_with      = Feed::SHARE_WITH_CONNECTIONS;
629
                }
1 www 630
 
631
 
632
 
9545 nelberth 633
 
1 www 634
                $feed->total_comments   = 0;
635
                $feed->total_shared     = 0;
636
 
637
 
638
                $files = $this->getRequest()->getFiles()->toArray();
639
                $file_type = '';
640
                if(isset($files['file']) && empty($files['file']['error'])) {
641
                    $feed_tmp_filename  = $files['file']['tmp_name'];
642
                    $feed_filename      = $this->normalizeString($files['file']['name']);
643
 
644
                    $mime_type = mime_content_type($feed_tmp_filename);
645
                    if($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
646
                        $file_type = Feed::FILE_TYPE_IMAGE;
647
                    } else if($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
648
                        $file_type = Feed::FILE_TYPE_VIDEO;
649
                    } else if($mime_type == 'application/pdf') {
650
                        $file_type = Feed::FILE_TYPE_DOCUMENT;
651
                    }
652
                }
9549 nelberth 653
 
1 www 654
 
655
                $feedMapper = FeedMapper::getInstance($this->adapter);
656
                $result = $feedMapper->insert($feed);
9565 nelberth 657
 
9551 nelberth 658
 
1 www 659
                if($result) {
9552 nelberth 660
 
661
                    $feed = $feedMapper->fetchOne($feed->id);
9553 nelberth 662
 
1 www 663
                    if( $file_type == Feed::FILE_TYPE_DOCUMENT) {
664
                        try {
665
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
666
                            if(!file_exists($target_path)) {
667
                                mkdir($target_path, 0755);
668
                            }
669
 
670
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR .$feed_filename;
671
                            $result = move_uploaded_file($feed_tmp_filename , $full_filename);
672
                            if($result) {
673
 
674
                                $feed->file_type = $file_type;
675
                                $feed->file_name = basename($feed_filename);
676
                                $feedMapper->update($feed);
677
                            } else {
678
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename );
679
                            }
680
                        } catch(\Throwable $e) {
681
                            error_log($e->getTraceAsString());
682
                        }
683
                    }
684
                    if( $file_type == Feed::FILE_TYPE_IMAGE) {
685
                        try {
686
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
687
                            if(!file_exists($target_path)) {
688
                                mkdir($target_path, 0755);
689
                            }
690
 
691
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
692
 
693
                            $feed_filename = substr($feed_filename, 0, strrpos($feed_filename, '.'))  . '.png';
694
                            $crop_to_dimensions = false;
695
                            if(Image::uploadImage($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height, $crop_to_dimensions )) {
696
                                $feed->file_type = $file_type;
697
                                $feed->file_name = basename($feed_filename);
698
                                $feedMapper->update($feed);
699
                            }
700
                        } catch(\Throwable $e) {
701
                            error_log($e->getTraceAsString());
702
                        }
703
                    }
704
                    if( $file_type == Feed::FILE_TYPE_VIDEO) {
705
                        try {
706
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
707
                            if(!file_exists($target_path)) {
708
                                mkdir($target_path, 0755);
709
                            }
710
 
711
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
712
 
713
                            $result = move_uploaded_file($feed_tmp_filename , $full_filename);
714
                            if($result) {
715
 
716
                                $size = $this->config['leaderslinked.image_sizes.feed_image_size'];
717
                                $getFromSecound = 2;
718
 
719
                                //extracción del cover
720
                                $generateFileName = substr($feed_filename, 0, strrpos($feed_filename, '.'));
721
                                $generateFile =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.png';
722
                                $cmd            = "/usr/bin/ffmpeg -i $full_filename -an -ss $getFromSecound -s $size $generateFile";
723
                                exec($cmd);
724
 
725
 
726
                                $feed->file_type = $file_type;
727
                                $feed->file_name = basename($feed_filename);
728
                                $feed->file_image_preview = basename($generateFile);
729
                                $feedMapper->update($feed);
730
 
731
                                $videoConvert = new VideoConvert();
732
                                $videoConvert->filename = $full_filename;
733
                                $videoConvert->type = VideoConvert::TYPE_FEED;
734
 
735
                                $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
736
                                $videoConvertMapper->insert($videoConvert);
737
                            } else {
738
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename );
739
                            }
740
                        } catch(\Throwable $e) {
741
                            error_log($e->getTraceAsString());
742
                        }
743
                    }
744
 
9564 nelberth 745
 
9557 nelberth 746
                    $response = [
747
                        'success'   => true,
10668 eleazar 748
                        'data'   => $this->renderFeed($feed->id)
9557 nelberth 749
                    ];
9558 nelberth 750
 
1 www 751
                } else {
752
                    $response = [
753
                        'success'   => false,
754
                        'data'   => $feedMapper->getError()
755
                    ];
756
                }
757
            } else {
758
                $messages = [];
759
                $form_messages = (array) $form->getMessages();
760
                foreach($form_messages  as $fieldname => $field_messages)
761
                {
762
                    $messages[$fieldname] = array_values($field_messages);
763
                }
764
 
765
                $response = [
766
                    'success'   => false,
767
                    'data'   => $messages
768
                ];
769
            }
770
 
771
        } else {
772
            $response = [
773
                'success' => false,
774
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
775
            ];
776
        }
777
 
778
        return new JsonModel($response);
779
    }
780
 
781
 
10962 eleazar 782
    public function timelineAction() {
1 www 783
        $currentUserPlugin = $this->plugin('currentUserPlugin');
784
        $currentUser = $currentUserPlugin->getUser();
785
        $currentCompany = $currentUserPlugin->getCompany();
8679 nelberth 786
 
7366 nelberth 787
 
7381 nelberth 788
 
7378 nelberth 789
 
7357 nelberth 790
 
7366 nelberth 791
        $request = $this->getRequest();
1 www 792
        if($request->isGet()) {
8672 nelberth 793
 
8673 nelberth 794
 
8672 nelberth 795
            $page = (int) $this->params()->fromQuery('page');
796
            $type   = $this->params()->fromRoute('type');
797
            $topic_uuid   = $this->params()->fromRoute('topic_uuid');
798
            $urgent   = $this->params()->fromRoute('urgent');
10820 eleazar 799
            $myt_id =  $this->params()->fromRoute('myt_id');
10827 eleazar 800
 
10820 eleazar 801
            /*if(isset($type)&& $type=='hptg'){
7839 nelberth 802
                $id =  $this->params()->fromRoute('id');
7381 nelberth 803
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
7839 nelberth 804
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($id);
7425 nelberth 805
 
7414 nelberth 806
                if (!$highPerformanceTeamsGroups) {
807
                    $data = [
808
                        'success' => false,
809
                        'data' => 'ERROR_RECORD_NOT_FOUND'
810
                    ];
811
 
812
                    return new JsonModel($data);
813
                }
814
 
815
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
816
 
817
                    return new JsonModel([
818
                        'success' => false,
819
                        'data' => 'ERROR_UNAUTHORIZED'
820
                    ]);
821
 
822
                }
823
 
824
                $highPerformanceTeamsGroupsMemberMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
825
                $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMemberMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
826
                if($highPerformanceTeamsGroupsMember) {
827
                    if($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN ){
7701 nelberth 828
                        $queryMapper = QueryMapper::getInstance($this->adapter);
829
                        $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
830
                        $select->columns(['id']);
831
                        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
832
                        $select->where->equalTo('company_id',$currentCompany->id);
833
                        $select->where->equalTo('high_performance_group_id',$highPerformanceTeamsGroups->id);
8739 nelberth 834
 
8737 nelberth 835
 
7643 nelberth 836
                        if(isset($topic_uuid)){
9539 nelberth 837
                            $topicMapper = TopicMapper::getInstance($this->adapter);
838
                            $topic = $topicMapper->fetchOneByUuid($topic_uuid);
9537 nelberth 839
                            if($topic){
840
                                if($topic->status!='a'){
7766 nelberth 841
                                    return new JsonModel([
842
                                        'success' => false,
843
                                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
844
                                    ]);
845
                                }
9569 nelberth 846
                                $select->where->equalTo('topic_id',$topic->id);
7701 nelberth 847
 
7643 nelberth 848
                            }else{
849
                                return new JsonModel([
850
                                    'success' => false,
8689 nelberth 851
                                    'data' => 'ERROR_RECORD_NOT_FOUND'
7643 nelberth 852
                                ]);
853
                            }
7701 nelberth 854
                        }else{
8739 nelberth 855
                            if(isset($urgent)){
856
                                $select->where->equalTo('priority', Feed::PRIORITY_URGENT);
857
                            }else{
9569 nelberth 858
                                $select->where->IsNull('topic_id');
8739 nelberth 859
                            }
7701 nelberth 860
 
7643 nelberth 861
                        }
7701 nelberth 862
 
863
 
7420 nelberth 864
                        $select->where->and->equalTo('type', Feed::TYPE_HPTG);
865
                        $select->order('added_on desc');
866
 
7414 nelberth 867
                    }else{
868
                        return new JsonModel([
869
                            'success' => false,
870
                            'data' => 'ERROR_UNAUTHORIZED'
871
                        ]);
872
                    }
873
 
7381 nelberth 874
                } else {
7414 nelberth 875
 
876
                    return new JsonModel([
877
                        'success' => false,
878
                        'data' => 'ERROR_RECORD_NOT_FOUND'
879
                    ]);
7381 nelberth 880
                }
7379 nelberth 881
 
7381 nelberth 882
 
7414 nelberth 883
 
7420 nelberth 884
 
7381 nelberth 885
 
10820 eleazar 886
            } else*/
887
 
888
            if($myt_id){
10601 eleazar 889
                $queryMapper = QueryMapper::getInstance($this->adapter);
890
                $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
891
                $select->columns(['id']);
892
                $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
893
                $select->where->equalTo('company_id',$currentCompany->id);
10962 eleazar 894
                $select->where->equalTo('title', '');
10601 eleazar 895
                $select->where->and->equalTo('type', Feed::TYPE_MYT);
896
                $select->order('added_on desc');
10557 eleazar 897
 
7350 nelberth 898
            }else{
7380 nelberth 899
 
7350 nelberth 900
                $queryMapper = QueryMapper::getInstance($this->adapter);
901
                $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
902
                $select->columns(['id']);
903
                $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
904
                $select->where->equalTo('company_id',$currentCompany->id);
905
                $select->where->and->equalTo('type', Feed::TYPE_COMPANY);
906
                $select->order('added_on desc');
907
            }
908
 
1 www 909
 
910
 
911
            $dbSelect = new DbSelect($select, $this->adapter);
912
            $paginator = new Paginator($dbSelect);
913
            $paginator->setCurrentPageNumber($page ? $page : 1);
914
            $paginator->setItemCountPerPage(10);
915
 
916
            $items = [];
917
            $feeds = $paginator->getCurrentItems();
10400 nelberth 918
 
1 www 919
            foreach($feeds as $feed)
920
            {
10418 nelberth 921
                $items[] = $this->renderFeed($feed->id);
1 www 922
            }
923
 
924
            $response = [
925
                'success' => true,
926
                'data' => [
927
                    'total' => [
928
                        'count' => $paginator->getTotalItemCount(),
929
                        'pages' => $paginator->getPages()->pageCount,
930
                    ],
931
                    'current' => [
932
                        'items'    => $items,
933
                        'page'     => $paginator->getCurrentPageNumber(),
934
                        'count'    => $paginator->getCurrentItemCount(),
935
                    ]
936
                ]
937
            ];
938
 
939
            return new JsonModel($response);
940
        }
941
 
942
        $response = [
943
            'success' => false,
944
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
945
        ];
946
 
947
 
948
        return new JsonModel($response);
949
    }
950
 
10119 nelberth 951
    public function oneFeedAction(){
10118 nelberth 952
        $currentUserPlugin = $this->plugin('currentUserPlugin');
953
        $currentUser = $currentUserPlugin->getUser();
954
        $currentCompany = $currentUserPlugin->getCompany();
955
 
956
        $request = $this->getRequest();
957
        if($request->isGet()) {
958
 
959
            $feed_uuid =  $this->params()->fromRoute('feed_uuid');
960
 
961
            if(!isset($feed_uuid)){
962
                    $data = [
963
                        'success'   => false,
964
                        'data'   => 'ERROR_INVALID_PARAMETER'
965
                    ];
966
 
967
                    return new JsonModel($data);
968
            }
969
 
970
            $items = [];
971
            $feedMapper = FeedMapper::getInstance($this->adapter);
972
            $feed  = $feedMapper->fetchOneByUuid($feed_uuid);
973
 
974
            if (!$feed) {
975
                $data = [
976
                    'success' => false,
977
                    'data' => 'ERROR_RECORD_NOT_FOUND'
978
                ];
979
 
980
                return new JsonModel($data);
981
            }
982
 
983
            if($feed->type=='hptg'){
984
 
985
                $group_uuid =  $this->params()->fromRoute('group_uuid');
986
                $topic_uuid   = $this->params()->fromRoute('topic_uuid');
987
 
988
                if(!isset($topic_uuid)){
989
 
990
                    $data = [
991
                        'success'   => false,
992
                        'data'   => 'ERROR_INVALID_PARAMETER'
993
                    ];
994
 
995
                    return new JsonModel($data);
996
                }
997
                if(!isset($group_uuid)){
998
 
999
                    $data = [
1000
                        'success'   => false,
1001
                        'data'   => 'ERROR_INVALID_PARAMETER'
1002
                    ];
1003
 
1004
                    return new JsonModel($data);
1005
                }
1006
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
1007
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
1008
 
1009
                if (!$highPerformanceTeamsGroups) {
1010
                    $data = [
1011
                        'success' => false,
1012
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1013
                    ];
1014
 
1015
                    return new JsonModel($data);
1016
                }
1017
 
1018
 
1019
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
1020
 
1021
                    return new JsonModel([
1022
                        'success' => false,
1023
                        'data' => 'ERROR_UNAUTHORIZED'
1024
                    ]);
1025
 
1026
                }
1027
 
1028
                if($feed->high_performance_group_id!=$highPerformanceTeamsGroups->id){
1029
                    return new JsonModel([
1030
                        'success' => false,
1031
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
1032
                    ]);
1033
                }
1034
 
1035
                $topicMapper = TopicMapper::getInstance($this->adapter);
1036
                $topic = $topicMapper->fetchOneByUuid($topic_uuid);
1037
 
1038
                if (!$topic) {
1039
                    $data = [
1040
                        'success' => false,
1041
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1042
                    ];
1043
 
1044
                    return new JsonModel($data);
1045
                }
1046
 
1047
                if($feed->topic_id!=$topic->id){
1048
                    return new JsonModel([
1049
                        'success' => false,
1050
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
1051
                    ]);
1052
                }
10521 eleazar 1053
 
1054
                $items = $this->renderFeed($feed->id,$group_uuid);
10118 nelberth 1055
 
1056
            }
1057
 
10521 eleazar 1058
 
10118 nelberth 1059
 
1060
            $response = [
1061
                'success' => true,
1062
                'data' => [
1063
                    'item' =>$items,
1064
                    'topic_title'=>$topic->title,
1065
                    'feed_title'=>$feed->title
1066
                ]
1067
            ];
1068
 
1069
 
1070
            return new JsonModel($response);
1071
        }
1072
    }
1 www 1073
    /**
1074
     *
1075
     * @param string $str
1076
     * @return string
1077
     */
10118 nelberth 1078
    private function normalizeString ($str = ''){
1 www 1079
        $basename  = substr($str, 0, strrpos($str, '.'));
1080
        $basename  = str_replace('.', '-', $basename);
1081
 
1082
        $extension  = substr($str, strrpos($str, '.'));
1083
 
1084
        $str = $basename . $extension;
1085
 
1086
        $str = strip_tags($str);
1087
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1088
        $str = preg_replace('/[\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1089
        $str = strtolower($str);
1090
        $str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
1091
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1092
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1093
        $str = str_replace(' ', '-', $str);
1094
        $str = rawurlencode($str);
1095
        $str = str_replace('%', '-', $str);
1096
        return trim(strtolower($str));
1097
    }
1098
 
1099
 
1100
 
1101
    /**
1102
     *
1103
     * @param int $feed_id
1104
     * @param Company $company
1105
     * @return array
1106
     */
10415 nelberth 1107
    private function renderFeed($feed_id,$group_uuid="")
1 www 1108
    {
9562 nelberth 1109
 
1 www 1110
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
1111
        if($sandbox) {
1112
            $company_profile_url = $this->config['leaderslinked.frontend.sandbox_company_profile'];
1113
            $user_profile_url = $this->config['leaderslinked.frontend.sandbox_user_profile'];
1114
 
1115
        } else {
1116
            $company_profile_url = $this->config['leaderslinked.frontend.production_company_profile'];
1117
            $user_profile_url = $this->config['leaderslinked.frontend.production_user_profile'];
1118
        }
1119
 
1120
 
1121
 
9561 nelberth 1122
 
1 www 1123
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1124
        $currentUser = $currentUserPlugin->getUser();
1125
        $currentCompany = $currentUserPlugin->getCompany();
1126
 
1127
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1128
        $company = $companyMapper->fetchOne($currentCompany->id);
1129
 
1130
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1131
        $owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
1132
 
1133
 
1134
 
1135
        $acl = $this->getEvent()->getViewModel()->getVariable('acl');
1136
 
1137
        $feedMapper = FeedMapper::getInstance($this->adapter);
1138
        $feed = $feedMapper->fetchOne($feed_id);
1139
 
1140
 
1141
        $params = [
1142
            'id' => $feed->uuid
1143
        ];
1144
 
1145
 
1146
        $item = [
1147
            'feed_unique' => uniqid(),
1148
            'feed_uuid' => $feed->uuid,
8218 nelberth 1149
            'feed_delete_url' =>  $this->url()->fromRoute('feeds/delete',  $params),
1 www 1150
            'feed_content_type' => $feed->file_type ? $feed->file_type : '',
8627 nelberth 1151
            'feed_priority'=> $feed->priority,
1 www 1152
        ];
8205 nelberth 1153
        if($feed->type == Feed::TYPE_HPTG)
1154
        {
10394 nelberth 1155
 
10415 nelberth 1156
                $uuid =  $this->params()->fromRoute('id');
1157
                if($group_uuid!=''){
1158
                    $uuid=$group_uuid;
1159
                }
8217 nelberth 1160
                $userMapper = UserMapper::getInstance($this->adapter);
1161
                $user = $userMapper->fetchOne($feed->user_id);
8224 nelberth 1162
                $item['owner_url'] =  str_replace('[uuid]', $user->uuid, $user_profile_url);
8217 nelberth 1163
                $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $user->uuid, 'type' => 'user', 'filename' => $user->image]);
1164
                $item['owner_name'] = $user->first_name . ' ' . $user->last_name;
10406 nelberth 1165
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
10415 nelberth 1166
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($uuid);
10393 nelberth 1167
 
1168
                if (!$highPerformanceTeamsGroups) {
1169
                    $data = [
1170
                        'success' => false,
1171
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1172
                    ];
1173
 
1174
                    return new JsonModel($data);
1175
                }
1176
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
1177
 
1178
                    return new JsonModel([
1179
                        'success' => false,
1180
                        'data' => 'ERROR_UNAUTHORIZED'
1181
                    ]);
1182
 
1183
                }
10392 nelberth 1184
                $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
1185
                $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
1186
                if(!$highPerformanceTeamsGroupsMember) {
1187
                    return new JsonModel([
1188
                        'success' => false,
1189
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1190
                    ]);
1191
                }
1192
                if($feed->user_id == $currentUser->id||$highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER) {
10391 nelberth 1193
                    $item['feed_delete_url'] = $this->url()->fromRoute('feeds/delete',  $params);
1194
                }else{
1195
                    $item['feed_delete_url'] = '';
8217 nelberth 1196
                }
10406 nelberth 1197
 
10620 eleazar 1198
        }else if($feed->type == Feed::TYPE_MYT){
1199
            $item['feed_delete_url'] = $this->url()->fromRoute('feeds/delete',  $params);
1200
            $item['owner_url'] =  str_replace('[uuid]', $company->uuid, $company_profile_url);
1201
            $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $company->uuid, 'type' => 'user', 'filename' => $company->image]);
1202
            $item['owner_name'] = $company->name;
8205 nelberth 1203
        } else {
10402 nelberth 1204
            $item['feed_delete_url'] = $this->url()->fromRoute('feeds/delete',  $params);
8205 nelberth 1205
            $item['owner_url'] =  str_replace('[uuid]', $company->uuid, $company_profile_url);
1206
            $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $company->uuid, 'type' => 'company', 'filename' => $company->image]);
1207
            $item['owner_name'] = $company->name;
1208
        }
1 www 1209
 
1210
        $userMapper = UserMapper::getInstance($this->adapter);
1211
        $user = $userMapper->fetchOne($feed->user_id);
1212
 
1213
 
7235 stevensc 1214
        $item['owner_description'] = strip_tags($feed->description, 'p');
1 www 1215
        $item['owner_shared'] = $feed->total_shared;
1216
        $item['owner_comments'] = $feed->total_comments;
1217
 
1218
 
1219
        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $feed->added_on);
1220
        $item['owner_time_elapse'] = Functions::timeElapsedString( $dt->getTimestamp());
1221
 
1222
        if($feed->file_type == Feed::FILE_TYPE_IMAGE) {
1223
            $item['owner_file_image'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1224
 
1225
        }
1226
        if($feed->file_type == Feed::FILE_TYPE_DOCUMENT) {
1227
            $item['owner_file_document'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1228
        }
1229
        if($feed->file_type == Feed::FILE_TYPE_VIDEO) {
1230
            $item['owner_file_image_preview'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_image_preview]);
1231
            $item['owner_file_video'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1232
        }
9021 nelberth 1233
        if($feed->file_type == Feed::FILE_TYPE_MEETING) {
9081 nelberth 1234
            $dt = \DateTime::createFromFormat('Y-m-d', $feed->date);
9086 nelberth 1235
            $dt2 = \DateTime::createFromFormat('H:i:s', $feed->time);
9023 nelberth 1236
            $item['owner_meeting'] =  true;
9082 nelberth 1237
            $item['owner_meeting_date'] =  $dt->format('d/m/Y');
9088 nelberth 1238
            $item['owner_meeting_time'] =  $dt2->format('h:i a');
9021 nelberth 1239
            $item['owner_meeting_link_type'] =  $feed->link_type;
1240
            $item['owner_meeting_link_name'] =  $feed->link_name;
1241
 
1242
        }
1 www 1243
 
1244
 
1245
 
1246
 
1247
        $commentMapper = CommentMapper::getInstance($this->adapter);
1248
        $records = $commentMapper->fetchAllPublishedByFeedId($feed->id);
1249
 
1250
 
1251
        $comments = [];
10073 nelberth 1252
        $comment_count=0;
1 www 1253
        foreach($records as $record)
1254
        {
1255
            $user = $userMapper->fetchOne($record->user_id);
1256
            $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
1257
 
1258
            $comment = [];
1259
            $comment['unique'] = uniqid();
10073 nelberth 1260
            $comment_count++;
1261
            $comment['comment_index']=$comment_count;
1 www 1262
            $user = $userMapper->fetchOne($record->user_id);
1263
            if($user->id == $owner->user_id) {
1264
                $comment['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image ]);
1265
                $comment['user_url'] =  str_replace('[uuid]', $company->uuid, $company_profile_url);
1266
                $comment['user_name'] = $company->name;
1267
            } else {
1268
                $comment['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image ]);
1269
                $comment['user_url'] = str_replace('[uuid]', $user->uuid, $user_profile_url);
1270
                $comment['user_name'] = $user->first_name . ' ' . $user->last_name;
10406 nelberth 1271
            }
10395 nelberth 1272
            if($feed->type == Feed::TYPE_HPTG)
1273
            {
10397 nelberth 1274
                if($record->user_id == $currentUser->id||$highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER) {
10395 nelberth 1275
                    $comment['link_delete'] = $this->url()->fromRoute('feeds/comments/delete',['id' => $feed->uuid, 'comment' => $record->uuid ]);
1276
                }else{
10398 nelberth 1277
                    $comment['link_delete'] = '';
10395 nelberth 1278
                }
1279
            }else{
1280
                $comment['link_delete'] = $this->url()->fromRoute('feeds/comments/delete',['id' => $feed->uuid, 'comment' => $record->uuid ]);
1281
            }
10406 nelberth 1282
 
8385 nelberth 1283
            $comment['link_answer_add'] = $this->url()->fromRoute('feeds/comments/answer',['id' => $feed->uuid, 'comment' => $record->uuid ]);
1 www 1284
            $comment['time_elapsed'] = Functions::timeElapsedString($dt->getTimestamp());
1285
            $comment['comment'] = $record->comment;
1286
 
8414 nelberth 1287
            $records2 = $commentMapper->fetchAllPublishedByCommentId($record->id);
1288
            $answers = [];
8499 nelberth 1289
            $contador=0;
8414 nelberth 1290
            foreach($records2 as $record2)
1291
            {
1292
                $user = $userMapper->fetchOne($record2->user_id);
1293
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record2->added_on);
1294
 
1295
                $answer = [];
1296
                $answer['unique'] = uniqid();
1297
 
1298
 
1299
                $user = $userMapper->fetchOne($record2->user_id);
1300
                if($user->id == $owner->user_id) {
1301
                    $answer['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image ]);
1302
                    $answer['user_url'] =  str_replace('[uuid]', $company->uuid, $company_profile_url);
1303
                    $answer['user_name'] = $company->name;
1304
                } else {
1305
                    $answer['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image ]);
1306
                    $answer['user_url'] = str_replace('[uuid]', $user->uuid, $user_profile_url);
1307
                    $answer['user_name'] = $user->first_name . ' ' . $user->last_name;
10406 nelberth 1308
                }
10398 nelberth 1309
                if($feed->type == Feed::TYPE_HPTG)
1310
                {
10399 nelberth 1311
                    if($record2->user_id == $currentUser->id||$highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER) {
10398 nelberth 1312
                        $answer['link_delete'] = $this->url()->fromRoute('feeds/comments/delete',['id' => $feed->uuid, 'comment' => $record2->uuid ]);
1313
                    }else{
1314
                        $answer['link_delete'] ='';
1315
                    }
1316
                }else{
1317
                    $answer['link_delete'] = $this->url()->fromRoute('feeds/comments/delete',['id' => $feed->uuid, 'comment' => $record2->uuid ]);
1318
                }
10406 nelberth 1319
 
8414 nelberth 1320
                $answer['time_elapsed'] = Functions::timeElapsedString($dt->getTimestamp());
1321
                $answer['comment'] = $record2->comment;
1322
 
1323
                $records2 = $commentMapper->fetchAllPublishedByCommentId($record2->id);
1324
 
8499 nelberth 1325
                $contador++;
8414 nelberth 1326
                array_push($answers, $answer);
1327
            }
8499 nelberth 1328
            $comment['number_answers']=$contador;
8416 nelberth 1329
            $comment['answers'] = $answers;
1 www 1330
            array_push($comments, $comment);
1331
        }
1332
        $item['comment_add_url'] = $this->url()->fromRoute('feeds/comments', ['id' => $feed->uuid]);
9064 nelberth 1333
            $item['comments'] = $comments;
1 www 1334
 
9064 nelberth 1335
 
9065 nelberth 1336
 
1 www 1337
        return $item;
1338
    }
1339
 
1340
    /**
1341
     *
1342
     * @param int $comment_id
1343
     * @return array
1344
     */
1345
    private function renderComment($comment_id)
1346
    {
1347
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1348
        $currentUser = $currentUserPlugin->getUser();
1349
        $currentCompany = $currentUserPlugin->getCompany();
1350
 
1351
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1352
        $company = $companyMapper->fetchOne($currentCompany->id);
1353
 
1354
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1355
        $owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
1356
 
1357
 
1358
 
1359
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
1360
        if($sandbox) {
1361
            $company_profile_url = $this->config['leaderslinked.frontend.sandbox_company_profile'];
1362
            $user_profile_url = $this->config['leaderslinked.frontend.sandbox_user_profile'];
1363
        } else {
1364
            $company_profile_url = $this->config['leaderslinked.frontend.production_company_profile'];
1365
            $user_profile_url = $this->config['leaderslinked.frontend.production_user_profile'];
1366
        }
1367
 
1368
 
1369
        $item = [];
1370
 
1371
        $commentMapper = CommentMapper::getInstance($this->adapter);
1372
        $record = $commentMapper->fetchOne($comment_id);
1373
 
1374
        $feedMapper = FeedMapper::getInstance($this->adapter);
1375
        $feed = $feedMapper->fetchOne($record->feed_id);
1376
 
1377
        if($record) {
1378
            $userMapper = UserMapper::getInstance($this->adapter);
1379
 
1380
            $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
1381
 
1382
 
1383
            $item = [];
1384
            $item['unique'] = uniqid();
1385
 
1386
 
1387
            $user = $userMapper->fetchOne($record->user_id);
1388
            if($user->id == $owner->user_id) {
1389
                $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image ]);
1390
                $item['user_url'] =  str_replace('[uuid]', $company->uuid, $company_profile_url);
1391
                $item['user_name'] = $company->name;
1392
            } else {
1393
                $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image ]);
1394
                $item['user_url'] = str_replace('[uuid]', $user->uuid, $user_profile_url);
1395
                $item['user_name'] = $user->first_name . ' ' . $user->last_name;
1396
            }
1397
 
1398
 
1399
 
8446 nelberth 1400
            $item['link_answer_add'] = $this->url()->fromRoute('feeds/comments/answer',['id' => $feed->uuid, 'comment' => $record->uuid ]);
1 www 1401
            $item['link_delete'] = $this->url()->fromRoute('feeds/comments/delete',['id' => $feed->uuid, 'comment' => $record->uuid ]);
1402
            $item['time_elapsed'] = Functions::timeElapsedString($dt->getTimestamp());
1403
            $item['comment'] = $record->comment;
1404
 
1405
        }
1406
        return $item;
1407
    }
1408
 
1409
    /**
1410
     *
1411
     * @param string $path
1412
     * @return boolean
1413
     */
1414
    private function deletePath($path)
1415
    {
1416
        try {
1417
            if (is_dir($path)){
1418
                if ($dh = opendir($path)) {
1419
                    while (($file = readdir($dh)) !== false)
1420
                    {
1421
                        if($file == '.' || $file == '..') {
1422
                            continue;
1423
                        }
1424
                        unlink($path . DIRECTORY_SEPARATOR . $file);
1425
                    }
1426
                    closedir($dh);
1427
                }
1428
 
1429
                rmdir($path);
1430
            }
1431
            return true;
1432
        }
1433
        catch (\Throwable $e)
1434
        {
1435
            error_log($e->getTraceAsString());
1436
            return false;
1437
        }
1438
    }
1439
 
1440
}