Proyectos de Subversion LeadersLinked - Backend

Rev

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