Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17018 | Rev 17058 | 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;
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();
307
 
17055 stevensc 308
        if(!$request->isPost()) {
309
            $data = [
310
                'success' => false,
311
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
312
            ];
313
 
314
            return new JsonModel($data);
315
        }
316
 
317
        $form = new  CapsuleAddForm($currentCompany->internal);
318
        $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
319
 
320
        $form->setData($dataPost);
321
 
322
        if(!$form->isValid()) {
323
            $messages = [];
324
            $form_messages = (array) $form->getMessages();
325
            foreach($form_messages  as $fieldname => $field_messages)
326
            {
327
 
328
                $messages[$fieldname] = array_values($field_messages);
329
            }
330
 
1 www 331
            return new JsonModel([
332
                'success'   => false,
17055 stevensc 333
                'data'   => $messages
1 www 334
            ]);
17055 stevensc 335
        }
336
 
337
        $dataPost = (array) $form->getData();
338
        $hydrator = new ObjectPropertyHydrator();
339
        $capsule = new MicrolearningCapsule();
340
        $hydrator->hydrate($dataPost, $capsule);
341
        $capsule->company_id = $currentCompany->id;
342
        $capsule->image = '';
343
        $capsule->marketplace = '';
1 www 344
 
17055 stevensc 345
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
346
 
347
        if(!$capsuleMapper->insert($capsule)) {
1 www 348
            return new JsonModel([
349
                'success'   => false,
17055 stevensc 350
                'data'      => $capsuleMapper->getError()
1 www 351
            ]);
352
        }
17055 stevensc 353
 
354
        $capsule = $capsuleMapper->fetchOne($capsule->id);
355
        $storage = Storage::getInstance($this->config, $this->adapter);
1 www 356
 
17055 stevensc 357
        $storage->setFiles($request->getFiles()->toArray());
1 www 358
 
17055 stevensc 359
        if (!$storage->setCurrentFilename('file')) {
360
            return new JsonModel([
361
                'success'   => false,
362
                'data'      => 'ERROR_UPLOAD_IMAGE'
363
            ]);
1 www 364
        }
17055 stevensc 365
 
366
        $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
367
        list($target_width, $target_height) = explode('x', $target_size);
368
 
369
        $source_filename = $storage->getTmpFilename();
370
        $filename = 'capsule-' . uniqid() . '.jpg';
371
        $target_filename = $storage->composePathToFilename(
372
            Storage::TYPE_MICROLEARNING_CAPSULE,
373
            $capsule->uuid,
374
            $filename
375
        );
376
 
377
        if (!$storage->uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)) {
378
            return new JsonModel([
379
                'success'   => false,
380
                'data'      => 'ERROR_UPLOAD_IMAGE'
381
            ]);
382
        }
383
 
384
        if(!$storage->setCurrentFilename('marketplace')) {
385
            return new JsonModel([
386
                'success'   => false,
387
                'data'      => 'ERROR_UPLOAD_IMAGE'
388
            ]);
389
        }
390
 
391
        $target_size = $this->config['leaderslinked.image_sizes.marketplace'];
392
        list($target_width, $target_height) = explode('x', $target_size);
393
 
394
        $marketplace_source_filename = $storage->getTmpFilename();
395
        $marketplace_filename = 'marketplace-' . uniqid() . '.jpg';
396
        $marketplace_target_filename = $storage->composePathToFilename(
397
            Storage::TYPE_MICROLEARNING_CAPSULE,
398
            $capsule->uuid,
399
            $marketplace_filename
400
        );
401
 
402
        if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
403
            return new JsonModel([
404
                'success'   => false,
405
                'data'      => 'ERROR_UPLOAD_IMAGE'
406
            ]);
407
        }
408
 
409
        $capsule->marketplace = $marketplace_filename;
410
        $capsule->image = $filename;
411
 
412
        if(!$capsuleMapper->update($capsule)) {
413
            return new JsonModel([
414
                'success'   => false,
415
                'data'      => $capsuleMapper->getError()
416
            ]);
417
        }
1 www 418
 
17055 stevensc 419
        $this->logger->info('Se agrego la cápsula ' . $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
420
 
421
        return new JsonModel([
422
            'success'   => true,
423
            'data'   => 'LABEL_RECORD_ADDED'
424
        ]);
1 www 425
    }
426
 
427
    /**
428
     *
429
     * Borrar un perfil excepto el público
430
     * @return \Laminas\View\Model\JsonModel
431
     */
432
    public function deleteAction()
433
    {
434
        $currentUserPlugin = $this->plugin('currentUserPlugin');
435
        $currentUser    = $currentUserPlugin->getUser();
436
        $currentCompany = $currentUserPlugin->getCompany();
437
 
438
        $request        = $this->getRequest();
6829 nelberth 439
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1 www 440
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
441
 
442
 
17002 efrain 443
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
6829 nelberth 444
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
445
        if(!$topic) {
1 www 446
            return new JsonModel([
447
                'success'   => false,
448
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
449
            ]);
450
        }
451
 
6829 nelberth 452
        if($topic->company_id != $currentCompany->id) {
1 www 453
            return new JsonModel([
454
                'success'   => false,
455
                'data'   => 'ERROR_UNAUTHORIZED'
456
            ]);
457
        }
458
 
17002 efrain 459
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 460
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
461
        if(!$capsule) {
462
            return new JsonModel([
463
                'success'   => false,
464
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
465
            ]);
466
        }
467
 
17002 efrain 468
        if($capsule->company_id != $currentCompany->id) {
1 www 469
            return new JsonModel([
470
                'success'   => false,
471
                'data'   => 'ERROR_UNAUTHORIZED'
472
            ]);
473
        }
474
 
475
        if($request->isPost()) {
476
 
477
            $result =  $capsuleMapper->delete($capsule);
478
            if($result) {
479
                $this->logger->info('Se borro la cápsula : ' .  $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
17002 efrain 480
 
1 www 481
 
17018 efrain 482
                $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 483
                $target_path = $storage->getPathMicrolearningCapsule();
1 www 484
 
17002 efrain 485
                $storage->deleteFile($target_path, $capsule->uuid, $capsule->image);
486
 
487
                if($capsule->marketplace) {
488
 
489
                    $storage->deleteFile($target_path, $topic->uuid, $capsule->marketplace);
490
                }
491
 
1 www 492
                $data = [
493
                    'success' => true,
494
                    'data' => 'LABEL_RECORD_DELETED'
495
                ];
496
            } else {
497
 
498
                $data = [
499
                    'success'   => false,
500
                    'data'      => $capsuleMapper->getError()
501
                ];
502
 
503
                return new JsonModel($data);
504
            }
505
 
506
        } else {
507
            $data = [
508
                'success' => false,
509
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
510
            ];
511
 
512
            return new JsonModel($data);
513
        }
514
 
515
        return new JsonModel($data);
516
    }
517
 
518
 
519
    public function editAction()
520
    {
521
        $currentUserPlugin = $this->plugin('currentUserPlugin');
522
        $currentUser    = $currentUserPlugin->getUser();
523
        $currentCompany = $currentUserPlugin->getCompany();
524
 
525
        $request    = $this->getRequest();
6829 nelberth 526
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1 www 527
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
528
 
529
 
17002 efrain 530
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
6829 nelberth 531
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
532
        if(!$topic) {
1 www 533
            return new JsonModel([
534
                'success'   => false,
535
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
536
            ]);
537
        }
538
 
539
 
540
 
6829 nelberth 541
        if($topic->company_id != $currentCompany->id) {
1 www 542
            return new JsonModel([
543
                'success'   => false,
544
                'data'   => 'ERROR_UNAUTHORIZED'
545
            ]);
546
        }
547
 
17002 efrain 548
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 549
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
550
        if(!$capsule) {
551
            return new JsonModel([
552
                'success'   => false,
553
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
554
            ]);
555
        }
556
 
557
 
17002 efrain 558
        if($capsule->company_id != $currentCompany->id) {
1 www 559
            return new JsonModel([
560
                'success'   => false,
561
                'data'   => 'ERROR_UNAUTHORIZED'
562
            ]);
563
        }
564
 
565
        if($request->isGet()) {
17018 efrain 566
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 567
            $path = $storage->getPathMicrolearningCapsule();
568
 
569
 
1 www 570
            $data = [
571
                'success' => true,
572
                'data' => [
573
                    'name' => $capsule->name,
574
                    'description' => $capsule->description,
575
                    'status' => $capsule->status,
576
                    'privacy' => $capsule->privacy,
577
                    'type' => $capsule->type,
6974 nelberth 578
                    'cost' => $capsule->cost,
17002 efrain 579
                    'image' => $storage->getGenericImage($path, $capsule->uuid, $capsule->image),
580
                    'marketplace' => $capsule->marketplace ?  $storage->getGenericImage($path, $capsule->uuid, $capsule->marketplace) : '',
6982 nelberth 581
 
1 www 582
                ]
583
            ];
584
 
585
            return new JsonModel($data);
586
        }
587
        else if($request->isPost()) {
588
            $form = new  CapsuleEditForm($currentCompany->internal);
589
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
590
 
591
            $form->setData($dataPost);
592
 
593
            if($form->isValid()) {
594
                $dataPost = (array) $form->getData();
595
 
596
                $hydrator = new ObjectPropertyHydrator();
597
                $hydrator->hydrate($dataPost, $capsule);
598
 
599
                $capsule->image = null;
600
                $capsule->marketplace = null;
601
 
17002 efrain 602
                $image = Image::getInstance($this->config);
603
                $target_path = $image->getStorage()->getPathMicrolearningCapsule();
604
 
1 www 605
                if($capsuleMapper->update($capsule)) {
606
 
607
                    $capsule = $capsuleMapper->fetchOne($capsule->id);
16943 efrain 608
 
1 www 609
                    $files = $this->getRequest()->getFiles()->toArray();
17002 efrain 610
 
16943 efrain 611
 
1 www 612
                    if(isset($files['file']) && empty($files['file']['error'])) {
613
                        $tmp_filename  = $files['file']['tmp_name'];
17018 efrain 614
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
1 www 615
 
616
                        try {
16943 efrain 617
                            if($capsule->image) {
1 www 618
 
17002 efrain 619
                                if(!$image->getStorage()->deleteFile($target_path, $capsule->uuid, $capsule->image)) {
1 www 620
                                    return new JsonModel([
621
                                        'success'   => false,
622
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
623
                                    ]);
624
                                }
625
                            }
626
 
16943 efrain 627
 
1 www 628
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
629
 
16943 efrain 630
                            $filename = 'capsule-' .uniqid() . '.jpg';
631
                            $crop_to_dimensions = false;
17002 efrain 632
                            $unlink_source = true;
633
 
17018 efrain 634
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
17002 efrain 635
 
1 www 636
                                $capsule->image = $filename;
637
                                $capsuleMapper->update($capsule);
638
                            }
639
                        } catch(\Throwable $e) {
640
                            error_log($e->getTraceAsString());
641
                        }
642
                    }
16945 efrain 643
 
1 www 644
                    if(isset($files['marketplace']) && empty($files['marketplace']['error'])) {
16945 efrain 645
 
646
 
647
 
1 www 648
                        $tmp_filename  = $files['marketplace']['tmp_name'];
17018 efrain 649
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
1 www 650
 
651
                        try {
17002 efrain 652
                            if($capsule->marketplace) {
1 www 653
 
17002 efrain 654
                                if(!$image->getStorage()->deleteFile($target_path, $capsule->uuid, $capsule->marketplace)) {
1 www 655
                                    return new JsonModel([
656
                                        'success'   => false,
657
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
658
                                    ]);
659
                                }
660
                            }
661
 
16945 efrain 662
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.marketplace']);
1 www 663
 
16943 efrain 664
                            $filename = 'marketplace-' .uniqid() . '.jpg';
665
                            $crop_to_dimensions = false;
17002 efrain 666
                            $unlink_source = true;
667
 
17018 efrain 668
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
17002 efrain 669
 
1 www 670
                                $capsule->marketplace = $filename;
671
                                $capsuleMapper->update($capsule);
672
                            }
673
                        } catch(\Throwable $e) {
674
                            error_log($e->getTraceAsString());
675
                        }
676
                    }
16943 efrain 677
 
16945 efrain 678
 
679
 
16943 efrain 680
 
1 www 681
                    $this->logger->info('Se edito la cápsula ' . $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
682
 
683
                    $data = [
684
                        'success'   => true,
685
                        'data'   => 'LABEL_RECORD_UPDATED'
686
                    ];
687
                } else {
688
                    $data = [
689
                        'success'   => false,
690
                        'data'      => $capsuleMapper->getError()
691
                    ];
692
 
693
                }
694
 
695
                return new JsonModel($data);
696
 
697
            } else {
698
                $messages = [];
699
                $form_messages = (array) $form->getMessages();
700
                foreach($form_messages  as $fieldname => $field_messages)
701
                {
702
 
703
                    $messages[$fieldname] = array_values($field_messages);
704
                }
705
 
706
                return new JsonModel([
707
                    'success'   => false,
708
                    'data'   => $messages
709
                ]);
710
            }
711
        } else {
712
            $data = [
713
                'success' => false,
714
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
715
            ];
716
 
717
            return new JsonModel($data);
718
        }
719
 
720
        return new JsonModel($data);
721
    }
722
 
723
 
724
    public function usersAction()
725
    {
726
        $currentUserPlugin = $this->plugin('currentUserPlugin');
727
        $currentUser    = $currentUserPlugin->getUser();
728
        $currentCompany = $currentUserPlugin->getCompany();
729
 
730
        $request        = $this->getRequest();
6829 nelberth 731
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1 www 732
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
733
        $type           = $this->params()->fromRoute('type');
734
 
735
 
17002 efrain 736
 
1 www 737
 
17002 efrain 738
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 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
 
17002 efrain 747
        if($capsule->company_id != $currentCompany->id) {
1 www 748
            return new JsonModel([
749
                'success'   => false,
750
                'data'   => 'ERROR_UNAUTHORIZED'
751
            ]);
752
        }
753
 
754
        if($request->isGet()) {
755
 
756
 
757
            $queryMapper = QueryMapper::getInstance($this->adapter);
758
            $sql = $queryMapper->getSql();
759
            $select = $sql->select();
760
            $select->columns(['access', 'paid_from', 'paid_to', 'added_on']);
17002 efrain 761
            $select->from(['tb1' => MicrolearningCapsuleUserMapper::_TABLE] );
1 www 762
            $select->join(['tb2' => UserMapper::_TABLE], 'tb1.user_id = tb2.id', ['uuid', 'first_name', 'last_name', 'email']);
763
            $select->where->equalTo('tb1.company_id', $capsule->company_id);
764
            $select->where->equalTo('tb1.capsule_id', $capsule->id);
765
 
766
            if($type == 'active') {
767
                $now = date('Y-m-d H:i:s');
17002 efrain 768
                $select->where->nest->equalTo('access', MicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
769
                ->equalTo('access', MicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
1 www 770
                ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
771
 
772
            }
773
 
774
 
775
            $select->order(['first_name', 'last_name', 'email']);
776
            $records  = $queryMapper->fetchAll($select);
777
 
778
 
779
            $items = [ ];
780
            foreach($records as $record)
781
            {
782
 
783
 
784
 
785
 
786
                switch($record['access'])
787
                {
17002 efrain 788
                    case MicrolearningCapsuleUser::ACCESS_UNLIMITED :
1 www 789
                        $details['access'] = 'LABEL_UNLIMIT';
790
                        break;
791
 
17002 efrain 792
                    case MicrolearningCapsuleUser::ACCESS_REVOKE :
1 www 793
                        $details['access'] = 'LABEL_REVOKED';
794
                        break;
795
 
17002 efrain 796
                    case MicrolearningCapsuleUser::ACCESS_PAY_PERIOD :
1 www 797
                        $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
798
                        $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
799
 
800
                        $details['access'] = 'LABEL_PAY_PERIOD';
801
                        $details['paid_from'] = $dt_paid_from->format('d/m/Y');
802
                        $details['paid_to'] = $dt_paid_to->format('d/m/Y');
803
                        break;
804
 
17002 efrain 805
                    case MicrolearningCapsuleUser::ACCESS_SUPENDED :
1 www 806
                        $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
807
                        $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
808
 
809
                        $details['access'] = 'LABEL_SUSPENDED';
810
                        $details['paid_from'] = $dt_paid_from->format('d/m/Y');
811
                        $details['paid_to'] = $dt_paid_to->format('d/m/Y');
812
                        break;
813
 
17002 efrain 814
                    case MicrolearningCapsuleUser::ACCESS_CANCELLED :
1 www 815
                        $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
816
                        $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
817
 
818
                        $details['access'] = 'LABEL_CANCELLED';
819
                        $details['paid_from'] = $dt_paid_from->format('d/m/Y');
820
                        $details['paid_to'] = $dt_paid_to->format('d/m/Y');
821
                        break;
822
 
823
                }
824
 
825
 
826
                $item = [
827
                    'first_name' => $record['first_name'],
828
                    'last_name' => $record['last_name'],
829
                    'email' => $record['email'],
830
                    'details' => $details,
831
                ];
832
 
833
 
834
 
835
                array_push($items, $item);
836
 
837
 
838
            }
839
 
840
            return new JsonModel([
841
                'success' => true,
842
                'data' => [
6829 nelberth 843
                    'topic' => $topic->name,
1 www 844
                    'capsule' => $capsule->name,
845
                    'items' => $items,
846
                ]
847
            ]);
848
 
849
 
850
 
851
        } else {
852
            $data = [
853
                'success' => false,
854
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
855
            ];
856
 
857
            return new JsonModel($data);
858
        }
859
 
860
        return new JsonModel($data);
861
    }
862
 
863
}