Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
 
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
15
use LeadersLinked\Library\Image;
16
 
17
 
18
use LeadersLinked\Mapper\CompanyMapper;
19
use LeadersLinked\Mapper\CompanyMicrolearningTopicMapper;
20
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleMapper;
21
use LeadersLinked\Mapper\CompanyMicrolearningSlideMapper;
22
use LeadersLinked\Model\CompanyMicrolearningSlide;
23
use LeadersLinked\Model\VideoConvert;
24
use LeadersLinked\Mapper\VideoConvertMapper;
25
use LeadersLinked\Form\SlideForm;
26
use LeadersLinked\Form\SlideTextAddForm;
27
use LeadersLinked\Form\SlideTextEditForm;
28
use LeadersLinked\Form\SlideImageAddForm;
29
use LeadersLinked\Form\SlideImageEditForm;
30
use LeadersLinked\Form\SlideVideoAddForm;
31
use LeadersLinked\Form\SlideDocumentAddForm;
32
use LeadersLinked\Form\SlideDocumentEditForm;
33
use LeadersLinked\Form\SlideAudioAddForm;
34
use LeadersLinked\Form\SlideAudioEditForm;
35
use LeadersLinked\Form\SlideQuizzAddForm;
36
use LeadersLinked\Form\SlideQuizzEditForm;
37
use LeadersLinked\Form\SlideVideoEditForm;
38
 
39
 
40
class MicroLearningSlideController extends AbstractActionController
41
{
42
    /**
43
     *
44
     * @var AdapterInterface
45
     */
46
    private $adapter;
47
 
48
 
49
    /**
50
     *
51
     * @var AbstractAdapter
52
     */
53
    private $cache;
54
 
55
    /**
56
     *
57
     * @var  LoggerInterface
58
     */
59
    private $logger;
60
 
61
 
62
    /**
63
     *
64
     * @var array
65
     */
66
    private $config;
67
 
68
    /**
69
     *
70
     * @param AdapterInterface $adapter
71
     * @param AbstractAdapter $cache
72
     * @param LoggerInterface $logger
73
     * @param array $config
74
     */
75
    public function __construct($adapter, $cache , $logger,  $config)
76
    {
77
        $this->adapter      = $adapter;
78
        $this->cache        = $cache;
79
        $this->logger       = $logger;
80
        $this->config       = $config;
81
 
82
    }
83
 
84
    /**
85
     *
86
     * Generación del listado de perfiles
87
     * {@inheritDoc}
88
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
89
     */
90
    public function indexAction()
91
    {
92
        $currentUserPlugin = $this->plugin('currentUserPlugin');
93
        $currentUser = $currentUserPlugin->getUser();
94
        $currentCompany = $currentUserPlugin->getCompany();
95
 
96
        $request = $this->getRequest();
97
        if($request->isGet()) {
98
 
99
 
100
            $headers  = $request->getHeaders();
101
 
102
            $isJson = false;
103
            if($headers->has('Accept')) {
104
                $accept = $headers->get('Accept');
105
 
106
                $prioritized = $accept->getPrioritized();
107
 
108
                foreach($prioritized as $key => $value) {
109
                    $raw = trim($value->getRaw());
110
 
111
                    if(!$isJson) {
112
                        $isJson = strpos($raw, 'json');
113
                    }
114
 
115
                }
116
            }
117
 
118
            if($isJson) {
119
 
120
 
121
                $topic_uuid     = filter_var($this->params()->fromQuery('topic_uuid'), FILTER_SANITIZE_STRING);
122
                $capsule_uuid   = filter_var($this->params()->fromQuery('capsule_uuid'), FILTER_SANITIZE_STRING);
123
 
124
                $data = [
125
                    'link_add' => '',
126
                    'items' => [] ,
127
                    'total' => 0,
128
 
129
                ];
130
 
131
 
132
                if(!$topic_uuid) {
133
                    return new JsonModel([
134
                        'success' => true,
135
                        'data' => $data
136
                    ]);
137
 
138
                }
139
 
140
 
141
                $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
142
                $topic = $topicMapper->fetchOneByUuid($topic_uuid);
143
                if(!$topic) {
144
                    return new JsonModel([
145
                        'success' => true,
146
                        'data' => 'ERROR_TOPIC_NOT_FOUND'
147
                    ]);
148
                }
149
 
150
                if($topic->company_id != $currentCompany->id) {
151
                    return new JsonModel([
152
                        'success' => true,
153
                        'data' => 'ERROR_UNAUTHORIZED'
154
                    ]);
155
                }
156
 
157
 
158
 
159
                if(!$capsule_uuid) {
160
                    $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
161
                    $records = $capsuleMapper->fetchAllByCompanyIdAndTopicId($topic->company_id, $topic->id);
162
 
163
                    $capsules = [];
164
                    foreach($records as $record)
165
                    {
166
                        if(!$capsule_uuid) {
167
                            $capsule_uuid = $record->uuid;
168
                        }
169
 
170
                        $capsules[ $record->uuid ] = $record->name;
171
                    }
172
 
173
                    $data['capsules']  = $capsules;
174
                }
175
 
176
                if(!$capsule_uuid) {
177
                    return new JsonModel([
178
                        'success' => true,
179
                        'data' => $data
180
                    ]);
181
 
182
                }
183
 
184
                $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
185
                $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
186
 
187
 
188
 
189
                if(!$capsule) {
190
                    return new JsonModel([
191
                        'success' => true,
192
                        'data' => 'ERROR_CAPSULE_NOT_FOUND'
193
                    ]);
194
                }
195
 
196
                if($capsule->topic_id != $topic->id) {
197
                    return new JsonModel([
198
                        'success' => true,
199
                        'data' => 'ERROR_UNAUTHORIZED'
200
                    ]);
201
                }
202
 
203
                $search = $this->params()->fromQuery('search', []);
204
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
205
 
206
                $page               = intval($this->params()->fromQuery('start', 1), 10);
207
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
208
                $order =  $this->params()->fromQuery('order', []);
209
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
210
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
211
 
212
                $fields =  ['name'];
213
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
214
 
215
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
216
                    $order_direction = 'ASC';
217
                }
218
 
219
 
220
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
221
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/slides/add');
222
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/slides/edit');
223
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/slides/delete');
224
 
225
                $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
226
                $paginator = $slideMapper->fetchAllDataTableByCompanyIdAndTopicIdAndCapsuleId($currentCompany->id, $topic->id, $capsule->id,  $search, $page, $records_x_page, $order_field, $order_direction);
227
 
228
                $records = $paginator->getCurrentItems();
229
 
230
 
231
                $items = [];
232
                foreach($records as $record)
233
                {
234
 
235
                    $params = [
236
                        'topic_uuid'    => $topic->uuid,
237
                        'capsule_uuid'  => $capsule->uuid,
238
                        'slide_uuid'    => $record->uuid,
239
 
240
                    ];
241
 
242
 
243
                    $item = [
244
 
245
                        'name' => $record->name,
246
                        'details' => [
247
                            'type' => '',
248
                         ],
249
                         'media' => [
250
                            'image' => '',
251
                            'audio' => '',
252
                            'video' => '',
253
                            'document' => '',
254
                            'text'
255
                        ],
256
 
257
                        'actions' => [
258
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('microlearning/content/slides/edit', $params)  : '',
259
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('microlearning/content/slides/delete', $params )  : '',
260
                         ],
261
 
262
                    ];
263
 
264
 
265
                    switch($record->type)
266
                    {
267
                        case CompanyMicrolearningSlide::TYPE_AUDIO :
268
 
269
                            $item['media']['audio'] =  $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
270
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
271
                            $item['details']['type'] = 'LABEL_AUDIO';
272
                            break;
273
 
274
                        case CompanyMicrolearningSlide::TYPE_VIDEO :
275
                            $item['media']['video'] =  $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
276
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
277
                            $item['details']['type'] = 'LABEL_VIDEO';
278
                            break;
279
 
280
                        case CompanyMicrolearningSlide::TYPE_DOCUMENT :
281
                            $item['media']['document'] =  $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
282
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
283
                            $item['details']['type'] = 'LABEL_DOCUMENT';
284
                            break;
285
 
286
                        case CompanyMicrolearningSlide::TYPE_TEXT :
287
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
288
                            $item['media']['text'] = $this->url()->fromRoute('microlearning/content/slides/text', $params);
289
                            $item['details']['type'] = 'LABEL_TEXT';
290
                            break;
291
 
292
                        case CompanyMicrolearningSlide::TYPE_QUIZ :
293
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
294
                            $item['details']['type'] = 'LABEL_QUIZ';
295
                            break;
296
 
297
                        case CompanyMicrolearningSlide::TYPE_IMAGE :
298
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
299
                            $item['details']['type'] = 'LABEL_IMAGE';
300
                            break;
301
 
302
                    }
303
 
304
 
305
                    array_push($items, $item);
306
 
307
                }
308
 
309
                if($allowAdd && $topic && $capsule) {
310
                    $data['link_add'] = $this->url()->fromRoute('microlearning/content/slides/add', ['topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid ]);
311
                }
312
                $data['items'] = $items;
313
                $data['total'] = $paginator->getTotalItemCount();
314
 
315
                return new JsonModel([
316
                    'success' => true,
317
                    'data' => $data
318
                ]);
319
 
320
            } else {
321
                $image_size = $this->config['leaderslinked.image_sizes.microlearning_image_upload'];
322
 
323
                $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
324
                $topics = $topicMapper->fetchAllByCompanyId($currentCompany->id);
325
 
326
                if($topics) {
327
                    $topic_id = $topics[0]->id;
328
                }  else {
329
                    $topic_id = 0;
330
                }
331
 
332
 
333
                $form = new SlideForm($this->adapter,  $currentCompany->id, $topic_id);
334
                $formTextAdd = new SlideTextAddForm();
335
                $formTextEdit = new SlideTextEditForm();
336
                $formImageAdd = new SlideImageAddForm();
337
                $formImageEdit = new SlideImageEditForm();
338
                $formVideoAdd = new SlideVideoAddForm();
339
                $formVideoEdit = new SlideVideoEditForm();
340
                $formDocumentAdd = new SlideDocumentAddForm();
341
                $formDocumentEdit = new SlideDocumentEditForm();
342
                $formAudioAdd = new SlideAudioAddForm();
343
                $formAudioEdit = new SlideAudioEditForm();
344
                $formQuizzAdd = new SlideQuizzAddForm($this->adapter, $currentCompany->id);
345
                $formQuizzEdit = new SlideQuizzEditForm($this->adapter, $currentCompany->id);
346
 
347
                $this->layout()->setTemplate('layout/layout-backend.phtml');
348
                $viewModel = new ViewModel();
349
                $viewModel->setTemplate('leaders-linked/microlearning-slides/index.phtml');
350
                $viewModel->setVariables([
351
                    'form' => $form,
352
                    'formTextAdd' => $formTextAdd,
353
                    'formTextEdit' => $formTextEdit,
354
                    'formImageAdd' => $formImageAdd,
355
                    'formImageEdit' => $formImageEdit,
356
                    'formVideoAdd' => $formVideoAdd,
357
                    'formVideoEdit' => $formVideoEdit,
358
                    'formDocumentAdd' => $formDocumentAdd,
359
                    'formDocumentEdit' => $formDocumentEdit,
360
                    'formAudioAdd' => $formAudioAdd,
361
                    'formAudioEdit' => $formAudioEdit,
362
                    'formQuizzAdd' => $formQuizzAdd,
363
                    'formQuizzEdit' => $formQuizzEdit,
364
                    'image_size' => $image_size,
365
 
366
                ]);
367
                return $viewModel ;
368
            }
369
        }
370
 
371
        return new JsonModel([
372
            'success' => false,
373
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
374
        ]);
375
 
376
 
377
 
378
 
379
 
380
 
381
 
382
 
383
        $topic_id   = $this->params()->fromRoute('topic_id');
384
        $capsule_id = $this->params()->fromRoute('capsule_id');
385
 
386
        $currentUserPlugin = $this->plugin('currentUserPlugin');
387
        $currentUser = $currentUserPlugin->getUser();
388
 
389
        $companyMapper = CompanyMapper::getInstance($this->adapter);
390
        $company = $companyMapper->fetchOneByUuid($id);
391
        if(!$company) {
392
            $flashMessenger->addErrorMessage('ERROR_COMPANY_NOT_FOUND');
393
            return $this->redirect()->toRoute('dashboard');
394
        }
395
 
396
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
397
        $topic = $topicMapper->fetchOneByUuid($topic_id);
398
        if(!$topic) {
399
            $flashMessenger->addErrorMessage('ERROR_TOPIC_NOT_FOUND');
400
            return $this->redirect()->toRoute('dashboard');
401
        }
402
 
403
        if($topic->company_id != $company->id) {
404
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
405
            return $this->redirect()->toRoute('dashboard');
406
        }
407
 
408
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
409
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
410
        if(!$capsule) {
411
            $flashMessenger->addErrorMessage('ERROR_CAPSULE_NOT_FOUND');
412
            return $this->redirect()->toRoute('dashboard');
413
        }
414
 
415
        if($capsule->topic_id != $topic->id) {
416
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
417
            return $this->redirect()->toRoute('dashboard');
418
        }
419
 
420
        $request = $this->getRequest();
421
        if($request->isGet()) {
422
 
423
 
424
            $headers  = $request->getHeaders();
425
 
426
            $isJson = false;
427
            if($headers->has('Accept')) {
428
                $accept = $headers->get('Accept');
429
 
430
                $prioritized = $accept->getPrioritized();
431
 
432
                foreach($prioritized as $key => $value) {
433
                    $raw = trim($value->getRaw());
434
 
435
                    if(!$isJson) {
436
                        $isJson = strpos($raw, 'json');
437
                    }
438
 
439
                }
440
            }
441
 
442
            if($isJson) {
443
 
444
 
445
 
446
            } else {
447
 
448
 
449
            }
450
 
451
        } else {
452
            return new JsonModel([
453
                'success' => false,
454
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
455
            ]);
456
        }
457
    }
458
 
459
    public function addAction()
460
    {
461
 
462
        $currentUserPlugin = $this->plugin('currentUserPlugin');
463
        $currentUser = $currentUserPlugin->getUser();
464
        $currentCompany = $currentUserPlugin->getCompany();
465
 
466
        $request    = $this->getRequest();
467
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
468
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
469
 
470
 
471
 
472
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
473
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
474
        if(!$topic) {
475
            return new JsonModel([
476
                'success'   => false,
477
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
478
            ]);
479
        }
480
 
481
        if($topic->company_id != $currentCompany->id) {
482
            return new JsonModel([
483
                'success'   => false,
484
                'data'   => 'ERROR_UNAUTHORIZED'
485
            ]);
486
        }
487
 
488
 
489
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
490
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
491
        if(!$capsule) {
492
            return new JsonModel([
493
                'success'   => false,
494
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
495
            ]);
496
        }
497
 
498
        if($capsule->topic_id != $topic->id) {
499
            return new JsonModel([
500
                'success'   => false,
501
                'data'   => 'ERROR_UNAUTHORIZED'
502
            ]);
503
        }
504
 
505
 
506
        if($request->isPost()) {
507
 
508
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
509
 
510
            switch($dataPost['type'])
511
            {
512
                case CompanyMicrolearningSlide::TYPE_AUDIO :
513
                    $form = new SlideAudioAddForm();
514
                    break;
515
 
516
                case CompanyMicrolearningSlide::TYPE_VIDEO :
517
                    $form = new SlideVideoAddForm();
518
                    break;
519
 
520
                case CompanyMicrolearningSlide::TYPE_DOCUMENT:
521
                    $form = new SlideDocumentAddForm();
522
                    break;
523
 
524
                case CompanyMicrolearningSlide::TYPE_IMAGE :
525
                    $form = new SlideImageAddForm();
526
                    break;
527
 
528
 
529
                case CompanyMicrolearningSlide::TYPE_QUIZ:
530
                    $form = new SlideQuizzAddForm($this->adapter, $currentCompany->id);
531
                    break;
532
 
533
                default :
534
                    $form = new SlideTextAddForm();
535
                    break;
536
            }
537
 
538
            $form->setData($dataPost);
539
 
540
            if($form->isValid()) {
541
                $dataPost = (array) $form->getData();
542
 
543
                $hydrator = new ObjectPropertyHydrator();
544
                $slide = new CompanyMicrolearningSlide();
545
                $hydrator->hydrate($dataPost, $slide);
546
 
547
                $slide->company_id = $topic->company_id;
548
                $slide->topic_id = $topic->id;
549
                $slide->capsule_id = $capsule->id;
550
                $slide->file = null;
551
                $slide->background = null;
552
 
553
                $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
554
                if($slideMapper->insert($slide)) {
555
                    $slide = $slideMapper->fetchOne($slide->id);
556
 
557
                    $files = $this->getRequest()->getFiles()->toArray();
558
 
559
                    $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
560
                    if(!file_exists($target_path)) {
561
                        mkdir($target_path, 0755, true);
562
                    }
563
 
564
 
565
                    if(isset($files['file']) && empty($files['file']['error'])) {
566
                        $tmp_filename       = $files['file']['tmp_name'];
567
                        $original_filename  = trim(strtolower($files['file']['name']));
568
 
569
 
570
 
571
 
572
 
573
                        if($slide->file) {
574
                            if(!image ::delete($target_path, $slide->file)) {
575
                                return new JsonModel([
576
                                    'success'   => false,
577
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
578
                                ]);
579
                            }
580
                        }
581
 
582
                        if($slide->type == CompanyMicrolearningSlide::TYPE_DOCUMENT) {
583
                            try {
584
 
585
                                $parts = explode('.', $original_filename);
586
                                $filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
587
 
588
                                $full_filename = $target_path  . DIRECTORY_SEPARATOR .$filename;
589
                                if(move_uploaded_file($tmp_filename, $full_filename)) {
590
                                    $slide->file = basename($full_filename);
591
                                    $slideMapper->update($slide);
592
                                }
593
                            } catch(\Throwable $e) {
594
                                error_log($e->getTraceAsString());
595
                            }
596
                        }
597
 
598
                        if($slide->type == CompanyMicrolearningSlide::TYPE_AUDIO) {
599
                            try {
600
 
601
                                $parts = explode('.', $original_filename);
602
                                $filename = 'audio-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
603
 
604
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
605
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
606
 
607
 
608
                                    $generateFileName   = substr($filename, 0, strrpos($filename, '.'));
609
                                    $generateFile       =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.mp3';
610
                                    $cmd                = "/usr/bin/ffmpeg -i $full_filename -b:a 320000 $generateFile";
611
                                    exec($cmd);
612
 
613
                                    $slide->file = basename($full_filename);
614
                                    $slideMapper->update($slide);
615
                                }
616
 
617
                            } catch(\Throwable $e) {
618
                                error_log($e->getTraceAsString());
619
                            }
620
                        }
621
 
622
                        if($slide->type == CompanyMicrolearningSlide::TYPE_VIDEO) {
623
                            try {
624
                                $parts = explode('.', $original_filename);
625
                                $filename = 'video-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
626
 
627
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
628
 
629
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
630
 
631
                                    $videoConvert = new VideoConvert();
632
                                    $videoConvert->filename = $full_filename;
633
                                    $videoConvert->type = VideoConvert::TYPE_MICRO_LEARNING;
634
 
635
                                    $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
636
                                    $videoConvertMapper->insert($videoConvert);
637
 
638
                                    $slide->file = basename($full_filename);
639
                                    $slideMapper->update($slide);
640
                                }
641
 
642
                            } catch(\Throwable $e) {
643
                                echo $e->getMessage();
644
 
645
                                error_log($e->getTraceAsString());
646
                            }
647
                        }
648
 
649
                        if($slide->type == CompanyMicrolearningSlide::TYPE_IMAGE) {
650
                            try {
651
                                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
652
 
653
                                $filename = 'slide-' .uniqid() . '.png';
654
                                $crop_to_dimensions = true;
655
                                if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
656
                                    $slide->file = $filename;
657
                                    $slideMapper->update($slide);
658
                                }
659
                            } catch(\Throwable $e) {
660
                                error_log($e->getTraceAsString());
661
                            }
662
                        }
663
                    }
664
 
665
                    if(isset($files['background']) && empty($files['background']['error'])) {
666
                        if($slide->background) {
667
                            if(!image ::delete($target_path, $slide->background)) {
668
                                return new JsonModel([
669
                                    'success'   => false,
670
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
671
                                ]);
672
                            }
673
                        }
674
 
675
                        $tmp_filename  = $files['background']['tmp_name'];
676
 
677
                        try {
678
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
679
 
680
                            $filename = 'background-' .uniqid() . '.png';
681
                            $crop_to_dimensions = true;
682
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
683
                                $slide->background = $filename;
684
                                $slideMapper->update($slide);
685
                            }
686
                        } catch(\Throwable $e) {
687
                            error_log($e->getTraceAsString());
688
                        }
689
                    }
690
 
691
                    $this->logger->info('Se agrego la diapositiva ' . $slide->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
692
 
693
                    $data = [
694
                        'success'   => true,
695
                        'data'   => 'LABEL_RECORD_ADDED'
696
                    ];
697
                } else {
698
                    $data = [
699
                        'success'   => false,
700
                        'data'      => $topicMapper->getError()
701
                    ];
702
 
703
                }
704
 
705
                return new JsonModel($data);
706
 
707
            } else {
708
                $messages = [];
709
                $form_messages = (array) $form->getMessages();
710
                foreach($form_messages  as $fieldname => $field_messages)
711
                {
712
 
713
                    $messages[$fieldname] = array_values($field_messages);
714
                }
715
 
716
                return new JsonModel([
717
                    'success'   => false,
718
                    'data'   => $messages
719
                ]);
720
            }
721
 
722
        } else {
723
            $data = [
724
                'success' => false,
725
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
726
            ];
727
 
728
            return new JsonModel($data);
729
        }
730
 
731
        return new JsonModel($data);
732
    }
733
 
734
    /**
735
     *
736
     * Borrar un perfil excepto el público
737
     * @return \Laminas\View\Model\JsonModel
738
     */
739
    public function deleteAction()
740
    {
741
        $currentUserPlugin = $this->plugin('currentUserPlugin');
742
        $currentUser = $currentUserPlugin->getUser();
743
        $currentCompany = $currentUserPlugin->getCompany();
744
 
745
        $request    = $this->getRequest();
746
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
747
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
748
        $slide_uuid = $this->params()->fromRoute('slide_uuid');
749
 
750
 
751
 
752
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
753
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
754
        if(!$topic) {
755
            return new JsonModel([
756
                'success'   => false,
757
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
758
            ]);
759
        }
760
 
761
        if($topic->company_id != $currentCompany->id) {
762
            return new JsonModel([
763
                'success'   => false,
764
                'data'   => 'ERROR_UNAUTHORIZED'
765
            ]);
766
        }
767
 
768
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
769
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
770
        if(!$capsule) {
771
            return new JsonModel([
772
                'success'   => false,
773
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
774
            ]);
775
        }
776
 
777
        if($capsule->topic_id != $topic->id) {
778
            return new JsonModel([
779
                'success'   => false,
780
                'data'   => 'ERROR_UNAUTHORIZED'
781
            ]);
782
        }
783
 
784
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
785
        $slide = $slideMapper->fetchOneByUuid($slide_uuid);
786
        if(!$slide) {
787
            return new JsonModel([
788
                'success'   => false,
789
                'data'   => 'ERROR_SLIDE_NOT_FOUND'
790
            ]);
791
        }
792
 
793
        if($slide->capsule_id != $capsule->id) {
794
            return new JsonModel([
795
                'success'   => false,
796
                'data'   => 'ERROR_UNAUTHORIZED'
797
            ]);
798
        }
799
 
800
        if($request->isPost()) {
801
 
802
            $result =  $slideMapper->delete($slide);
803
            if($result) {
804
                $this->logger->info('Se borro la diapositiva : ' .  $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
805
                try {
806
                    $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] . $slide->uuid;
807
                    if(file_exists($target_path)) {
808
                        Functions::rmDirRecursive($target_path);
809
                    }
810
                } catch(\Throwable $e) {
811
                    error_log($e->getTraceAsString());
812
                }
813
 
814
 
815
                $data = [
816
                    'success' => true,
817
                    'data' => 'LABEL_RECORD_DELETED'
818
                ];
819
            } else {
820
 
821
                $data = [
822
                    'success'   => false,
823
                    'data'      => $capsuleMapper->getError()
824
                ];
825
 
826
                return new JsonModel($data);
827
            }
828
 
829
        } else {
830
            $data = [
831
                'success' => false,
832
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
833
            ];
834
 
835
            return new JsonModel($data);
836
        }
837
 
838
        return new JsonModel($data);
839
    }
840
 
841
 
842
    public function editAction()
843
    {
844
        $currentUserPlugin = $this->plugin('currentUserPlugin');
845
        $currentUser = $currentUserPlugin->getUser();
846
        $currentCompany = $currentUserPlugin->getCompany();
847
 
848
        $request    = $this->getRequest();
849
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
850
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
851
        $slide_uuid   = $this->params()->fromRoute('slide_uuid');
852
 
853
 
854
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
855
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
856
        if(!$topic) {
857
            return new JsonModel([
858
                'success'   => false,
859
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
860
            ]);
861
        }
862
 
863
        if($topic->company_id != $currentCompany->id) {
864
            return new JsonModel([
865
                'success'   => false,
866
                'data'   => 'ERROR_UNAUTHORIZED'
867
            ]);
868
        }
869
 
870
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
871
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
872
        if(!$capsule) {
873
            return new JsonModel([
874
                'success'   => false,
875
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
876
            ]);
877
        }
878
 
879
        if($capsule->topic_id != $topic->id) {
880
            return new JsonModel([
881
                'success'   => false,
882
                'data'   => 'ERROR_UNAUTHORIZED'
883
            ]);
884
        }
885
 
886
 
887
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
888
        $slide = $slideMapper->fetchOneByUuid($slide_uuid);
889
        if(!$slide) {
890
            return new JsonModel([
891
                'success'   => false,
892
                'data'   => 'ERROR_SLIDE_NOT_FOUND'
893
            ]);
894
        }
895
 
896
        if($slide->capsule_id != $capsule->id) {
897
            return new JsonModel([
898
                'success'   => false,
899
                'data'   => 'ERROR_UNAUTHORIZED'
900
            ]);
901
        }
902
 
903
        if($request->isGet()) {
904
            $data = [
905
                'success' => true,
906
                'data' => [
907
                    'name' => $slide->name,
908
                    'type' => $slide->type,
909
                    'description' => $slide->description,
910
                    'order' => $slide->order,
911
                    'quiz_id' => $slide->quiz_id
912
                ]
913
            ];
914
 
915
            return new JsonModel($data);
916
        }
917
        else if($request->isPost()) {
918
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
919
 
920
            switch($dataPost['type'])
921
            {
922
                case CompanyMicrolearningSlide::TYPE_AUDIO :
923
                    $form = new SlideAudioEditForm();
924
                    break;
925
 
926
                case CompanyMicrolearningSlide::TYPE_VIDEO :
927
                    $form = new SlideVideoEditForm();
928
                    break;
929
 
930
                case CompanyMicrolearningSlide::TYPE_DOCUMENT:
931
                    $form = new SlideDocumentEditForm();
932
                    break;
933
 
934
                case CompanyMicrolearningSlide::TYPE_IMAGE :
935
                    $form = new SlideImageEditForm();
936
                    break;
937
 
938
                case CompanyMicrolearningSlide::TYPE_QUIZ:
939
                    $form = new SlideQuizzEditForm($this->adapter, $currentCompany->id);
940
                    break;
941
 
942
                default :
943
                    $form = new SlideTextEditForm();
944
                    break;
945
 
946
            }
947
            $form->setData($dataPost);
948
 
949
            if($form->isValid()) {
950
                $dataPost = (array) $form->getData();
951
                $hydrator = new ObjectPropertyHydrator();
952
                $hydrator->hydrate($dataPost, $slide);
953
 
954
                $slide->file = null;
955
                $slide->background = null;
956
 
957
                if($slideMapper->update($slide)) {
958
                    $slide = $slideMapper->fetchOne($slide->id);
959
 
960
 
961
                    $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
962
                    if(!file_exists($target_path)) {
963
                        mkdir($target_path, 0755, true);
964
                    }
965
 
966
                    $files = $this->getRequest()->getFiles()->toArray();
967
 
968
                    if(isset($files['file']) && empty($files['file']['error'])) {
969
                        $tmp_filename       = $files['file']['tmp_name'];
970
                        $original_filename  = trim(strtolower($files['file']['name']));
971
 
972
 
973
                        if($slide->file) {
974
                            if(!image ::delete($target_path, $slide->file)) {
975
                                return new JsonModel([
976
                                    'success'   => false,
977
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
978
                                ]);
979
                            }
980
                        }
981
 
982
                        if($slide->type == CompanyMicrolearningSlide::TYPE_DOCUMENT) {
983
                            try {
984
 
985
                                $parts = explode('.', $original_filename);
986
                                $filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
987
 
988
                                $full_filename = $target_path  . DIRECTORY_SEPARATOR .$filename;
989
                                if(move_uploaded_file($tmp_filename, $full_filename)) {
990
                                    $slide->file = basename($full_filename);
991
                                    $slideMapper->update($slide);
992
                                }
993
                            } catch(\Throwable $e) {
994
                                error_log($e->getTraceAsString());
995
                            }
996
                        }
997
 
998
                        if($slide->type == CompanyMicrolearningSlide::TYPE_AUDIO) {
999
                            try {
1000
 
1001
                                $parts = explode('.', $original_filename);
1002
                                $filename = 'audio-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
1003
 
1004
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
1005
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
1006
 
1007
 
1008
                                    $generateFileName   = substr($filename, 0, strrpos($filename, '.'));
1009
                                    $generateFile       =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.mp3';
1010
                                    $cmd                = "/usr/bin/ffmpeg -i $full_filename -b:a 320000 $generateFile";
1011
                                    exec($cmd);
1012
 
1013
                                    $slide->file = basename($full_filename);
1014
                                    $slideMapper->update($slide);
1015
                                }
1016
 
1017
                            } catch(\Throwable $e) {
1018
                                error_log($e->getTraceAsString());
1019
                            }
1020
                        }
1021
 
1022
                        if($slide->type == CompanyMicrolearningSlide::TYPE_VIDEO) {
1023
                            try {
1024
                                $parts = explode('.', $original_filename);
1025
                                $filename = 'video-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
1026
 
1027
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
1028
 
1029
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
1030
 
1031
                                    $videoConvert = new VideoConvert();
1032
                                    $videoConvert->filename = $full_filename;
1033
                                    $videoConvert->type = VideoConvert::TYPE_MICRO_LEARNING;
1034
 
1035
                                    $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
1036
                                    $videoConvertMapper->insert($videoConvert);
1037
 
1038
                                    $slide->file = basename($full_filename);
1039
                                    $slideMapper->update($slide);
1040
                                }
1041
 
1042
                            } catch(\Throwable $e) {
1043
                                echo $e->getMessage();
1044
 
1045
                                error_log($e->getTraceAsString());
1046
                            }
1047
                        }
1048
 
1049
                        if($slide->type == CompanyMicrolearningSlide::TYPE_IMAGE) {
1050
                            try {
1051
                                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
1052
 
1053
                                $filename = 'slide-' .uniqid() . '.png';
1054
                                $crop_to_dimensions = true;
1055
                                if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
1056
                                    $slide->file = $filename;
1057
                                    $slideMapper->update($slide);
1058
                                }
1059
                            } catch(\Throwable $e) {
1060
                                error_log($e->getTraceAsString());
1061
                            }
1062
                        }
1063
                    }
1064
 
1065
                    if(isset($files['background']) && empty($files['background']['error'])) {
1066
                        if($slide->background) {
1067
                            if(!image ::delete($target_path, $slide->background)) {
1068
                                return new JsonModel([
1069
                                    'success'   => false,
1070
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1071
                                ]);
1072
                            }
1073
                        }
1074
 
1075
                        $tmp_filename  = $files['background']['tmp_name'];
1076
 
1077
                        try {
1078
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
1079
 
1080
                            $filename = 'background-' .uniqid() . '.png';
1081
                            $crop_to_dimensions = true;
1082
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
1083
                                $slide->background = $filename;
1084
                                $slideMapper->update($slide);
1085
                            }
1086
                        } catch(\Throwable $e) {
1087
                            error_log($e->getTraceAsString());
1088
                        }
1089
                    }
1090
 
1091
                    $this->logger->info('Se edito la diapositiva ' . $slide->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1092
 
1093
                    $data = [
1094
                        'success'   => true,
1095
                        'data'   => 'LABEL_RECORD_UPDATED'
1096
                    ];
1097
                } else {
1098
                    $data = [
1099
                        'success'   => false,
1100
                        'data'      => $capsuleMapper->getError()
1101
                    ];
1102
 
1103
                }
1104
 
1105
                return new JsonModel($data);
1106
 
1107
            } else {
1108
                $messages = [];
1109
                $form_messages = (array) $form->getMessages();
1110
                foreach($form_messages  as $fieldname => $field_messages)
1111
                {
1112
 
1113
                    $messages[$fieldname] = array_values($field_messages);
1114
                }
1115
 
1116
                return new JsonModel([
1117
                    'success'   => false,
1118
                    'data'   => $messages
1119
                ]);
1120
            }
1121
        } else {
1122
            $data = [
1123
                'success' => false,
1124
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1125
            ];
1126
 
1127
            return new JsonModel($data);
1128
        }
1129
 
1130
        return new JsonModel($data);
1131
    }
1132
 
1133
 
1134
    public function textAction()
1135
    {
1136
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1137
        $currentUser = $currentUserPlugin->getUser();
1138
        $currentCompany = $currentUserPlugin->getCompany();
1139
 
1140
        $request    = $this->getRequest();
1141
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
1142
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
1143
        $slide_uuid   = $this->params()->fromRoute('slide_uuid');
1144
 
1145
 
1146
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
1147
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
1148
        if(!$topic) {
1149
            return new JsonModel([
1150
                'success'   => false,
1151
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
1152
            ]);
1153
        }
1154
 
1155
        if($topic->company_id != $currentCompany->id) {
1156
            return new JsonModel([
1157
                'success'   => false,
1158
                'data'   => 'ERROR_UNAUTHORIZED'
1159
            ]);
1160
        }
1161
 
1162
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
1163
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
1164
        if(!$capsule) {
1165
            return new JsonModel([
1166
                'success'   => false,
1167
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
1168
            ]);
1169
        }
1170
 
1171
        if($capsule->topic_id != $topic->id) {
1172
            return new JsonModel([
1173
                'success'   => false,
1174
                'data'   => 'ERROR_UNAUTHORIZED'
1175
            ]);
1176
        }
1177
 
1178
 
1179
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
1180
        $slide = $slideMapper->fetchOneByUuid($slide_uuid);
1181
        if(!$slide) {
1182
            return new JsonModel([
1183
                'success'   => false,
1184
                'data'   => 'ERROR_SLIDE_NOT_FOUND'
1185
            ]);
1186
        }
1187
 
1188
        if($slide->capsule_id != $capsule->id) {
1189
            return new JsonModel([
1190
                'success'   => false,
1191
                'data'   => 'ERROR_UNAUTHORIZED'
1192
            ]);
1193
        }
1194
 
1195
        if($request->isGet()) {
1196
            $data = [
1197
                'success' => true,
1198
                'data' => $slide->description,
1199
            ];
1200
 
1201
            return new JsonModel($data);
1202
 
1203
        } else {
1204
            $data = [
1205
                'success' => false,
1206
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1207
            ];
1208
 
1209
            return new JsonModel($data);
1210
        }
1211
 
1212
        return new JsonModel($data);
1213
    }
1214
 
1215
 
1216
 
1217
 
1218
 
1219
}