Proyectos de Subversion LeadersLinked - Backend

Rev

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