Proyectos de Subversion LeadersLinked - Backend

Rev

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