Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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