Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 4726 | Rev 4728 | 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);
191
 
4579 eleazar 192
                if ($result) {
193
                    $this->logger->info('Se agrego la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
4727 eleazar 194
                    // return new JsonModel([
195
                    //     'success' => false,
196
                    //     'data' => $result,
197
                    // ]);
4722 eleazar 198
 
4579 eleazar 199
                    if ($record) {
200
 
201
                        $data = [
202
                            'success' => true,
203
                            'data' => 'LABEL_RECORD_ADDED'
204
                        ];
205
                    } else {
206
 
207
                        $data = [
208
                            'success' => false,
209
                            'data' => 'ERROR_RECORD_NOT_FOUND'
210
                        ];
211
                    }
212
                } else {
213
                    $data = [
214
                        'success' => false,
215
                        'data' => $surveyMapper->getError()
216
                    ];
217
                }
218
 
219
                return new JsonModel($data);
220
            } else {
221
                $messages = [];
222
                $form_messages = (array) $form->getMessages();
223
                foreach ($form_messages as $fieldname => $field_messages) {
224
 
225
                    $messages[$fieldname] = array_values($field_messages);
226
                }
227
 
228
                return new JsonModel([
229
                    'success' => false,
230
                    'data' => $messages
231
                ]);
232
            }
233
        } else {
234
            $data = [
235
                'success' => false,
236
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
237
            ];
238
 
239
            return new JsonModel($data);
240
        }
241
 
242
        return new JsonModel($data);
243
    }
244
 
245
    public function editAction() {
246
        $request = $this->getRequest();
247
        $currentUserPlugin = $this->plugin('currentUserPlugin');
248
        $currentCompany = $currentUserPlugin->getCompany();
249
        $currentUser = $currentUserPlugin->getUser();
250
 
251
        $request = $this->getRequest();
252
        $uuid = $this->params()->fromRoute('id');
253
 
254
 
255
        if (!$uuid) {
256
            $data = [
257
                'success' => false,
258
                'data' => 'ERROR_INVALID_PARAMETER'
259
            ];
260
 
261
            return new JsonModel($data);
262
        }
263
 
264
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
265
        $survey = $surveyMapper->fetchOneByUuid($uuid);
266
 
267
        if (!$survey) {
268
            $data = [
269
                'success' => false,
270
                'data' => 'ERROR_RECORD_NOT_FOUND'
271
            ];
272
 
273
            return new JsonModel($data);
274
        }
275
 
276
        if ($survey->company_id != $currentCompany->id) {
277
            return new JsonModel([
278
                'success' => false,
279
                'data' => 'ERROR_UNAUTHORIZED'
280
            ]);
281
        }
282
 
283
 
284
        if ($request->isPost()) {
285
            $form = new SurveyForm();
286
            $dataPost = $request->getPost()->toArray();
287
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
288
 
289
            $form->setData($dataPost);
290
 
291
            if ($form->isValid()) {
292
                $dataPost = (array) $form->getData();
293
 
294
                $hydrator = new ObjectPropertyHydrator();
295
                $hydrator->hydrate($dataPost, $survey);
296
 
297
                if (!$survey->status) {
298
                    $survey->status = Survey::STATUS_INACTIVE;
299
                }
300
 
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
 
334
            $data = [
335
                'success' => true,
336
                'data' => [
4653 eleazar 337
                    'name' => $survey->name,
338
                    'form_id' => $survey->form_id,
339
                    'target' => $survey->target,
340
                    'identity' => $survey->identity,
341
                    'since_date' => $survey->since_date,
342
                    'last_date' => $survey->last_date,
343
                    'status' => $survey->status,
4579 eleazar 344
                ]
345
            ];
346
 
347
            return new JsonModel($data);
348
        } else {
349
            $data = [
350
                'success' => false,
351
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
352
            ];
353
 
354
            return new JsonModel($data);
355
        }
356
 
357
        return new JsonModel($data);
358
    }
359
 
360
    public function deleteAction() {
361
        $request = $this->getRequest();
362
        $currentUserPlugin = $this->plugin('currentUserPlugin');
363
        $currentCompany = $currentUserPlugin->getCompany();
364
        $currentUser = $currentUserPlugin->getUser();
365
 
366
        $request = $this->getRequest();
367
        $uuid = $this->params()->fromRoute('id');
368
 
369
        if (!$uuid) {
370
            $data = [
371
                'success' => false,
372
                'data' => 'ERROR_INVALID_PARAMETER'
373
            ];
374
 
375
            return new JsonModel($data);
376
        }
377
 
378
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
379
        $survey = $surveyMapper->fetchOneByUuid($uuid);
380
        if (!$survey) {
381
            $data = [
382
                'success' => false,
383
                'data' => 'ERROR_RECORD_NOT_FOUND'
384
            ];
385
 
386
            return new JsonModel($data);
387
        }
388
 
389
        if ($survey->company_id != $currentCompany->id) {
390
            return new JsonModel([
391
                'success' => false,
392
                'data' => 'ERROR_UNAUTHORIZED'
393
            ]);
394
        }
395
 
396
        if ($request->isPost()) {
397
 
398
            $result = $surveyMapper->delete($survey->id);
399
            if ($result) {
400
                $this->logger->info('Se borro la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
401
 
402
                $data = [
403
                    'success' => true,
404
                    'data' => 'LABEL_RECORD_DELETED'
405
                ];
406
            } else {
407
 
408
                $data = [
409
                    'success' => false,
410
                    'data' => $surveyMapper->getError()
411
                ];
412
 
413
                return new JsonModel($data);
414
            }
415
        } else {
416
            $data = [
417
                'success' => false,
418
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
419
            ];
420
 
421
            return new JsonModel($data);
422
        }
423
 
424
        return new JsonModel($data);
425
    }
426
 
4384 eleazar 427
}