Proyectos de Subversion LeadersLinked - Backend

Rev

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