Proyectos de Subversion LeadersLinked - Backend

Rev

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