Proyectos de Subversion LeadersLinked - Backend

Rev

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