Proyectos de Subversion LeadersLinked - Backend

Rev

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