Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14989 | Rev 15460 | 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;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
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
     *
29
     * @var AbstractAdapter
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
48
     * @param AbstractAdapter $cache
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');
101
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
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);
107
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
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,
368
                'data' => $hydrator->extract($behavior)
369
            ];
1115 geraldo 370
 
1102 geraldo 371
            return new JsonModel($data);
372
        } else {
373
            $data = [
374
                'success' => false,
375
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
376
            ];
1115 geraldo 377
 
1102 geraldo 378
            return new JsonModel($data);
379
        }
1115 geraldo 380
 
1102 geraldo 381
        return new JsonModel($data);
382
    }
1115 geraldo 383
 
14989 efrain 384
    public function deleteAction()
385
    {
14303 kerby 386
        $currentUserPlugin = $this->plugin('currentUserPlugin');
387
        $currentUser = $currentUserPlugin->getUser();
14989 efrain 388
        $currentCompany = $currentUserPlugin->getCompany();
1115 geraldo 389
 
14303 kerby 390
        $request = $this->getRequest();
14989 efrain 391
        if ($request->isPost()) {
392
 
393
            $id = $this->params()->fromRoute('id');
394
            if (!$id) {
395
                $response = [
396
                    'success' => false,
397
                    'data' => 'ERROR_INVALID_PARAMETER'
398
                ];
399
 
400
                return new JsonModel($response);
401
            }
402
 
403
 
404
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
405
            $behavior = $behaviorMapper->fetchOneByUuid($id);
406
            if (!$behavior) {
407
                $response = [
408
                    'success' => false,
409
                    'data' => 'ERROR_RECORD_NOT_FOUND'
410
                ];
411
 
412
                return new JsonModel($response);
413
            }
414
 
415
            if($currentCompany) {
416
 
417
                if($behavior->company_id != $currentCompany->id) {
418
                    $response = [
419
                        'success' => false,
420
                        'data' =>  'ERROR_UNAUTHORIZED'
421
                    ];
422
 
423
                    return new JsonModel($response);
424
                }
425
 
426
            } else {
427
                if($behavior->company_id) {
428
                    $response = [
429
                        'success' => false,
430
                        'data' =>  'ERROR_UNAUTHORIZED'
431
                    ];
432
 
433
                    return new JsonModel($response);
434
                }
435
            }
1115 geraldo 436
 
14989 efrain 437
 
1115 geraldo 438
 
14989 efrain 439
 
440
            $result = $behaviorMapper->delete($behavior->id);
441
            if ($result) {
442
                $this->logger->info('Se borro la conducta observable : ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1115 geraldo 443
 
14989 efrain 444
                $response = [
445
                    'success' => true,
446
                    'data' => 'LABEL_RECORD_DELETED'
447
                ];
448
            } else {
449
 
450
                $response = [
451
                    'success' => false,
452
                    'data' => $behaviorMapper->getError()
453
                ];
454
 
455
            }
456
        } else {
457
            $response = [
14305 kerby 458
                'success' => false,
14989 efrain 459
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
14305 kerby 460
            ];
1115 geraldo 461
 
14305 kerby 462
        }
14291 kerby 463
 
14989 efrain 464
        return new JsonModel($response);
465
    }
466
 
467
    public function importAction() {
468
        $currentUserPlugin = $this->plugin('currentUserPlugin');
469
        $currentUser = $currentUserPlugin->getUser();
470
        $currentCompany = $currentUserPlugin->getCompany();
471
 
472
        if (!$currentCompany) {
14305 kerby 473
            $data = [
474
                'success' => false,
14989 efrain 475
                'data' => 'ERROR_UNAUTHORIZED'
14305 kerby 476
            ];
14989 efrain 477
 
14305 kerby 478
            return new JsonModel($data);
479
        }
14989 efrain 480
 
481
        $request = $this->getRequest();
482
 
14306 kerby 483
        if ($request->isPost()) {
14989 efrain 484
 
485
            $behaviorMapper =BehaviorMapper::getInstance($this->adapter);
486
            $behaviorsDefault = $behaviorMapper->fetchAllByDefault();
487
 
488
            $new_records = 0;
489
            foreach ($behaviorsDefault as $behaviorDefault) {
490
 
491
                $behavior = $behaviorMapper->fetchOneByCompanyIdAndIdDefault($currentCompany->id, $behaviorDefault->id);
492
                if (!$behavior) {
493
                    $behavior = new Behavior();
494
                    $behavior->behavior_id_default  = $behaviorDefault->id;
495
                    $behavior->company_id           = $currentCompany->id;
496
                    $behavior->description          = $behaviorDefault->description;
497
                    $behavior->status               = $behaviorDefault->status;
498
 
499
 
500
 
501
 
502
 
503
                    if ($behaviorMapper->insert($behavior)) {
504
                        $new_records++;
505
                    } else {
506
                        $data = [
507
                            'success' => false,
508
                            'data' => 'ERROR_CANT_ADD_BEHAVIOR'
509
                        ];
510
 
511
                        return new JsonModel($data);
512
                    }
513
                }
514
            }
1115 geraldo 515
 
14989 efrain 516
            if ($new_records) {
517
 
518
                if (1 == $new_records) {
519
                    $data = [
520
                        'success' => true,
521
                        'data' => 'LABEL_1_BEHAVIOR_IMPORTED'
522
                    ];
523
 
524
                    return new JsonModel($data);
525
                } else {
526
                    $data = [
527
                        'success' => true,
528
                        'data' => $new_records . ' LABEL_MULTI_BEHAVIORS_IMPORTED'
529
                    ];
530
 
531
                    return new JsonModel($data);
532
                }
533
            } else {
14306 kerby 534
                $data = [
535
                    'success' => true,
14989 efrain 536
                    'data' => 'LABEL_NO_BEHAVIOR_IMPORTED'
14306 kerby 537
                ];
14989 efrain 538
 
14306 kerby 539
                return new JsonModel($data);
540
            }
541
        } else {
1102 geraldo 542
            $data = [
543
                'success' => false,
544
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
545
            ];
14989 efrain 546
 
14306 kerby 547
            return new JsonModel($data);
548
        }
14989 efrain 549
 
1102 geraldo 550
        return new JsonModel($data);
551
    }
14989 efrain 552
 
553
 
554
 
555
 
1115 geraldo 556
 
1102 geraldo 557
}