Proyectos de Subversion LeadersLinked - Backend

Rev

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

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