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
 
40
use LeadersLinked\Mapper\TopicMapper;
41
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
42
use LeadersLinked\Model\HighPerformanceTeamsGroups;
43
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
44
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
45
 
16701 efrain 46
 
11898 nelberth 47
class HighPerformanceTeamsGroupsViewFeedController 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
 
98
 
99
 
100
    public function commentAction()
101
    {
102
 
103
 
104
        $currentUserPlugin = $this->plugin('currentUserPlugin');
105
        $currentUser = $currentUserPlugin->getUser();
106
        $currentCompany = $currentUserPlugin->getCompany();
107
 
108
 
109
        $id = $this->params()->fromRoute('id');
11924 nelberth 110
        $group_uuid = $this->params()->fromRoute('group_id');
111
 
112
        if(!$group_uuid) {
113
            $data = [
114
                'success'   => false,
115
                'data'   => 'ERROR_INVALID_PARAMETER'
116
            ];
117
 
118
            return new JsonModel($data);
119
        }
120
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
121
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
122
 
123
        if (!$highPerformanceTeamsGroups) {
124
            $data = [
125
                'success' => false,
126
                'data' => 'ERROR_RECORD_NOT_FOUND'
127
            ];
128
 
129
            return new JsonModel($data);
130
        }
131
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
132
 
133
            return new JsonModel([
134
                'success' => false,
135
                'data' => 'ERROR_UNAUTHORIZED'
136
            ]);
137
 
138
        }
139
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
140
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
141
        if(!$highPerformanceTeamsGroupsMember) {
142
            return new JsonModel([
143
                'success' => false,
11989 nelberth 144
                'data' => 'ERROR_UNAUTHORIZED'
11924 nelberth 145
            ]);
146
        }
147
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
148
            return new JsonModel([
149
                'success' => false,
150
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
151
            ]);
152
        }
153
 
154
 
11898 nelberth 155
        $request = $this->getRequest();
156
        if($request->isPost()) {
157
            $feedMapper = FeedMapper::getInstance($this->adapter);
158
            $feed = $feedMapper->fetchOneByUuid($id);
159
            if(!$feed) {
160
                $response = [
161
                    'success' => false,
162
                    'data' => 'ERROR_POST_NOT_FOUND'
163
                ];
164
                return new JsonModel($response);
165
            }
166
 
11925 nelberth 167
 
11898 nelberth 168
 
169
            $dataPost = $request->getPost()->toArray();
170
            $form = new CommentForm();
171
            $form->setData($dataPost);
172
 
173
            if($form->isValid()) {
16701 efrain 174
 
11898 nelberth 175
 
176
                $dataPost = (array) $form->getData();
177
                $comment = new Comment();
178
                $comment->comment = $dataPost['comment'];
179
                $comment->feed_id = $feed->id;
11924 nelberth 180
                $comment->user_id = $currentUser->id;
11898 nelberth 181
 
182
 
183
                $commentMapper = CommentMapper::getInstance($this->adapter);
16701 efrain 184
                $now = $commentMapper->getDatebaseNow();
185
 
11898 nelberth 186
                if($commentMapper->insert($comment)) {
187
 
188
                    $total_comments = $commentMapper->fetchCountCommentByFeedId($comment->feed_id);
189
 
190
                    $feed->total_comments = $total_comments;
191
                    $feedMapper->update($feed);
192
 
193
                    $response = [
194
                        'success'   => true,
14740 efrain 195
                        'data'   => $this->renderComment($comment->id, $now, $group_uuid),
11898 nelberth 196
                        'total_comments' => $total_comments
197
                    ];
198
 
199
                    return new JsonModel($response);
200
 
201
                } else {
202
 
203
                    $response = [
204
                        'success'   => false,
205
                        'data'   => $commentMapper->getError()
206
                    ];
207
 
208
                    return new JsonModel($response);
209
                }
210
 
211
            } else {
212
                $message = '';;
213
                $form_messages = (array) $form->getMessages();
214
                foreach($form_messages  as $fieldname => $field_messages)
215
                {
216
                    foreach( $field_messages as $key => $value)
217
                    {
218
                        $message = $value;
219
                    }
220
                }
221
 
222
                $response = [
223
                    'success'   => false,
224
                    'data'   => $message
225
                ];
226
 
227
                return new JsonModel($response);
228
            }
229
 
230
 
231
        } else {
232
            $response = [
233
                'success' => false,
234
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
235
            ];
236
 
237
            return new JsonModel($response);
238
        }
239
 
240
 
241
    }
242
 
243
    public function answerAction()
244
    {
245
 
246
 
247
        $currentUserPlugin = $this->plugin('currentUserPlugin');
248
        $currentUser = $currentUserPlugin->getUser();
249
        $currentCompany = $currentUserPlugin->getCompany();
250
 
251
 
252
        $id = $this->params()->fromRoute('id');
253
        $comment_uuid = $this->params()->fromRoute('comment');
11989 nelberth 254
        $group_uuid = $this->params()->fromRoute('group_id');
255
 
256
        if(!$group_uuid) {
257
            $data = [
258
                'success'   => false,
259
                'data'   => 'ERROR_INVALID_PARAMETER'
260
            ];
261
 
262
            return new JsonModel($data);
263
        }
264
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
265
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
266
 
267
        if (!$highPerformanceTeamsGroups) {
268
            $data = [
269
                'success' => false,
270
                'data' => 'ERROR_RECORD_NOT_FOUND'
271
            ];
272
 
273
            return new JsonModel($data);
274
        }
275
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
276
 
277
            return new JsonModel([
278
                'success' => false,
279
                'data' => 'ERROR_UNAUTHORIZED'
280
            ]);
281
 
282
        }
283
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
284
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
285
        if(!$highPerformanceTeamsGroupsMember) {
286
            return new JsonModel([
287
                'success' => false,
288
                'data' => 'ERROR_UNAUTHORIZED'
289
            ]);
290
        }
291
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
292
            return new JsonModel([
293
                'success' => false,
294
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
295
            ]);
296
        }
297
 
298
 
11898 nelberth 299
        $request = $this->getRequest();
300
        if($request->isPost()) {
301
            $feedMapper = FeedMapper::getInstance($this->adapter);
16701 efrain 302
            $now = $feedMapper->getDatebaseNow();
303
 
11898 nelberth 304
            $feed = $feedMapper->fetchOneByUuid($id);
305
            if(!$feed) {
306
                $response = [
307
                    'success' => false,
308
                    'data' => 'ERROR_POST_NOT_FOUND'
309
                ];
310
                return new JsonModel($response);
311
            }
312
 
313
            if($feed->company_id != $currentCompany->id) {
314
                $response = [
315
                    'success' => false,
316
                    'data' => 'ERROR_UNAUTHORIZED'
317
                ];
318
                return new JsonModel($response);
319
            }
320
 
321
            $dataPost = $request->getPost()->toArray();
322
            $form = new CommentAnswerForm();
323
            $form->setData($dataPost);
324
 
325
            if($form->isValid()) {
16701 efrain 326
 
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()) {
16701 efrain 665
 
666
 
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);
16701 efrain 687
 
688
                $now = $highPerformanceTeamsGroupsMapper->getDatebaseNow();
11987 nelberth 689
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
690
 
691
                if (!$highPerformanceTeamsGroups) {
692
                    $data = [
693
                        'success' => false,
694
                        'data' => 'ERROR_RECORD_NOT_FOUND'
695
                    ];
11898 nelberth 696
 
11987 nelberth 697
                    return new JsonModel($data);
698
                }
11898 nelberth 699
 
11987 nelberth 700
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
11898 nelberth 701
 
11987 nelberth 702
                    return new JsonModel([
703
                        'success' => false,
704
                        'data' => 'ERROR_UNAUTHORIZED'
705
                    ]);
706
 
707
                }
708
 
709
                $highPerformanceTeamsGroupsMemberMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
710
                $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMemberMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
711
                if($highPerformanceTeamsGroupsMember) {
712
                    if($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN ){
713
                        $id =  $highPerformanceTeamsGroups->id;
714
                    }else{
715
 
11898 nelberth 716
                        return new JsonModel([
717
                            'success' => false,
718
                            'data' => 'ERROR_UNAUTHORIZED'
719
                        ]);
11987 nelberth 720
                    }
721
 
722
                } else {
11898 nelberth 723
 
11987 nelberth 724
                    return new JsonModel([
725
                        'success' => false,
11988 nelberth 726
                        'data' => 'ERROR_UNAUTHORIZED'
11987 nelberth 727
                    ]);
728
                }
729
                $topic_uuid =  $this->params()->fromRoute('topic_id');
730
                if($topic_uuid){
731
                    $topicMapper = TopicMapper::getInstance($this->adapter);
732
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
733
                    if($topic){
734
                        $feed->topic_id = $topic->id;
735
                    }else{
11898 nelberth 736
                        return new JsonModel([
737
                            'success' => false,
738
                            'data' => 'ERROR_RECORD_NOT_FOUND'
739
                        ]);
740
                    }
11987 nelberth 741
 
11898 nelberth 742
                }
743
 
744
 
11987 nelberth 745
                $feed->company_id       = $currentCompany->id;
746
                $feed->group_id         = null;
747
                $feed->high_performance_group_id = $id;
748
                $feed->user_id          = $currentUser->id;
749
                $feed->type             = Feed::TYPE_HPTG;
750
                $feed->posted_or_shared = Feed::POSTED;
751
                $feed->shared_with      = Feed::SHARE_WITH_CONNECTIONS;
11898 nelberth 752
                $feed->total_comments   = 0;
753
                $feed->total_shared     = 0;
754
 
755
 
756
                $files = $this->getRequest()->getFiles()->toArray();
757
                $file_type = '';
758
                if(isset($files['file']) && empty($files['file']['error'])) {
759
                    $feed_tmp_filename  = $files['file']['tmp_name'];
760
                    $feed_filename      = $this->normalizeString($files['file']['name']);
761
 
762
                    $mime_type = mime_content_type($feed_tmp_filename);
763
                    if($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
764
                        $file_type = Feed::FILE_TYPE_IMAGE;
765
                    } else if($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
766
                        $file_type = Feed::FILE_TYPE_VIDEO;
767
                    } else if($mime_type == 'application/pdf') {
768
                        $file_type = Feed::FILE_TYPE_DOCUMENT;
769
                    }
770
                }
771
 
772
 
773
                $feedMapper = FeedMapper::getInstance($this->adapter);
774
                $result = $feedMapper->insert($feed);
775
 
776
 
777
                if($result) {
778
 
779
                    $feed = $feedMapper->fetchOne($feed->id);
780
 
781
                    if( $file_type == Feed::FILE_TYPE_DOCUMENT) {
782
                        try {
783
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
784
                            if(!file_exists($target_path)) {
785
                                mkdir($target_path, 0755);
786
                            }
787
 
788
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR .$feed_filename;
789
                            $result = move_uploaded_file($feed_tmp_filename , $full_filename);
790
                            if($result) {
791
 
792
                                $feed->file_type = $file_type;
793
                                $feed->file_name = basename($feed_filename);
794
                                $feedMapper->update($feed);
795
                            } else {
796
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename );
797
                            }
798
                        } catch(\Throwable $e) {
799
                            error_log($e->getTraceAsString());
800
                        }
801
                    }
802
                    if( $file_type == Feed::FILE_TYPE_IMAGE) {
803
                        try {
804
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
805
                            if(!file_exists($target_path)) {
806
                                mkdir($target_path, 0755);
807
                            }
808
 
809
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
810
 
811
                            $feed_filename = substr($feed_filename, 0, strrpos($feed_filename, '.'))  . '.png';
812
                            $crop_to_dimensions = false;
813
                            if(Image::uploadImage($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height, $crop_to_dimensions )) {
814
                                $feed->file_type = $file_type;
815
                                $feed->file_name = basename($feed_filename);
816
                                $feedMapper->update($feed);
817
                            }
818
                        } catch(\Throwable $e) {
819
                            error_log($e->getTraceAsString());
820
                        }
821
                    }
822
                    if( $file_type == Feed::FILE_TYPE_VIDEO) {
823
                        try {
824
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
825
                            if(!file_exists($target_path)) {
826
                                mkdir($target_path, 0755);
827
                            }
828
 
829
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
830
 
831
                            $result = move_uploaded_file($feed_tmp_filename , $full_filename);
832
                            if($result) {
833
 
834
                                $size = $this->config['leaderslinked.image_sizes.feed_image_size'];
835
                                $getFromSecound = 2;
836
 
837
                                //extracción del cover
838
                                $generateFileName = substr($feed_filename, 0, strrpos($feed_filename, '.'));
839
                                $generateFile =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.png';
840
                                $cmd            = "/usr/bin/ffmpeg -i $full_filename -an -ss $getFromSecound -s $size $generateFile";
841
                                exec($cmd);
842
 
843
 
844
                                $feed->file_type = $file_type;
845
                                $feed->file_name = basename($feed_filename);
846
                                $feed->file_image_preview = basename($generateFile);
847
                                $feedMapper->update($feed);
848
 
849
                                $videoConvert = new VideoConvert();
850
                                $videoConvert->filename = $full_filename;
851
                                $videoConvert->type = VideoConvert::TYPE_FEED;
852
 
853
                                $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
854
                                $videoConvertMapper->insert($videoConvert);
855
                            } else {
856
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename );
857
                            }
858
                        } catch(\Throwable $e) {
859
                            error_log($e->getTraceAsString());
860
                        }
861
                    }
862
 
863
 
864
                    $response = [
865
                        'success'   => true,
14740 efrain 866
                        'data'   => $this->renderFeed($feed->id, $now, $group_uuid)
11898 nelberth 867
                    ];
868
 
869
                } else {
870
                    $response = [
871
                        'success'   => false,
872
                        'data'   => $feedMapper->getError()
873
                    ];
874
                }
875
            } else {
876
                $messages = [];
877
                $form_messages = (array) $form->getMessages();
878
                foreach($form_messages  as $fieldname => $field_messages)
879
                {
880
                    $messages[$fieldname] = array_values($field_messages);
881
                }
882
 
883
                $response = [
884
                    'success'   => false,
885
                    'data'   => $messages
886
                ];
887
            }
888
 
889
        } else {
890
            $response = [
891
                'success' => false,
892
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
893
            ];
894
        }
895
 
896
        return new JsonModel($response);
897
    }
898
 
899
 
900
    public function timelineAction() {
901
 
902
        $currentUserPlugin = $this->plugin('currentUserPlugin');
903
        $currentUser = $currentUserPlugin->getUser();
904
        $currentCompany = $currentUserPlugin->getCompany();
905
 
906
 
907
 
908
 
909
 
910
        $request = $this->getRequest();
911
        if($request->isGet()) {
912
 
14740 efrain 913
 
914
 
11898 nelberth 915
            $page = (int) $this->params()->fromQuery('page');
916
            $topic_uuid   = $this->params()->fromRoute('topic_id');
917
            $urgent   = $this->params()->fromRoute('urgent');
11914 nelberth 918
            $group_uuid =  $this->params()->fromRoute('group_id');
11898 nelberth 919
 
11914 nelberth 920
            if(!$group_uuid){
11910 nelberth 921
                return new JsonModel([
922
                    'success' => false,
923
                    'data' => 'ERROR_RECORD_NOT_FOUND'
924
                ]);
925
            }
11898 nelberth 926
 
16701 efrain 927
 
928
 
14740 efrain 929
 
11910 nelberth 930
 
931
            $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
16701 efrain 932
            $now = $highPerformanceTeamsGroupsMapper->getDatebaseNow();
11914 nelberth 933
            $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
11910 nelberth 934
 
935
            if (!$highPerformanceTeamsGroups) {
936
                $data = [
937
                    'success' => false,
938
                    'data' => 'ERROR_RECORD_NOT_FOUND'
939
                ];
940
                return new JsonModel($data);
941
            }
11898 nelberth 942
 
11910 nelberth 943
            if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
944
                return new JsonModel([
945
                    'success' => false,
946
                    'data' => 'ERROR_UNAUTHORIZED'
947
                ]);
11898 nelberth 948
 
11910 nelberth 949
            }
11898 nelberth 950
 
11910 nelberth 951
            $highPerformanceTeamsGroupsMemberMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
952
            $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMemberMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
953
            if($highPerformanceTeamsGroupsMember) {
954
                if($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN ){
955
                    $queryMapper = QueryMapper::getInstance($this->adapter);
956
                    $select = $queryMapper->getSql()->select(FeedMapper::_TABLE);
957
                    $select->columns(['id']);
958
                    $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
959
                    $select->where->equalTo('company_id',$currentCompany->id);
960
                    $select->where->equalTo('high_performance_group_id',$highPerformanceTeamsGroups->id);
961
 
962
 
963
                    if(isset($topic_uuid)){
964
                        $topicMapper = TopicMapper::getInstance($this->adapter);
965
                        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
966
                        if($topic){
967
                            if($topic->status!='a'){
11898 nelberth 968
                                return new JsonModel([
969
                                    'success' => false,
11910 nelberth 970
                                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
11898 nelberth 971
                                ]);
972
                            }
11910 nelberth 973
                            $select->where->equalTo('topic_id',$topic->id);
974
 
11898 nelberth 975
                        }else{
11910 nelberth 976
                            return new JsonModel([
977
                                'success' => false,
978
                                'data' => 'ERROR_RECORD_NOT_FOUND'
979
                            ]);
11898 nelberth 980
                        }
11910 nelberth 981
                    }else{
982
                        if(isset($urgent)){
983
                            $select->where->equalTo('priority', Feed::PRIORITY_URGENT);
984
                        }else{
985
                            $select->where->IsNull('topic_id');
986
                        }
11898 nelberth 987
 
988
                    }
11910 nelberth 989
 
990
 
991
                    $select->where->and->equalTo('type', Feed::TYPE_HPTG);
992
                    $select->order('added_on desc');
11898 nelberth 993
 
11910 nelberth 994
                }else{
11898 nelberth 995
                    return new JsonModel([
996
                        'success' => false,
11910 nelberth 997
                        'data' => 'ERROR_UNAUTHORIZED'
11898 nelberth 998
                    ]);
999
                }
11910 nelberth 1000
 
1001
            } else {
11898 nelberth 1002
 
11910 nelberth 1003
                return new JsonModel([
1004
                    'success' => false,
1005
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1006
                ]);
11898 nelberth 1007
            }
1008
 
1009
 
1010
            $dbSelect = new DbSelect($select, $this->adapter);
1011
            $paginator = new Paginator($dbSelect);
1012
            $paginator->setCurrentPageNumber($page ? $page : 1);
1013
            $paginator->setItemCountPerPage(10);
1014
 
1015
            $items = [];
1016
            $feeds = $paginator->getCurrentItems();
11926 nelberth 1017
 
1018
            foreach($feeds as $feed)
1019
            {
14740 efrain 1020
                $items[] = $this->renderFeed($feed->id, $now, $group_uuid);
11926 nelberth 1021
            }
11928 nelberth 1022
 
11898 nelberth 1023
            $response = [
1024
                'success' => true,
1025
                'data' => [
1026
                    'total' => [
1027
                        'count' => $paginator->getTotalItemCount(),
1028
                        'pages' => $paginator->getPages()->pageCount,
1029
                    ],
1030
                    'current' => [
1031
                        'items'    => $items,
1032
                        'page'     => $paginator->getCurrentPageNumber(),
1033
                        'count'    => $paginator->getCurrentItemCount(),
1034
                    ]
1035
                ]
1036
            ];
1037
 
1038
            return new JsonModel($response);
1039
        }
1040
 
1041
        $response = [
1042
            'success' => false,
1043
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1044
        ];
1045
 
1046
 
1047
        return new JsonModel($response);
1048
    }
1049
 
1050
    public function oneFeedAction(){
1051
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1052
        $currentUser = $currentUserPlugin->getUser();
1053
        $currentCompany = $currentUserPlugin->getCompany();
1054
 
1055
        $request = $this->getRequest();
1056
        if($request->isGet()) {
1057
 
14740 efrain 1058
 
1059
 
11898 nelberth 1060
            $feed_uuid =  $this->params()->fromRoute('feed_id');
1061
            $myt_id =  $this->params()->fromRoute('myt_id');
1062
 
1063
            if(!isset($feed_uuid)){
1064
                    $data = [
1065
                        'success'   => false,
1066
                        'data'   => 'ERROR_INVALID_PARAMETER'
1067
                    ];
1068
 
1069
                    return new JsonModel($data);
1070
            }
1071
 
1072
            $items = [];
1073
            $feedMapper = FeedMapper::getInstance($this->adapter);
1074
            $feed  = $feedMapper->fetchOneByUuid($feed_uuid);
1075
 
1076
            if (!$feed) {
1077
                $data = [
1078
                    'success' => false,
1079
                    'data' => 'ERROR_RECORD_NOT_FOUND'
1080
                ];
1081
 
1082
                return new JsonModel($data);
1083
            }
1084
 
11996 nelberth 1085
           if($feed->type=='hptg'){
11898 nelberth 1086
 
1087
                $group_uuid =  $this->params()->fromRoute('group_id');
1088
                $topic_uuid   = $this->params()->fromRoute('topic_id');
1089
 
1090
                if(!isset($topic_uuid)){
1091
 
1092
                    $data = [
1093
                        'success'   => false,
1094
                        'data'   => 'ERROR_INVALID_PARAMETER'
1095
                    ];
1096
 
1097
                    return new JsonModel($data);
1098
                }
1099
                if(!isset($group_uuid)){
1100
 
1101
                    $data = [
1102
                        'success'   => false,
1103
                        'data'   => 'ERROR_INVALID_PARAMETER'
1104
                    ];
1105
 
1106
                    return new JsonModel($data);
1107
                }
1108
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
1109
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
1110
 
1111
                if (!$highPerformanceTeamsGroups) {
1112
                    $data = [
1113
                        'success' => false,
1114
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1115
                    ];
1116
 
1117
                    return new JsonModel($data);
1118
                }
1119
 
1120
 
1121
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
1122
 
1123
                    return new JsonModel([
1124
                        'success' => false,
1125
                        'data' => 'ERROR_UNAUTHORIZED'
1126
                    ]);
1127
 
1128
                }
1129
 
1130
                if($feed->high_performance_group_id!=$highPerformanceTeamsGroups->id){
1131
                    return new JsonModel([
1132
                        'success' => false,
1133
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
1134
                    ]);
1135
                }
1136
 
1137
                $topicMapper = TopicMapper::getInstance($this->adapter);
16701 efrain 1138
                $now = $topicMapper->getDatebaseNow();
1139
 
11898 nelberth 1140
                $topic = $topicMapper->fetchOneByUuid($topic_uuid);
1141
 
1142
                if (!$topic) {
1143
                    $data = [
1144
                        'success' => false,
1145
                        'data' => 'ERROR_RECORD_NOT_FOUND'
1146
                    ];
1147
 
1148
                    return new JsonModel($data);
1149
                }
1150
 
1151
                if($feed->topic_id!=$topic->id){
1152
                    return new JsonModel([
1153
                        'success' => false,
1154
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
1155
                    ]);
1156
                }
14740 efrain 1157
 
16701 efrain 1158
 
14740 efrain 1159
 
11898 nelberth 1160
 
14740 efrain 1161
                $items = $this->renderFeed($feed->id, $now, $group_uuid);
11898 nelberth 1162
 
1163
                $response = [
1164
                    'success' => true,
1165
                    'data' => [
1166
                        'item' =>$items,
1167
                        'topic_title'=>$topic->title,
1168
                        'feed_title'=>$feed->title
1169
                    ]
1170
                ];
1171
            }
1172
 
1173
            return new JsonModel($response);
1174
        }
1175
    }
1176
    /**
1177
     *
1178
     * @param string $str
1179
     * @return string
1180
     */
1181
    private function normalizeString ($str = ''){
1182
        $basename  = substr($str, 0, strrpos($str, '.'));
1183
        $basename  = str_replace('.', '-', $basename);
1184
 
1185
        $extension  = substr($str, strrpos($str, '.'));
1186
 
1187
        $str = $basename . $extension;
1188
 
1189
        $str = strip_tags($str);
1190
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1191
        $str = preg_replace('/[\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1192
        $str = strtolower($str);
1193
        $str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
1194
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1195
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1196
        $str = str_replace(' ', '-', $str);
1197
        $str = rawurlencode($str);
1198
        $str = str_replace('%', '-', $str);
1199
        return trim(strtolower($str));
1200
    }
1201
 
1202
 
1203
 
1204
    /**
1205
     *
1206
     * @param int $feed_id
1207
     * @param Company $company
1208
     * @return array
1209
     */
14740 efrain 1210
    private function renderFeed($feed_id, $now, $group_uuid)
11898 nelberth 1211
    {
15351 efrain 1212
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1213
        $network = $currentNetworkPlugin->getNetwork();
11898 nelberth 1214
 
1215
 
1216
 
1217
 
1218
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1219
        $currentUser = $currentUserPlugin->getUser();
1220
        $currentCompany = $currentUserPlugin->getCompany();
1221
 
1222
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1223
        $company = $companyMapper->fetchOne($currentCompany->id);
1224
 
1225
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1226
        $owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
1227
 
1228
 
1229
 
1230
        $acl = $this->getEvent()->getViewModel()->getVariable('acl');
1231
 
1232
        $feedMapper = FeedMapper::getInstance($this->adapter);
1233
        $feed = $feedMapper->fetchOne($feed_id);
1234
 
1235
 
1236
        $item = [
1237
            'feed_unique' => uniqid(),
1238
            'feed_uuid' => $feed->uuid,
1239
            'feed_content_type' => $feed->file_type ? $feed->file_type : '',
1240
            'feed_priority'=> $feed->priority,
1241
        ];
1242
 
11914 nelberth 1243
 
11912 nelberth 1244
        $userMapper = UserMapper::getInstance($this->adapter);
1245
        $user = $userMapper->fetchOne($feed->user_id);
15351 efrain 1246
        $item['owner_url'] = 'https://'. $network->main_hostname . '/profile/view/' . $user->uuid;
11912 nelberth 1247
        $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $user->uuid, 'type' => 'user', 'filename' => $user->image]);
1248
        $item['owner_name'] = $user->first_name . ' ' . $user->last_name;
1249
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
11915 nelberth 1250
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
11912 nelberth 1251
 
1252
        if (!$highPerformanceTeamsGroups) {
1253
            $data = [
1254
                'success' => false,
1255
                'data' => 'ERROR_RECORD_NOT_FOUND'
1256
            ];
11898 nelberth 1257
 
11912 nelberth 1258
            return new JsonModel($data);
1259
        }
1260
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
11898 nelberth 1261
 
11912 nelberth 1262
            return new JsonModel([
1263
                'success' => false,
1264
                'data' => 'ERROR_UNAUTHORIZED'
1265
            ]);
1266
 
1267
        }
1268
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
1269
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
1270
        if(!$highPerformanceTeamsGroupsMember) {
1271
            return new JsonModel([
1272
                'success' => false,
1273
                'data' => 'ERROR_RECORD_NOT_FOUND'
1274
            ]);
1275
        }
1276
        if($feed->user_id == $currentUser->id||$highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER) {
11916 nelberth 1277
            $item['feed_delete_url'] = $this->url()->fromRoute('high-performance-teams/groups/view/feeds/delete',['group_id'=>$group_uuid,'id'=>$feed->uuid]);
11912 nelberth 1278
        }else{
1279
            $item['feed_delete_url'] = '';
11898 nelberth 1280
        }
11912 nelberth 1281
 
11898 nelberth 1282
 
11912 nelberth 1283
 
11898 nelberth 1284
        $userMapper = UserMapper::getInstance($this->adapter);
1285
        $user = $userMapper->fetchOne($feed->user_id);
1286
 
1287
 
1288
        $item['owner_description'] = strip_tags($feed->description, 'p');
1289
        $item['owner_shared'] = $feed->total_shared;
1290
        $item['owner_comments'] = $feed->total_comments;
1291
 
1292
 
14740 efrain 1293
        $item['owner_time_elapse'] = Functions::timeAgo( $feed->added_on, $now);
11898 nelberth 1294
 
1295
        if($feed->file_type == Feed::FILE_TYPE_IMAGE) {
1296
            $item['owner_file_image'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1297
 
1298
        }
1299
        if($feed->file_type == Feed::FILE_TYPE_DOCUMENT) {
1300
            $item['owner_file_document'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1301
        }
1302
        if($feed->file_type == Feed::FILE_TYPE_VIDEO) {
1303
            $item['owner_file_image_preview'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_image_preview]);
1304
            $item['owner_file_video'] = $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]);
1305
        }
1306
        if($feed->file_type == Feed::FILE_TYPE_MEETING) {
1307
            $dt = \DateTime::createFromFormat('Y-m-d', $feed->date);
1308
            $dt2 = \DateTime::createFromFormat('H:i:s', $feed->time);
1309
            $item['owner_meeting'] =  true;
1310
            $item['owner_meeting_date'] =  $dt->format('d/m/Y');
1311
            $item['owner_meeting_time'] =  $dt2->format('h:i a');
1312
            $item['owner_meeting_link_type'] =  $feed->link_type;
1313
            $item['owner_meeting_link_name'] =  $feed->link_name;
1314
 
1315
        }
1316
 
1317
 
1318
 
1319
 
1320
        $commentMapper = CommentMapper::getInstance($this->adapter);
1321
        $records = $commentMapper->fetchAllPublishedByFeedId($feed->id);
1322
 
1323
 
1324
        $comments = [];
1325
        $comment_count=0;
1326
        foreach($records as $record)
1327
        {
1328
            $user = $userMapper->fetchOne($record->user_id);
1329
 
14740 efrain 1330
 
11898 nelberth 1331
            $comment = [];
1332
            $comment['unique'] = uniqid();
1333
            $comment_count++;
1334
            $comment['comment_index']=$comment_count;
1335
            $user = $userMapper->fetchOne($record->user_id);
1336
            if($user->id == $owner->user_id) {
1337
                $comment['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image ]);
15351 efrain 1338
                $comment['user_url'] =  'https://'. $network->main_hostname . '/company/view/' . $company->uuid;
11898 nelberth 1339
                $comment['user_name'] = $company->name;
1340
            } else {
1341
                $comment['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image ]);
15351 efrain 1342
                $comment['user_url'] = 'https://'. $network->main_hostname . '/profile/view/' . $user->uuid;
11898 nelberth 1343
                $comment['user_name'] = $user->first_name . ' ' . $user->last_name;
1344
            }
11912 nelberth 1345
 
1346
            if($record->user_id == $currentUser->id||$highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER) {
11916 nelberth 1347
                $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 1348
            }else{
11912 nelberth 1349
                $comment['link_delete'] = '';
11898 nelberth 1350
            }
1351
 
11916 nelberth 1352
            $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 1353
            $comment['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
11898 nelberth 1354
            $comment['comment'] = $record->comment;
1355
 
1356
            $records2 = $commentMapper->fetchAllPublishedByCommentId($record->id);
1357
            $answers = [];
1358
            $contador=0;
1359
            foreach($records2 as $record2)
1360
            {
1361
                $user = $userMapper->fetchOne($record2->user_id);
14740 efrain 1362
 
11898 nelberth 1363
 
1364
                $answer = [];
1365
                $answer['unique'] = uniqid();
1366
 
1367
 
1368
                $user = $userMapper->fetchOne($record2->user_id);
1369
                if($user->id == $owner->user_id) {
1370
                    $answer['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image ]);
15351 efrain 1371
                    $answer['user_url'] =  'https://'. $network->main_hostname . '/company/view/' . $company->uuid;
11898 nelberth 1372
                    $answer['user_name'] = $company->name;
1373
                } else {
1374
                    $answer['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image ]);
15351 efrain 1375
                    $answer['user_url'] = 'https://'. $network->main_hostname . '/profile/view/' . $user->uuid;
11898 nelberth 1376
                    $answer['user_name'] = $user->first_name . ' ' . $user->last_name;
1377
                }
11912 nelberth 1378
 
1379
                if($record2->user_id == $currentUser->id||$highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER) {
11916 nelberth 1380
                    $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 1381
                }else{
11912 nelberth 1382
                    $answer['link_delete'] ='';
11898 nelberth 1383
                }
1384
 
11912 nelberth 1385
 
14740 efrain 1386
                $answer['time_elapsed'] = Functions::timeAgo($record2->added_on, $now);
11898 nelberth 1387
                $answer['comment'] = $record2->comment;
1388
 
1389
                $records2 = $commentMapper->fetchAllPublishedByCommentId($record2->id);
1390
 
1391
                $contador++;
1392
                array_push($answers, $answer);
1393
            }
1394
            $comment['number_answers']=$contador;
1395
            $comment['answers'] = $answers;
1396
            array_push($comments, $comment);
1397
        }
11916 nelberth 1398
        $item['comment_add_url'] = $this->url()->fromRoute('high-performance-teams/groups/view/feeds/comments', ['group_id'=>$group_uuid,'id' => $feed->uuid]);
11898 nelberth 1399
            $item['comments'] = $comments;
1400
 
1401
 
1402
 
1403
        return $item;
1404
    }
1405
 
1406
    /**
1407
     *
1408
     * @param int $comment_id
1409
     * @return array
1410
     */
14740 efrain 1411
    private function renderComment($comment_id, $now, $group_uuid)
11898 nelberth 1412
    {
1413
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1414
        $currentUser = $currentUserPlugin->getUser();
1415
        $currentCompany = $currentUserPlugin->getCompany();
1416
 
1417
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1418
        $company = $companyMapper->fetchOne($currentCompany->id);
1419
 
1420
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1421
        $owner = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
1422
 
15351 efrain 1423
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1424
        $network = $currentNetworkPlugin->getNetwork();
11898 nelberth 1425
 
15351 efrain 1426
 
11898 nelberth 1427
 
1428
 
1429
        $item = [];
1430
 
1431
        $commentMapper = CommentMapper::getInstance($this->adapter);
1432
        $record = $commentMapper->fetchOne($comment_id);
1433
 
1434
        $feedMapper = FeedMapper::getInstance($this->adapter);
1435
        $feed = $feedMapper->fetchOne($record->feed_id);
1436
 
1437
        if($record) {
1438
            $userMapper = UserMapper::getInstance($this->adapter);
1439
 
1440
 
1441
            $item = [];
1442
            $item['unique'] = uniqid();
1443
 
1444
 
1445
            $user = $userMapper->fetchOne($record->user_id);
1446
            if($user->id == $owner->user_id) {
1447
                $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'company',  'code' => $company->uuid, 'filename' =>  $company->image ]);
15351 efrain 1448
                $item['user_url'] = 'https://'. $network->main_hostname . '/profile/view/' . $company->uuid;
11898 nelberth 1449
                $item['user_name'] = $company->name;
1450
            } else {
1451
                $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image ]);
15351 efrain 1452
                $item['user_url'] ='https://'. $network->main_hostname . '/profile/view/' . $user->uuid;
11898 nelberth 1453
                $item['user_name'] = $user->first_name . ' ' . $user->last_name;
1454
            }
1455
 
1456
 
1457
 
11935 nelberth 1458
            $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 ]);
1459
            $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 1460
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
11898 nelberth 1461
            $item['comment'] = $record->comment;
1462
 
1463
        }
1464
        return $item;
1465
    }
1466
 
1467
    /**
1468
     *
1469
     * @param string $path
1470
     * @return boolean
1471
     */
1472
    private function deletePath($path)
1473
    {
1474
        try {
1475
            if (is_dir($path)){
1476
                if ($dh = opendir($path)) {
1477
                    while (($file = readdir($dh)) !== false)
1478
                    {
1479
                        if($file == '.' || $file == '..') {
1480
                            continue;
1481
                        }
1482
                        unlink($path . DIRECTORY_SEPARATOR . $file);
1483
                    }
1484
                    closedir($dh);
1485
                }
1486
 
1487
                rmdir($path);
1488
            }
1489
            return true;
1490
        }
1491
        catch (\Throwable $e)
1492
        {
1493
            error_log($e->getTraceAsString());
1494
            return false;
1495
        }
1496
    }
1497
 
1498
}