Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1074 | Rev 1076 | 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
 
109
                $items = [];
110
                $records = $paginator->getCurrentItems();
111
                foreach ($records as $record) {
112
 
113
                    $item = [
114
                        'id' => $record->id,
115
                        'name' => $record->name,
1051 geraldo 116
                        'type' => '',
977 geraldo 117
                        'status' => $record->status,
118
                        'actions' => [
119
                            'link_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
120
                            'link_delete' => $this->url()->fromRoute('performance-evaluation/forms/delete', ['id' => $record->uuid])
121
                        ]
122
                    ];
123
 
124
                    array_push($items, $item);
125
                }
126
 
127
                return new JsonModel([
128
                    'success' => true,
129
                    'data' => [
130
                        'items' => $items,
131
                        'total' => $paginator->getTotalItemCount(),
132
                    ]
133
                ]);
134
            } else {
135
 
1064 geraldo 136
                $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 137
 
1051 geraldo 138
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
139
                $jobsDescription = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
140
 
977 geraldo 141
                $this->layout()->setTemplate('layout/layout-backend');
142
                $viewModel = new ViewModel();
143
                $viewModel->setTemplate('leaders-linked/performance-evaluation-forms/index.phtml');
144
                $viewModel->setVariable('form', $form);
1051 geraldo 145
                $viewModel->setVariable('jobsDescription', $jobsDescription);
977 geraldo 146
                return $viewModel;
147
            }
148
        } else {
149
            return new JsonModel([
150
                'success' => false,
151
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
152
            ]);
153
            ;
154
        }
155
    }
156
 
157
    public function addAction() {
158
        $request = $this->getRequest();
159
        $currentUserPlugin = $this->plugin('currentUserPlugin');
160
        $currentCompany = $currentUserPlugin->getCompany();
161
        $currentUser = $currentUserPlugin->getUser();
162
 
163
        $request = $this->getRequest();
164
 
165
 
166
        if ($request->isPost()) {
1064 geraldo 167
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 168
            $dataPost = $request->getPost()->toArray();
1059 geraldo 169
 
170
 
977 geraldo 171
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
172
 
173
            $form->setData($dataPost);
174
 
175
            if ($form->isValid()) {
1058 geraldo 176
 
177
 
977 geraldo 178
                $dataPost = (array) $form->getData();
179
 
180
                $hydrator = new ObjectPropertyHydrator();
181
                $companyPerformanceEvaluation = new CompanyPerformanceEvaluationForm();
182
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
183
 
184
                if (!$companyPerformanceEvaluation->status) {
185
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
186
                }
187
                $companyPerformanceEvaluation->company_id = $currentCompany->id;
1075 geraldo 188
 
189
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
190
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
191
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
192
 
977 geraldo 193
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
194
                $result = $companyPerformanceEvaluationMapper->insert($companyPerformanceEvaluation);
195
 
196
                if ($result) {
197
                    $this->logger->info('Se agrego el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
198
 
199
                    // Get record by id
200
                    $record = $companyPerformanceEvaluationMapper->fetchOne($companyPerformanceEvaluation->id);
201
 
202
                    if ($record) {
203
 
204
                        $data = [
205
                            'success' => true,
206
                            'id' => $record->id,
207
                            'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
208
                            'data' => 'LABEL_RECORD_ADDED'
209
                        ];
210
 
211
                    } else {
212
 
213
                        $data = [
214
                            'success' => false,
215
                            'data' => 'ERROR_RECORD_NOT_FOUND'
216
                        ];
217
                    }
218
 
219
                } else {
220
                    $data = [
221
                        'success' => false,
222
                        'data' => $companyPerformanceEvaluationMapper->getError()
223
                    ];
224
                }
225
 
226
                return new JsonModel($data);
227
            } else {
228
                $messages = [];
229
                $form_messages = (array) $form->getMessages();
230
                foreach ($form_messages as $fieldname => $field_messages) {
231
 
232
                    $messages[$fieldname] = array_values($field_messages);
233
                }
234
 
235
                return new JsonModel([
236
                    'success' => false,
237
                    'data' => $messages
238
                ]);
239
            }
240
        } else {
241
            $data = [
242
                'success' => false,
243
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
244
            ];
245
 
246
            return new JsonModel($data);
247
        }
248
 
249
        return new JsonModel($data);
250
    }
251
 
252
    public function editAction() {
253
        $request = $this->getRequest();
254
        $currentUserPlugin = $this->plugin('currentUserPlugin');
255
        $currentCompany = $currentUserPlugin->getCompany();
256
        $currentUser = $currentUserPlugin->getUser();
257
 
258
        $request = $this->getRequest();
259
        $uuid = $this->params()->fromRoute('id');
260
 
261
 
262
        if (!$uuid) {
263
            $data = [
264
                'success' => false,
265
                'data' => 'ERROR_INVALID_PARAMETER'
266
            ];
267
 
268
            return new JsonModel($data);
269
        }
270
 
271
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
272
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
273
        if (!$companyPerformanceEvaluation) {
274
            $data = [
275
                'success' => false,
276
                'data' => 'ERROR_RECORD_NOT_FOUND'
277
            ];
278
 
279
            return new JsonModel($data);
280
        }
281
 
282
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
283
            return new JsonModel([
284
                'success' => false,
285
                'data' => 'ERROR_UNAUTHORIZED'
286
            ]);
287
        }
288
 
289
 
290
        if ($request->isPost()) {
1064 geraldo 291
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 292
            $dataPost = $request->getPost()->toArray();
293
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
294
 
295
            $form->setData($dataPost);
296
 
297
            if ($form->isValid()) {
298
                $dataPost = (array) $form->getData();
299
 
300
                $hydrator = new ObjectPropertyHydrator();
301
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
302
 
303
                if (!$companyPerformanceEvaluation->status) {
304
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
305
                }
306
 
307
                $result = $companyPerformanceEvaluationMapper->update($companyPerformanceEvaluation);
308
 
309
                if ($result) {
310
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
311
                    $data = [
312
                        'success' => true,
313
                        'id' => $companyPerformanceEvaluation->id,
314
                        'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $companyPerformanceEvaluation->uuid]),
315
                        'data' => 'LABEL_RECORD_UPDATED'
316
                    ];
317
                } else {
318
                    $data = [
319
                        'success' => false,
320
                        'data' => $companyPerformanceEvaluationMapper->getError()
321
                    ];
322
                }
323
 
324
                return new JsonModel($data);
325
            } else {
326
                $messages = [];
327
                $form_messages = (array) $form->getMessages();
328
                foreach ($form_messages as $fieldname => $field_messages) {
329
                    $messages[$fieldname] = array_values($field_messages);
330
                }
331
 
332
                return new JsonModel([
333
                    'success' => false,
334
                    'data' => $messages
335
                ]);
336
            }
337
        } else if ($request->isGet()) {
338
            $hydrator = new ObjectPropertyHydrator();
339
 
340
            $data = [
341
                'success' => true,
342
                'data' => [
343
                    'id' => $companyPerformanceEvaluation->uuid,
344
                    'name' => $companyPerformanceEvaluation->name,
345
                    'description' => $companyPerformanceEvaluation->description,
346
                    'text' => $companyPerformanceEvaluation->text,
347
                    'language' => $companyPerformanceEvaluation->language,
348
                    'status' => $companyPerformanceEvaluation->status,
349
                    'content' => $companyPerformanceEvaluation->content ? json_decode($companyPerformanceEvaluation->content) : [],
350
                ]
351
            ];
352
 
353
            return new JsonModel($data);
354
        } else {
355
            $data = [
356
                'success' => false,
357
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
358
            ];
359
 
360
            return new JsonModel($data);
361
        }
362
 
363
        return new JsonModel($data);
364
    }
365
 
366
    public function deleteAction() {
367
        $request = $this->getRequest();
368
        $currentUserPlugin = $this->plugin('currentUserPlugin');
369
        $currentCompany = $currentUserPlugin->getCompany();
370
        $currentUser = $currentUserPlugin->getUser();
371
 
372
        $request = $this->getRequest();
373
        $uuid = $this->params()->fromRoute('id');
374
 
375
        if (!$uuid) {
376
            $data = [
377
                'success' => false,
378
                'data' => 'ERROR_INVALID_PARAMETER'
379
            ];
380
 
381
            return new JsonModel($data);
382
        }
383
 
384
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
385
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
386
        if (!$companyPerformanceEvaluation) {
387
            $data = [
388
                'success' => false,
389
                'data' => 'ERROR_RECORD_NOT_FOUND'
390
            ];
391
 
392
            return new JsonModel($data);
393
        }
394
 
395
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
396
            return new JsonModel([
397
                'success' => false,
398
                'data' => 'ERROR_UNAUTHORIZED'
399
            ]);
400
        }
401
 
402
        if ($request->isPost()) {
403
 
404
            //Falta borrar los test  primeramente
405
            $companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
406
            $companyPerformanceEvaluationFormUserMapper->deleteAllByFormId($companyPerformanceEvaluation->id);
407
 
408
 
409
            $result = $companyPerformanceEvaluationMapper->delete($companyPerformanceEvaluation->id);
410
            if ($result) {
411
                $this->logger->info('Se borro el formulario de evaluación de desempeño ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
412
 
413
                $data = [
414
                    'success' => true,
415
                    'data' => 'LABEL_RECORD_DELETED'
416
                ];
417
            } else {
418
 
419
                $data = [
420
                    'success' => false,
421
                    'data' => $companyPerformanceEvaluationMapper->getError()
422
                ];
423
 
424
                return new JsonModel($data);
425
            }
426
        } else {
427
            $data = [
428
                'success' => false,
429
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
430
            ];
431
 
432
            return new JsonModel($data);
433
        }
434
 
435
        return new JsonModel($data);
436
    }
437
 
438
}