Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 4384 | Rev 4653 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 4384 Rev 4579
Línea 6... Línea 6...
6
 
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Mvc\Controller\AbstractActionController;
-
 
10
use Laminas\Log\LoggerInterface;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
-
 
12
use Laminas\View\Model\JsonModel;
-
 
13
use LeadersLinked\Mapper\SurveyMapper;
-
 
14
use LeadersLinked\Form\SurveyForm;
-
 
15
use LeadersLinked\Model\Survey;
Línea 11... Línea 16...
11
use Laminas\View\Model\JsonModel;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
Línea 12... Línea 17...
12
 
17
 
13
class SurveyController extends AbstractActionController {
18
class SurveyController extends AbstractActionController {
Línea 49... Línea 54...
49
        $this->logger = $logger;
54
        $this->logger = $logger;
50
        $this->config = $config;
55
        $this->config = $config;
51
    }
56
    }
Línea 52... Línea 57...
52
 
57
 
-
 
58
    public function indexAction() {
-
 
59
        $request = $this->getRequest();
-
 
60
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
61
        $currentCompany = $currentUserPlugin->getCompany();
Línea -... Línea 62...
-
 
62
        $currentUser = $currentUserPlugin->getUser();
-
 
63
 
-
 
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
 
-
 
102
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
-
 
103
                $paginator = $surveyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
-
 
104
 
-
 
105
                $items = [];
-
 
106
                $records = $paginator->getCurrentItems();
-
 
107
              
-
 
108
                foreach ($records as $record) {
-
 
109
                   
-
 
110
                    $item = [
-
 
111
                        'id' => $record->id,
-
 
112
                        'name' => $record->name,
-
 
113
                        'status' => $record->status,
-
 
114
                        'actions' => [
-
 
115
                            'link_edit' => $this->url()->fromRoute('survey/edit', ['id' => $record->uuid]),
-
 
116
                            'link_delete' => $this->url()->fromRoute('survey/delete', ['id' => $record->uuid]),
-
 
117
                            'link_segment' => $this->url()->fromRoute('survey/segment', ['id' => $record->uuid])
-
 
118
                        ]
-
 
119
                    ];
-
 
120
 
-
 
121
                    array_push($items, $item);
-
 
122
                }
-
 
123
 
-
 
124
                return new JsonModel([
-
 
125
                    'success' => true,
-
 
126
                    'data' => [
-
 
127
                        'items' => $items,
-
 
128
                        'total' => $paginator->getTotalItemCount(),
-
 
129
                    ]
-
 
130
                ]);
-
 
131
            } else {
-
 
132
 
-
 
133
                $form = new SurveyForm($this->adapter, $currentCompany->id);
-
 
134
 
-
 
135
                $this->layout()->setTemplate('layout/layout-backend');
-
 
136
                $viewModel = new ViewModel();
-
 
137
                $viewModel->setTemplate('leaders-linked/survey/index.phtml');
-
 
138
                $viewModel->setVariable('form', $form);
-
 
139
                return $viewModel;
53
    public function indexAction() {
140
            }
54
 
141
        } else {
55
        return new JsonModel([
142
            return new JsonModel([
-
 
143
                'success' => false,
-
 
144
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
56
            'success' => false,
145
            ]);
-
 
146
            ;
-
 
147
        }
-
 
148
    }
-
 
149
 
-
 
150
    public function addAction() {
-
 
151
        $request = $this->getRequest();
-
 
152
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
153
        $currentCompany = $currentUserPlugin->getCompany();
-
 
154
        $currentUser = $currentUserPlugin->getUser();
-
 
155
 
-
 
156
        $request = $this->getRequest();
-
 
157
 
-
 
158
        if ($request->isPost()) {
-
 
159
            
-
 
160
            $form = new SurveyForm($this->adapter, $currentCompany->id);
-
 
161
            
-
 
162
            $dataPost = $request->getPost()->toArray();
-
 
163
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
-
 
164
 
-
 
165
          
-
 
166
            
-
 
167
 
-
 
168
            $form->setData($dataPost);
-
 
169
 
-
 
170
            if ($form->isValid()) {
-
 
171
                $dataPost = (array) $form->getData();
-
 
172
 
-
 
173
                $hydrator = new ObjectPropertyHydrator();
-
 
174
                $survey = new Survey();
-
 
175
                $hydrator->hydrate($dataPost, $survey);
-
 
176
 
-
 
177
                if (!$survey->status) {
-
 
178
                    $survey->status = Survey::STATUS_INACTIVE;
-
 
179
                }
-
 
180
                $survey->company_id = $currentCompany->id;
-
 
181
 
-
 
182
 
-
 
183
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
-
 
184
                $result = $surveyMapper->insert($survey);
-
 
185
 
-
 
186
                if ($result) {
-
 
187
                    $this->logger->info('Se agrego la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
188
 
-
 
189
                    // Get record by id
-
 
190
                    $record = $surveyMapper->fetchOne($survey->id);
-
 
191
 
-
 
192
                    if ($record) {
-
 
193
 
-
 
194
                        $data = [
-
 
195
                            'success' => true,
-
 
196
                            'id' => $record->id,
-
 
197
                            'data' => 'LABEL_RECORD_ADDED'
-
 
198
                        ];
-
 
199
                    } else {
-
 
200
 
-
 
201
                        $data = [
-
 
202
                            'success' => false,
-
 
203
                            'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
204
                        ];
-
 
205
                    }
-
 
206
                } else {
-
 
207
                    $data = [
-
 
208
                        'success' => false,
-
 
209
                        'data' => $surveyMapper->getError()
-
 
210
                    ];
-
 
211
                }
-
 
212
 
-
 
213
                return new JsonModel($data);
-
 
214
            } else {
-
 
215
                $messages = [];
-
 
216
                $form_messages = (array) $form->getMessages();
-
 
217
                foreach ($form_messages as $fieldname => $field_messages) {
-
 
218
 
-
 
219
                    $messages[$fieldname] = array_values($field_messages);
-
 
220
                }
-
 
221
 
-
 
222
                return new JsonModel([
-
 
223
                    'success' => false,
-
 
224
                    'data' => $messages
-
 
225
                ]);
-
 
226
            }
-
 
227
        } else {
-
 
228
            $data = [
-
 
229
                'success' => false,
-
 
230
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
231
            ];
-
 
232
 
-
 
233
            return new JsonModel($data);
-
 
234
        }
-
 
235
 
-
 
236
        return new JsonModel($data);
-
 
237
    }
-
 
238
 
-
 
239
    public function editAction() {
-
 
240
        $request = $this->getRequest();
-
 
241
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
242
        $currentCompany = $currentUserPlugin->getCompany();
-
 
243
        $currentUser = $currentUserPlugin->getUser();
-
 
244
 
-
 
245
        $request = $this->getRequest();
-
 
246
        $uuid = $this->params()->fromRoute('id');
-
 
247
 
-
 
248
 
-
 
249
        if (!$uuid) {
-
 
250
            $data = [
-
 
251
                'success' => false,
-
 
252
                'data' => 'ERROR_INVALID_PARAMETER'
-
 
253
            ];
-
 
254
 
-
 
255
            return new JsonModel($data);
-
 
256
        }
-
 
257
 
-
 
258
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
-
 
259
        $survey = $surveyMapper->fetchOneByUuid($uuid);
-
 
260
       
-
 
261
        if (!$survey) {
-
 
262
            $data = [
-
 
263
                'success' => false,
-
 
264
                'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
265
            ];
-
 
266
 
-
 
267
            return new JsonModel($data);
-
 
268
        }
-
 
269
 
-
 
270
        if ($survey->company_id != $currentCompany->id) {
-
 
271
            return new JsonModel([
-
 
272
                'success' => false,
-
 
273
                'data' => 'ERROR_UNAUTHORIZED'
-
 
274
            ]);
-
 
275
        }
-
 
276
 
-
 
277
 
-
 
278
        if ($request->isPost()) {
-
 
279
            $form = new SurveyForm();
-
 
280
            $dataPost = $request->getPost()->toArray();
-
 
281
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
-
 
282
 
-
 
283
            $form->setData($dataPost);
-
 
284
 
-
 
285
            if ($form->isValid()) {
-
 
286
                $dataPost = (array) $form->getData();
-
 
287
 
-
 
288
                $hydrator = new ObjectPropertyHydrator();
-
 
289
                $hydrator->hydrate($dataPost, $survey);
-
 
290
 
-
 
291
                if (!$survey->status) {
-
 
292
                    $survey->status = Survey::STATUS_INACTIVE;
-
 
293
                }
-
 
294
 
-
 
295
                $result = $surveyMapper->update($survey);
-
 
296
 
-
 
297
                if ($result) {
-
 
298
                    $this->logger->info('Se edito la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
299
                    $data = [
-
 
300
                        'success' => true,
-
 
301
                        'id' => $survey->id,
-
 
302
                        'action_edit' => $this->url()->fromRoute('survey/edit', ['id' => $survey->uuid]),
-
 
303
                        'data' => 'LABEL_RECORD_UPDATED'
-
 
304
                    ];
-
 
305
                } else {
-
 
306
                    $data = [
-
 
307
                        'success' => false,
-
 
308
                        'data' => $surveyMapper->getError()
-
 
309
                    ];
-
 
310
                }
-
 
311
 
-
 
312
                return new JsonModel($data);
-
 
313
            } else {
-
 
314
                $messages = [];
-
 
315
                $form_messages = (array) $form->getMessages();
-
 
316
                foreach ($form_messages as $fieldname => $field_messages) {
-
 
317
                    $messages[$fieldname] = array_values($field_messages);
-
 
318
                }
-
 
319
 
-
 
320
                return new JsonModel([
-
 
321
                    'success' => false,
-
 
322
                    'data' => $messages
-
 
323
                ]);
-
 
324
            }
-
 
325
        } else if ($request->isGet()) {
-
 
326
            $hydrator = new ObjectPropertyHydrator();
-
 
327
 
-
 
328
            $data = [
-
 
329
                'success' => true,
-
 
330
                'data' => [
-
 
331
                    //TODO 
-
 
332
                ]
-
 
333
            ];
-
 
334
 
-
 
335
            return new JsonModel($data);
-
 
336
        } else {
-
 
337
            $data = [
-
 
338
                'success' => false,
-
 
339
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
340
            ];
-
 
341
 
-
 
342
            return new JsonModel($data);
-
 
343
        }
-
 
344
 
-
 
345
        return new JsonModel($data);
-
 
346
    }
-
 
347
 
-
 
348
    public function deleteAction() {
-
 
349
        $request = $this->getRequest();
-
 
350
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
351
        $currentCompany = $currentUserPlugin->getCompany();
-
 
352
        $currentUser = $currentUserPlugin->getUser();
-
 
353
 
-
 
354
        $request = $this->getRequest();
-
 
355
        $uuid = $this->params()->fromRoute('id');
-
 
356
 
-
 
357
        if (!$uuid) {
-
 
358
            $data = [
-
 
359
                'success' => false,
-
 
360
                'data' => 'ERROR_INVALID_PARAMETER'
-
 
361
            ];
-
 
362
 
-
 
363
            return new JsonModel($data);
-
 
364
        }
-
 
365
 
-
 
366
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
-
 
367
        $survey = $surveyMapper->fetchOneByUuid($uuid);
-
 
368
        if (!$survey) {
-
 
369
            $data = [
-
 
370
                'success' => false,
-
 
371
                'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
372
            ];
-
 
373
 
-
 
374
            return new JsonModel($data);
-
 
375
        }
-
 
376
 
-
 
377
        if ($survey->company_id != $currentCompany->id) {
-
 
378
            return new JsonModel([
-
 
379
                'success' => false,
-
 
380
                'data' => 'ERROR_UNAUTHORIZED'
-
 
381
            ]);
-
 
382
        }
-
 
383
 
-
 
384
        if ($request->isPost()) {
-
 
385
 
-
 
386
            $result = $surveyMapper->delete($survey->id);
-
 
387
            if ($result) {
-
 
388
                $this->logger->info('Se borro la encuesta ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
389
 
-
 
390
                $data = [
-
 
391
                    'success' => true,
-
 
392
                    'data' => 'LABEL_RECORD_DELETED'
-
 
393
                ];
-
 
394
            } else {
-
 
395
 
-
 
396
                $data = [
-
 
397
                    'success' => false,
-
 
398
                    'data' => $surveyMapper->getError()
-
 
399
                ];
-
 
400
 
-
 
401
                return new JsonModel($data);
-
 
402
            }
-
 
403
        } else {
-
 
404
            $data = [
-
 
405
                'success' => false,
-
 
406
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
407
            ];
-
 
408
 
-
 
409
            return new JsonModel($data);
-
 
410
        }
57
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
411
 
Línea 58... Línea 412...
58
        ]);
412
        return new JsonModel($data);