Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 210 | Rev 212 | 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,
211 geraldo 209
                        'id' => $record,
115 efrain 210
                        'data'   => 'LABEL_RECORD_ADDED'
211
                    ];
210 geraldo 212
 
213
                }
214
 
115 efrain 215
                } else {
216
                    $data = [
217
                        'success'   => false,
218
                        'data'      => $companySelfEvaluationMapper->getError()
219
                    ];
220
 
221
                }
222
 
223
                return new JsonModel($data);
224
 
225
            } else {
226
                $messages = [];
227
                $form_messages = (array) $form->getMessages();
228
                foreach($form_messages  as $fieldname => $field_messages)
229
                {
230
 
231
                    $messages[$fieldname] = array_values($field_messages);
232
                }
233
 
234
                return new JsonModel([
235
                    'success'   => false,
236
                    'data'   => $messages
237
                ]);
238
            }
239
 
240
        } else {
241
            $data = [
242
                'success' => false,
243
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
244
            ];
245
 
246
            return new JsonModel($data);
247
        }
248
 
249
        return new JsonModel($data);
250
    }
251
 
252
    public function editAction()
253
    {
254
        $request = $this->getRequest();
255
        $currentUserPlugin = $this->plugin('currentUserPlugin');
256
        $currentCompany = $currentUserPlugin->getCompany();
257
        $currentUser    = $currentUserPlugin->getUser();
258
 
259
        $request = $this->getRequest();
260
        $uuid = $this->params()->fromRoute('id');
261
 
262
 
263
        if(!$uuid) {
264
            $data = [
265
                'success'   => false,
266
                'data'   => 'ERROR_INVALID_PARAMETER'
267
            ];
268
 
269
            return new JsonModel($data);
270
        }
271
 
272
        $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
273
        $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
274
        if(!$companySelfEvaluation) {
275
            $data = [
276
                'success'   => false,
277
                'data'   => 'ERROR_RECORD_NOT_FOUND'
278
            ];
279
 
280
            return new JsonModel($data);
281
        }
282
 
283
        if($companySelfEvaluation->company_id != $currentCompany->id) {
284
            return new JsonModel([
285
                'success'   => false,
286
                'data'   => 'ERROR_UNAUTHORIZED'
287
            ]);
288
        }
289
 
290
 
291
        if($request->isPost()) {
292
            $form = new  CompanySelfEvaluationFormForm();
293
            $dataPost = $request->getPost()->toArray();
294
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : CompanySelfEvaluationForm::STATUS_INACTIVE;
295
 
296
            $form->setData($dataPost);
297
 
298
            if($form->isValid()) {
299
                $dataPost = (array) $form->getData();
300
 
301
                $hydrator = new ObjectPropertyHydrator();
302
                $hydrator->hydrate($dataPost, $companySelfEvaluation);
303
 
304
                if(!$companySelfEvaluation->status) {
305
                    $companySelfEvaluation->status = CompanySelfEvaluationForm::STATUS_INACTIVE;
306
                }
307
 
308
                $result = $companySelfEvaluationMapper->update($companySelfEvaluation);
309
 
310
                if($result) {
311
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
312
 
313
 
314
                    $data = [
315
                        'success' => true,
316
                        'data' => 'LABEL_RECORD_UPDATED'
317
                    ];
318
                } else {
319
                    $data = [
320
                        'success'   => false,
321
                        'data'      => $companySelfEvaluationMapper->getErrno()
322
                    ];
323
                }
324
 
325
                return new JsonModel($data);
326
 
327
            } else {
328
                $messages = [];
329
                $form_messages = (array) $form->getMessages();
330
                foreach($form_messages  as $fieldname => $field_messages)
331
                {
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,
195 efrain 345
                'data' => [
346
                    'id' => $companySelfEvaluation->uuid,
347
                    'name' => $companySelfEvaluation->name,
348
                    'description' => $companySelfEvaluation->description,
349
                    'text' => $companySelfEvaluation->text,
350
                    'language' => $companySelfEvaluation->language,
351
                    'status' => $companySelfEvaluation->status,
352
                    'content' => $companySelfEvaluation->content ? json_decode($companySelfEvaluation->content) : [],
353
 
354
                ]
115 efrain 355
            ];
356
 
357
            return new JsonModel($data);
358
        } else {
359
            $data = [
360
                'success' => false,
361
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
362
            ];
363
 
364
            return new JsonModel($data);
365
        }
366
 
367
        return new JsonModel($data);
368
    }
369
 
370
    public function deleteAction()
371
    {
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
        $companySelfEvaluationMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
390
        $companySelfEvaluation = $companySelfEvaluationMapper->fetchOneByUuid($uuid);
391
        if(!$companySelfEvaluation) {
392
            $data = [
393
                'success'   => false,
394
                'data'   => 'ERROR_RECORD_NOT_FOUND'
395
            ];
396
 
397
            return new JsonModel($data);
398
        }
399
 
400
        if($companySelfEvaluation->company_id != $currentCompany->id) {
401
            return new JsonModel([
402
                'success'   => false,
403
                'data'   => 'ERROR_UNAUTHORIZED'
404
            ]);
405
        }
190 efrain 406
 
115 efrain 407
 
408
 
409
        if($request->isPost()) {
189 efrain 410
 
411
 
412
            //Falta borrar los test  primeramente
188 efrain 413
            $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
414
            $companySelfEvaluationFormUserMapper->deleteAllByFormId($companySelfEvaluation->id);
415
 
416
 
417
            $result = $companySelfEvaluationMapper->delete($companySelfEvaluation->id);
115 efrain 418
            if($result) {
188 efrain 419
                $this->logger->info('Se borro el formulario de auto-evaluación ' . $companySelfEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
115 efrain 420
 
421
                $data = [
422
                    'success' => true,
423
                    'data' => 'LABEL_RECORD_DELETED'
424
                ];
425
            } else {
426
 
427
                $data = [
428
                    'success'   => false,
429
                    'data'      => $companySelfEvaluationMapper->getError()
430
                ];
431
 
432
                return new JsonModel($data);
433
            }
434
 
435
        } else {
436
            $data = [
437
                'success' => false,
438
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
439
            ];
440
 
441
            return new JsonModel($data);
442
        }
443
 
444
        return new JsonModel($data);
445
    }
446
}