Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
977 geraldo 1
<?php
2
 
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;
14
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormMapper;
15
use LeadersLinked\Form\CompanyPerformanceEvaluationFormForm;
16
use LeadersLinked\Model\CompanyPerformanceEvaluationForm;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1052 geraldo 18
use LeadersLinked\Mapper\JobDescriptionMapper;
977 geraldo 19
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormUserMapper;
20
 
1051 geraldo 21
 
977 geraldo 22
class PerformanceEvaluationFormController extends AbstractActionController {
23
 
24
    /**
25
     *
26
     * @var AdapterInterface
27
     */
28
    private $adapter;
29
 
30
    /**
31
     *
32
     * @var AbstractAdapter
33
     */
34
    private $cache;
35
 
36
    /**
37
     *
38
     * @var  LoggerInterface
39
     */
40
    private $logger;
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
        $this->adapter = $adapter;
57
        $this->cache = $cache;
58
        $this->logger = $logger;
59
        $this->config = $config;
60
    }
61
 
62
    public function indexAction() {
63
        $request = $this->getRequest();
64
        $currentUserPlugin = $this->plugin('currentUserPlugin');
65
        $currentCompany = $currentUserPlugin->getCompany();
66
        $currentUser = $currentUserPlugin->getUser();
67
 
68
 
69
        $request = $this->getRequest();
70
        if ($request->isGet()) {
71
 
72
            $headers = $request->getHeaders();
73
 
74
            $isJson = false;
75
            if ($headers->has('Accept')) {
76
                $accept = $headers->get('Accept');
77
 
78
                $prioritized = $accept->getPrioritized();
79
 
80
                foreach ($prioritized as $key => $value) {
81
                    $raw = trim($value->getRaw());
82
 
83
                    if (!$isJson) {
84
                        $isJson = strpos($raw, 'json');
85
                    }
86
                }
87
            }
88
 
89
            if ($isJson) {
90
                $search = $this->params()->fromQuery('search', []);
91
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
92
 
93
                $page = intval($this->params()->fromQuery('start', 1), 10);
94
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
95
                $order = $this->params()->fromQuery('order', []);
96
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
97
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
98
 
99
                $fields = ['name'];
100
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
101
 
102
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
103
                    $order_direction = 'ASC';
104
                }
105
 
106
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
107
                $paginator = $companyPerformanceEvaluationMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
108
 
1078 geraldo 109
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
110
 
977 geraldo 111
                $items = [];
112
                $records = $paginator->getCurrentItems();
113
                foreach ($records as $record) {
114
 
1078 geraldo 115
 
116
 
117
                $jobDescription = $jobDescriptionMapper->fetchOne($record->job_description_id);
118
if($jobDescription){
119
 
977 geraldo 120
                    $item = [
121
                        'id' => $record->id,
122
                        'name' => $record->name,
1078 geraldo 123
                        'job_description' => $jobDescription->name,
977 geraldo 124
                        'status' => $record->status,
125
                        'actions' => [
126
                            'link_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
127
                            'link_delete' => $this->url()->fromRoute('performance-evaluation/forms/delete', ['id' => $record->uuid])
128
                        ]
129
                    ];
1078 geraldo 130
                }
977 geraldo 131
 
132
                    array_push($items, $item);
133
                }
134
 
135
                return new JsonModel([
136
                    'success' => true,
137
                    'data' => [
138
                        'items' => $items,
139
                        'total' => $paginator->getTotalItemCount(),
140
                    ]
141
                ]);
142
            } else {
143
 
1064 geraldo 144
                $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 145
 
1051 geraldo 146
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
147
                $jobsDescription = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
148
 
977 geraldo 149
                $this->layout()->setTemplate('layout/layout-backend');
150
                $viewModel = new ViewModel();
151
                $viewModel->setTemplate('leaders-linked/performance-evaluation-forms/index.phtml');
152
                $viewModel->setVariable('form', $form);
1051 geraldo 153
                $viewModel->setVariable('jobsDescription', $jobsDescription);
977 geraldo 154
                return $viewModel;
155
            }
156
        } else {
157
            return new JsonModel([
158
                'success' => false,
159
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
160
            ]);
161
            ;
162
        }
163
    }
164
 
165
    public function addAction() {
166
        $request = $this->getRequest();
167
        $currentUserPlugin = $this->plugin('currentUserPlugin');
168
        $currentCompany = $currentUserPlugin->getCompany();
169
        $currentUser = $currentUserPlugin->getUser();
170
 
171
        $request = $this->getRequest();
172
 
173
 
174
        if ($request->isPost()) {
1064 geraldo 175
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 176
            $dataPost = $request->getPost()->toArray();
1059 geraldo 177
 
178
 
977 geraldo 179
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
180
 
181
            $form->setData($dataPost);
182
 
183
            if ($form->isValid()) {
1058 geraldo 184
 
185
 
977 geraldo 186
                $dataPost = (array) $form->getData();
187
 
188
                $hydrator = new ObjectPropertyHydrator();
189
                $companyPerformanceEvaluation = new CompanyPerformanceEvaluationForm();
190
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
191
 
192
                if (!$companyPerformanceEvaluation->status) {
193
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
194
                }
195
                $companyPerformanceEvaluation->company_id = $currentCompany->id;
1075 geraldo 196
 
197
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
198
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
199
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
200
 
977 geraldo 201
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
1076 geraldo 202
 
977 geraldo 203
                $result = $companyPerformanceEvaluationMapper->insert($companyPerformanceEvaluation);
204
 
1081 geraldo 205
 
206
                return new JsonModel([
207
                    'success' => false,
208
                    'data' =>  $result
209
                ]);
210
 
977 geraldo 211
                if ($result) {
212
                    $this->logger->info('Se agrego el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
213
 
214
                    // Get record by id
215
                    $record = $companyPerformanceEvaluationMapper->fetchOne($companyPerformanceEvaluation->id);
216
 
217
                    if ($record) {
218
 
219
                        $data = [
220
                            'success' => true,
221
                            'id' => $record->id,
222
                            'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
223
                            'data' => 'LABEL_RECORD_ADDED'
224
                        ];
225
 
226
                    } else {
227
 
228
                        $data = [
229
                            'success' => false,
230
                            'data' => 'ERROR_RECORD_NOT_FOUND'
231
                        ];
232
                    }
233
 
234
                } else {
235
                    $data = [
236
                        'success' => false,
237
                        'data' => $companyPerformanceEvaluationMapper->getError()
238
                    ];
239
                }
240
 
241
                return new JsonModel($data);
242
            } else {
243
                $messages = [];
244
                $form_messages = (array) $form->getMessages();
245
                foreach ($form_messages as $fieldname => $field_messages) {
246
 
247
                    $messages[$fieldname] = array_values($field_messages);
248
                }
249
 
250
                return new JsonModel([
251
                    'success' => false,
252
                    'data' => $messages
253
                ]);
254
            }
255
        } else {
256
            $data = [
257
                'success' => false,
258
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
259
            ];
260
 
261
            return new JsonModel($data);
262
        }
263
 
264
        return new JsonModel($data);
265
    }
266
 
267
    public function editAction() {
268
        $request = $this->getRequest();
269
        $currentUserPlugin = $this->plugin('currentUserPlugin');
270
        $currentCompany = $currentUserPlugin->getCompany();
271
        $currentUser = $currentUserPlugin->getUser();
272
 
273
        $request = $this->getRequest();
274
        $uuid = $this->params()->fromRoute('id');
275
 
276
 
277
        if (!$uuid) {
278
            $data = [
279
                'success' => false,
280
                'data' => 'ERROR_INVALID_PARAMETER'
281
            ];
282
 
283
            return new JsonModel($data);
284
        }
285
 
286
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
287
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
288
        if (!$companyPerformanceEvaluation) {
289
            $data = [
290
                'success' => false,
291
                'data' => 'ERROR_RECORD_NOT_FOUND'
292
            ];
293
 
294
            return new JsonModel($data);
295
        }
296
 
297
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
298
            return new JsonModel([
299
                'success' => false,
300
                'data' => 'ERROR_UNAUTHORIZED'
301
            ]);
302
        }
303
 
304
 
305
        if ($request->isPost()) {
1064 geraldo 306
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 307
            $dataPost = $request->getPost()->toArray();
308
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
309
 
310
            $form->setData($dataPost);
311
 
312
            if ($form->isValid()) {
313
                $dataPost = (array) $form->getData();
314
 
315
                $hydrator = new ObjectPropertyHydrator();
316
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
317
 
318
                if (!$companyPerformanceEvaluation->status) {
319
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
320
                }
321
 
1080 geraldo 322
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
323
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
324
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
325
 
977 geraldo 326
                $result = $companyPerformanceEvaluationMapper->update($companyPerformanceEvaluation);
327
 
328
                if ($result) {
329
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
330
                    $data = [
331
                        'success' => true,
332
                        'id' => $companyPerformanceEvaluation->id,
333
                        'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $companyPerformanceEvaluation->uuid]),
334
                        'data' => 'LABEL_RECORD_UPDATED'
335
                    ];
336
                } else {
337
                    $data = [
338
                        'success' => false,
339
                        'data' => $companyPerformanceEvaluationMapper->getError()
340
                    ];
341
                }
342
 
343
                return new JsonModel($data);
344
            } else {
345
                $messages = [];
346
                $form_messages = (array) $form->getMessages();
347
                foreach ($form_messages as $fieldname => $field_messages) {
348
                    $messages[$fieldname] = array_values($field_messages);
349
                }
350
 
351
                return new JsonModel([
352
                    'success' => false,
353
                    'data' => $messages
354
                ]);
355
            }
356
        } else if ($request->isGet()) {
357
            $hydrator = new ObjectPropertyHydrator();
358
 
1080 geraldo 359
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
360
            $jobDescription = $jobDescriptionMapper->fetchOne($companyPerformanceEvaluation->job_description_id);
361
            if(!$jobDescription){
362
                $data = [
363
                    'success' => false,
364
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
365
                ];
366
 
367
                return new JsonModel($data);
368
 
369
            }
370
 
977 geraldo 371
            $data = [
372
                'success' => true,
373
                'data' => [
374
                    'id' => $companyPerformanceEvaluation->uuid,
375
                    'name' => $companyPerformanceEvaluation->name,
1080 geraldo 376
                    'job_description_id' => $jobDescription->uuid,
977 geraldo 377
                    'status' => $companyPerformanceEvaluation->status,
378
                    'content' => $companyPerformanceEvaluation->content ? json_decode($companyPerformanceEvaluation->content) : [],
379
                ]
380
            ];
381
 
382
            return new JsonModel($data);
383
        } else {
384
            $data = [
385
                'success' => false,
386
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
387
            ];
388
 
389
            return new JsonModel($data);
390
        }
391
 
392
        return new JsonModel($data);
393
    }
394
 
395
    public function deleteAction() {
396
        $request = $this->getRequest();
397
        $currentUserPlugin = $this->plugin('currentUserPlugin');
398
        $currentCompany = $currentUserPlugin->getCompany();
399
        $currentUser = $currentUserPlugin->getUser();
400
 
401
        $request = $this->getRequest();
402
        $uuid = $this->params()->fromRoute('id');
403
 
404
        if (!$uuid) {
405
            $data = [
406
                'success' => false,
407
                'data' => 'ERROR_INVALID_PARAMETER'
408
            ];
409
 
410
            return new JsonModel($data);
411
        }
412
 
413
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
414
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
415
        if (!$companyPerformanceEvaluation) {
416
            $data = [
417
                'success' => false,
418
                'data' => 'ERROR_RECORD_NOT_FOUND'
419
            ];
420
 
421
            return new JsonModel($data);
422
        }
423
 
424
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
425
            return new JsonModel([
426
                'success' => false,
427
                'data' => 'ERROR_UNAUTHORIZED'
428
            ]);
429
        }
430
 
431
        if ($request->isPost()) {
432
 
433
            //Falta borrar los test  primeramente
434
            $companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
435
            $companyPerformanceEvaluationFormUserMapper->deleteAllByFormId($companyPerformanceEvaluation->id);
436
 
437
 
438
            $result = $companyPerformanceEvaluationMapper->delete($companyPerformanceEvaluation->id);
439
            if ($result) {
440
                $this->logger->info('Se borro el formulario de evaluación de desempeño ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
441
 
442
                $data = [
443
                    'success' => true,
444
                    'data' => 'LABEL_RECORD_DELETED'
445
                ];
446
            } else {
447
 
448
                $data = [
449
                    'success' => false,
450
                    'data' => $companyPerformanceEvaluationMapper->getError()
451
                ];
452
 
453
                return new JsonModel($data);
454
            }
455
        } else {
456
            $data = [
457
                'success' => false,
458
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
459
            ];
460
 
461
            return new JsonModel($data);
462
        }
463
 
464
        return new JsonModel($data);
465
    }
466
 
467
}