Proyectos de Subversion LeadersLinked - Backend

Rev

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