Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
13304 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\FeedMapper;
15
use LeadersLinked\Mapper\TopicMapper;
14748 kerby 16
use LeadersLinked\Mapper\FeedTopicMapper;
17
use LeadersLinked\Mapper\QueryMapper;
18
use Laminas\Paginator\Adapter\DbSelect;
19
use Laminas\Paginator\Paginator;
14880 kerby 20
use LeadersLinked\Form\DevelopmentContentCreateFeedForm;
14748 kerby 21
use LeadersLinked\Form\TopicForm;
22
use Laminas\Hydrator\ArraySerializableHydrator;
23
use Laminas\Db\ResultSet\HydratingResultSet;
24
use LeadersLinked\Model\FeedTopic;
13304 eleazar 25
use LeadersLinked\Model\Feed;
14748 kerby 26
use LeadersLinked\Model\Topic;
27
use LeadersLinked\Mapper\CompanyMapper;
28
use LeadersLinked\Model\Company;
13304 eleazar 29
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
30
 
14748 kerby 31
 
14743 kerby 32
class DevelopmentContentController extends AbstractActionController
33
{
13304 eleazar 34
 
35
    /**
36
     *
37
     * @var AdapterInterface
38
     */
39
    private $adapter;
40
 
41
    /**
42
     *
43
     * @var AbstractAdapter
44
     */
45
    private $cache;
46
 
47
    /**
48
     *
49
     * @var  LoggerInterface
50
     */
51
    private $logger;
52
 
53
    /**
54
     *
55
     * @var array
56
     */
57
    private $config;
58
 
59
    /**
60
     *
61
     * @param AdapterInterface $adapter
62
     * @param AbstractAdapter $cache
63
     * @param LoggerInterface $logger
64
     * @param array $config
65
     */
14743 kerby 66
    public function __construct($adapter, $cache, $logger, $config)
67
    {
13304 eleazar 68
        $this->adapter = $adapter;
69
        $this->cache = $cache;
70
        $this->logger = $logger;
71
        $this->config = $config;
72
    }
15028 efrain 73
 
14743 kerby 74
    public function indexAction()
75
    {
15028 efrain 76
        return new JsonModel([
77
            'success' => false,
78
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
79
        ]);
80
    }
81
 
82
    public function listingAction()
83
    {
13304 eleazar 84
        $request = $this->getRequest();
85
        $currentUserPlugin = $this->plugin('currentUserPlugin');
86
        $currentCompany = $currentUserPlugin->getCompany();
87
        $currentUser = $currentUserPlugin->getUser();
88
 
89
        if ($request->isGet()) {
90
 
91
            $headers = $request->getHeaders();
92
 
93
            $isJson = false;
94
            if ($headers->has('Accept')) {
95
                $accept = $headers->get('Accept');
96
 
97
                $prioritized = $accept->getPrioritized();
98
 
99
                foreach ($prioritized as $key => $value) {
100
                    $raw = trim($value->getRaw());
101
 
102
                    if (!$isJson) {
103
                        $isJson = strpos($raw, 'json');
104
                    }
105
                }
106
            }
107
 
14997 kerby 108
 
13304 eleazar 109
            if ($isJson) {
14749 kerby 110
 
14750 kerby 111
 
13304 eleazar 112
                $search = $this->params()->fromQuery('search', []);
113
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
114
 
15371 efrain 115
                $start = intval($this->params()->fromQuery('start', 0), 10);
13304 eleazar 116
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
15371 efrain 117
                $page =  intval($start / $records_x_page);
118
                $page++;
13304 eleazar 119
 
14743 kerby 120
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
121
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/add');
122
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/edit');
123
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/delete');
124
                $allowAproved = $acl->isAllowed($currentUser->usertype_id, 'development-and-content/aproved');
14751 kerby 125
 
13304 eleazar 126
                $items = [];
127
                $feedMapper = FeedMapper::getInstance($this->adapter);
14967 kerby 128
                $category_uuid = $this->params()->fromRoute('category_id');
129
 
130
                if (!$category_uuid) {
15029 kerby 131
                    if($allowEdit){
132
                        $paginator = $feedMapper->fetchAllByDevelop();
133
                    }else{
134
                        $paginator = $feedMapper->fetchAllByDevelopContentPublished();
135
                    }
136
 
14967 kerby 137
                }else{
138
                    $topicMapper = TopicMapper::getInstance($this->adapter);
139
                    $topic = $topicMapper->fetchOneByUuid($category_uuid);
140
                    if (!$topic) {
141
                        $data = [
142
                            'success' => false,
143
                            'data' => 'ERROR_RECORD_NOT_FOUND'
144
                        ];
14743 kerby 145
 
14967 kerby 146
                        return new JsonModel($data);
147
                    }
15029 kerby 148
                    if($allowEdit){
149
                        $paginator = $feedMapper->fetchAllByDevelopContentByCategoryId($topic->id);
150
                    }
15041 kerby 151
                    $paginator = $feedMapper->fetchAllByDevelopContentPublishedByCategoryId($topic->id);
14967 kerby 152
                }
153
 
154
 
13304 eleazar 155
                foreach ($paginator as $record) {
14743 kerby 156
                    $topicMapper = TopicMapper::getInstance($this->adapter);
157
                    $topic = $topicMapper->fetchOne($record->topic_id);
158
                    $topic_title =  ($topic) ? $topic->title : '';
159
                    $preview = ($record->file_image_preview != null && $record->file_image_preview != '') ? $this->url()->fromRoute('storage', ['code' => $record->uuid, 'type' => 'feed', 'filename' => $record->file_image_preview]) : '';
13304 eleazar 160
                    $item = [
14743 kerby 161
                        'uuid' => $record->uuid,
13304 eleazar 162
                        'title' => $record->title,
14743 kerby 163
                        'topic' => $topic_title,
14605 kerby 164
                        'added_on' => $record->added_on,
13304 eleazar 165
                        'description' => $record->description,
14604 kerby 166
                        'file' => $record->file_type ? $record->file_type : '',
167
                        'link_media' => $record->link_media ? $record->link_media : '',
168
                        'link_name' => $record->link_name,
169
                        'file_name' => $record->file_name ? $record->file_name : '',
170
                        'link_file' => $this->url()->fromRoute('storage', ['code' => $record->uuid, 'type' => 'feed', 'filename' => $record->file_name]),
14743 kerby 171
                        'status' => $record->status,
172
                        'preview' => $preview,
173
                        'actions' => [
14754 kerby 174
                            'link_edit' => ($allowEdit) ? $this->url()->fromRoute('development-and-content/edit', ['id' => $record->uuid]) : '',
175
                            'link_aproved' => ($allowAproved) ? $this->url()->fromRoute('development-and-content/aproved', ['id' => $record->uuid]) : '',
176
                            'link_delete' => ($allowDelete) ? $this->url()->fromRoute('development-and-content/delete', ['id' => $record->uuid]) : '',
14743 kerby 177
                        ]
13304 eleazar 178
                    ];
179
 
180
                    array_push($items, $item);
181
                }
14756 kerby 182
 
13304 eleazar 183
                return new JsonModel([
184
                    'success' => true,
185
                    'data' => [
186
                        'items' => $items,
187
                    ]
188
                ]);
189
            } else {
14997 kerby 190
                $topicMapper = TopicMapper::getInstance($this->adapter);
191
                $result = $topicMapper->fetchAllDevelopment();
14999 kerby 192
                $categories=[];
14997 kerby 193
                foreach ($result as $record) {
15004 kerby 194
 
14997 kerby 195
                    $item = [
196
                        'uuid'=>$record->uuid,
197
                        'title'=>$record->title,
15040 kerby 198
                        'filter'=>$this->url()->fromRoute('development-and-content/listing', ['category_id' => $record->uuid]),
14997 kerby 199
                    ];
200
                    array_push($categories, $item);
201
                }
15004 kerby 202
 
14997 kerby 203
 
14880 kerby 204
                $form = new DevelopmentContentCreateFeedForm($this->adapter, $currentCompany->id);
13304 eleazar 205
 
206
                $this->layout()->setTemplate('layout/layout-backend');
207
                $viewModel = new ViewModel();
13309 eleazar 208
                $viewModel->setTemplate('leaders-linked/development-content/index.phtml');
14743 kerby 209
                $viewModel->setVariables([
210
                    'form' => $form,
211
                    'categories' => $categories,
212
                ]);
13304 eleazar 213
                return $viewModel;
214
            }
215
        } else {
216
            return new JsonModel([
217
                'success' => false,
218
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
219
            ]);
220
        }
14743 kerby 221
    }
222
 
223
    public function addAction()
224
    {
13304 eleazar 225
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
14743 kerby 226
        $currentCompany     = $currentUserPlugin->getCompany();
13304 eleazar 227
        $currentUser        = $currentUserPlugin->getUser();
228
 
14743 kerby 229
        $request    = $this->getRequest();
15038 kerby 230
 
14743 kerby 231
            if ($request->isPost()) {
14880 kerby 232
                $form = new  DevelopmentContentCreateFeedForm($this->adapter);
14743 kerby 233
 
234
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
235
                $form->setData($dataPost);
236
 
237
                if ($form->isValid()) {
238
                    $dataPost = (array) $form->getData();
239
                    $hydrator = new ObjectPropertyHydrator();
240
                    $feed = new Feed();
241
                    $hydrator->hydrate($dataPost, $feed);
242
 
243
                    $topicMapper = TopicMapper::getInstance($this->adapter);
14829 kerby 244
                    $topic = $topicMapper->fetchOneByUuid($dataPost['category_content']);
14743 kerby 245
 
246
                    $feed->user_id = $currentUser->id;
247
                    $feed->company_id = $currentCompany->id;
248
                    $feed->user_id          = $currentUser->id;
249
                    $feed->status           = Feed::STATUS_SAVED;
250
                    $feed->topic_id         = $topic->id;
251
                    $feed->type             = Feed::TYPE_DC;
252
                    $feed->posted_or_shared = Feed::POSTED;
253
                    $feed->shared_with      = Feed::SHARE_WITH_PUBLIC;
254
 
255
                    $feed->total_comments   = 0;
256
                    $feed->total_shared     = 0;
257
 
258
 
14834 kerby 259
                    $filesArray = $this->getRequest()->getFiles()->toArray();
14743 kerby 260
                    $file_type = '';
14834 kerby 261
                    $files = null;
14833 kerby 262
 
14834 kerby 263
                    if (isset($filesArray['doc']) && empty($filesArray['doc']['error'])) {
264
                        $files = $filesArray['doc'];
265
                    }
266
                    if (isset($filesArray['video']) && empty($filesArray['video']['error'])) {
267
                        $files = $filesArray['video'];
268
                    }
14743 kerby 269
 
14834 kerby 270
                    if (isset($files) && empty($files['error'])) {
271
                        $feed_tmp_filename  = $files['tmp_name'];
272
                        $feed_filename      = $this->normalizeString($files['name']);
273
 
14743 kerby 274
                        $mime_type = mime_content_type($feed_tmp_filename);
275
                        if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
276
                            $file_type = Feed::FILE_TYPE_IMAGE;
277
                        } else if ($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
278
                            $file_type = Feed::FILE_TYPE_VIDEO;
279
                        } else if ($mime_type == 'application/pdf') {
280
                            $file_type = Feed::FILE_TYPE_DOCUMENT;
281
                        }
13304 eleazar 282
                    }
14743 kerby 283
                    //preview
14832 kerby 284
                    $fileImg = $this->params()->fromFiles('image');
14743 kerby 285
                    if (isset($fileImg) && empty($fileImg['error'])) {
13304 eleazar 286
 
14743 kerby 287
                        $img_tmp_filename  = $fileImg['tmp_name'];
288
                        $img_filename  = $this->normalizeString($fileImg['name']);
289
                    } else {
290
                        $img_tmp_filename = null;
291
                        $img_filename = null;
292
                    }
13304 eleazar 293
 
14743 kerby 294
                    $feedMapper = FeedMapper::getInstance($this->adapter);
13304 eleazar 295
 
14743 kerby 296
                    if ($feedMapper->insert($feed)) {
13304 eleazar 297
                        $feed = $feedMapper->fetchOne($feed->id);
14743 kerby 298
                        $feedMapper->update($feed);
299
                        if ($file_type == Feed::FILE_TYPE_DOCUMENT) {
15029 kerby 300
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
301
                            if (!file_exists($target_path)) {
302
                                mkdir($target_path, 0755);
303
                            }
14743 kerby 304
 
15029 kerby 305
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
306
                            $result = move_uploaded_file($feed_tmp_filename, $full_filename);
307
                            if ($result) {
14743 kerby 308
 
15029 kerby 309
                                $feed->file_type = $file_type;
310
                                $feed->file_name = basename($feed_filename);
311
                                $feedMapper->update($feed);
312
                            } else {
313
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename);
13304 eleazar 314
                            }
315
                        }
14743 kerby 316
                        if ($file_type == Feed::FILE_TYPE_IMAGE) {
15029 kerby 317
 
318
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
319
                            if (!file_exists($target_path)) {
320
                                mkdir($target_path, 0755);
321
                            }
14743 kerby 322
 
15029 kerby 323
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
14743 kerby 324
 
15029 kerby 325
                            $feed_filename = substr($feed_filename, 0, strrpos($feed_filename, '.'))  . '.png';
326
                            $crop_to_dimensions = false;
327
                            if (Image::uploadImage($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height, $crop_to_dimensions)) {
328
                                $feed->file_type = $file_type;
329
                                $feed->file_name = basename($feed_filename);
330
                                $feedMapper->update($feed);
13304 eleazar 331
                            }
332
                        }
14743 kerby 333
                        if ($file_type == Feed::FILE_TYPE_VIDEO) {
15029 kerby 334
 
335
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
336
                            if (!file_exists($target_path)) {
337
                                mkdir($target_path, 0755);
338
                            }
14743 kerby 339
 
15029 kerby 340
                            $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
14743 kerby 341
 
15029 kerby 342
                            $result = move_uploaded_file($feed_tmp_filename, $full_filename);
343
                            if ($result) {
14743 kerby 344
 
15029 kerby 345
                                $size = $this->config['leaderslinked.image_sizes.feed_image_size'];
346
                                $getFromSecound = 2;
14743 kerby 347
 
15029 kerby 348
                                //extracción del cover
349
                                $generateFileName = substr($feed_filename, 0, strrpos($feed_filename, '.'));
350
                                $generateFile =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.png';
351
                                $cmd            = "/usr/bin/ffmpeg -i $full_filename -an -ss $getFromSecound -s $size $generateFile";
352
                                exec($cmd);
14743 kerby 353
 
354
 
15029 kerby 355
                                $feed->file_type = $file_type;
356
                                $feed->file_name = basename($feed_filename);
357
                                $feed->file_image_preview = basename($generateFile);
358
                                $feedMapper->update($feed);
14743 kerby 359
 
15029 kerby 360
                                $videoConvert = new VideoConvert();
361
                                $videoConvert->filename = $full_filename;
362
                                $videoConvert->type = VideoConvert::TYPE_FEED;
14743 kerby 363
 
15029 kerby 364
                                $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
365
                                $videoConvertMapper->insert($videoConvert);
366
                            } else {
367
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename);
13304 eleazar 368
                            }
369
                        }
14743 kerby 370
 
371
                        if ($file_type != Feed::FILE_TYPE_VIDEO && !empty($img_tmp_filename) && !empty($img_filename)) {
372
 
15029 kerby 373
 
374
                            $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
375
                            if (!file_exists($target_path)) {
376
                                mkdir($target_path, 0755);
377
                            }
14743 kerby 378
 
15029 kerby 379
                            Functions::deleteFilename($target_path, $feed->file_image_preview);
14743 kerby 380
 
15029 kerby 381
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
14743 kerby 382
 
15029 kerby 383
                            $img_filename = substr($img_filename, 0, strrpos($img_filename, '.'))  . '.png';
14743 kerby 384
 
385
 
15029 kerby 386
                            if (Functions::uploadImage($img_tmp_filename, $target_path, $img_filename, $target_width, $target_height)) {
387
                                $feed->file_image_preview = basename($img_filename);
388
                                $feedMapper->update($feed);
389
                            } else {
390
                                error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename);
14743 kerby 391
                            }
392
                        }
393
 
394
 
13304 eleazar 395
                        $response = [
396
                            'success'   => true,
397
                            'data'   => 'success'
398
                        ];
14743 kerby 399
                    } else {
400
                        $messages = [];
401
                        $form_messages = (array) $form->getMessages();
402
                        foreach ($form_messages  as $fieldname => $field_messages) {
403
                            $messages[$fieldname] = array_values($field_messages);
404
                        }
405
                    }
13304 eleazar 406
                } else {
14743 kerby 407
                    $response = [
408
                        'success' => false,
409
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
410
                    ];
13304 eleazar 411
                }
14743 kerby 412
 
413
                return new JsonModel($response);
414
            }
415
    }
416
 
417
    public function deleteAction()
418
    {
419
        $currentUserPlugin = $this->plugin('currentUserPlugin');
420
        $currentUser = $currentUserPlugin->getUser();
421
        $currentCompany = $currentUserPlugin->getCompany();
422
 
423
        $request = $this->getRequest();
424
        if ($request->isPost()) {
425
 
426
 
427
            $id =  $this->params()->fromRoute('id');
428
 
429
            $feedMapper = FeedMapper::getInstance($this->adapter);
430
            $feed = $feedMapper->fetchOneByUuid($id);
431
            if (!$feed) {
432
                $response = [
433
                    'success' => false,
434
                    'data' => 'ERROR_POST_NOT_FOUND'
435
                ];
436
                return new JsonModel($response);
437
            }
438
 
439
            if ($feed->company_id != $currentCompany->id) {
440
                $response = [
441
                    'success' => false,
442
                    'data' => 'ERROR_UNAUTHORIZED'
443
                ];
444
                return new JsonModel($response);
445
            }
446
 
447
            $feedMapper = FeedMapper::getInstance($this->adapter);
448
            $feed->status = Feed::STATUS_DELETED;
449
            if ($feedMapper->update($feed)) {
450
 
451
                $response = [
452
                    'success' => true,
453
                    'data' => 'LABEL_FEED_WAS_DELETED'
454
                ];
13304 eleazar 455
            } else {
456
                $response = [
457
                    'success' => false,
14743 kerby 458
                    'data' => $feedMapper->getError()
13304 eleazar 459
                ];
460
            }
14743 kerby 461
 
462
 
13304 eleazar 463
            return new JsonModel($response);
14743 kerby 464
        } else {
465
            $response = [
466
                'success' => false,
467
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
468
            ];
13304 eleazar 469
        }
14743 kerby 470
 
471
        return new JsonModel($response);
13304 eleazar 472
    }
473
 
14743 kerby 474
    private function normalizeString($str = '')
475
    {
476
        $basename  = substr($str, 0, strrpos($str, '.'));
477
        $basename  = str_replace('.', '-', $basename);
478
 
479
        $extension  = substr($str, strrpos($str, '.'));
480
 
15043 kerby 481
        $str = $basename . $extension;
14743 kerby 482
 
15043 kerby 483
        return Functions::normalizeString($str);
14743 kerby 484
    }
485
 
486
    public function aprovedAction()
487
    {
13304 eleazar 488
        $currentUserPlugin = $this->plugin('currentUserPlugin');
489
        $currentUser = $currentUserPlugin->getUser();
490
        $currentCompany = $currentUserPlugin->getCompany();
14743 kerby 491
 
13304 eleazar 492
        $request = $this->getRequest();
14743 kerby 493
        if ($request->isPost()) {
13304 eleazar 494
 
495
            $id =  $this->params()->fromRoute('id');
14743 kerby 496
 
13304 eleazar 497
            $feedMapper = FeedMapper::getInstance($this->adapter);
498
            $feed = $feedMapper->fetchOneByUuid($id);
14743 kerby 499
            if (!$feed) {
13304 eleazar 500
                $response = [
501
                    'success' => false,
502
                    'data' => 'ERROR_POST_NOT_FOUND'
503
                ];
504
                return new JsonModel($response);
505
            }
14743 kerby 506
 
507
            if ($feed->company_id != $currentCompany->id) {
13304 eleazar 508
                $response = [
509
                    'success' => false,
510
                    'data' => 'ERROR_UNAUTHORIZED'
511
                ];
512
                return new JsonModel($response);
513
            }
14743 kerby 514
 
13304 eleazar 515
            $feedMapper = FeedMapper::getInstance($this->adapter);
14743 kerby 516
            $feed->status = Feed::STATUS_PUBLISHED;
517
            if ($feedMapper->update($feed)) {
13304 eleazar 518
                $response = [
519
                    'success' => true,
14743 kerby 520
                    'data' => 'LABEL_FEED_WAS_ADDED'
13304 eleazar 521
                ];
522
            } else {
523
                $response = [
524
                    'success' => false,
525
                    'data' => $feedMapper->getError()
526
                ];
527
            }
528
 
14743 kerby 529
 
13304 eleazar 530
            return new JsonModel($response);
531
        } else {
532
            $response = [
533
                'success' => false,
534
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
535
            ];
536
        }
14743 kerby 537
 
13304 eleazar 538
        return new JsonModel($response);
539
    }
14743 kerby 540
 
541
    public function editAction()
542
    {
543
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
544
        $currentCompany     = $currentUserPlugin->getCompany();
545
        $currentUser        = $currentUserPlugin->getUser();
546
        $request    = $this->getRequest();
547
 
548
 
15028 efrain 549
        $uuid = $this->params()->fromRoute('id');
550
        if (!$uuid) {
551
            $data = [
552
                'success' => false,
553
                'data' => 'ERROR_INVALID_PARAMETER'
554
            ];
14743 kerby 555
 
15028 efrain 556
            return new JsonModel($data);
557
        }
14743 kerby 558
 
15028 efrain 559
        $feedMapper = FeedMapper::getInstance($this->adapter);
560
        $feed = $feedMapper->fetchOneByUuid($uuid);
14743 kerby 561
 
562
 
15028 efrain 563
        if (!$feed) {
564
            $data = [
565
                'success' => false,
566
                'data' => 'ERROR_RECORD_NOT_FOUND'
567
             ];
568
 
569
            return new JsonModel($data);
570
        }
571
 
572
        if ($currentCompany) {
573
            if ($feed->company_id != $currentCompany->id) {
14743 kerby 574
                $data = [
575
                    'success' => false,
15028 efrain 576
                    'data' => 'ERROR_UNAUTHORIZED'
14743 kerby 577
                ];
578
 
579
                return new JsonModel($data);
580
            }
15028 efrain 581
        } else {
582
            if ($feed->company_id) {
583
                $data = [
584
                    'success' => false,
585
                    'data' => 'ERROR_UNAUTHORIZED'
586
                ];
14743 kerby 587
 
15028 efrain 588
                return new JsonModel($data);
14743 kerby 589
            }
15028 efrain 590
        }
591
 
592
        if($request->isGet()) {
593
 
594
            $topicMapper = TopicMapper::getInstance($this->adapter);
595
            $topic = $topicMapper->fetchOne($feed->topic_id);
596
            $preview = ($feed->file_image_preview != null && $feed->file_image_preview != '') ? $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_image_preview]) : null;
597
            return new JsonModel([
598
                'success' => true,
599
                'data' => [
600
                    'feed' => [
601
                        'uuid' => $feed->uuid,
602
                        'title' => $feed->title,
603
                        'added_on' => $feed->added_on,
604
                        'description' => $feed->description,
605
                        'file' => $feed->file_type ? $feed->file_type : '',
606
                        'link_media' => $feed->link_media ? $feed->link_media : '',
607
                        'link_name' => $feed->link_name,
608
                        'file_name' => $feed->file_name ? $feed->file_name : '',
15039 kerby 609
                        'link_file' => ($feed->file_name != null && $feed->file_name != '') ? $this->url()->fromRoute('storage', ['code' => $feed->uuid, 'type' => 'feed', 'filename' => $feed->file_name]) : null,
15028 efrain 610
                        'status' => $feed->status,
611
                    ],
15039 kerby 612
                    'topic' => [
613
                       'uuid'=> $topic->uuid
614
                    ],
15028 efrain 615
                    'action' => $this->url()->fromRoute('development-and-content/edit', ['id' => $feed->uuid]),
616
                    'preview' => $preview
617
                ]
618
            ]);
619
 
620
        } else  if ($request->isPost()) {
14743 kerby 621
 
622
 
14880 kerby 623
                $form = new DevelopmentContentCreateFeedForm($this->adapter);
14743 kerby 624
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
14829 kerby 625
                $categoryUuid = $dataPost['category_content'];
14743 kerby 626
                $dataPost['category_id'] = null;
627
                $form->setData($dataPost);
628
 
629
                if ($form->isValid()) {
630
                    $dataPost = (array) $form->getData();
14835 kerby 631
                    $filesArray = $this->getRequest()->getFiles()->toArray();
14743 kerby 632
                    $files = null;
633
 
14835 kerby 634
                    if (isset($filesArray['doc']) && empty($filesArray['doc']['error'])) {
635
                        $files = $filesArray['doc'];
14743 kerby 636
                    }
14835 kerby 637
                    if (isset($filesArray['video']) && empty($filesArray['video']['error'])) {
638
                        $files = $filesArray['video'];
14743 kerby 639
                    }
640
 
641
                    $file_type = '';
642
                    if (isset($files) && empty($files['error'])) {
643
                        $feed_tmp_filename  = $files['tmp_name'];
644
                        $feed_filename      = $this->normalizeString($files['name']);
645
 
646
                        $mime_type = mime_content_type($feed_tmp_filename);
647
                        if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
648
                            $file_type = Feed::FILE_TYPE_IMAGE;
649
                        } else if ($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
650
                            $file_type = Feed::FILE_TYPE_VIDEO;
651
                        } else if ($mime_type == 'application/pdf') {
652
                            $file_type = Feed::FILE_TYPE_DOCUMENT;
653
                        }
654
                    }
655
                    //preview
14834 kerby 656
                    $fileImg = $this->params()->fromFiles('image');
14743 kerby 657
                    if (isset($fileImg) && empty($fileImg['error'])) {
658
 
659
                        $img_tmp_filename  = $fileImg['tmp_name'];
660
                        $img_filename  = $this->normalizeString($fileImg['name']);
661
                    } else {
662
                        $img_tmp_filename = null;
663
                        $img_filename = null;
664
                    }
665
 
666
                    $feedMapper = FeedMapper::getInstance($this->adapter);
667
                    $feed = $feedMapper->fetchOne($feed->id);
668
 
14946 kerby 669
                    $hydrator = new ObjectPropertyHydrator();
670
                    $hydrator->hydrate($dataPost, $feed);
671
 
14743 kerby 672
                    $categoriesMapper = TopicMapper::getInstance($this->adapter);
673
                    $category = $categoriesMapper->fetchOneByUuidOrTitle($categoryUuid);
674
 
675
                    $feed->topic_id = $category->id;
676
                    $feed->status = Feed::STATUS_SAVED;
14946 kerby 677
 
14743 kerby 678
                    if ($feedMapper->update($feed)) {
679
 
680
                        if ($file_type == Feed::FILE_TYPE_DOCUMENT) {
681
 
682
                                $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
683
 
684
                                if (!file_exists($target_path)) {
685
                                    mkdir($target_path, 0755);
686
                                }
687
 
688
                                Functions::deleteFilename($target_path, $feed->file_name);
689
 
690
                                $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
691
                                $result = move_uploaded_file($feed_tmp_filename, $full_filename);
692
                                if ($result) {
693
 
694
                                    $feed->file_type = $file_type;
695
                                    $feed->file_name = basename($feed_filename);
696
                                    $feedMapper->update($feed);
697
                                } else {
698
                                    error_log('no se pudo mover o renombrar el documento : ' . $feed_tmp_filename . ' al directorio : ' . $full_filename);
699
                                }
700
                        }
701
                        if ($file_type == Feed::FILE_TYPE_IMAGE) {
15029 kerby 702
 
14743 kerby 703
                                $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
704
                                if (!file_exists($target_path)) {
705
                                    mkdir($target_path, 0755);
706
                                }
707
 
708
                                Functions::deleteFilename($target_path, $feed->file_name);
709
 
710
                                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
711
 
712
                                $feed_filename = substr($feed_filename, 0, strrpos($feed_filename, '.'))  . '.png';
713
                                $crop_to_dimensions = false;
714
                                if (Image::uploadImage($feed_tmp_filename, $target_path, $feed_filename, $target_width, $target_height, $crop_to_dimensions)) {
715
                                    $feed->file_type = $file_type;
716
                                    $feed->file_name = basename($feed_filename);
717
                                    $feedMapper->update($feed);
718
                                }
719
                        }
720
                        if ($file_type == Feed::FILE_TYPE_VIDEO) {
15029 kerby 721
 
14743 kerby 722
                                $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
723
                                if (!file_exists($target_path)) {
724
                                    mkdir($target_path, 0755);
725
                                }
726
 
727
 
728
                                Functions::deleteFilename($target_path, $feed->file_name);
729
 
730
                                Functions::deleteFilename($target_path, $feed->file_image_preview);
731
 
732
                                $full_filename = $target_path  . DIRECTORY_SEPARATOR . $feed_filename;
733
 
734
                                $result = move_uploaded_file($feed_tmp_filename, $full_filename);
735
                                if ($result) {
736
 
737
                                    $size = $this->config['leaderslinked.image_sizes.feed_image_size'];
738
                                    $getFromSecound = 2;
739
 
740
                                    //extracción del cover
741
                                    $generateFileName = substr($feed_filename, 0, strrpos($feed_filename, '.'));
742
                                    $generateFile =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.png';
743
                                    $cmd            = "/usr/bin/ffmpeg -i $full_filename -an -ss $getFromSecound -s $size $generateFile";
744
                                    exec($cmd);
745
 
746
 
747
                                    $feed->file_type = $file_type;
748
                                    $feed->file_name = basename($feed_filename);
749
                                    $feed->file_image_preview = basename($generateFile);
750
                                    $feedMapper->update($feed);
751
 
752
                                    $videoConvert = new VideoConvert();
753
                                    $videoConvert->filename = $full_filename;
754
                                    $videoConvert->type = VideoConvert::TYPE_FEED;
755
 
756
                                    $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
15029 kerby 757
                                    $videoConvertMapper->insert($videoConvert);}
14743 kerby 758
                        }
759
                        if ($file_type != Feed::FILE_TYPE_VIDEO && !empty($img_tmp_filename) && !empty($img_filename)) {
760
 
761
                                $target_path = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid;
762
                                if (!file_exists($target_path)) {
763
                                    mkdir($target_path, 0755);
764
                                }
765
 
766
                                Functions::deleteFilename($target_path, $feed->file_image_preview);
767
 
768
                                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.feed_image_size']);
769
 
770
                                $img_filename = substr($img_filename, 0, strrpos($img_filename, '.'))  . '.png';
771
 
772
 
773
                                if (Functions::uploadImage($img_tmp_filename, $target_path, $img_filename, $target_width, $target_height)) {
774
                                    $feed->file_image_preview = basename($img_filename);
775
                                    $feedMapper->update($feed);
776
                                }
777
                        }
778
 
779
                        $response = [
780
                            'success'   => true,
781
                            'data'   => 'success'
782
                        ];
783
                    } else {
784
                        $messages = [];
785
                        $form_messages = (array) $form->getMessages();
786
                        foreach ($form_messages  as $fieldname => $field_messages) {
787
                            $messages[$fieldname] = array_values($field_messages);
788
                        }
789
                        $response = [
790
                            'success' => false,
791
                            'data' => $messages
792
                        ];
793
                    }
794
                } else {
795
                    $response = [
796
                        'success' => false,
797
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
798
                    ];
799
                }
800
 
801
                return new JsonModel($response);
802
            }
15028 efrain 803
 
14743 kerby 804
    }
805
 
806
 
807
}