Proyectos de Subversion LeadersLinked - Backend

Rev

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