Proyectos de Subversion LeadersLinked - Backend

Rev

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