Proyectos de Subversion LeadersLinked - Backend

Rev

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

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