Proyectos de Subversion LeadersLinked - Backend

Rev

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