Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1070 | Rev 1072 | 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
 
1060 geraldo 170
            /*return new JsonModel([
1059 geraldo 171
                'success' => false,
172
                'data' => $dataPost
1060 geraldo 173
            ]);*/
1059 geraldo 174
 
977 geraldo 175
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
176
 
177
            $form->setData($dataPost);
178
 
179
            if ($form->isValid()) {
1058 geraldo 180
 
181
 
977 geraldo 182
                $dataPost = (array) $form->getData();
183
 
184
                $hydrator = new ObjectPropertyHydrator();
185
                $companyPerformanceEvaluation = new CompanyPerformanceEvaluationForm();
186
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
187
 
1071 geraldo 188
 
977 geraldo 189
                if (!$companyPerformanceEvaluation->status) {
190
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
191
                }
192
                $companyPerformanceEvaluation->company_id = $currentCompany->id;
193
 
1071 geraldo 194
                return new JsonModel([
195
                    'success' => false,
196
                    'data' => $companyPerformanceEvaluation
197
                ]);
977 geraldo 198
 
1071 geraldo 199
 
977 geraldo 200
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
201
                $result = $companyPerformanceEvaluationMapper->insert($companyPerformanceEvaluation);
202
 
203
                if ($result) {
204
                    $this->logger->info('Se agrego el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
205
 
206
                    // Get record by id
207
                    $record = $companyPerformanceEvaluationMapper->fetchOne($companyPerformanceEvaluation->id);
208
 
209
                    if ($record) {
210
 
211
                        $data = [
212
                            'success' => true,
213
                            'id' => $record->id,
214
                            'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
215
                            'data' => 'LABEL_RECORD_ADDED'
216
                        ];
217
 
218
                    } else {
219
 
220
                        $data = [
221
                            'success' => false,
222
                            'data' => 'ERROR_RECORD_NOT_FOUND'
223
                        ];
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
 
314
                $result = $companyPerformanceEvaluationMapper->update($companyPerformanceEvaluation);
315
 
316
                if ($result) {
317
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
318
                    $data = [
319
                        'success' => true,
320
                        'id' => $companyPerformanceEvaluation->id,
321
                        'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $companyPerformanceEvaluation->uuid]),
322
                        'data' => 'LABEL_RECORD_UPDATED'
323
                    ];
324
                } else {
325
                    $data = [
326
                        'success' => false,
327
                        'data' => $companyPerformanceEvaluationMapper->getError()
328
                    ];
329
                }
330
 
331
                return new JsonModel($data);
332
            } else {
333
                $messages = [];
334
                $form_messages = (array) $form->getMessages();
335
                foreach ($form_messages as $fieldname => $field_messages) {
336
                    $messages[$fieldname] = array_values($field_messages);
337
                }
338
 
339
                return new JsonModel([
340
                    'success' => false,
341
                    'data' => $messages
342
                ]);
343
            }
344
        } else if ($request->isGet()) {
345
            $hydrator = new ObjectPropertyHydrator();
346
 
347
            $data = [
348
                'success' => true,
349
                'data' => [
350
                    'id' => $companyPerformanceEvaluation->uuid,
351
                    'name' => $companyPerformanceEvaluation->name,
352
                    'description' => $companyPerformanceEvaluation->description,
353
                    'text' => $companyPerformanceEvaluation->text,
354
                    'language' => $companyPerformanceEvaluation->language,
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
}