Proyectos de Subversion LeadersLinked - Backend

Rev

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