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;
17002 efrain 16
use LeadersLinked\Mapper\MicrolearningTopicMapper;
17
use LeadersLinked\Mapper\MicrolearningCapsuleMapper;
18
use LeadersLinked\Model\MicrolearningCapsule;
19
use LeadersLinked\Form\Microlearning\CapsuleAddForm;
20
use LeadersLinked\Form\Microlearning\CapsuleEditForm;
21
use LeadersLinked\Mapper\MicrolearningCapsuleUserMapper;
22
use LeadersLinked\Form\Microlearning\CapsuleForm;
1 www 23
use LeadersLinked\Mapper\QueryMapper;
24
use LeadersLinked\Mapper\UserMapper;
17002 efrain 25
use LeadersLinked\Model\MicrolearningCapsuleUser;
26
use LeadersLinked\Library\Storage;
1 www 27
 
28
class MicrolearningCapsuleController extends AbstractActionController
29
{
30
    /**
31
     *
16769 efrain 32
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 33
     */
34
    private $adapter;
35
 
36
    /**
37
     *
16769 efrain 38
     * @var \LeadersLinked\Cache\CacheInterface
1 www 39
     */
16769 efrain 40
    private $cache;
41
 
42
 
43
    /**
44
     *
45
     * @var \Laminas\Log\LoggerInterface
46
     */
1 www 47
    private $logger;
48
 
49
    /**
50
     *
51
     * @var array
52
     */
53
    private $config;
54
 
16769 efrain 55
 
1 www 56
    /**
57
     *
16769 efrain 58
     * @var \Laminas\Mvc\I18n\Translator
59
     */
60
    private $translator;
61
 
62
 
63
    /**
64
     *
65
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
66
     * @param \LeadersLinked\Cache\CacheInterface $cache
67
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 68
     * @param array $config
16769 efrain 69
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 70
     */
16769 efrain 71
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 72
    {
16769 efrain 73
        $this->adapter      = $adapter;
74
        $this->cache        = $cache;
75
        $this->logger       = $logger;
76
        $this->config       = $config;
77
        $this->translator   = $translator;
1 www 78
    }
79
 
80
    /**
81
     *
82
     * Generación del listado de perfiles
83
     * {@inheritDoc}
84
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
85
     */
86
    public function indexAction()
87
    {
88
        $request = $this->getRequest();
89
 
90
        $currentUserPlugin = $this->plugin('currentUserPlugin');
91
        $currentUser = $currentUserPlugin->getUser();
92
        $currentCompany = $currentUserPlugin->getCompany();
93
 
94
 
95
 
96
 
97
        $request = $this->getRequest();
98
        if($request->isGet()) {
99
 
100
 
101
            $headers  = $request->getHeaders();
102
 
103
            $isJson = false;
104
            if($headers->has('Accept')) {
105
                $accept = $headers->get('Accept');
106
 
107
                $prioritized = $accept->getPrioritized();
108
 
109
                foreach($prioritized as $key => $value) {
110
                    $raw = trim($value->getRaw());
111
 
112
                    if(!$isJson) {
113
                        $isJson = strpos($raw, 'json');
114
                    }
115
 
116
                }
117
            }
118
 
119
            if($isJson) {
120
 
121
 
122
                $search = $this->params()->fromQuery('search', []);
16766 efrain 123
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 124
 
125
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
6973 nelberth 126
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
1 www 127
                $order =  $this->params()->fromQuery('order', []);
128
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 129
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 130
 
131
                $fields =  ['name'];
132
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
133
 
134
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
135
                    $order_direction = 'ASC';
136
                }
137
 
138
 
139
 
140
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
141
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/add');
142
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/edit');
143
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/delete');
144
                $allowUsers = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/users');
145
 
17002 efrain 146
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
147
                $paginator = $capsuleMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
1 www 148
 
149
 
17002 efrain 150
                $microlearningCapsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
1 www 151
                 $records = $paginator->getCurrentItems();
152
 
17018 efrain 153
                 $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 154
                 $path = $storage->getPathMicrolearningCapsule();
155
 
1 www 156
 
157
                $items = [];
158
                foreach($records as $record)
159
                {
17002 efrain 160
                    $total_users        = $microlearningCapsuleUserMapper->fetchCountUsersByCompanyIdAndCapsuleId($currentCompany->id,  $record->id);
161
                    $total_users_active = $microlearningCapsuleUserMapper->fetchCountUsersActiveByCompanyIdAndCapsuleId($currentCompany->id, $record->id);
1 www 162
 
163
                    switch($record->status) {
17002 efrain 164
                        case  MicrolearningCapsule::STATUS_ACTIVE :
1 www 165
                            $status = 'LABEL_ACTIVE';
166
                            break;
167
 
17002 efrain 168
                        case  MicrolearningCapsule::STATUS_INACTIVE :
1 www 169
                            $status = 'LABEL_INACTIVE';
170
                            break;
171
 
172
                        default :
173
                            $status = '';
174
                            break;
175
                    }
176
 
177
 
178
                    switch($record->privacy) {
17002 efrain 179
                        case  MicrolearningCapsule::PRIVACY_PUBLIC :
1 www 180
                            $privacy = 'LABEL_PUBLIC';
181
                            break;
182
 
17002 efrain 183
                        case  MicrolearningCapsule::PRIVACY_PRIVATE :
1 www 184
                            $privacy = 'LABEL_PRIVATE';
185
                            break;
186
 
187
                        default :
188
                            $privacy = '';
189
                            break;
190
                    }
191
 
192
                    switch($record->type) {
17002 efrain 193
                        case  MicrolearningCapsule::TYPE_FREE :
1 www 194
                            $type = 'LABEL_FREE';
195
                            break;
196
 
17002 efrain 197
                        case  MicrolearningCapsule::TYPE_PRIVATE :
1 www 198
                            $type = 'LABEL_PRIVATE';
199
                            break;
200
 
17002 efrain 201
                        case  MicrolearningCapsule::TYPE_SELLING :
1 www 202
                            $type = 'LABEL_SELLING';
203
                            break;
204
 
205
 
206
                        default :
207
                            $privacy = '';
208
                            break;
209
                    }
210
 
211
                    $params = [
212
                        'capsule_uuid'  => $record->uuid
213
                    ];
214
 
215
 
216
                    $item = [
217
 
218
                        'name' => $record->name,
219
                        'details' => [
220
                            'status' => $status,
221
                            'privacy' => $privacy,
222
                            'type' => $type,
223
                            'cost' => $record->cost,
224
                            'total_users' => $total_users,
225
                            'total_users_active' => $total_users_active,
226
                         ],
227
 
228
                        'images' => [
17002 efrain 229
                            'image' =>  $storage->getGenericImage($path, $record->uuid, $record->image),
230
                            'marketplace' => $record->marketplace ?  $storage->getGenericImage($path, $record->uuid, $record->marketplace) : '',
1 www 231
                        ] ,
232
                        'actions' => [
233
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('microlearning/content/capsules/edit', $params)  : '',
234
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('microlearning/content/capsules/delete', $params)  : '',
235
                            'link_total_users' => $allowUsers && $total_users ? $this->url()->fromRoute('microlearning/content/capsules/users', [
236
                                'capsule_uuid'  => $record->uuid,
237
                                'type' => 'all'
238
                             ])  : '',
239
                            'link_total_users_actives' => $allowUsers && $total_users_active ? $this->url()->fromRoute('microlearning/content/capsules/users', [
240
                                'capsule_uuid'  => $record->uuid,
241
                                'type' => 'active'
242
                            ])  : '',
243
                        ] ,
244
 
245
 
246
                     ];
247
 
248
 
249
                    array_push($items, $item);
250
                }
251
 
252
 
253
 
254
                return new JsonModel([
255
                    'success' => true,
256
                    'data' => [
17002 efrain 257
                        'link_add' => $allowAdd ? $this->url()->fromRoute('microlearning/content/capsules/add' )  : '',
1 www 258
                        'items' => $items,
259
                        'total' => $paginator->getTotalItemCount(),
260
                    ]
261
                ]);
262
 
263
 
264
 
265
 
266
 
267
 
268
            } else {
269
 
270
                $image_size = $this->config['leaderslinked.image_sizes.microlearning_image_upload'];
271
                $marketplace_size = $this->config['leaderslinked.image_sizes.marketplace'];
272
 
273
                $form = new CapsuleForm($this->adapter, $currentCompany->id);
274
                $formAdd = new CapsuleAddForm($currentCompany->internal);
275
                $formEdit = new CapsuleEditForm($currentCompany->internal);
276
 
277
                $this->layout()->setTemplate('layout/layout-backend.phtml');
278
                $viewModel = new ViewModel();
279
                $viewModel->setTemplate('leaders-linked/microlearning-capsules/index.phtml');
280
                $viewModel->setVariables([
281
                   'form' => $form,
282
                   'formAdd' => $formAdd,
283
                   'formEdit' => $formEdit,
284
                   'company_uuid' => $currentCompany->uuid,
285
                   'image_size' => $image_size,
286
                   'marketplace_size' => $marketplace_size,
287
 
288
                ]);
289
                return $viewModel ;
290
            }
291
 
292
        } else {
293
            return new JsonModel([
294
                'success' => false,
295
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
296
            ]);
297
        }
298
    }
299
 
300
    public function addAction()
301
    {
302
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
303
        $currentUser        = $currentUserPlugin->getUser();
304
        $currentCompany     = $currentUserPlugin->getCompany();
305
 
306
        $request    = $this->getRequest();
6829 nelberth 307
        $topic_uuid = $this->params()->fromRoute('topic_uuid');
1 www 308
 
17002 efrain 309
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
6829 nelberth 310
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
311
        if(!$topic) {
1 www 312
            return new JsonModel([
313
                'success'   => false,
314
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
315
            ]);
316
        }
317
 
6829 nelberth 318
        if($topic->company_id != $currentCompany->id) {
1 www 319
            return new JsonModel([
320
                'success'   => false,
321
                'data'   => 'ERROR_UNAUTHORIZED'
322
            ]);
323
        }
324
 
325
 
326
        if($request->isPost()) {
327
            $form = new  CapsuleAddForm($currentCompany->internal);
328
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
329
 
330
            $form->setData($dataPost);
331
 
332
            if($form->isValid()) {
17002 efrain 333
                $image = Image::getInstance($this->config);
334
                $target_path = $image->getStorage()->getPathMicrolearningCapsule();
335
 
336
 
337
 
1 www 338
                $dataPost = (array) $form->getData();
339
 
340
                $hydrator = new ObjectPropertyHydrator();
17002 efrain 341
                $capsule = new MicrolearningCapsule();
1 www 342
                $hydrator->hydrate($dataPost, $capsule);
343
 
6829 nelberth 344
                $capsule->company_id = $topic->company_id;
1458 efrain 345
                $capsule->image = '';
346
                $capsule->marketplace = '';
1 www 347
 
17002 efrain 348
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 349
                if($capsuleMapper->insert($capsule)) {
350
 
351
                    $capsule = $capsuleMapper->fetchOne($capsule->id);
352
 
353
                    $files = $this->getRequest()->getFiles()->toArray();
354
                    if(isset($files['file']) && empty($files['file']['error'])) {
355
                        $tmp_filename  = $files['file']['tmp_name'];
17018 efrain 356
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
1 www 357
 
358
                        try {
359
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
360
 
16943 efrain 361
                            $filename = 'capsule-' .uniqid() . '.jpg';
362
                            $crop_to_dimensions = false;
17002 efrain 363
                            $unlink_source = true;
364
 
17018 efrain 365
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
1 www 366
                                $capsule->image = $filename;
367
                                $capsuleMapper->update($capsule);
368
                            }
369
                        } catch(\Throwable $e) {
370
                            error_log($e->getTraceAsString());
371
                        }
372
                    }
373
 
16943 efrain 374
                    if(isset($files['marketplace']) && empty($files['marketplace']['error'])) {
1 www 375
                        $tmp_filename  = $files['marketplace']['tmp_name'];
17018 efrain 376
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
1 www 377
 
378
                        try {
16945 efrain 379
                        list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.marketplace']);
1 www 380
 
16943 efrain 381
                            $filename = 'marketplace-' .uniqid() . '.jpg';
382
                            $crop_to_dimensions = false;
17002 efrain 383
                            $unlink_source = true;
384
 
17018 efrain 385
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
1 www 386
                                $capsule->marketplace = $filename;
387
                                $capsuleMapper->update($capsule);
388
                            }
389
                        } catch(\Throwable $e) {
390
                            error_log($e->getTraceAsString());
391
                        }
392
                    }
16943 efrain 393
 
394
 
6829 nelberth 395
                    $this->logger->info('Se agrego la cápsula ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1 www 396
 
397
                    $data = [
6945 nelberth 398
                        'success'   => true,
399
                        'data'   => 'LABEL_RECORD_ADDED'
1 www 400
                    ];
401
                } else {
402
                    $data = [
403
                        'success'   => false,
6829 nelberth 404
                        'data'      => $topicMapper->getError()
1 www 405
                    ];
406
 
407
                }
408
 
409
                return new JsonModel($data);
410
 
411
            } else {
412
                $messages = [];
413
                $form_messages = (array) $form->getMessages();
414
                foreach($form_messages  as $fieldname => $field_messages)
415
                {
416
 
417
                    $messages[$fieldname] = array_values($field_messages);
418
                }
419
 
420
                return new JsonModel([
421
                    'success'   => false,
422
                    'data'   => $messages
423
                ]);
424
            }
425
 
426
        } else {
427
            $data = [
428
                'success' => false,
429
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
430
            ];
431
 
432
            return new JsonModel($data);
433
        }
434
 
435
        return new JsonModel($data);
436
    }
437
 
438
    /**
439
     *
440
     * Borrar un perfil excepto el público
441
     * @return \Laminas\View\Model\JsonModel
442
     */
443
    public function deleteAction()
444
    {
445
        $currentUserPlugin = $this->plugin('currentUserPlugin');
446
        $currentUser    = $currentUserPlugin->getUser();
447
        $currentCompany = $currentUserPlugin->getCompany();
448
 
449
        $request        = $this->getRequest();
6829 nelberth 450
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1 www 451
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
452
 
453
 
17002 efrain 454
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
6829 nelberth 455
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
456
        if(!$topic) {
1 www 457
            return new JsonModel([
458
                'success'   => false,
459
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
460
            ]);
461
        }
462
 
6829 nelberth 463
        if($topic->company_id != $currentCompany->id) {
1 www 464
            return new JsonModel([
465
                'success'   => false,
466
                'data'   => 'ERROR_UNAUTHORIZED'
467
            ]);
468
        }
469
 
17002 efrain 470
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 471
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
472
        if(!$capsule) {
473
            return new JsonModel([
474
                'success'   => false,
475
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
476
            ]);
477
        }
478
 
17002 efrain 479
        if($capsule->company_id != $currentCompany->id) {
1 www 480
            return new JsonModel([
481
                'success'   => false,
482
                'data'   => 'ERROR_UNAUTHORIZED'
483
            ]);
484
        }
485
 
486
        if($request->isPost()) {
487
 
488
            $result =  $capsuleMapper->delete($capsule);
489
            if($result) {
490
                $this->logger->info('Se borro la cápsula : ' .  $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17002 efrain 491
 
1 www 492
 
17018 efrain 493
                $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 494
                $target_path = $storage->getPathMicrolearningCapsule();
1 www 495
 
17002 efrain 496
                $storage->deleteFile($target_path, $capsule->uuid, $capsule->image);
497
 
498
                if($capsule->marketplace) {
499
 
500
                    $storage->deleteFile($target_path, $topic->uuid, $capsule->marketplace);
501
                }
502
 
1 www 503
                $data = [
504
                    'success' => true,
505
                    'data' => 'LABEL_RECORD_DELETED'
506
                ];
507
            } else {
508
 
509
                $data = [
510
                    'success'   => false,
511
                    'data'      => $capsuleMapper->getError()
512
                ];
513
 
514
                return new JsonModel($data);
515
            }
516
 
517
        } else {
518
            $data = [
519
                'success' => false,
520
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
521
            ];
522
 
523
            return new JsonModel($data);
524
        }
525
 
526
        return new JsonModel($data);
527
    }
528
 
529
 
530
    public function editAction()
531
    {
532
        $currentUserPlugin = $this->plugin('currentUserPlugin');
533
        $currentUser    = $currentUserPlugin->getUser();
534
        $currentCompany = $currentUserPlugin->getCompany();
535
 
536
        $request    = $this->getRequest();
6829 nelberth 537
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1 www 538
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
539
 
540
 
17002 efrain 541
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
6829 nelberth 542
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
543
        if(!$topic) {
1 www 544
            return new JsonModel([
545
                'success'   => false,
546
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
547
            ]);
548
        }
549
 
550
 
551
 
6829 nelberth 552
        if($topic->company_id != $currentCompany->id) {
1 www 553
            return new JsonModel([
554
                'success'   => false,
555
                'data'   => 'ERROR_UNAUTHORIZED'
556
            ]);
557
        }
558
 
17002 efrain 559
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 560
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
561
        if(!$capsule) {
562
            return new JsonModel([
563
                'success'   => false,
564
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
565
            ]);
566
        }
567
 
568
 
17002 efrain 569
        if($capsule->company_id != $currentCompany->id) {
1 www 570
            return new JsonModel([
571
                'success'   => false,
572
                'data'   => 'ERROR_UNAUTHORIZED'
573
            ]);
574
        }
575
 
576
        if($request->isGet()) {
17018 efrain 577
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 578
            $path = $storage->getPathMicrolearningCapsule();
579
 
580
 
1 www 581
            $data = [
582
                'success' => true,
583
                'data' => [
584
                    'name' => $capsule->name,
585
                    'description' => $capsule->description,
586
                    'status' => $capsule->status,
587
                    'privacy' => $capsule->privacy,
588
                    'type' => $capsule->type,
6974 nelberth 589
                    'cost' => $capsule->cost,
17002 efrain 590
                    'image' => $storage->getGenericImage($path, $capsule->uuid, $capsule->image),
591
                    'marketplace' => $capsule->marketplace ?  $storage->getGenericImage($path, $capsule->uuid, $capsule->marketplace) : '',
6982 nelberth 592
 
1 www 593
                ]
594
            ];
595
 
596
            return new JsonModel($data);
597
        }
598
        else if($request->isPost()) {
599
            $form = new  CapsuleEditForm($currentCompany->internal);
600
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
601
 
602
            $form->setData($dataPost);
603
 
604
            if($form->isValid()) {
605
                $dataPost = (array) $form->getData();
606
 
607
                $hydrator = new ObjectPropertyHydrator();
608
                $hydrator->hydrate($dataPost, $capsule);
609
 
610
                $capsule->image = null;
611
                $capsule->marketplace = null;
612
 
17002 efrain 613
                $image = Image::getInstance($this->config);
614
                $target_path = $image->getStorage()->getPathMicrolearningCapsule();
615
 
1 www 616
                if($capsuleMapper->update($capsule)) {
617
 
618
                    $capsule = $capsuleMapper->fetchOne($capsule->id);
16943 efrain 619
 
1 www 620
                    $files = $this->getRequest()->getFiles()->toArray();
17002 efrain 621
 
16943 efrain 622
 
1 www 623
                    if(isset($files['file']) && empty($files['file']['error'])) {
624
                        $tmp_filename  = $files['file']['tmp_name'];
17018 efrain 625
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
1 www 626
 
627
                        try {
16943 efrain 628
                            if($capsule->image) {
1 www 629
 
17002 efrain 630
                                if(!$image->getStorage()->deleteFile($target_path, $capsule->uuid, $capsule->image)) {
1 www 631
                                    return new JsonModel([
632
                                        'success'   => false,
633
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
634
                                    ]);
635
                                }
636
                            }
637
 
16943 efrain 638
 
1 www 639
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
640
 
16943 efrain 641
                            $filename = 'capsule-' .uniqid() . '.jpg';
642
                            $crop_to_dimensions = false;
17002 efrain 643
                            $unlink_source = true;
644
 
17018 efrain 645
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
17002 efrain 646
 
1 www 647
                                $capsule->image = $filename;
648
                                $capsuleMapper->update($capsule);
649
                            }
650
                        } catch(\Throwable $e) {
651
                            error_log($e->getTraceAsString());
652
                        }
653
                    }
16945 efrain 654
 
1 www 655
                    if(isset($files['marketplace']) && empty($files['marketplace']['error'])) {
16945 efrain 656
 
657
 
658
 
1 www 659
                        $tmp_filename  = $files['marketplace']['tmp_name'];
17018 efrain 660
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
1 www 661
 
662
                        try {
17002 efrain 663
                            if($capsule->marketplace) {
1 www 664
 
17002 efrain 665
                                if(!$image->getStorage()->deleteFile($target_path, $capsule->uuid, $capsule->marketplace)) {
1 www 666
                                    return new JsonModel([
667
                                        'success'   => false,
668
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
669
                                    ]);
670
                                }
671
                            }
672
 
16945 efrain 673
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.marketplace']);
1 www 674
 
16943 efrain 675
                            $filename = 'marketplace-' .uniqid() . '.jpg';
676
                            $crop_to_dimensions = false;
17002 efrain 677
                            $unlink_source = true;
678
 
17018 efrain 679
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
17002 efrain 680
 
1 www 681
                                $capsule->marketplace = $filename;
682
                                $capsuleMapper->update($capsule);
683
                            }
684
                        } catch(\Throwable $e) {
685
                            error_log($e->getTraceAsString());
686
                        }
687
                    }
16943 efrain 688
 
16945 efrain 689
 
690
 
16943 efrain 691
 
1 www 692
                    $this->logger->info('Se edito la cápsula ' . $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
693
 
694
                    $data = [
695
                        'success'   => true,
696
                        'data'   => 'LABEL_RECORD_UPDATED'
697
                    ];
698
                } else {
699
                    $data = [
700
                        'success'   => false,
701
                        'data'      => $capsuleMapper->getError()
702
                    ];
703
 
704
                }
705
 
706
                return new JsonModel($data);
707
 
708
            } else {
709
                $messages = [];
710
                $form_messages = (array) $form->getMessages();
711
                foreach($form_messages  as $fieldname => $field_messages)
712
                {
713
 
714
                    $messages[$fieldname] = array_values($field_messages);
715
                }
716
 
717
                return new JsonModel([
718
                    'success'   => false,
719
                    'data'   => $messages
720
                ]);
721
            }
722
        } else {
723
            $data = [
724
                'success' => false,
725
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
726
            ];
727
 
728
            return new JsonModel($data);
729
        }
730
 
731
        return new JsonModel($data);
732
    }
733
 
734
 
735
    public function usersAction()
736
    {
737
        $currentUserPlugin = $this->plugin('currentUserPlugin');
738
        $currentUser    = $currentUserPlugin->getUser();
739
        $currentCompany = $currentUserPlugin->getCompany();
740
 
741
        $request        = $this->getRequest();
6829 nelberth 742
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1 www 743
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
744
        $type           = $this->params()->fromRoute('type');
745
 
746
 
17002 efrain 747
 
1 www 748
 
17002 efrain 749
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 750
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
751
        if(!$capsule) {
752
            return new JsonModel([
753
                'success'   => false,
754
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
755
            ]);
756
        }
757
 
17002 efrain 758
        if($capsule->company_id != $currentCompany->id) {
1 www 759
            return new JsonModel([
760
                'success'   => false,
761
                'data'   => 'ERROR_UNAUTHORIZED'
762
            ]);
763
        }
764
 
765
        if($request->isGet()) {
766
 
767
 
768
            $queryMapper = QueryMapper::getInstance($this->adapter);
769
            $sql = $queryMapper->getSql();
770
            $select = $sql->select();
771
            $select->columns(['access', 'paid_from', 'paid_to', 'added_on']);
17002 efrain 772
            $select->from(['tb1' => MicrolearningCapsuleUserMapper::_TABLE] );
1 www 773
            $select->join(['tb2' => UserMapper::_TABLE], 'tb1.user_id = tb2.id', ['uuid', 'first_name', 'last_name', 'email']);
774
            $select->where->equalTo('tb1.company_id', $capsule->company_id);
775
            $select->where->equalTo('tb1.capsule_id', $capsule->id);
776
 
777
            if($type == 'active') {
778
                $now = date('Y-m-d H:i:s');
17002 efrain 779
                $select->where->nest->equalTo('access', MicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
780
                ->equalTo('access', MicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
1 www 781
                ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
782
 
783
            }
784
 
785
 
786
            $select->order(['first_name', 'last_name', 'email']);
787
            $records  = $queryMapper->fetchAll($select);
788
 
789
 
790
            $items = [ ];
791
            foreach($records as $record)
792
            {
793
 
794
 
795
 
796
 
797
                switch($record['access'])
798
                {
17002 efrain 799
                    case MicrolearningCapsuleUser::ACCESS_UNLIMITED :
1 www 800
                        $details['access'] = 'LABEL_UNLIMIT';
801
                        break;
802
 
17002 efrain 803
                    case MicrolearningCapsuleUser::ACCESS_REVOKE :
1 www 804
                        $details['access'] = 'LABEL_REVOKED';
805
                        break;
806
 
17002 efrain 807
                    case MicrolearningCapsuleUser::ACCESS_PAY_PERIOD :
1 www 808
                        $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
809
                        $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
810
 
811
                        $details['access'] = 'LABEL_PAY_PERIOD';
812
                        $details['paid_from'] = $dt_paid_from->format('d/m/Y');
813
                        $details['paid_to'] = $dt_paid_to->format('d/m/Y');
814
                        break;
815
 
17002 efrain 816
                    case MicrolearningCapsuleUser::ACCESS_SUPENDED :
1 www 817
                        $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
818
                        $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
819
 
820
                        $details['access'] = 'LABEL_SUSPENDED';
821
                        $details['paid_from'] = $dt_paid_from->format('d/m/Y');
822
                        $details['paid_to'] = $dt_paid_to->format('d/m/Y');
823
                        break;
824
 
17002 efrain 825
                    case MicrolearningCapsuleUser::ACCESS_CANCELLED :
1 www 826
                        $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
827
                        $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
828
 
829
                        $details['access'] = 'LABEL_CANCELLED';
830
                        $details['paid_from'] = $dt_paid_from->format('d/m/Y');
831
                        $details['paid_to'] = $dt_paid_to->format('d/m/Y');
832
                        break;
833
 
834
                }
835
 
836
 
837
                $item = [
838
                    'first_name' => $record['first_name'],
839
                    'last_name' => $record['last_name'],
840
                    'email' => $record['email'],
841
                    'details' => $details,
842
                ];
843
 
844
 
845
 
846
                array_push($items, $item);
847
 
848
 
849
            }
850
 
851
            return new JsonModel([
852
                'success' => true,
853
                'data' => [
6829 nelberth 854
                    'topic' => $topic->name,
1 www 855
                    'capsule' => $capsule->name,
856
                    'items' => $items,
857
                ]
858
            ]);
859
 
860
 
861
 
862
        } else {
863
            $data = [
864
                'success' => false,
865
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
866
            ];
867
 
868
            return new JsonModel($data);
869
        }
870
 
871
        return new JsonModel($data);
872
    }
873
 
874
}