Proyectos de Subversion LeadersLinked - Backend

Rev

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

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