Proyectos de Subversion LeadersLinked - Backend

Rev

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