Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
15540 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 7
 
15540 efrain 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use LeadersLinked\Mapper\DailyPulseEmojiMapper;
15
use LeadersLinked\Form\DailyPulse\DailyPulseAddEmojiForm;
16
use LeadersLinked\Form\DailyPulse\DailyPulseEditEmojiForm;
17
use LeadersLinked\Model\DailyPulseEmoji;
18
use LeadersLinked\Library\Image;
17002 efrain 19
use LeadersLinked\Library\Storage;
15540 efrain 20
 
21
class DailyPulseEmojiController extends AbstractActionController
22
{
23
    /**
24
     *
16769 efrain 25
     * @var \Laminas\Db\Adapter\AdapterInterface
15540 efrain 26
     */
27
    private $adapter;
28
 
29
    /**
30
     *
16769 efrain 31
     * @var \LeadersLinked\Cache\CacheInterface
15540 efrain 32
     */
16769 efrain 33
    private $cache;
34
 
35
 
36
    /**
37
     *
38
     * @var \Laminas\Log\LoggerInterface
39
     */
15540 efrain 40
    private $logger;
41
 
42
    /**
43
     *
44
     * @var array
45
     */
46
    private $config;
47
 
16769 efrain 48
 
15540 efrain 49
    /**
50
     *
16769 efrain 51
     * @var \Laminas\Mvc\I18n\Translator
52
     */
53
    private $translator;
54
 
55
 
56
    /**
57
     *
58
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
59
     * @param \LeadersLinked\Cache\CacheInterface $cache
60
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15540 efrain 61
     * @param array $config
16769 efrain 62
     * @param \Laminas\Mvc\I18n\Translator $translator
15540 efrain 63
     */
16769 efrain 64
    public function __construct($adapter, $cache, $logger, $config, $translator)
15540 efrain 65
    {
16769 efrain 66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
        $this->translator   = $translator;
15540 efrain 71
    }
72
 
73
    public function indexAction()
74
    {
75
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
76
        $currentNetwork = $currentNetworkPlugin->getNetwork();
77
 
78
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
79
        $currentUser        = $currentUserPlugin->getUser();
80
        $currentCompany     = $currentUserPlugin->getCompany();
81
 
82
        $request = $this->getRequest();
83
        if($request->isGet()) {
84
 
85
 
86
            $headers  = $request->getHeaders();
87
 
88
            $isJson = false;
89
            if($headers->has('Accept')) {
90
                $accept = $headers->get('Accept');
91
 
92
                $prioritized = $accept->getPrioritized();
93
 
94
                foreach($prioritized as $key => $value) {
95
                    $raw = trim($value->getRaw());
96
 
97
                    if(!$isJson) {
98
                        $isJson = strpos($raw, 'json');
99
                    }
100
 
101
                }
102
            }
103
 
104
            if($isJson) {
105
                $search = $this->params()->fromQuery('search', []);
16766 efrain 106
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
15540 efrain 107
 
108
                $page               = intval($this->params()->fromQuery('start', 1), 10);
109
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
110
                $order =  $this->params()->fromQuery('order', []);
111
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 112
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
15540 efrain 113
 
114
                $fields =  ['name'];
115
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
116
 
117
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
118
                    $order_direction = 'ASC';
119
                }
120
 
121
                $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
122
                $paginator = $dailyPulseEmojiMapper->fetchAllDataTable($currentCompany->id, $search,  $page, $records_x_page, $order_field, $order_direction);
123
 
17002 efrain 124
                $storage = Storage::getInstance($this->config);
125
                $path = $storage->getPathDailyPulse();
15540 efrain 126
 
127
                $items = [];
128
                $records = $paginator->getCurrentItems();
129
                foreach($records as $record)
130
                {
131
                    switch($record->type)
132
                    {
133
                        case DailyPulseEmoji::TYPE_HOW_ARE_YOU_FEEL :
134
                            $type = 'LABEL_HOW_ARE_YOU_FEEL';
135
                            break;
136
 
137
                        case DailyPulseEmoji::TYPE_CLIMATE_ON_YOUR_ORGANIZATION :
138
                            $type = 'LABEL_CLIMATE_ON_YOUR_ORGANIZATION';
139
                            break;
140
 
141
                        default :
142
                            $type = 'LABEL_UNKNOWN';
143
                            break;
144
 
145
                    }
146
 
147
                    switch($record->status)
148
                    {
149
                        case DailyPulseEmoji::STATUS_ACTIVE :
150
                            $status = 'LABEL_ACTIVE';
151
                            break;
152
 
153
                        case DailyPulseEmoji::STATUS_INACTIVE :
154
                            $status = 'LABEL_INACTIVE';
155
                            break;
156
 
157
                        default :
158
                            $status = 'LABEL_UNKNOWN';
159
                            break;
160
                    }
161
 
162
 
163
 
164
                    $item = [
165
                        'name' => $record->name,
166
                        'details' => [
167
                            'type' => $type,
168
                            'status' => $status,
169
                            'order' => $record->order,
170
                            'points' => $record->points,
171
                        ],
17002 efrain 172
                        'image' => $storage->getGenericImage($path, $record->uuid, $record->image),
15540 efrain 173
                        'actions' => [
174
                            'link_edit' => $this->url()->fromRoute('daily-pulse/emojis/edit', ['id' => $record->uuid ]),
175
                            'link_delete' => $this->url()->fromRoute('daily-pulse/emojis/delete', ['id' => $record->uuid ]),
176
                        ]
177
                    ];
178
 
179
                    array_push($items, $item);
180
                }
181
 
182
                return new JsonModel([
183
                    'success' => true,
184
                    'data' => [
185
                        'items' => $items,
186
                        'total' => $paginator->getTotalItemCount(),
187
                    ]
188
                ]);
189
            } else  {
15542 efrain 190
 
191
                $target_size = $this->config['leaderslinked.image_sizes.emoji'];
192
 
15540 efrain 193
                $formAdd = new DailyPulseAddEmojiForm();
194
                $formEdit = new DailyPulseEditEmojiForm();
195
 
196
                $this->layout()->setTemplate('layout/layout-backend');
197
                $viewModel = new ViewModel();
16796 efrain 198
                $viewModel->setTemplate('leaders-linked/daily-pulse/emojis');
15540 efrain 199
                $viewModel->setVariables([
200
                    'formAdd' => $formAdd,
201
                    'formEdit' => $formEdit,
15542 efrain 202
                    'targetSize' => $target_size,
15540 efrain 203
                ]);
204
                return $viewModel ;
205
            }
206
        } else {
207
            return new JsonModel([
208
                'success' => false,
209
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
210
            ]);;
211
        }
212
    }
213
 
214
 
215
 
216
    public function addAction()
217
    {
218
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
219
        $currentUser        = $currentUserPlugin->getUser();
220
        $currentCompany     = $currentUserPlugin->getCompany();
221
 
222
        $request            = $this->getRequest();
223
 
224
 
225
        if($request->isPost()) {
226
            $form = new  DailyPulseAddEmojiForm();
227
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
228
            $dataPost['status'] = empty($dataPost['status'])  ? DailyPulseEmoji::STATUS_INACTIVE : $dataPost['status'];
229
 
230
 
231
            $form->setData($dataPost);
232
 
233
            if($form->isValid()) {
234
                $dataPost = (array) $form->getData();
235
 
236
                $dailyPulseEmoji = new DailyPulseEmoji();
237
                $dailyPulseEmoji->company_id = $currentCompany->id;
238
                $dailyPulseEmoji->name = $dataPost['name'];
239
                $dailyPulseEmoji->order = $dataPost['order'];
240
                $dailyPulseEmoji->points = $dataPost['points'];
241
                $dailyPulseEmoji->status = $dataPost['status'];
242
                $dailyPulseEmoji->type = $dataPost['type'];
243
                $dailyPulseEmoji->image = '';
244
 
245
                $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
246
                if($dailyPulseEmojiMapper->insert($dailyPulseEmoji)) {
247
 
248
                    $dailyPulseEmoji = $dailyPulseEmojiMapper->fetchOne($dailyPulseEmoji->id);
249
 
15542 efrain 250
                    $target_size = $this->config['leaderslinked.image_sizes.emoji'];
251
                    list($target_width, $target_height) = explode('x', $target_size);
252
 
253
 
17002 efrain 254
                    $image = Image::getInstance($this->config);
255
                    $target_path = $image->getStorage()->getPathDailyPulse();
15540 efrain 256
 
257
                    $files = $this->getRequest()->getFiles()->toArray();
258
 
259
 
260
                    if(isset($files['image']) && empty($files['image']['error'])) {
261
                        $tmp_filename  = $files['image']['tmp_name'];
262
                        $filename      = explode('.',  $files['image']['name']);
263
                        $filename       = Functions::normalizeStringFilename(uniqid() . '-' . $filename[0].'.png');
264
 
265
                        $crop_to_dimensions = true;
17002 efrain 266
                        $unlink_source = true;
15540 efrain 267
 
17002 efrain 268
                        if($image->uploadImageChangeSize($tmp_filename, $target_path, $dailyPulseEmoji->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
15540 efrain 269
                            $dailyPulseEmoji->image = $filename;
270
                            $dailyPulseEmojiMapper->update($dailyPulseEmoji);
271
                        }
272
                    }
273
 
274
 
275
                    $this->logger->info('Se agrego el emoji ' . $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
276
 
277
                    $data = [
278
                        'success'   => true,
279
                        'data'   => 'LABEL_RECORD_ADDED'
280
                    ];
281
                } else {
282
                    $data = [
283
                        'success'   => false,
284
                        'data'      => $dailyPulseEmojiMapper->getError()
285
                    ];
286
 
287
                }
288
 
289
                return new JsonModel($data);
290
 
291
            } else {
292
                $messages = [];
293
                $form_messages = (array) $form->getMessages();
294
                foreach($form_messages  as $fieldname => $field_messages)
295
                {
296
 
297
                    $messages[$fieldname] = array_values($field_messages);
298
                }
299
 
300
                return new JsonModel([
301
                    'success'   => false,
302
                    'data'   => $messages
303
                ]);
304
            }
305
 
306
        } else {
307
            $data = [
308
                'success' => false,
309
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
310
            ];
311
 
312
            return new JsonModel($data);
313
        }
314
 
315
        return new JsonModel($data);
316
    }
317
 
318
    /**
319
     *
320
     * Borrar un perfil excepto el público
321
     * @return \Laminas\View\Model\JsonModel
322
     */
323
    public function deleteAction()
324
    {
325
        $currentUserPlugin = $this->plugin('currentUserPlugin');
326
        $currentUser    = $currentUserPlugin->getUser();
327
        $currentCompany = $currentUserPlugin->getCompany();
328
 
329
        $request        = $this->getRequest();
330
        $id         = $this->params()->fromRoute('id');
331
 
332
 
333
 
334
        $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
335
        $dailyPulseEmoji = $dailyPulseEmojiMapper->fetchOneByUuid($id);
336
        if(!$dailyPulseEmoji) {
337
            return new JsonModel([
338
                'success'   => false,
339
                'data'   => 'ERROR_RECORD_NOT_FOUND'
340
            ]);
341
        }
342
 
343
        if($dailyPulseEmoji->company_id != $currentCompany->id) {
344
            return new JsonModel([
345
                'success'   => false,
346
                'data'   => 'ERROR_UNAUTHORIZED'
347
            ]);
348
        }
349
 
350
        if($request->isPost()) {
351
 
352
            $result =  $dailyPulseEmojiMapper->delete($dailyPulseEmoji);
353
            if($result) {
354
                $this->logger->info('Se borro el emoji : ' .  $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
355
 
356
                if($dailyPulseEmoji->image) {
17002 efrain 357
 
358
                    $image = Image::getInstance($this->config);
359
                    $target_path = $image->getStorage()->getPathDailyPulse();
360
                    $image->getStorage()->deleteFile($target_path, $dailyPulseEmoji->uuid, $dailyPulseEmoji->image);
15540 efrain 361
                }
362
 
363
                $data = [
364
                    'success' => true,
365
                    'data' => 'LABEL_RECORD_DELETED'
366
                ];
367
            } else {
368
 
369
                $data = [
370
                    'success'   => false,
371
                    'data'      => $dailyPulseEmojiMapper->getError()
372
                ];
373
 
374
                return new JsonModel($data);
375
            }
376
 
377
        } else {
378
            $data = [
379
                'success' => false,
380
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
381
            ];
382
 
383
            return new JsonModel($data);
384
        }
385
 
386
        return new JsonModel($data);
387
    }
388
 
389
 
390
    public function editAction()
391
    {
392
        $currentUserPlugin = $this->plugin('currentUserPlugin');
393
        $currentUser    = $currentUserPlugin->getUser();
394
        $currentCompany = $currentUserPlugin->getCompany();
395
 
396
        $request    = $this->getRequest();
397
        $id    = $this->params()->fromRoute('id');
398
 
399
 
400
        $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
401
        $dailyPulseEmoji = $dailyPulseEmojiMapper->fetchOneByUuid($id);
402
        if(!$dailyPulseEmoji) {
403
            return new JsonModel([
404
                'success'   => false,
405
                'data'   => 'ERROR_RECORD_NOT_FOUND'
406
            ]);
407
        }
408
 
409
 
410
        if($dailyPulseEmoji->company_id != $currentCompany->id) {
411
            return new JsonModel([
412
                'success'   => false,
413
                'data'   => 'ERROR_UNAUTHORIZED'
414
            ]);
415
        }
416
 
417
        if($request->isGet()) {
17002 efrain 418
            $storage = Storage::getInstance($this->config);
419
            $path = $storage->getPathDailyPulse();
420
 
421
 
15540 efrain 422
            $data = [
423
                'success' => true,
424
                'data' => [
425
                    'name' => $dailyPulseEmoji->name,
426
                    'order' => $dailyPulseEmoji->order,
427
                    'status' => $dailyPulseEmoji->status,
428
                    'type' => $dailyPulseEmoji->type,
429
                    'points' => $dailyPulseEmoji->points,
17002 efrain 430
                    'image' => $dailyPulseEmoji->image ? $storage->getGenericImage($path,  $dailyPulseEmoji->uuid, $dailyPulseEmoji->image) : '',
15540 efrain 431
                ]
432
            ];
433
 
434
            return new JsonModel($data);
435
        }
436
        else if($request->isPost()) {
437
            $form = new  DailyPulseEditEmojiForm();
438
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
439
            $dataPost['status'] = empty($dataPost['status'])  ? DailyPulseEmoji::STATUS_INACTIVE : $dataPost['status'];
440
 
441
 
442
            $form->setData($dataPost);
443
 
444
            if($form->isValid()) {
445
                $dataPost = (array) $form->getData();
446
 
447
                $dailyPulseEmoji->name = $dataPost['name'];
448
                $dailyPulseEmoji->order = $dataPost['order'];
449
                $dailyPulseEmoji->points = $dataPost['points'];
450
                $dailyPulseEmoji->status = $dataPost['status'];
451
                $dailyPulseEmoji->type = $dataPost['type'];
452
 
453
                if($dailyPulseEmojiMapper->update($dailyPulseEmoji)) {
454
 
455
 
15542 efrain 456
 
457
                    $target_size = $this->config['leaderslinked.image_sizes.emoji'];
458
                    list($target_width, $target_height) = explode('x', $target_size);
17002 efrain 459
 
15542 efrain 460
 
461
 
462
 
15540 efrain 463
                    $files = $this->getRequest()->getFiles()->toArray();
464
                    if(isset($files['image']) && empty($files['image']['error'])) {
465
 
17002 efrain 466
                        $image = Image::getInstance($this->config);
467
                        $target_path = $image->getStorage()->getPathDailyPulse();
468
 
15540 efrain 469
                        $tmp_filename  = $files['image']['tmp_name'];
470
                        $filename      = explode('.',  $files['image']['name']);
471
                        $filename       = Functions::normalizeStringFilename(uniqid() . '-' . $filename[0].'.png');
472
 
473
                        $crop_to_dimensions = true;
17002 efrain 474
                        $unlink_source = true;
15542 efrain 475
 
17002 efrain 476
                        if($image->uploadImageChangeSize($tmp_filename, $target_path, $dailyPulseEmoji->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
15540 efrain 477
 
478
                            if($dailyPulseEmoji->image) {
17002 efrain 479
                                $image->getStorage()->deleteFile($target_path, $dailyPulseEmoji->uuid, $dailyPulseEmoji->image);
15540 efrain 480
                            }
481
 
482
                            $dailyPulseEmoji->image = $filename;
483
                            $dailyPulseEmojiMapper->update($dailyPulseEmoji);
484
                        }
485
                    }
486
 
487
 
488
 
489
                    $this->logger->info('Se edito el emoji' . $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
490
 
491
                    $data = [
492
                        'success'   => true,
493
                        'data'   => 'LABEL_RECORD_UPDATED'
494
                    ];
495
                } else {
496
                    $data = [
497
                        'success'   => false,
498
                        'data'      => $dailyPulseEmojiMapper->getError()
499
                    ];
500
 
501
                }
502
 
503
                return new JsonModel($data);
504
 
505
            } else {
506
                $messages = [];
507
                $form_messages = (array) $form->getMessages();
508
                foreach($form_messages  as $fieldname => $field_messages)
509
                {
510
 
511
                    $messages[$fieldname] = array_values($field_messages);
512
                }
513
 
514
                return new JsonModel([
515
                    'success'   => false,
516
                    'data'   => $messages
517
                ]);
518
            }
519
        } else {
520
            $data = [
521
                'success' => false,
522
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
523
            ];
524
 
525
            return new JsonModel($data);
526
        }
527
 
528
        return new JsonModel($data);
529
    }
530
 
531
 
532
}