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