Proyectos de Subversion LeadersLinked - Backend

Rev

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