Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 16769 Rev 16817
Línea 9... Línea 9...
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;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Library\Functions;
-
 
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use LeadersLinked\Mapper\SurveyFormMapper;
15
use LeadersLinked\Mapper\SurveyFormMapper;
15
use LeadersLinked\Form\SurveyFormForm;
16
use LeadersLinked\Form\Survey\SurveyFormForm;
16
use LeadersLinked\Model\SurveyForm;
17
use LeadersLinked\Model\SurveyForm;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
-
 
Línea 18... Línea 18...
18
 
18
 
Línea 19... Línea 19...
19
class SurveyFormController extends AbstractActionController {
19
class SurveyFormController extends AbstractActionController {
20
 
20
 
Línea 114... Línea 114...
114
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
114
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
115
                    $order_direction = 'ASC';
115
                    $order_direction = 'ASC';
116
                }
116
                }
Línea 117... Línea 117...
117
 
117
 
118
                $surveyMapper = SurveyFormMapper::getInstance($this->adapter);
118
                $surveyMapper = SurveyFormMapper::getInstance($this->adapter);
Línea 119... Línea 119...
119
                $paginator = $surveyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
119
                $paginator = $surveyMapper->fetchAllDataTableNormalByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
120
 
120
 
121
                $items = [];
-
 
122
                $records = $paginator->getCurrentItems();
-
 
123
                
-
 
124
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
-
 
125
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'survey/form/edit');
-
 
Línea 126... Línea 121...
126
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'survey/form/delete');
121
                $items = [];
Línea 127... Línea 122...
127
                
122
                $records = $paginator->getCurrentItems();
128
              
123
              
129
                foreach ($records as $record) {
124
                foreach ($records as $record) {
-
 
125
                   
130
                   
126
                    $item = [
131
                    $item = [
127
                        'id' => $record->id,
132
                        'id' => $record->id,
128
                        'name' => $record->name,
133
                        'name' => $record->name,
129
                        'text' => $record->text,
134
                        'status' => $record->status,
130
                        'status' => $record->status,
135
                        'actions' => [
131
                        'actions' => [
Línea 136... Línea 132...
136
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('survey/form/edit', ['id' => $record->uuid]) : '',
132
                            'link_edit' => $this->url()->fromRoute('survey/form/edit', ['id' => $record->uuid]),
137
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('survey/form/delete', ['id' => $record->uuid]) : '',
133
                            'link_delete' => $this->url()->fromRoute('survey/form/delete', ['id' => $record->uuid])
Línea 165... Línea 161...
165
            ]);
161
            ]);
166
            ;
162
            ;
167
        }
163
        }
168
    }
164
    }
Línea 169... Línea 165...
169
 
165
 
-
 
166
    public function addAction() 
170
    public function addAction() {
167
    {
171
        $request = $this->getRequest();
168
        $request = $this->getRequest();
172
        $currentUserPlugin = $this->plugin('currentUserPlugin');
169
        $currentUserPlugin = $this->plugin('currentUserPlugin');
173
        $currentCompany = $currentUserPlugin->getCompany();
170
        $currentCompany = $currentUserPlugin->getCompany();
Línea 174... Línea 171...
174
        $currentUser = $currentUserPlugin->getUser();
171
        $currentUser = $currentUserPlugin->getUser();
Línea 175... Línea 172...
175
 
172
 
-
 
173
        $request = $this->getRequest();
-
 
174
 
-
 
175
        if ($request->isPost()) {
-
 
176
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
-
 
177
            $id = $this->params()->fromRoute('id');
-
 
178
            
-
 
179
            if($id) {
-
 
180
                $surveyForm = $surveyFormMapper->fetchOneByUuid($id);
-
 
181
                if (!$surveyForm) {
-
 
182
                    $data = [
-
 
183
                        'success' => false,
-
 
184
                        'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
185
                    ];
-
 
186
                    
-
 
187
                    return new JsonModel($data);
-
 
188
                }
-
 
189
                
-
 
190
                if ($surveyForm ->company_id != $currentCompany->id) {
-
 
191
                    return new JsonModel([
-
 
192
                        'success' => false,
-
 
193
                        'data' => 'ERROR_UNAUTHORIZED'
-
 
194
                    ]);
-
 
195
                }
-
 
196
                
-
 
197
                
-
 
198
            } else {
Línea 176... Línea 199...
176
        $request = $this->getRequest();
199
                $surveyForm = new SurveyForm();
Línea 177... Línea 200...
177
 
200
            }
178
        if ($request->isPost()) {
201
            
Línea 179... Línea -...
179
            
-
 
180
            $form = new SurveyFormForm($this->adapter, $currentCompany->id);
-
 
181
            
-
 
182
            $dataPost = $request->getPost()->toArray();
202
            
Línea 183... Línea 203...
183
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
203
            $form = new SurveyFormForm($this->adapter, $currentCompany->id);
-
 
204
            
184
 
205
            $dataPost = $request->getPost()->toArray();
185
          
206
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyForm::STATUS_INACTIVE;
186
            
207
 
187
 
-
 
188
            $form->setData($dataPost);
208
            $form->setData($dataPost);
Línea -... Línea 209...
-
 
209
 
-
 
210
            if ($form->isValid()) {
-
 
211
               
189
 
212
                $dataPost = (array) $form->getData();
-
 
213
                
-
 
214
                $hydrator = new ObjectPropertyHydrator();
190
            if ($form->isValid()) {
215
                $hydrator->hydrate($dataPost, $surveyForm);
-
 
216
 
-
 
217
                
-
 
218
                
191
                $dataPost = (array) $form->getData();
219
                
192
 
-
 
193
                $hydrator = new ObjectPropertyHydrator();
-
 
194
                $survey = new SurveyForm();
-
 
195
                $hydrator->hydrate($dataPost, $survey);
-
 
196
 
-
 
Línea 197... Línea 220...
197
                if (!$survey->status) {
220
                if($surveyForm->id) {
198
                    $survey->status = SurveyForm::STATUS_INACTIVE;
-
 
199
                }
221
                    $result = $surveyFormMapper->update($surveyForm);
200
                $survey->company_id = $currentCompany->id;
222
                } else {
201
 
223
                    $surveyForm->type = SurveyForm::TYPE_NORMAL;
202
 
-
 
203
                $surveyMapper = SurveyFormMapper::getInstance($this->adapter);
-
 
204
                $result = $surveyMapper->insert($survey);
224
                    $surveyForm->company_id = $currentCompany->id;
205
 
-
 
206
                if ($result) {
-
 
207
                    $this->logger->info('Se agrego el formulario' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
208
 
225
                    $result = $surveyFormMapper->insert($surveyForm);
209
                    // Get record by id
-
 
210
                    $record = $surveyMapper->fetchOne($survey->id);
-
 
211
 
226
                    
-
 
227
                }
212
                    if ($record) {
228
 
213
 
-
 
-
 
229
                if ($result) {
214
                        $data = [
230
                    
-
 
231
                    if($surveyForm->uuid) {
215
                            'success' => true,
232
                        $message = 'LABEL_RECORD_UPDATED';
216
                            'id' => $record->id,
-
 
-
 
233
                        
217
                            'action_edit' => $this->url()->fromRoute('survey/form/edit', ['id' => $record->uuid]),
234
                        $this->logger->info('Se actualizo el formulario' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
235
                    } else {
-
 
236
                        $message = 'LABEL_RECORD_ADDED';
-
 
237
                        
-
 
238
                        
-
 
239
                        $this->logger->info('Se agrego el formulario' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
240
                    
-
 
241
                        $surveyForm = $surveyFormMapper->fetchOne($surveyForm->id);
-
 
242
                    
218
                            'data' => 'LABEL_RECORD_ADDED'
243
                    }
219
                        ];
244
                    
220
                    } else {
245
                    $data = [
221
 
246
                        'success' => true,
222
                        $data = [
247
                        'data' => [
223
                            'success' => false,
248
                            'message' => $message,
Línea 224... Línea 249...
224
                            'data' => 'ERROR_RECORD_NOT_FOUND'
249
                            'action' => $this->url()->fromRoute('survey/form/add', ['id'=> $surveyForm->uuid]),
225
                        ];
250
                        ]     
Línea 255... Línea 280...
255
        }
280
        }
Línea 256... Línea 281...
256
 
281
 
257
        return new JsonModel($data);
282
        return new JsonModel($data);
Línea 258... Línea 283...
258
    }
283
    }
-
 
284
 
259
 
285
    public function editAction() 
260
    public function editAction() {
286
    {
261
        $request = $this->getRequest();
287
        $request = $this->getRequest();
262
        $currentUserPlugin = $this->plugin('currentUserPlugin');
288
        $currentUserPlugin = $this->plugin('currentUserPlugin');
Línea 274... Línea 300...
274
            ];
300
            ];
Línea 275... Línea 301...
275
 
301
 
276
            return new JsonModel($data);
302
            return new JsonModel($data);
Línea 277... Línea 303...
277
        }
303
        }
278
 
304
 
Línea 279... Línea 305...
279
        $surveyMapper = SurveyFormMapper::getInstance($this->adapter);
305
        $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
280
        $survey = $surveyMapper->fetchOneByUuid($uuid);
306
        $surveyForm = $surveyFormMapper->fetchOneByUuid($uuid);
281
       
307
       
282
        if (!$survey) {
308
        if (!$surveyForm) {
283
            $data = [
309
            $data = [
Línea 284... Línea 310...
284
                'success' => false,
310
                'success' => false,
285
                'data' => 'ERROR_RECORD_NOT_FOUND'
311
                'data' => 'ERROR_RECORD_NOT_FOUND'
Línea 286... Línea 312...
286
            ];
312
            ];
287
 
313
 
288
            return new JsonModel($data);
314
            return new JsonModel($data);
289
        }
315
        }
290
 
316
 
291
        if ($survey->company_id != $currentCompany->id) {
317
        if ($surveyForm ->company_id != $currentCompany->id) {
Línea 305... Línea 331...
305
 
331
 
306
            if ($form->isValid()) {
332
            if ($form->isValid()) {
Línea 307... Línea 333...
307
                $dataPost = (array) $form->getData();
333
                $dataPost = (array) $form->getData();
308
 
334
 
Línea 309... Línea -...
309
                $hydrator = new ObjectPropertyHydrator();
-
 
310
                $hydrator->hydrate($dataPost, $survey);
-
 
311
 
-
 
Línea 312... Línea 335...
312
                if (!$survey->status) {
335
                $hydrator = new ObjectPropertyHydrator();
Línea 313... Línea 336...
313
                    $survey->status = SurveyForm::STATUS_INACTIVE;
336
                $hydrator->hydrate($dataPost, $surveyForm);
314
                }
337
 
315
 
338
 
316
                $result = $surveyMapper->update($survey);
339
                $result = $surveyFormMapper->update($surveyForm);
317
 
340
 
-
 
341
                if ($result) {
318
                if ($result) {
342
                    $this->logger->info('Se edito el formulario : ' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
319
                    $this->logger->info('Se edito el formulario ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
343
                    $data = [
320
                    $data = [
344
                        'success' => true,
321
                        'success' => true,
345
                        'data' => [ 
322
                        'id' => $survey->id,
346
                            'message' => 'LABEL_RECORD_UPDATED',
323
                        'action_edit' => $this->url()->fromRoute('survey/form/edit', ['id' => $survey->uuid]),
347
                            'action' => $this->url()->fromRoute('survey/form/add', ['id'=> $surveyForm->uuid]),
324
                        'data' => 'LABEL_RECORD_UPDATED'
348
                        ],    
325
                    ];
349
                    ];
326
                } else {
350
                } else {
Línea 327... Línea 351...
327
                    $data = [
351
                    $data = [
328
                        'success' => false,
352
                        'success' => false,
Línea 342... Línea 366...
342
                    'success' => false,
366
                    'success' => false,
343
                    'data' => $messages
367
                    'data' => $messages
344
                ]);
368
                ]);
345
            }
369
            }
346
        } else if ($request->isGet()) {
370
        } else if ($request->isGet()) {
347
            $hydrator = new ObjectPropertyHydrator();
-
 
Línea 348... Línea 371...
348
 
371
 
349
            $data = [
372
            $data = [
350
                'success' => true,
373
                'success' => true,
351
                'data' => [
-
 
352
                    'id' => $survey->uuid,
374
                'data' => [
353
                    'name' => $survey->name,
-
 
354
                    'description' => $survey->description,
375
                    'name'          => $surveyForm->name,
355
                    'text' => $survey->text,
376
                    'text'          => $surveyForm->text,
356
                    'status' => $survey->status,
377
                    'status'        => $surveyForm->status,
357
                    'content' => $survey->content ? json_decode($survey->content) : [],
378
                    'content'       => $surveyForm->content ? json_decode($surveyForm->content, true) : [],
358
                ]
379
                ]
Línea 359... Línea 380...
359
            ];
380
            ];
360
 
381
 
Línea 369... Línea 390...
369
        }
390
        }
Línea 370... Línea 391...
370
 
391
 
371
        return new JsonModel($data);
392
        return new JsonModel($data);
Línea 372... Línea 393...
372
    }
393
    }
-
 
394
 
373
 
395
    public function deleteAction() 
374
    public function deleteAction() {
396
    {
375
        $request = $this->getRequest();
397
        $request = $this->getRequest();
376
        $currentUserPlugin = $this->plugin('currentUserPlugin');
398
        $currentUserPlugin = $this->plugin('currentUserPlugin');
Línea 387... Línea 409...
387
            ];
409
            ];
Línea 388... Línea 410...
388
 
410
 
389
            return new JsonModel($data);
411
            return new JsonModel($data);
Línea 390... Línea 412...
390
        }
412
        }
391
 
413
 
392
        $surveyMapper = SurveyFormMapper::getInstance($this->adapter);
414
        $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
393
        $survey = $surveyMapper->fetchOneByUuid($uuid);
415
        $surveyForm = $surveyFormMapper->fetchOneByUuid($uuid);
394
        if (!$survey) {
416
        if (!$surveyForm) {
395
            $data = [
417
            $data = [
396
                'success' => false,
418
                'success' => false,
Línea 397... Línea 419...
397
                'data' => 'ERROR_RECORD_NOT_FOUND'
419
                'data' => 'ERROR_RECORD_NOT_FOUND'
398
            ];
420
            ];
Línea 399... Línea 421...
399
 
421
 
400
            return new JsonModel($data);
422
            return new JsonModel($data);
401
        }
423
        }
402
 
424
 
403
        if ($survey->company_id != $currentCompany->id) {
425
        if ($surveyForm->company_id != $currentCompany->id) {
404
            return new JsonModel([
426
            return new JsonModel([
Línea 405... Línea 427...
405
                'success' => false,
427
                'success' => false,
Línea 406... Línea 428...
406
                'data' => 'ERROR_UNAUTHORIZED'
428
                'data' => 'ERROR_UNAUTHORIZED'
407
            ]);
429
            ]);
408
        }
430
        }
Línea 409... Línea 431...
409
 
431
 
410
        if ($request->isPost()) {
432
        if ($request->isPost()) {
411
 
433
 
412
            $result = $surveyMapper->delete($survey->id);
434
            $result = $surveyFormMapper->delete($surveyForm->id);
413
            if ($result) {
435
            if ($result) {
Línea 414... Línea 436...
414
                $this->logger->info('Se borro el formulario ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
436
                $this->logger->info('Se borro el formulario : ' . $surveyForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
415
 
437
 
416
                $data = [
438
                $data = [
417
                    'success' => true,
439
                    'success' => true,
Línea 418... Línea 440...
418
                    'data' => 'LABEL_RECORD_DELETED'
440
                    'data' => 'LABEL_RECORD_DELETED'
419
                ];
441
                ];
420
            } else {
442
            } else {