Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16983 | Rev 17008 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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