Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1081 | Rev 1083 | 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;
1082 geraldo 196
                $companyPerformanceEvaluation->uuid='1';
1075 geraldo 197
 
198
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
199
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
200
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
201
 
977 geraldo 202
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
1076 geraldo 203
 
977 geraldo 204
                $result = $companyPerformanceEvaluationMapper->insert($companyPerformanceEvaluation);
205
 
1081 geraldo 206
 
207
                return new JsonModel([
208
                    'success' => false,
209
                    'data' =>  $result
210
                ]);
211
 
977 geraldo 212
                if ($result) {
213
                    $this->logger->info('Se agrego el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
214
 
215
                    // Get record by id
216
                    $record = $companyPerformanceEvaluationMapper->fetchOne($companyPerformanceEvaluation->id);
217
 
218
                    if ($record) {
219
 
220
                        $data = [
221
                            'success' => true,
222
                            'id' => $record->id,
223
                            'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
224
                            'data' => 'LABEL_RECORD_ADDED'
225
                        ];
226
 
227
                    } else {
228
 
229
                        $data = [
230
                            'success' => false,
231
                            'data' => 'ERROR_RECORD_NOT_FOUND'
232
                        ];
233
                    }
234
 
235
                } else {
236
                    $data = [
237
                        'success' => false,
238
                        'data' => $companyPerformanceEvaluationMapper->getError()
239
                    ];
240
                }
241
 
242
                return new JsonModel($data);
243
            } else {
244
                $messages = [];
245
                $form_messages = (array) $form->getMessages();
246
                foreach ($form_messages as $fieldname => $field_messages) {
247
 
248
                    $messages[$fieldname] = array_values($field_messages);
249
                }
250
 
251
                return new JsonModel([
252
                    'success' => false,
253
                    'data' => $messages
254
                ]);
255
            }
256
        } else {
257
            $data = [
258
                'success' => false,
259
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
260
            ];
261
 
262
            return new JsonModel($data);
263
        }
264
 
265
        return new JsonModel($data);
266
    }
267
 
268
    public function editAction() {
269
        $request = $this->getRequest();
270
        $currentUserPlugin = $this->plugin('currentUserPlugin');
271
        $currentCompany = $currentUserPlugin->getCompany();
272
        $currentUser = $currentUserPlugin->getUser();
273
 
274
        $request = $this->getRequest();
275
        $uuid = $this->params()->fromRoute('id');
276
 
277
 
278
        if (!$uuid) {
279
            $data = [
280
                'success' => false,
281
                'data' => 'ERROR_INVALID_PARAMETER'
282
            ];
283
 
284
            return new JsonModel($data);
285
        }
286
 
287
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
288
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
289
        if (!$companyPerformanceEvaluation) {
290
            $data = [
291
                'success' => false,
292
                'data' => 'ERROR_RECORD_NOT_FOUND'
293
            ];
294
 
295
            return new JsonModel($data);
296
        }
297
 
298
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
299
            return new JsonModel([
300
                'success' => false,
301
                'data' => 'ERROR_UNAUTHORIZED'
302
            ]);
303
        }
304
 
305
 
306
        if ($request->isPost()) {
1064 geraldo 307
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 308
            $dataPost = $request->getPost()->toArray();
309
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
310
 
311
            $form->setData($dataPost);
312
 
313
            if ($form->isValid()) {
314
                $dataPost = (array) $form->getData();
315
 
316
                $hydrator = new ObjectPropertyHydrator();
317
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
318
 
319
                if (!$companyPerformanceEvaluation->status) {
320
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
321
                }
322
 
1080 geraldo 323
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
324
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
325
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
326
 
977 geraldo 327
                $result = $companyPerformanceEvaluationMapper->update($companyPerformanceEvaluation);
328
 
329
                if ($result) {
330
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
331
                    $data = [
332
                        'success' => true,
333
                        'id' => $companyPerformanceEvaluation->id,
334
                        'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $companyPerformanceEvaluation->uuid]),
335
                        'data' => 'LABEL_RECORD_UPDATED'
336
                    ];
337
                } else {
338
                    $data = [
339
                        'success' => false,
340
                        'data' => $companyPerformanceEvaluationMapper->getError()
341
                    ];
342
                }
343
 
344
                return new JsonModel($data);
345
            } else {
346
                $messages = [];
347
                $form_messages = (array) $form->getMessages();
348
                foreach ($form_messages as $fieldname => $field_messages) {
349
                    $messages[$fieldname] = array_values($field_messages);
350
                }
351
 
352
                return new JsonModel([
353
                    'success' => false,
354
                    'data' => $messages
355
                ]);
356
            }
357
        } else if ($request->isGet()) {
358
            $hydrator = new ObjectPropertyHydrator();
359
 
1080 geraldo 360
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
361
            $jobDescription = $jobDescriptionMapper->fetchOne($companyPerformanceEvaluation->job_description_id);
362
            if(!$jobDescription){
363
                $data = [
364
                    'success' => false,
365
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
366
                ];
367
 
368
                return new JsonModel($data);
369
 
370
            }
371
 
977 geraldo 372
            $data = [
373
                'success' => true,
374
                'data' => [
375
                    'id' => $companyPerformanceEvaluation->uuid,
376
                    'name' => $companyPerformanceEvaluation->name,
1080 geraldo 377
                    'job_description_id' => $jobDescription->uuid,
977 geraldo 378
                    'status' => $companyPerformanceEvaluation->status,
379
                    'content' => $companyPerformanceEvaluation->content ? json_decode($companyPerformanceEvaluation->content) : [],
380
                ]
381
            ];
382
 
383
            return new JsonModel($data);
384
        } else {
385
            $data = [
386
                'success' => false,
387
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
388
            ];
389
 
390
            return new JsonModel($data);
391
        }
392
 
393
        return new JsonModel($data);
394
    }
395
 
396
    public function deleteAction() {
397
        $request = $this->getRequest();
398
        $currentUserPlugin = $this->plugin('currentUserPlugin');
399
        $currentCompany = $currentUserPlugin->getCompany();
400
        $currentUser = $currentUserPlugin->getUser();
401
 
402
        $request = $this->getRequest();
403
        $uuid = $this->params()->fromRoute('id');
404
 
405
        if (!$uuid) {
406
            $data = [
407
                'success' => false,
408
                'data' => 'ERROR_INVALID_PARAMETER'
409
            ];
410
 
411
            return new JsonModel($data);
412
        }
413
 
414
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
415
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
416
        if (!$companyPerformanceEvaluation) {
417
            $data = [
418
                'success' => false,
419
                'data' => 'ERROR_RECORD_NOT_FOUND'
420
            ];
421
 
422
            return new JsonModel($data);
423
        }
424
 
425
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
426
            return new JsonModel([
427
                'success' => false,
428
                'data' => 'ERROR_UNAUTHORIZED'
429
            ]);
430
        }
431
 
432
        if ($request->isPost()) {
433
 
434
            //Falta borrar los test  primeramente
435
            $companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
436
            $companyPerformanceEvaluationFormUserMapper->deleteAllByFormId($companyPerformanceEvaluation->id);
437
 
438
 
439
            $result = $companyPerformanceEvaluationMapper->delete($companyPerformanceEvaluation->id);
440
            if ($result) {
441
                $this->logger->info('Se borro el formulario de evaluación de desempeño ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
442
 
443
                $data = [
444
                    'success' => true,
445
                    'data' => 'LABEL_RECORD_DELETED'
446
                ];
447
            } else {
448
 
449
                $data = [
450
                    'success' => false,
451
                    'data' => $companyPerformanceEvaluationMapper->getError()
452
                ];
453
 
454
                return new JsonModel($data);
455
            }
456
        } else {
457
            $data = [
458
                'success' => false,
459
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
460
            ];
461
 
462
            return new JsonModel($data);
463
        }
464
 
465
        return new JsonModel($data);
466
    }
467
 
468
}