Proyectos de Subversion LeadersLinked - Backend

Rev

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

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