Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17287 | | Comparar con el anterior | Ultima modificación | Ver Log |

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