Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 211 | Rev 213 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
115 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
 
9
 
10
use Laminas\Mvc\Controller\AbstractActionController;
11
 
12
use Laminas\Log\LoggerInterface;
13
 
14
use Laminas\View\Model\ViewModel;
15
use Laminas\View\Model\JsonModel;
16
use LeadersLinked\Library\Functions;
17
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
18
use LeadersLinked\Form\CompanySelfEvaluationFormForm;
19
use LeadersLinked\Model\CompanySelfEvaluationForm;
20
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
188 efrain 21
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
115 efrain 22
 
23
class SelfEvaluationFormController extends AbstractActionController
24
{
25
    /**
26
     *
27
     * @var AdapterInterface
28
     */
29
    private $adapter;
30
 
31
 
32
    /**
33
     *
34
     * @var AbstractAdapter
35
     */
36
    private $cache;
37
 
38
    /**
39
     *
40
     * @var  LoggerInterface
41
     */
42
    private $logger;
43
 
44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
49
 
50
    /**
51
     *
52
     * @param AdapterInterface $adapter
53
     * @param AbstractAdapter $cache
54
     * @param LoggerInterface $logger
55
     * @param array $config
56
     */
57
    public function __construct($adapter, $cache , $logger, $config)
58
    {
59
        $this->adapter      = $adapter;
60
        $this->cache        = $cache;
61
        $this->logger       = $logger;
62
        $this->config       = $config;
63
 
64
    }
65
 
66
    public function indexAction()
67
    {
68
        $request = $this->getRequest();
69
        $currentUserPlugin = $this->plugin('currentUserPlugin');
70
        $currentCompany = $currentUserPlugin->getCompany();
71
        $currentUser    = $currentUserPlugin->getUser();
72
 
73
 
74
        $request = $this->getRequest();
75
        if($request->isGet()) {
76
 
77
            $headers  = $request->getHeaders();
78
 
79
            $isJson = false;
80
            if($headers->has('Accept')) {
81
                $accept = $headers->get('Accept');
82
 
83
                $prioritized = $accept->getPrioritized();
84
 
85
                foreach($prioritized as $key => $value) {
86
                    $raw = trim($value->getRaw());
87
 
88
                    if(!$isJson) {
89
                        $isJson = strpos($raw, 'json');
90
                    }
91
 
92
                }
93
            }
94
 
95
            if($isJson) {
96
                $search = $this->params()->fromQuery('search', []);
97
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
98
 
99
                $page               = intval($this->params()->fromQuery('start', 1), 10);
100
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
101
                $order              =  $this->params()->fromQuery('order', []);
102
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
103
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
104
 
105
                $fields =  ['name'];
106
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
107
 
108
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
109
                    $order_direction = 'ASC';
110
                }
111
 
112
                $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
113
                $paginator = $companySelfEvaluationMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
114
 
115
                $items = [];
116
                $records = $paginator->getCurrentItems();
117
                foreach($records as $record)
118
                {
119
 
120
                    if($record->language == CompanySelfEvaluationForm::LANGUAGE_SPANISH) {
121
                        $language = 'LABEL_SPANISH';
122
                    } else if($record->language == CompanySelfEvaluationForm::LANGUAGE_ENGLISH) {
123
                        $language = 'LABEL_ENGLISH';
124
                    } else {
125
                        $language = '';
126
                    }
127
 
128
                    $item = [
129
                        'id' => $record->id,
130
                        'name' => $record->name,
131
                        'language' => $language,
132
                        'status' => $record->status,
133
                        'actions' => [
134
                            'link_edit' => $this->url()->fromRoute('self-evaluation/forms/edit', ['id' => $record->uuid ]),
135
                            'link_delete' => $this->url()->fromRoute('self-evaluation/forms/delete', ['id' =>$record->uuid ])
136
                        ]
137
                    ];
138
 
139
                    array_push($items, $item);
140
                }
141
 
142
                return new JsonModel([
143
                    'success' => true,
144
                    'data' => [
145
                        'items' => $items,
146
                        'total' => $paginator->getTotalItemCount(),
147
                    ]
148
                ]);
149
            } else {
150
 
151
                $form = new CompanySelfEvaluationFormForm();
152
 
153
                $this->layout()->setTemplate('layout/layout-backend');
154
                $viewModel = new ViewModel();
155
                $viewModel->setTemplate('leaders-linked/self-evaluation-forms/index.phtml');
156
                $viewModel->setVariable('form', $form);
157
                return $viewModel ;
158
            }
159
 
160
        } else {
161
            return new JsonModel([
162
                'success' => false,
163
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
164
            ]);;
165
        }
166
    }
167
 
168
    public function addAction()
169
    {
170
        $request = $this->getRequest();
171
        $currentUserPlugin = $this->plugin('currentUserPlugin');
172
        $currentCompany = $currentUserPlugin->getCompany();
173
        $currentUser    = $currentUserPlugin->getUser();
174
 
175
        $request = $this->getRequest();
176
 
177
 
178
        if($request->isPost()) {
179
            $form = new  CompanySelfEvaluationFormForm();
180
            $dataPost = $request->getPost()->toArray();
181
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : CompanySelfEvaluationForm::STATUS_INACTIVE;
182
 
183
            $form->setData($dataPost);
184
 
185
            if($form->isValid()) {
186
                $dataPost = (array) $form->getData();
187
 
188
                $hydrator = new ObjectPropertyHydrator();
189
                $companySelfEvaluation = new CompanySelfEvaluationForm();
190
                $hydrator->hydrate($dataPost, $companySelfEvaluation);
191
 
192
                if(!$companySelfEvaluation->status) {
193
                    $companySelfEvaluation->status = CompanySelfEvaluationForm::STATUS_INACTIVE;
194
                }
195
                $companySelfEvaluation->company_id = $currentCompany->id;
196
 
197
 
198
                $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
199
                $result = $companySelfEvaluationMapper->insert($companySelfEvaluation);
200
 
201
                if($result) {
202
                    $this->logger->info('Se agrego el tamaño de empresa ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
203
 
210 geraldo 204
                    $record = $companySelfEvaluationMapper->fetchOne($companySelfEvaluation->id);
205
                    if($record) {
206
 
115 efrain 207
                    $data = [
208
                        'success'   => true,
212 geraldo 209
                        'id' => $record->id,
210
                        'action_edit' => $this->url()->fromRoute('self-evaluation/forms/edit', ['id' => $record->uuid ]),
115 efrain 211
                        'data'   => 'LABEL_RECORD_ADDED'
212
                    ];
210 geraldo 213
 
214
                }
215
 
115 efrain 216
                } else {
217
                    $data = [
218
                        'success'   => false,
219
                        'data'      => $companySelfEvaluationMapper->getError()
220
                    ];
221
 
222
                }
223
 
224
                return new JsonModel($data);
225
 
226
            } else {
227
                $messages = [];
228
                $form_messages = (array) $form->getMessages();
229
                foreach($form_messages  as $fieldname => $field_messages)
230
                {
231
 
232
                    $messages[$fieldname] = array_values($field_messages);
233
                }
234
 
235
                return new JsonModel([
236
                    'success'   => false,
237
                    'data'   => $messages
238
                ]);
239
            }
240
 
241
        } else {
242
            $data = [
243
                'success' => false,
244
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
245
            ];
246
 
247
            return new JsonModel($data);
248
        }
249
 
250
        return new JsonModel($data);
251
    }
252
 
253
    public function editAction()
254
    {
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
        $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
274
        $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
275
        if(!$companySelfEvaluation) {
276
            $data = [
277
                'success'   => false,
278
                'data'   => 'ERROR_RECORD_NOT_FOUND'
279
            ];
280
 
281
            return new JsonModel($data);
282
        }
283
 
284
        if($companySelfEvaluation->company_id != $currentCompany->id) {
285
            return new JsonModel([
286
                'success'   => false,
287
                'data'   => 'ERROR_UNAUTHORIZED'
288
            ]);
289
        }
290
 
291
 
292
        if($request->isPost()) {
293
            $form = new  CompanySelfEvaluationFormForm();
294
            $dataPost = $request->getPost()->toArray();
295
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : CompanySelfEvaluationForm::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, $companySelfEvaluation);
304
 
305
                if(!$companySelfEvaluation->status) {
306
                    $companySelfEvaluation->status = CompanySelfEvaluationForm::STATUS_INACTIVE;
307
                }
308
 
309
                $result = $companySelfEvaluationMapper->update($companySelfEvaluation);
310
 
311
                if($result) {
312
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
313
 
314
 
315
                    $data = [
316
                        'success' => true,
317
                        'data' => 'LABEL_RECORD_UPDATED'
318
                    ];
319
                } else {
320
                    $data = [
321
                        'success'   => false,
322
                        'data'      => $companySelfEvaluationMapper->getErrno()
323
                    ];
324
                }
325
 
326
                return new JsonModel($data);
327
 
328
            } else {
329
                $messages = [];
330
                $form_messages = (array) $form->getMessages();
331
                foreach($form_messages  as $fieldname => $field_messages)
332
                {
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,
195 efrain 346
                'data' => [
347
                    'id' => $companySelfEvaluation->uuid,
348
                    'name' => $companySelfEvaluation->name,
349
                    'description' => $companySelfEvaluation->description,
350
                    'text' => $companySelfEvaluation->text,
351
                    'language' => $companySelfEvaluation->language,
352
                    'status' => $companySelfEvaluation->status,
353
                    'content' => $companySelfEvaluation->content ? json_decode($companySelfEvaluation->content) : [],
354
 
355
                ]
115 efrain 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
    {
373
        $request = $this->getRequest();
374
        $currentUserPlugin = $this->plugin('currentUserPlugin');
375
        $currentCompany = $currentUserPlugin->getCompany();
376
        $currentUser    = $currentUserPlugin->getUser();
377
 
378
        $request = $this->getRequest();
379
        $uuid = $this->params()->fromRoute('id');
380
 
381
        if(!$uuid) {
382
            $data = [
383
                'success'   => false,
384
                'data'   => 'ERROR_INVALID_PARAMETER'
385
            ];
386
 
387
            return new JsonModel($data);
388
        }
389
 
390
        $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
391
        $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
392
        if(!$companySelfEvaluation) {
393
            $data = [
394
                'success'   => false,
395
                'data'   => 'ERROR_RECORD_NOT_FOUND'
396
            ];
397
 
398
            return new JsonModel($data);
399
        }
400
 
401
        if($companySelfEvaluation->company_id != $currentCompany->id) {
402
            return new JsonModel([
403
                'success'   => false,
404
                'data'   => 'ERROR_UNAUTHORIZED'
405
            ]);
406
        }
190 efrain 407
 
115 efrain 408
 
409
 
410
        if($request->isPost()) {
189 efrain 411
 
412
 
413
            //Falta borrar los test  primeramente
188 efrain 414
            $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
415
            $companySelfEvaluationFormUserMapper->deleteAllByFormId($companySelfEvaluation->id);
416
 
417
 
418
            $result = $companySelfEvaluationMapper->delete($companySelfEvaluation->id);
115 efrain 419
            if($result) {
188 efrain 420
                $this->logger->info('Se borro el formulario de auto-evaluación ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
115 efrain 421
 
422
                $data = [
423
                    'success' => true,
424
                    'data' => 'LABEL_RECORD_DELETED'
425
                ];
426
            } else {
427
 
428
                $data = [
429
                    'success'   => false,
430
                    'data'      => $companySelfEvaluationMapper->getError()
431
                ];
432
 
433
                return new JsonModel($data);
434
            }
435
 
436
        } else {
437
            $data = [
438
                'success' => false,
439
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
440
            ];
441
 
442
            return new JsonModel($data);
443
        }
444
 
445
        return new JsonModel($data);
446
    }
447
}