Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 4749 | Rev 4754 | 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) {
116
 
117
                    $item = [
118
                        'id' => $record->id,
119
                        'name' => $record->name,
4753 eleazar 120
                        'form' => $record->form_id,
4579 eleazar 121
                        'status' => $record->status,
122
                        'actions' => [
123
                            'link_edit' => $this->url()->fromRoute('survey/edit', ['id' => $record->uuid]),
124
                            'link_delete' => $this->url()->fromRoute('survey/delete', ['id' => $record->uuid]),
125
                            'link_segment' => $this->url()->fromRoute('survey/segment', ['id' => $record->uuid])
126
                        ]
127
                    ];
128
 
129
                    array_push($items, $item);
130
                }
131
 
132
                return new JsonModel([
133
                    'success' => true,
134
                    'data' => [
135
                        'items' => $items,
136
                        'total' => $paginator->getTotalItemCount(),
137
                    ]
138
                ]);
139
            } else {
140
 
141
                $form = new SurveyForm($this->adapter, $currentCompany->id);
142
 
143
                $this->layout()->setTemplate('layout/layout-backend');
144
                $viewModel = new ViewModel();
145
                $viewModel->setTemplate('leaders-linked/survey/index.phtml');
146
                $viewModel->setVariable('form', $form);
147
                return $viewModel;
148
            }
149
        } else {
150
            return new JsonModel([
151
                'success' => false,
152
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
153
            ]);
154
        }
4384 eleazar 155
    }
156
 
4579 eleazar 157
    public function addAction() {
158
        $request = $this->getRequest();
159
        $currentUserPlugin = $this->plugin('currentUserPlugin');
160
        $currentCompany = $currentUserPlugin->getCompany();
161
        $currentUser = $currentUserPlugin->getUser();
162
 
163
        $request = $this->getRequest();
164
 
165
        if ($request->isPost()) {
166
 
167
            $form = new SurveyForm($this->adapter, $currentCompany->id);
4672 eleazar 168
 
4579 eleazar 169
            $dataPost = $request->getPost()->toArray();
4661 eleazar 170
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
4579 eleazar 171
 
172
            $form->setData($dataPost);
173
 
174
            if ($form->isValid()) {
175
                $dataPost = (array) $form->getData();
176
 
177
                $hydrator = new ObjectPropertyHydrator();
178
                $survey = new Survey();
179
                $hydrator->hydrate($dataPost, $survey);
180
 
181
                if (!$survey->status) {
182
                    $survey->status = Survey::STATUS_INACTIVE;
183
                }
184
                $survey->company_id = $currentCompany->id;
185
 
4677 eleazar 186
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
187
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
188
                $survey->form_id = $surveyForm->id;
4579 eleazar 189
 
4724 eleazar 190
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
191
                $result = $surveyMapper->insert($survey);
4730 eleazar 192
 
4579 eleazar 193
                if ($result) {
4734 eleazar 194
 
4728 eleazar 195
                    if ($result) {
4579 eleazar 196
 
197
                        $data = [
198
                            'success' => true,
199
                            'data' => 'LABEL_RECORD_ADDED'
200
                        ];
201
                    } else {
202
 
203
                        $data = [
204
                            'success' => false,
205
                            'data' => 'ERROR_RECORD_NOT_FOUND'
206
                        ];
207
                    }
208
                } else {
209
                    $data = [
210
                        'success' => false,
211
                        'data' => $surveyMapper->getError()
212
                    ];
213
                }
214
 
215
                return new JsonModel($data);
216
            } else {
217
                $messages = [];
218
                $form_messages = (array) $form->getMessages();
219
                foreach ($form_messages as $fieldname => $field_messages) {
220
 
221
                    $messages[$fieldname] = array_values($field_messages);
222
                }
223
 
224
                return new JsonModel([
225
                    'success' => false,
226
                    'data' => $messages
227
                ]);
228
            }
229
        } else {
230
            $data = [
231
                'success' => false,
232
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
233
            ];
234
 
235
            return new JsonModel($data);
236
        }
237
 
238
        return new JsonModel($data);
239
    }
240
 
241
    public function editAction() {
242
        $request = $this->getRequest();
243
        $currentUserPlugin = $this->plugin('currentUserPlugin');
244
        $currentCompany = $currentUserPlugin->getCompany();
245
        $currentUser = $currentUserPlugin->getUser();
246
 
247
        $request = $this->getRequest();
248
        $uuid = $this->params()->fromRoute('id');
249
 
250
 
251
        if (!$uuid) {
252
            $data = [
253
                'success' => false,
254
                'data' => 'ERROR_INVALID_PARAMETER'
255
            ];
256
 
257
            return new JsonModel($data);
258
        }
259
 
260
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
261
        $survey = $surveyMapper->fetchOneByUuid($uuid);
262
 
263
        if (!$survey) {
264
            $data = [
265
                'success' => false,
266
                'data' => 'ERROR_RECORD_NOT_FOUND'
267
            ];
268
 
269
            return new JsonModel($data);
270
        }
271
 
272
        if ($survey->company_id != $currentCompany->id) {
273
            return new JsonModel([
274
                'success' => false,
275
                'data' => 'ERROR_UNAUTHORIZED'
276
            ]);
277
        }
278
 
279
 
280
        if ($request->isPost()) {
281
            $form = new SurveyForm();
282
            $dataPost = $request->getPost()->toArray();
283
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
284
 
285
            $form->setData($dataPost);
286
 
287
            if ($form->isValid()) {
288
                $dataPost = (array) $form->getData();
289
 
290
                $hydrator = new ObjectPropertyHydrator();
291
                $hydrator->hydrate($dataPost, $survey);
292
 
293
                if (!$survey->status) {
294
                    $survey->status = Survey::STATUS_INACTIVE;
295
                }
4749 eleazar 296
 
297
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
298
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
299
                $survey->form_id = $surveyForm->id;
300
 
4579 eleazar 301
                $result = $surveyMapper->update($survey);
302
 
303
                if ($result) {
304
                    $this->logger->info('Se edito la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
305
                    $data = [
306
                        'success' => true,
307
                        'id' => $survey->id,
308
                        'action_edit' => $this->url()->fromRoute('survey/edit', ['id' => $survey->uuid]),
309
                        'data' => 'LABEL_RECORD_UPDATED'
310
                    ];
311
                } else {
312
                    $data = [
313
                        'success' => false,
314
                        'data' => $surveyMapper->getError()
315
                    ];
316
                }
317
 
318
                return new JsonModel($data);
319
            } else {
320
                $messages = [];
321
                $form_messages = (array) $form->getMessages();
322
                foreach ($form_messages as $fieldname => $field_messages) {
323
                    $messages[$fieldname] = array_values($field_messages);
324
                }
325
 
326
                return new JsonModel([
327
                    'success' => false,
328
                    'data' => $messages
329
                ]);
330
            }
331
        } else if ($request->isGet()) {
332
            $hydrator = new ObjectPropertyHydrator();
333
 
4749 eleazar 334
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
335
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
336
 
4579 eleazar 337
            $data = [
338
                'success' => true,
339
                'data' => [
4653 eleazar 340
                    'name' => $survey->name,
4749 eleazar 341
                    'form_id' => $surveyForm->uuid,
4653 eleazar 342
                    'target' => $survey->target,
343
                    'identity' => $survey->identity,
344
                    'since_date' => $survey->since_date,
345
                    'last_date' => $survey->last_date,
346
                    'status' => $survey->status,
4579 eleazar 347
                ]
348
            ];
349
 
350
            return new JsonModel($data);
351
        } else {
352
            $data = [
353
                'success' => false,
354
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
355
            ];
356
 
357
            return new JsonModel($data);
358
        }
359
 
360
        return new JsonModel($data);
361
    }
362
 
363
    public function deleteAction() {
364
        $request = $this->getRequest();
365
        $currentUserPlugin = $this->plugin('currentUserPlugin');
366
        $currentCompany = $currentUserPlugin->getCompany();
367
        $currentUser = $currentUserPlugin->getUser();
368
 
369
        $request = $this->getRequest();
370
        $uuid = $this->params()->fromRoute('id');
371
 
372
        if (!$uuid) {
373
            $data = [
374
                'success' => false,
375
                'data' => 'ERROR_INVALID_PARAMETER'
376
            ];
377
 
378
            return new JsonModel($data);
379
        }
380
 
381
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
382
        $survey = $surveyMapper->fetchOneByUuid($uuid);
383
        if (!$survey) {
384
            $data = [
385
                'success' => false,
386
                'data' => 'ERROR_RECORD_NOT_FOUND'
387
            ];
388
 
389
            return new JsonModel($data);
390
        }
391
 
392
        if ($survey->company_id != $currentCompany->id) {
393
            return new JsonModel([
394
                'success' => false,
395
                'data' => 'ERROR_UNAUTHORIZED'
396
            ]);
397
        }
398
 
399
        if ($request->isPost()) {
400
 
401
            $result = $surveyMapper->delete($survey->id);
402
            if ($result) {
4735 eleazar 403
                //$this->logger->info('Se borro la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4579 eleazar 404
 
405
                $data = [
406
                    'success' => true,
407
                    'data' => 'LABEL_RECORD_DELETED'
408
                ];
409
            } else {
410
 
411
                $data = [
412
                    'success' => false,
413
                    'data' => $surveyMapper->getError()
414
                ];
415
 
416
                return new JsonModel($data);
417
            }
418
        } else {
419
            $data = [
420
                'success' => false,
421
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
422
            ];
423
 
424
            return new JsonModel($data);
425
        }
426
 
427
        return new JsonModel($data);
428
    }
429
 
4384 eleazar 430
}