Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17002 | | 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
 
17018 efrain 124
                $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 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
 
17018 efrain 254
 
17002 efrain 255
                    $image = Image::getInstance($this->config);
17018 efrain 256
                    $storage = Storage::getInstance($this->config, $this->adapter);
257
                    $target_path = $storage->getPathDailyPulse();
15540 efrain 258
 
259
                    $files = $this->getRequest()->getFiles()->toArray();
260
 
261
 
262
                    if(isset($files['image']) && empty($files['image']['error'])) {
263
                        $tmp_filename  = $files['image']['tmp_name'];
264
                        $filename      = explode('.',  $files['image']['name']);
265
                        $filename       = Functions::normalizeStringFilename(uniqid() . '-' . $filename[0].'.png');
266
 
267
                        $crop_to_dimensions = true;
17002 efrain 268
                        $unlink_source = true;
15540 efrain 269
 
17018 efrain 270
                        if($image->uploadProcessChangeSize($tmp_filename, $target_path, $dailyPulseEmoji->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
15540 efrain 271
                            $dailyPulseEmoji->image = $filename;
272
                            $dailyPulseEmojiMapper->update($dailyPulseEmoji);
273
                        }
274
                    }
275
 
276
 
277
                    $this->logger->info('Se agrego el emoji ' . $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
278
 
279
                    $data = [
280
                        'success'   => true,
281
                        'data'   => 'LABEL_RECORD_ADDED'
282
                    ];
283
                } else {
284
                    $data = [
285
                        'success'   => false,
286
                        'data'      => $dailyPulseEmojiMapper->getError()
287
                    ];
288
 
289
                }
290
 
291
                return new JsonModel($data);
292
 
293
            } else {
294
                $messages = [];
295
                $form_messages = (array) $form->getMessages();
296
                foreach($form_messages  as $fieldname => $field_messages)
297
                {
298
 
299
                    $messages[$fieldname] = array_values($field_messages);
300
                }
301
 
302
                return new JsonModel([
303
                    'success'   => false,
304
                    'data'   => $messages
305
                ]);
306
            }
307
 
308
        } else {
309
            $data = [
310
                'success' => false,
311
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
312
            ];
313
 
314
            return new JsonModel($data);
315
        }
316
 
317
        return new JsonModel($data);
318
    }
319
 
320
    /**
321
     *
322
     * Borrar un perfil excepto el público
323
     * @return \Laminas\View\Model\JsonModel
324
     */
325
    public function deleteAction()
326
    {
327
        $currentUserPlugin = $this->plugin('currentUserPlugin');
328
        $currentUser    = $currentUserPlugin->getUser();
329
        $currentCompany = $currentUserPlugin->getCompany();
330
 
331
        $request        = $this->getRequest();
332
        $id         = $this->params()->fromRoute('id');
333
 
334
 
335
 
336
        $dailyPulseEmojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
337
        $dailyPulseEmoji = $dailyPulseEmojiMapper->fetchOneByUuid($id);
338
        if(!$dailyPulseEmoji) {
339
            return new JsonModel([
340
                'success'   => false,
341
                'data'   => 'ERROR_RECORD_NOT_FOUND'
342
            ]);
343
        }
344
 
345
        if($dailyPulseEmoji->company_id != $currentCompany->id) {
346
            return new JsonModel([
347
                'success'   => false,
348
                'data'   => 'ERROR_UNAUTHORIZED'
349
            ]);
350
        }
351
 
352
        if($request->isPost()) {
353
 
354
            $result =  $dailyPulseEmojiMapper->delete($dailyPulseEmoji);
355
            if($result) {
356
                $this->logger->info('Se borro el emoji : ' .  $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
357
 
358
                if($dailyPulseEmoji->image) {
17018 efrain 359
 
360
                    $storage = Storage::getInstance($this->config, $this->adapter);
361
                    $target_path = $storage->getPathDailyPulse();
362
                    $storage->deleteFile($target_path, $dailyPulseEmoji->uuid, $dailyPulseEmoji->image);
15540 efrain 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()) {
17018 efrain 420
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 421
            $path = $storage->getPathDailyPulse();
422
 
423
 
15540 efrain 424
            $data = [
425
                'success' => true,
426
                'data' => [
427
                    'name' => $dailyPulseEmoji->name,
428
                    'order' => $dailyPulseEmoji->order,
429
                    'status' => $dailyPulseEmoji->status,
430
                    'type' => $dailyPulseEmoji->type,
431
                    'points' => $dailyPulseEmoji->points,
17002 efrain 432
                    'image' => $dailyPulseEmoji->image ? $storage->getGenericImage($path,  $dailyPulseEmoji->uuid, $dailyPulseEmoji->image) : '',
15540 efrain 433
                ]
434
            ];
435
 
436
            return new JsonModel($data);
437
        }
438
        else if($request->isPost()) {
439
            $form = new  DailyPulseEditEmojiForm();
440
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
441
            $dataPost['status'] = empty($dataPost['status'])  ? DailyPulseEmoji::STATUS_INACTIVE : $dataPost['status'];
442
 
443
 
444
            $form->setData($dataPost);
445
 
446
            if($form->isValid()) {
447
                $dataPost = (array) $form->getData();
448
 
449
                $dailyPulseEmoji->name = $dataPost['name'];
450
                $dailyPulseEmoji->order = $dataPost['order'];
451
                $dailyPulseEmoji->points = $dataPost['points'];
452
                $dailyPulseEmoji->status = $dataPost['status'];
453
                $dailyPulseEmoji->type = $dataPost['type'];
454
 
455
                if($dailyPulseEmojiMapper->update($dailyPulseEmoji)) {
456
 
457
 
15542 efrain 458
 
459
                    $target_size = $this->config['leaderslinked.image_sizes.emoji'];
460
                    list($target_width, $target_height) = explode('x', $target_size);
17002 efrain 461
 
15542 efrain 462
 
463
 
464
 
15540 efrain 465
                    $files = $this->getRequest()->getFiles()->toArray();
466
                    if(isset($files['image']) && empty($files['image']['error'])) {
467
 
17002 efrain 468
                        $image = Image::getInstance($this->config);
17018 efrain 469
                        $storage = Storage::getInstance($this->config, $this->adapter);
470
                        $target_path = $storage->getPathDailyPulse();
17002 efrain 471
 
15540 efrain 472
                        $tmp_filename  = $files['image']['tmp_name'];
473
                        $filename      = explode('.',  $files['image']['name']);
474
                        $filename       = Functions::normalizeStringFilename(uniqid() . '-' . $filename[0].'.png');
475
 
476
                        $crop_to_dimensions = true;
17018 efrain 477
                        $full_tmp_filename = $image->uploadProcessChangeSize($tmp_filename, $filename, $target_width, $target_height,  $crop_to_dimensions);
478
 
479
                        if($full_tmp_filename ) {
15540 efrain 480
                            if($dailyPulseEmoji->image) {
17018 efrain 481
                                $storage->deleteFile($target_path, $dailyPulseEmoji->uuid, $dailyPulseEmoji->image);
482
 
483
                                $dailyPulseEmoji->image = $filename;
484
                                $dailyPulseEmojiMapper->update($dailyPulseEmoji);
15540 efrain 485
                            }
486
 
17018 efrain 487
                            if($storage->putFile($target_path, $dailyPulseEmoji->uuid, $full_tmp_filename)) {
488
 
489
 
490
 
491
                                $dailyPulseEmoji->image = $filename;
492
                                $dailyPulseEmojiMapper->update($dailyPulseEmoji);
493
                            }
15540 efrain 494
                        }
495
                    }
496
 
497
 
498
 
499
                    $this->logger->info('Se edito el emoji' . $dailyPulseEmoji->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
500
 
501
                    $data = [
502
                        'success'   => true,
503
                        'data'   => 'LABEL_RECORD_UPDATED'
504
                    ];
505
                } else {
506
                    $data = [
507
                        'success'   => false,
508
                        'data'      => $dailyPulseEmojiMapper->getError()
509
                    ];
510
 
511
                }
512
 
513
                return new JsonModel($data);
514
 
515
            } else {
516
                $messages = [];
517
                $form_messages = (array) $form->getMessages();
518
                foreach($form_messages  as $fieldname => $field_messages)
519
                {
520
 
521
                    $messages[$fieldname] = array_values($field_messages);
522
                }
523
 
524
                return new JsonModel([
525
                    'success'   => false,
526
                    'data'   => $messages
527
                ]);
528
            }
529
        } else {
530
            $data = [
531
                'success' => false,
532
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
533
            ];
534
 
535
            return new JsonModel($data);
536
        }
537
 
538
        return new JsonModel($data);
539
    }
540
 
541
 
542
}