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