Proyectos de Subversion LeadersLinked - Backend

Rev

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