Proyectos de Subversion LeadersLinked - Backend

Rev

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