Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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