Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 4670 | Rev 4677 | 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;
14
use LeadersLinked\Form\SurveyForm;
15
use LeadersLinked\Model\Survey;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
4384 eleazar 17
 
18
class SurveyController extends AbstractActionController {
19
 
20
    /**
21
     *
22
     * @var AdapterInterface
23
     */
24
    private $adapter;
25
 
26
    /**
27
     *
28
     * @var AbstractAdapter
29
     */
30
    private $cache;
31
 
32
    /**
33
     *
34
     * @var  LoggerInterface
35
     */
36
    private $logger;
37
 
38
    /**
39
     *
40
     * @var array
41
     */
42
    private $config;
43
 
44
    /**
45
     *
46
     * @param AdapterInterface $adapter
47
     * @param AbstractAdapter $cache
48
     * @param LoggerInterface $logger
49
     * @param array $config
50
     */
51
    public function __construct($adapter, $cache, $logger, $config) {
52
        $this->adapter = $adapter;
53
        $this->cache = $cache;
54
        $this->logger = $logger;
55
        $this->config = $config;
56
    }
57
 
58
    public function indexAction() {
4579 eleazar 59
        $request = $this->getRequest();
60
        $currentUserPlugin = $this->plugin('currentUserPlugin');
61
        $currentCompany = $currentUserPlugin->getCompany();
62
        $currentUser = $currentUserPlugin->getUser();
4384 eleazar 63
 
4579 eleazar 64
 
65
        $request = $this->getRequest();
66
        if ($request->isGet()) {
67
 
68
            $headers = $request->getHeaders();
69
 
70
            $isJson = false;
71
            if ($headers->has('Accept')) {
72
                $accept = $headers->get('Accept');
73
 
74
                $prioritized = $accept->getPrioritized();
75
 
76
                foreach ($prioritized as $key => $value) {
77
                    $raw = trim($value->getRaw());
78
 
79
                    if (!$isJson) {
80
                        $isJson = strpos($raw, 'json');
81
                    }
82
                }
83
            }
84
 
85
            if ($isJson) {
86
                $search = $this->params()->fromQuery('search', []);
87
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
88
 
89
                $page = intval($this->params()->fromQuery('start', 1), 10);
90
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
91
                $order = $this->params()->fromQuery('order', []);
92
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
93
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
94
 
95
                $fields = ['name'];
96
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
97
 
98
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
99
                    $order_direction = 'ASC';
100
                }
101
 
4661 eleazar 102
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
103
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'survey/add');
104
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'survey/edit');
105
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'survey/delete');
106
                $allowSegment = $acl->isAllowed($currentUser->usertype_id, 'survey/segment');
107
 
4579 eleazar 108
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
109
                $paginator = $surveyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
110
 
111
                $items = [];
112
                $records = $paginator->getCurrentItems();
113
 
114
                foreach ($records as $record) {
115
 
116
                    $item = [
117
                        'id' => $record->id,
118
                        'name' => $record->name,
119
                        'status' => $record->status,
120
                        'actions' => [
121
                            'link_edit' => $this->url()->fromRoute('survey/edit', ['id' => $record->uuid]),
122
                            'link_delete' => $this->url()->fromRoute('survey/delete', ['id' => $record->uuid]),
123
                            'link_segment' => $this->url()->fromRoute('survey/segment', ['id' => $record->uuid])
124
                        ]
125
                    ];
126
 
127
                    array_push($items, $item);
128
                }
129
 
130
                return new JsonModel([
131
                    'success' => true,
132
                    'data' => [
133
                        'items' => $items,
134
                        'total' => $paginator->getTotalItemCount(),
135
                    ]
136
                ]);
137
            } else {
138
 
139
                $form = new SurveyForm($this->adapter, $currentCompany->id);
140
 
141
                $this->layout()->setTemplate('layout/layout-backend');
142
                $viewModel = new ViewModel();
143
                $viewModel->setTemplate('leaders-linked/survey/index.phtml');
144
                $viewModel->setVariable('form', $form);
145
                return $viewModel;
146
            }
147
        } else {
148
            return new JsonModel([
149
                'success' => false,
150
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
151
            ]);
152
        }
4384 eleazar 153
    }
154
 
4579 eleazar 155
    public function addAction() {
156
        $request = $this->getRequest();
157
        $currentUserPlugin = $this->plugin('currentUserPlugin');
158
        $currentCompany = $currentUserPlugin->getCompany();
159
        $currentUser = $currentUserPlugin->getUser();
160
 
161
        $request = $this->getRequest();
162
 
163
        if ($request->isPost()) {
164
 
165
            $form = new SurveyForm($this->adapter, $currentCompany->id);
4672 eleazar 166
 
4579 eleazar 167
            $dataPost = $request->getPost()->toArray();
4661 eleazar 168
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
4579 eleazar 169
 
170
            $form->setData($dataPost);
171
 
172
            if ($form->isValid()) {
173
                $dataPost = (array) $form->getData();
174
 
175
                $hydrator = new ObjectPropertyHydrator();
176
                $survey = new Survey();
177
                $hydrator->hydrate($dataPost, $survey);
178
 
179
                if (!$survey->status) {
180
                    $survey->status = Survey::STATUS_INACTIVE;
181
                }
182
                $survey->company_id = $currentCompany->id;
183
 
184
 
185
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
186
                $result = $surveyMapper->insert($survey);
187
 
188
                if ($result) {
189
                    $this->logger->info('Se agrego la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
190
 
191
                    // Get record by id
192
                    $record = $surveyMapper->fetchOne($survey->id);
193
 
194
                    if ($record) {
195
 
196
                        $data = [
197
                            'success' => true,
198
                            'id' => $record->id,
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
                }
296
 
297
                $result = $surveyMapper->update($survey);
298
 
299
                if ($result) {
300
                    $this->logger->info('Se edito la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
301
                    $data = [
302
                        'success' => true,
303
                        'id' => $survey->id,
304
                        'action_edit' => $this->url()->fromRoute('survey/edit', ['id' => $survey->uuid]),
305
                        'data' => 'LABEL_RECORD_UPDATED'
306
                    ];
307
                } else {
308
                    $data = [
309
                        'success' => false,
310
                        'data' => $surveyMapper->getError()
311
                    ];
312
                }
313
 
314
                return new JsonModel($data);
315
            } else {
316
                $messages = [];
317
                $form_messages = (array) $form->getMessages();
318
                foreach ($form_messages as $fieldname => $field_messages) {
319
                    $messages[$fieldname] = array_values($field_messages);
320
                }
321
 
322
                return new JsonModel([
323
                    'success' => false,
324
                    'data' => $messages
325
                ]);
326
            }
327
        } else if ($request->isGet()) {
328
            $hydrator = new ObjectPropertyHydrator();
329
 
330
            $data = [
331
                'success' => true,
332
                'data' => [
4653 eleazar 333
                    'name' => $survey->name,
334
                    'form_id' => $survey->form_id,
335
                    'target' => $survey->target,
336
                    'identity' => $survey->identity,
337
                    'since_date' => $survey->since_date,
338
                    'last_date' => $survey->last_date,
339
                    'status' => $survey->status,
4579 eleazar 340
                ]
341
            ];
342
 
343
            return new JsonModel($data);
344
        } else {
345
            $data = [
346
                'success' => false,
347
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
348
            ];
349
 
350
            return new JsonModel($data);
351
        }
352
 
353
        return new JsonModel($data);
354
    }
355
 
356
    public function deleteAction() {
357
        $request = $this->getRequest();
358
        $currentUserPlugin = $this->plugin('currentUserPlugin');
359
        $currentCompany = $currentUserPlugin->getCompany();
360
        $currentUser = $currentUserPlugin->getUser();
361
 
362
        $request = $this->getRequest();
363
        $uuid = $this->params()->fromRoute('id');
364
 
365
        if (!$uuid) {
366
            $data = [
367
                'success' => false,
368
                'data' => 'ERROR_INVALID_PARAMETER'
369
            ];
370
 
371
            return new JsonModel($data);
372
        }
373
 
374
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
375
        $survey = $surveyMapper->fetchOneByUuid($uuid);
376
        if (!$survey) {
377
            $data = [
378
                'success' => false,
379
                'data' => 'ERROR_RECORD_NOT_FOUND'
380
            ];
381
 
382
            return new JsonModel($data);
383
        }
384
 
385
        if ($survey->company_id != $currentCompany->id) {
386
            return new JsonModel([
387
                'success' => false,
388
                'data' => 'ERROR_UNAUTHORIZED'
389
            ]);
390
        }
391
 
392
        if ($request->isPost()) {
393
 
394
            $result = $surveyMapper->delete($survey->id);
395
            if ($result) {
396
                $this->logger->info('Se borro la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
397
 
398
                $data = [
399
                    'success' => true,
400
                    'data' => 'LABEL_RECORD_DELETED'
401
                ];
402
            } else {
403
 
404
                $data = [
405
                    'success' => false,
406
                    'data' => $surveyMapper->getError()
407
                ];
408
 
409
                return new JsonModel($data);
410
            }
411
        } else {
412
            $data = [
413
                'success' => false,
414
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
415
            ];
416
 
417
            return new JsonModel($data);
418
        }
419
 
420
        return new JsonModel($data);
421
    }
422
 
4384 eleazar 423
}