Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2516 nelberth 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Authentication\AuthenticationService;
7
use Laminas\Authentication\Result as AuthResult;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Mvc\I18n\Translator;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Model\PlanningObjectivesAndGoalsTask;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Form\PlanningObjectivesAndGoalsTaskForm;
18
 
19
use LeadersLinked\Library\Functions;
20
 
21
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsTaskMapper;
2565 nelberth 22
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsGoalsMapper;
2516 nelberth 23
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsObjectivesMapper;
24
 
25
class PlanningObjectivesAndGoalsTaskController extends AbstractActionController
26
{
27
    /**
28
     *
29
     * @var AdapterInterface
30
     */
31
    private $adapter;
32
 
33
 
34
    /**
35
     *
36
     * @var AbstractAdapter
37
     */
38
    private $cache;
39
 
40
    /**
41
     *
42
     * @var  LoggerInterface
43
     */
44
    private $logger;
45
 
46
    /**
47
     *
48
     * @var array
49
     */
50
    private $config;
51
 
52
 
53
 
54
    /**
55
     *
56
     * @param AdapterInterface $adapter
57
     * @param AbstractAdapter $cache
58
     * @param LoggerInterface $logger
59
     * @param array $config
60
     */
61
    public function __construct($adapter, $cache , $logger, $config)
62
    {
63
        $this->adapter      = $adapter;
64
        $this->cache        = $cache;
65
        $this->logger       = $logger;
66
        $this->config       = $config;
67
 
68
 
69
    }
70
 
2565 nelberth 71
 
2516 nelberth 72
 
73
    public function indexAction()
74
    {
2781 nelberth 75
        $uuidObjective = $this->params()->fromRoute('uuid_objective');
76
        $uuidGoals= $this->params()->fromRoute('uuid_goals');
2516 nelberth 77
        $request = $this->getRequest();
78
 
79
        if($request->isGet()) {
80
 
81
 
82
            $headers  = $request->getHeaders();
83
 
84
            $isJson = false;
85
            if($headers->has('Accept')) {
86
                $accept = $headers->get('Accept');
87
 
88
                $prioritized = $accept->getPrioritized();
89
 
90
                foreach($prioritized as $key => $value) {
91
                    $raw = trim($value->getRaw());
92
 
93
                    if(!$isJson) {
94
                        $isJson = strpos($raw, 'json');
95
                    }
96
 
97
                }
98
            }
99
 
100
            if($isJson) {
2826 nelberth 101
                $currentUserPlugin = $this->plugin('currentUserPlugin');
102
                $currentUser = $currentUserPlugin->getUser();
2825 nelberth 103
                $currentCompany = $currentUserPlugin->getCompany();
2824 nelberth 104
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
2823 nelberth 105
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'planning-objectives-and-goals/objectives/goals/task/edit');
106
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'planning-objectives-and-goals/objectives/goals/task/delete');
107
                $allowView = $acl->isAllowed($currentUser->usertype_id, 'planning-objectives-and-goals/objectives/goals/task/view');
2516 nelberth 108
 
109
                $search = $this->params()->fromQuery('search', []);
110
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
111
 
112
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
3335 nelberth 113
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
2516 nelberth 114
                $order =  $this->params()->fromQuery('order', []);
115
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
116
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
117
 
118
                $fields =  ['title'];
119
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
120
 
121
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
122
                    $order_direction = 'ASC';
123
                }
2825 nelberth 124
 
2781 nelberth 125
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
126
                $objective = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuidObjective);
127
 
128
                if (!$objective) {
2516 nelberth 129
                    $data = [
130
                        'success' => false,
2781 nelberth 131
                        'data' => 'ERROR_RECORD_NOT_FOUND'
2516 nelberth 132
                    ];
2781 nelberth 133
 
2516 nelberth 134
 
135
                        return new JsonModel($data);
2781 nelberth 136
 
137
                }else{
138
                    if($objective->id_company==$currentCompany->id){
139
                        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
140
                        $goals = $planningObjectivesAndGoalsGoalsMapper->fetchOneByUuid($uuidGoals);
141
 
142
                        if(!$goals){
143
                            $data = [
144
                                'success' => false,
145
                                'data' => 'ERROR_RECORD_NOT_FOUND'
146
                            ];
147
 
148
 
149
                                return new JsonModel($data);
150
                        }
151
                    }else{
152
                        $data = [
153
                            'success' => false,
154
                            'data' => 'ERROR_UNAUTHORIZED',
155
                        ];
156
 
157
                            return new JsonModel($data);
158
                    }
159
 
160
 
161
 
2516 nelberth 162
                }
2781 nelberth 163
 
2516 nelberth 164
 
165
                $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
2565 nelberth 166
                $paginator = $planningObjectivesAndGoalsTaskMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $goals->id);
2516 nelberth 167
 
168
                $items = [];
169
 
170
                $records = $paginator->getCurrentItems();
3030 nelberth 171
 
3058 nelberth 172
 
2516 nelberth 173
                foreach($records as $record)
174
                {
175
 
176
                    $item = [
177
                        'title' => $record->title,
178
                        'description' => $record->description,
2884 nelberth 179
                        'progress'=> $record->indicator,
2846 nelberth 180
                        'cost'=> $record->cost,
3045 nelberth 181
                        'status' => $record->status,
2516 nelberth 182
                        'actions' => [
2828 nelberth 183
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('planning-objectives-and-goals/objectives/goals/task/edit', ['uuid_objective' => $uuidObjective,'uuid_goals' => $uuidGoals,'id' => $record->uuid]) : '',
3003 nelberth 184
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('planning-objectives-and-goals/objectives/goals/task/delete', ['uuid_objective' => $uuidObjective,'uuid_goals' => $uuidGoals,'id' => $record->uuid]) : ''
185
                            ]
2516 nelberth 186
 
187
                    ];
3058 nelberth 188
                    array_push($items, $item);
189
                }
3065 nelberth 190
 
3303 nelberth 191
                $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($goals->id);
192
 
3070 nelberth 193
 
3058 nelberth 194
                $costGoals=0;
195
                $indicatorGoals=0;
3311 nelberth 196
 
3171 nelberth 197
                foreach($recordsTask as $record)
3058 nelberth 198
                {
3030 nelberth 199
                    $indicatorGoals=$indicatorGoals+$record->indicator;
200
                    $costGoals=$costGoals+$record->cost;
2516 nelberth 201
                }
3317 nelberth 202
                $countRecordsTask = count($recordsTask);
203
                if($countRecordsTask >0){
3301 nelberth 204
 
3333 nelberth 205
                    $indicatorGoals=round($indicatorGoals/$countRecordsTask, 2);
3193 nelberth 206
                }else{
207
                    $indicatorGoals=-1;
208
                }
3281 nelberth 209
 
2516 nelberth 210
 
211
                return new JsonModel([
212
                    'success' => true,
213
                    'data' => [
214
                        'items' => $items,
215
                        'total' => $paginator->getTotalItemCount(),
3030 nelberth 216
                        'goals' => [
217
                            'titleGoals' =>$goals->title,
218
                            'descriptionGoals' =>$goals->description,
219
                            'costGoals'=>'$'.$costGoals,
3193 nelberth 220
                            'indicatorGoals'=> $indicatorGoals,
3049 nelberth 221
                            'statusGoals'=> $goals->status=='a'?'LABEL_ACTIVE':'LABEL_INACTIVE',
2516 nelberth 222
 
223
                        ]
224
                    ]
225
                ]);
226
            } else  {
227
                $formAdd = new PlanningObjectivesAndGoalsTaskForm();
228
                $this->layout()->setTemplate('layout/layout-backend');
229
                $viewModel = new ViewModel();
2547 nelberth 230
                $viewModel->setTemplate('leaders-linked/planning-objectives-and-goals-task/index.phtml');
2516 nelberth 231
                $viewModel->setVariables([
232
                    'formAdd' => $formAdd,
2781 nelberth 233
                    'uuid_objective'=> $uuidObjective,
234
                    'uuid_goals'=> $uuidGoals,
2516 nelberth 235
 
2530 nelberth 236
 
2516 nelberth 237
                ]);
238
                return $viewModel ;
239
            }
240
        } else {
241
            return new JsonModel([
242
                'success' => false,
243
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
244
            ]);
245
        }
246
    }
247
    public function addAction()
248
    {
249
      $currentUserPlugin = $this->plugin('currentUserPlugin');
250
        $currentUser = $currentUserPlugin->getUser();
251
        $currentCompany = $currentUserPlugin->getCompany();
2781 nelberth 252
        $uuidObjective = $this->params()->fromRoute('uuid_objective');
253
        $uuidGoals = $this->params()->fromRoute('uuid_goals');
2516 nelberth 254
        $request = $this->getRequest();
255
        if($request->isPost()) {
256
            $form = new  PlanningObjectivesAndGoalsTaskForm();
257
            $dataPost = $request->getPost()->toArray();
2981 nelberth 258
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsTask::STATUS_INACTIVE;
3024 nelberth 259
            if($dataPost['indicator']!=100){
260
                $dataPost['evaluation']='';
261
                $dataPost['detour']='';
262
            }
2516 nelberth 263
            $form->setData($dataPost);
264
 
265
            if($form->isValid()) {
2782 nelberth 266
                $currentUserPlugin = $this->plugin('currentUserPlugin');
267
                $currentCompany = $currentUserPlugin->getCompany();
268
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
269
                $objective = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuidObjective);
270
 
271
                if (!$objective) {
2516 nelberth 272
                    $data = [
273
                        'success' => false,
2782 nelberth 274
                        'data' => 'ERROR_RECORD_NOT_FOUND'
2516 nelberth 275
                    ];
2782 nelberth 276
 
2516 nelberth 277
 
278
                        return new JsonModel($data);
2782 nelberth 279
 
280
                }else{
281
                    if($objective->id_company==$currentCompany->id){
282
                        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
283
                        $goals = $planningObjectivesAndGoalsGoalsMapper->fetchOneByUuid($uuidGoals);
284
 
285
                        if(!$goals){
286
                            $data = [
287
                                'success' => false,
288
                                'data' => 'ERROR_RECORD_NOT_FOUND'
289
                            ];
290
 
291
 
292
                                return new JsonModel($data);
293
                        }
294
                    }else{
295
                        $data = [
296
                            'success' => false,
297
                            'data' => 'ERROR_UNAUTHORIZED',
298
                        ];
299
 
300
                            return new JsonModel($data);
301
                    }
302
 
2516 nelberth 303
                }
2782 nelberth 304
 
2516 nelberth 305
                $dataPost = (array) $form->getData();
2776 nelberth 306
                $dataPost['id_goals']=$goals->id;
2516 nelberth 307
 
308
                $hydrator = new ObjectPropertyHydrator();
309
                $planningObjectivesAndGoalsTask = new PlanningObjectivesAndGoalsTask();
310
                $hydrator->hydrate($dataPost, $planningObjectivesAndGoalsTask);
311
 
312
                $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
313
                $result = $planningObjectivesAndGoalsTaskMapper->insert($planningObjectivesAndGoalsTask);
314
 
315
                if($result) {
2776 nelberth 316
                    $this->logger->info('Se agrego la tarea ' . $planningObjectivesAndGoalsTask->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
2516 nelberth 317
 
318
                    $data = [
319
                        'success'   => true,
320
                        'data'   => 'LABEL_RECORD_ADDED'
321
                    ];
322
                } else {
323
                    $data = [
324
                        'success'   => false,
325
                        'data'      => $planningObjectivesAndGoalsTaskMapper->getError()
326
                    ];
327
 
328
                }
329
 
330
                return new JsonModel($data);
331
 
332
            } else {
333
                $messages = [];
334
                $form_messages = (array) $form->getMessages();
335
                foreach($form_messages  as $fieldname => $field_messages)
336
                {
337
 
338
                    $messages[$fieldname] = array_values($field_messages);
339
                }
340
 
341
                return new JsonModel([
342
                    'success'   => false,
343
                    'data'   => $messages
344
                ]);
345
            }
346
 
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
    }
360
 
361
 
362
 
363
     public function editAction(){
364
 
2783 nelberth 365
 
2516 nelberth 366
        $currentUserPlugin = $this->plugin('currentUserPlugin');
367
        $currentUser = $currentUserPlugin->getUser();
368
        $currentCompany = $currentUserPlugin->getCompany();
369
        $request = $this->getRequest();
370
        $uuid = $this->params()->fromRoute('id');
3000 nelberth 371
 
2516 nelberth 372
 
373
        if(!$uuid) {
374
            $data = [
375
                'success'   => false,
376
                'data'   => 'ERROR_INVALID_PARAMETER'
377
            ];
378
 
379
            return new JsonModel($data);
380
        }
381
 
2781 nelberth 382
        $uuidObjective = $this->params()->fromRoute('uuid_objective');
2783 nelberth 383
        $uuidGoals = $this->params()->fromRoute('uuid_goals');
2782 nelberth 384
        $currentUserPlugin = $this->plugin('currentUserPlugin');
385
        $currentCompany = $currentUserPlugin->getCompany();
386
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
387
        $objective = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuidObjective);
388
 
389
        if (!$objective) {
2516 nelberth 390
            $data = [
391
                'success' => false,
2782 nelberth 392
                'data' => 'ERROR_RECORD_NOT_FOUND'
2516 nelberth 393
            ];
2782 nelberth 394
 
2516 nelberth 395
 
396
                return new JsonModel($data);
2782 nelberth 397
 
398
        }else{
399
            if($objective->id_company==$currentCompany->id){
400
                $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
401
                $goals = $planningObjectivesAndGoalsGoalsMapper->fetchOneByUuid($uuidGoals);
402
 
403
                if(!$goals){
404
                    $data = [
405
                        'success' => false,
406
                        'data' => 'ERROR_RECORD_NOT_FOUND'
407
                    ];
408
 
409
 
410
                        return new JsonModel($data);
411
                }
412
            }else{
413
                $data = [
414
                    'success' => false,
415
                    'data' => 'ERROR_UNAUTHORIZED',
416
                ];
417
 
418
                    return new JsonModel($data);
419
            }
420
 
421
 
422
 
2516 nelberth 423
        }
424
        $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
2782 nelberth 425
        $task = $planningObjectivesAndGoalsTaskMapper->fetchOneByUuid($uuid);
2516 nelberth 426
 
2782 nelberth 427
        if (!$task) {
2516 nelberth 428
            $data = [
429
                'success' => false,
430
                'data' => 'ERROR_RECORD_NOT_FOUND'
431
            ];
432
 
433
            return new JsonModel($data);
434
        }
435
 
436
 
437
        if($request->isPost()) {
3000 nelberth 438
            $form = new  PlanningObjectivesAndGoalsTaskForm();
439
            $dataPost = $request->getPost()->toArray();
440
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsTask::STATUS_INACTIVE;
2516 nelberth 441
            $form->setData($dataPost);
442
 
443
            if($form->isValid()) {
444
                $dataPost = (array) $form->getData();
445
 
446
                $hydrator = new ObjectPropertyHydrator();
2782 nelberth 447
                $hydrator->hydrate($dataPost, $task);
448
                $result = $planningObjectivesAndGoalsTaskMapper->update($task);
2516 nelberth 449
 
450
                if($result) {
2782 nelberth 451
                    $this->logger->info('Se actualizo la tarea ' . $task->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
2516 nelberth 452
 
453
                    $data = [
454
                        'success' => true,
455
                        'data' => 'LABEL_RECORD_UPDATED'
456
                    ];
457
                } else {
458
                    $data = [
459
                        'success'   => false,
460
                        'data'      => $planningObjectivesAndGoalsTaskMapper->getError()
461
                    ];
462
                }
463
 
464
                return new JsonModel($data);
465
 
466
            } else {
467
                $messages = [];
468
                $form_messages = (array) $form->getMessages();
469
                foreach($form_messages  as $fieldname => $field_messages)
470
                {
471
                    $messages[$fieldname] = array_values($field_messages);
472
                }
473
 
474
                return new JsonModel([
475
                    'success'   => false,
476
                    'data'   => $messages
477
                ]);
478
            }
479
        }else if ($request->isGet()) {
480
            $hydrator = new ObjectPropertyHydrator();
481
 
482
            $data = [
483
                'success' => true,
2782 nelberth 484
                'data' => $hydrator->extract($task)
2516 nelberth 485
            ];
486
 
487
            return new JsonModel($data);
488
        } else {
489
            $data = [
490
                'success' => false,
491
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
492
            ];
493
 
494
            return new JsonModel($data);
495
        }
496
 
497
        return new JsonModel($data);
498
 
499
    }
500
 
501
 
502
 
503
 
504
    public function deleteAction(){
505
        $currentUserPlugin = $this->plugin('currentUserPlugin');
506
        $currentCompany = $currentUserPlugin->getCompany();
507
        $currentUser = $currentUserPlugin->getUser();
508
 
509
        $request = $this->getRequest();
510
        $uuid = $this->params()->fromRoute('id');
511
 
512
 
513
        if (!$uuid) {
514
            $data = [
515
                'success' => false,
516
                'data' => 'ERROR_INVALID_PARAMETER'
517
            ];
518
 
519
            return new JsonModel($data);
520
        }
2787 nelberth 521
        $uuidObjective = $this->params()->fromRoute('uuid_objective');
522
        $uuidGoals = $this->params()->fromRoute('uuid_goals');
2782 nelberth 523
        $currentUserPlugin = $this->plugin('currentUserPlugin');
524
        $currentCompany = $currentUserPlugin->getCompany();
525
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
526
        $objective = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuidObjective);
527
 
528
        if (!$objective) {
2516 nelberth 529
            $data = [
530
                'success' => false,
2782 nelberth 531
                'data' => 'ERROR_RECORD_NOT_FOUND'
2516 nelberth 532
            ];
533
                return new JsonModel($data);
2782 nelberth 534
 
535
        }else{
536
            if($objective->id_company==$currentCompany->id){
537
                $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
538
                $goals = $planningObjectivesAndGoalsGoalsMapper->fetchOneByUuid($uuidGoals);
539
 
540
                if(!$goals){
541
                    $data = [
542
                        'success' => false,
543
                        'data' => 'ERROR_RECORD_NOT_FOUND'
544
                    ];
545
 
546
 
547
                        return new JsonModel($data);
548
                }
549
            }else{
550
                $data = [
551
                    'success' => false,
552
                    'data' => 'ERROR_UNAUTHORIZED',
553
                ];
554
 
555
                    return new JsonModel($data);
3024 nelberth 556
            }
2782 nelberth 557
 
2516 nelberth 558
        }
559
        $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
2782 nelberth 560
        $task = $planningObjectivesAndGoalsTaskMapper->fetchOneByUuid($uuid);
2516 nelberth 561
 
2782 nelberth 562
        if (!$task) {
2516 nelberth 563
            $data = [
564
                'success' => false,
565
                'data' => 'ERROR_RECORD_NOT_FOUND'
566
            ];
567
 
568
            return new JsonModel($data);
569
        }
570
 
571
 
572
        if ($request->isPost()) {
573
 
574
 
2782 nelberth 575
            $result = $planningObjectivesAndGoalsTaskMapper->delete($task->id);
2516 nelberth 576
            if ($result) {
2782 nelberth 577
                $this->logger->info('Se borro la tarea con el titulo ' . $task->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
2516 nelberth 578
 
579
                $data = [
580
                    'success' => true,
581
                    'data' => 'LABEL_RECORD_DELETED'
582
                ];
583
            } else {
584
 
585
                $data = [
586
                    'success' => false,
587
                    'data' => $planningObjectivesAndGoalsTaskMapper->getError()
588
                ];
589
 
590
                return new JsonModel($data);
591
            }
592
        } else {
593
            $data = [
594
                'success' => false,
595
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
596
            ];
597
 
598
            return new JsonModel($data);
599
        }
600
 
601
        return new JsonModel($data);
602
    }
603
 
604
 
605
 
606
 
607
}