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