Proyectos de Subversion LeadersLinked - Backend

Rev

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