Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | Rev 17002 | 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;
19
 
20
class DailyPulseEmojiController extends AbstractActionController
21
{
22
    /**
23
     *
16769 efrain 24
     * @var \Laminas\Db\Adapter\AdapterInterface
15540 efrain 25
     */
26
    private $adapter;
27
 
28
    /**
29
     *
16769 efrain 30
     * @var \LeadersLinked\Cache\CacheInterface
15540 efrain 31
     */
16769 efrain 32
    private $cache;
33
 
34
 
35
    /**
36
     *
37
     * @var \Laminas\Log\LoggerInterface
38
     */
15540 efrain 39
    private $logger;
40
 
41
    /**
42
     *
43
     * @var array
44
     */
45
    private $config;
46
 
16769 efrain 47
 
15540 efrain 48
    /**
49
     *
16769 efrain 50
     * @var \Laminas\Mvc\I18n\Translator
51
     */
52
    private $translator;
53
 
54
 
55
    /**
56
     *
57
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
58
     * @param \LeadersLinked\Cache\CacheInterface $cache
59
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15540 efrain 60
     * @param array $config
16769 efrain 61
     * @param \Laminas\Mvc\I18n\Translator $translator
15540 efrain 62
     */
16769 efrain 63
    public function __construct($adapter, $cache, $logger, $config, $translator)
15540 efrain 64
    {
16769 efrain 65
        $this->adapter      = $adapter;
66
        $this->cache        = $cache;
67
        $this->logger       = $logger;
68
        $this->config       = $config;
69
        $this->translator   = $translator;
15540 efrain 70
    }
71
 
72
    public function indexAction()
73
    {
74
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
75
        $currentNetwork = $currentNetworkPlugin->getNetwork();
76
 
77
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
78
        $currentUser        = $currentUserPlugin->getUser();
79
        $currentCompany     = $currentUserPlugin->getCompany();
80
 
81
        $request = $this->getRequest();
82
        if($request->isGet()) {
83
 
84
 
85
            $headers  = $request->getHeaders();
86
 
87
            $isJson = false;
88
            if($headers->has('Accept')) {
89
                $accept = $headers->get('Accept');
90
 
91
                $prioritized = $accept->getPrioritized();
92
 
93
                foreach($prioritized as $key => $value) {
94
                    $raw = trim($value->getRaw());
95
 
96
                    if(!$isJson) {
97
                        $isJson = strpos($raw, 'json');
98
                    }
99
 
100
                }
101
            }
102
 
103
            if($isJson) {
104
                $search = $this->params()->fromQuery('search', []);
16766 efrain 105
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
15540 efrain 106
 
107
                $page               = intval($this->params()->fromQuery('start', 1), 10);
108
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
109
                $order =  $this->params()->fromQuery('order', []);
110
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 111
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
15540 efrain 112
 
113
                $fields =  ['name'];
114
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
115
 
116
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
117
                    $order_direction = 'ASC';
118
                }
119
 
120
                $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
121
                $paginator = $dailyPulseEmojiMapper->fetchAllDataTable($currentCompany->id, $search,  $page, $records_x_page, $order_field, $order_direction);
122
 
123
 
124
                $items = [];
125
                $records = $paginator->getCurrentItems();
126
                foreach($records as $record)
127
                {
128
                    switch($record->type)
129
                    {
130
                        case DailyPulseEmoji::TYPE_HOW_ARE_YOU_FEEL :
131
                            $type = 'LABEL_HOW_ARE_YOU_FEEL';
132
                            break;
133
 
134
                        case DailyPulseEmoji::TYPE_CLIMATE_ON_YOUR_ORGANIZATION :
135
                            $type = 'LABEL_CLIMATE_ON_YOUR_ORGANIZATION';
136
                            break;
137
 
138
                        default :
139
                            $type = 'LABEL_UNKNOWN';
140
                            break;
141
 
142
                    }
143
 
144
                    switch($record->status)
145
                    {
146
                        case DailyPulseEmoji::STATUS_ACTIVE :
147
                            $status = 'LABEL_ACTIVE';
148
                            break;
149
 
150
                        case DailyPulseEmoji::STATUS_INACTIVE :
151
                            $status = 'LABEL_INACTIVE';
152
                            break;
153
 
154
                        default :
155
                            $status = 'LABEL_UNKNOWN';
156
                            break;
157
                    }
158
 
159
 
160
 
161
                    $item = [
162
                        'name' => $record->name,
163
                        'details' => [
164
                            'type' => $type,
165
                            'status' => $status,
166
                            'order' => $record->order,
167
                            'points' => $record->points,
168
                        ],
169
                        'image' => $this->url()->fromRoute('storage', ['type' => 'daily-pulse', 'code' => $record->uuid, 'filename' => $record->image]),
170
                        'actions' => [
171
                            'link_edit' => $this->url()->fromRoute('daily-pulse/emojis/edit', ['id' => $record->uuid ]),
172
                            'link_delete' => $this->url()->fromRoute('daily-pulse/emojis/delete', ['id' => $record->uuid ]),
173
                        ]
174
                    ];
175
 
176
                    array_push($items, $item);
177
                }
178
 
179
                return new JsonModel([
180
                    'success' => true,
181
                    'data' => [
182
                        'items' => $items,
183
                        'total' => $paginator->getTotalItemCount(),
184
                    ]
185
                ]);
186
            } else  {
15542 efrain 187
 
188
                $target_size = $this->config['leaderslinked.image_sizes.emoji'];
189
 
15540 efrain 190
                $formAdd = new DailyPulseAddEmojiForm();
191
                $formEdit = new DailyPulseEditEmojiForm();
192
 
193
                $this->layout()->setTemplate('layout/layout-backend');
194
                $viewModel = new ViewModel();
16796 efrain 195
                $viewModel->setTemplate('leaders-linked/daily-pulse/emojis');
15540 efrain 196
                $viewModel->setVariables([
197
                    'formAdd' => $formAdd,
198
                    'formEdit' => $formEdit,
15542 efrain 199
                    'targetSize' => $target_size,
15540 efrain 200
                ]);
201
                return $viewModel ;
202
            }
203
        } else {
204
            return new JsonModel([
205
                'success' => false,
206
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
207
            ]);;
208
        }
209
    }
210
 
211
 
212
 
213
    public function addAction()
214
    {
215
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
216
        $currentUser        = $currentUserPlugin->getUser();
217
        $currentCompany     = $currentUserPlugin->getCompany();
218
 
219
        $request            = $this->getRequest();
220
 
221
 
222
        if($request->isPost()) {
223
            $form = new  DailyPulseAddEmojiForm();
224
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
225
            $dataPost['status'] = empty($dataPost['status'])  ? DailyPulseEmoji::STATUS_INACTIVE : $dataPost['status'];
226
 
227
 
228
            $form->setData($dataPost);
229
 
230
            if($form->isValid()) {
231
                $dataPost = (array) $form->getData();
232
 
233
                $dailyPulseEmoji = new DailyPulseEmoji();
234
                $dailyPulseEmoji->company_id = $currentCompany->id;
235
                $dailyPulseEmoji->name = $dataPost['name'];
236
                $dailyPulseEmoji->order = $dataPost['order'];
237
                $dailyPulseEmoji->points = $dataPost['points'];
238
                $dailyPulseEmoji->status = $dataPost['status'];
239
                $dailyPulseEmoji->type = $dataPost['type'];
240
                $dailyPulseEmoji->image = '';
241
 
242
                $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
243
                if($dailyPulseEmojiMapper->insert($dailyPulseEmoji)) {
244
 
245
                    $dailyPulseEmoji = $dailyPulseEmojiMapper->fetchOne($dailyPulseEmoji->id);
246
 
15542 efrain 247
                    $target_size = $this->config['leaderslinked.image_sizes.emoji'];
248
                    list($target_width, $target_height) = explode('x', $target_size);
249
 
250
 
15540 efrain 251
                    $target_path = $this->config['leaderslinked.fullpath.daily_pulse']  . $dailyPulseEmoji->uuid;
252
                    if(!file_exists($target_path)) {
253
                        mkdir($target_path, 0755, true);
254
                    }
255
 
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;
15542 efrain 266
 
15540 efrain 267
 
268
                        if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
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) {
357
 
358
                    $target_path = $this->config['leaderslinked.fullpath.daily_pulse']  . $dailyPulseEmoji->image;
359
                    if(file_exists($target_path)) {
360
                        Functions::rmDirRecursive($target_path);
361
                    }
362
 
363
                }
364
 
365
                $data = [
366
                    'success' => true,
367
                    'data' => 'LABEL_RECORD_DELETED'
368
                ];
369
            } else {
370
 
371
                $data = [
372
                    'success'   => false,
373
                    'data'      => $dailyPulseEmojiMapper->getError()
374
                ];
375
 
376
                return new JsonModel($data);
377
            }
378
 
379
        } else {
380
            $data = [
381
                'success' => false,
382
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
383
            ];
384
 
385
            return new JsonModel($data);
386
        }
387
 
388
        return new JsonModel($data);
389
    }
390
 
391
 
392
    public function editAction()
393
    {
394
        $currentUserPlugin = $this->plugin('currentUserPlugin');
395
        $currentUser    = $currentUserPlugin->getUser();
396
        $currentCompany = $currentUserPlugin->getCompany();
397
 
398
        $request    = $this->getRequest();
399
        $id    = $this->params()->fromRoute('id');
400
 
401
 
402
        $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
403
        $dailyPulseEmoji = $dailyPulseEmojiMapper->fetchOneByUuid($id);
404
        if(!$dailyPulseEmoji) {
405
            return new JsonModel([
406
                'success'   => false,
407
                'data'   => 'ERROR_RECORD_NOT_FOUND'
408
            ]);
409
        }
410
 
411
 
412
        if($dailyPulseEmoji->company_id != $currentCompany->id) {
413
            return new JsonModel([
414
                'success'   => false,
415
                'data'   => 'ERROR_UNAUTHORIZED'
416
            ]);
417
        }
418
 
419
        if($request->isGet()) {
420
            $data = [
421
                'success' => true,
422
                'data' => [
423
                    'name' => $dailyPulseEmoji->name,
424
                    'order' => $dailyPulseEmoji->order,
425
                    'status' => $dailyPulseEmoji->status,
426
                    'type' => $dailyPulseEmoji->type,
427
                    'points' => $dailyPulseEmoji->points,
428
                    'image' => $dailyPulseEmoji->image ? $this->url()->fromRoute('storage', ['type' => 'daily-pulse', 'code' => $dailyPulseEmoji->uuid, 'filename' => $dailyPulseEmoji->image]) : '',
429
                ]
430
            ];
431
 
432
            return new JsonModel($data);
433
        }
434
        else if($request->isPost()) {
435
            $form = new  DailyPulseEditEmojiForm();
436
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
437
            $dataPost['status'] = empty($dataPost['status'])  ? DailyPulseEmoji::STATUS_INACTIVE : $dataPost['status'];
438
 
439
 
440
            $form->setData($dataPost);
441
 
442
            if($form->isValid()) {
443
                $dataPost = (array) $form->getData();
444
 
445
                $dailyPulseEmoji->name = $dataPost['name'];
446
                $dailyPulseEmoji->order = $dataPost['order'];
447
                $dailyPulseEmoji->points = $dataPost['points'];
448
                $dailyPulseEmoji->status = $dataPost['status'];
449
                $dailyPulseEmoji->type = $dataPost['type'];
450
 
451
                if($dailyPulseEmojiMapper->update($dailyPulseEmoji)) {
452
 
453
 
15542 efrain 454
 
455
                    $target_size = $this->config['leaderslinked.image_sizes.emoji'];
456
                    list($target_width, $target_height) = explode('x', $target_size);
457
 
458
 
459
 
15540 efrain 460
                    $target_path = $this->config['leaderslinked.fullpath.daily_pulse']  . $dailyPulseEmoji->uuid;
461
                    if(!file_exists($target_path)) {
462
                        mkdir($target_path, 0755, true);
463
                    }
464
 
465
 
466
                    $files = $this->getRequest()->getFiles()->toArray();
467
                    if(isset($files['image']) && empty($files['image']['error'])) {
468
 
469
 
470
                        $tmp_filename  = $files['image']['tmp_name'];
471
                        $filename      = explode('.',  $files['image']['name']);
472
                        $filename       = Functions::normalizeStringFilename(uniqid() . '-' . $filename[0].'.png');
473
 
474
                        $crop_to_dimensions = true;
15542 efrain 475
 
15540 efrain 476
 
477
                        if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
478
 
479
                            if($dailyPulseEmoji->image) {
480
 
481
                                $target_path_delete = $target_path . $dailyPulseEmoji->image;
482
                                if(file_exists($target_path_delete)) {
483
                                    Functions::rmDirRecursive($target_path_delete);
484
                                }
485
 
486
                            }
487
 
488
                            $dailyPulseEmoji->image = $filename;
489
                            $dailyPulseEmojiMapper->update($dailyPulseEmoji);
490
                        }
491
                    }
492
 
493
 
494
 
495
                    $this->logger->info('Se edito el emoji' . $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
496
 
497
                    $data = [
498
                        'success'   => true,
499
                        'data'   => 'LABEL_RECORD_UPDATED'
500
                    ];
501
                } else {
502
                    $data = [
503
                        'success'   => false,
504
                        'data'      => $dailyPulseEmojiMapper->getError()
505
                    ];
506
 
507
                }
508
 
509
                return new JsonModel($data);
510
 
511
            } else {
512
                $messages = [];
513
                $form_messages = (array) $form->getMessages();
514
                foreach($form_messages  as $fieldname => $field_messages)
515
                {
516
 
517
                    $messages[$fieldname] = array_values($field_messages);
518
                }
519
 
520
                return new JsonModel([
521
                    'success'   => false,
522
                    'data'   => $messages
523
                ]);
524
            }
525
        } else {
526
            $data = [
527
                'success' => false,
528
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
529
            ];
530
 
531
            return new JsonModel($data);
532
        }
533
 
534
        return new JsonModel($data);
535
    }
536
 
537
 
538
}