Proyectos de Subversion LeadersLinked - Backend

Rev

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