Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 4661 | Rev 4672 | 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);
4670 eleazar 166
            return new JsonModel([
167
                'success' => false,
168
                'data' => 'test'
169
            ]);
4579 eleazar 170
            $dataPost = $request->getPost()->toArray();
4661 eleazar 171
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
4579 eleazar 172
 
173
            $form->setData($dataPost);
174
 
175
            if ($form->isValid()) {
176
                $dataPost = (array) $form->getData();
177
 
178
                $hydrator = new ObjectPropertyHydrator();
179
                $survey = new Survey();
180
                $hydrator->hydrate($dataPost, $survey);
181
 
182
                if (!$survey->status) {
183
                    $survey->status = Survey::STATUS_INACTIVE;
184
                }
185
                $survey->company_id = $currentCompany->id;
186
 
187
 
188
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
189
                $result = $surveyMapper->insert($survey);
190
 
191
                if ($result) {
192
                    $this->logger->info('Se agrego la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
193
 
194
                    // Get record by id
195
                    $record = $surveyMapper->fetchOne($survey->id);
196
 
197
                    if ($record) {
198
 
199
                        $data = [
200
                            'success' => true,
201
                            'id' => $record->id,
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
}