Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12737 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\PlanningObjectivesAndGoalsGoals;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Form\PlanningObjectivesAndGoalsGoalsForm;
18
 
19
use LeadersLinked\Library\Functions;
20
use LeadersLinked\Mapper\UserMapper;
21
 
22
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsTaskMapper;
23
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsGoalsMapper;
24
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsObjectivesMapper;
12748 nelberth 25
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
26
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
27
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
28
use LeadersLinked\Model\HighPerformanceTeamsGroups;
12737 nelberth 29
class HighPerformanceTeamsGroupsViewGoalsController extends AbstractActionController
30
{
31
    /**
32
     *
33
     * @var AdapterInterface
34
     */
35
    private $adapter;
36
 
37
 
38
    /**
39
     *
40
     * @var AbstractAdapter
41
     */
42
    private $cache;
43
 
44
    /**
45
     *
46
     * @var  LoggerInterface
47
     */
48
    private $logger;
49
 
50
    /**
51
     *
52
     * @var array
53
     */
54
    private $config;
55
 
56
 
57
 
58
    /**
59
     *
60
     * @param AdapterInterface $adapter
61
     * @param AbstractAdapter $cache
62
     * @param LoggerInterface $logger
63
     * @param array $config
64
     */
65
    public function __construct($adapter, $cache , $logger, $config)
66
    {
67
        $this->adapter      = $adapter;
68
        $this->cache        = $cache;
69
        $this->logger       = $logger;
70
        $this->config       = $config;
71
 
72
 
73
    }
74
   /*
75
  public function idObjective($uuid){
76
        $currentUserPlugin = $this->plugin('currentUserPlugin');
77
        $currentCompany = $currentUserPlugin->getCompany();
78
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
79
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuid);
80
 
81
        if (!$objectives) {
82
            return false;
83
 
84
        }else{
85
            if($objectives->company_id==$currentCompany->id){
86
                return $objectives;
87
            }else{
88
                return false;
89
            }
90
 
91
 
92
 
93
        }
94
 
95
 
96
   }*/
97
 
98
 
99
    public function indexAction()
100
    {
12741 nelberth 101
 
12737 nelberth 102
        $objective_uuid = $this->params()->fromRoute('objective_id');
103
        if(!$objective_uuid) {
104
            $data = [
105
                'success'   => false,
106
                'data'   => 'ERROR_INVALID_PARAMETER'
107
            ];
108
 
109
            return new JsonModel($data);
110
        }
111
 
112
        $request = $this->getRequest();
113
 
114
        if($request->isGet()) {
115
 
116
 
117
            $headers  = $request->getHeaders();
118
 
119
            $isJson = false;
120
            if($headers->has('Accept')) {
121
                $accept = $headers->get('Accept');
122
 
123
                $prioritized = $accept->getPrioritized();
124
 
125
                foreach($prioritized as $key => $value) {
126
                    $raw = trim($value->getRaw());
127
 
128
                    if(!$isJson) {
129
                        $isJson = strpos($raw, 'json');
130
                    }
131
 
132
                }
133
            }
134
 
135
            if($isJson) {
136
                $currentUserPlugin = $this->plugin('currentUserPlugin');
137
                $currentUser = $currentUserPlugin->getUser();
138
                $currentCompany = $currentUserPlugin->getCompany();
139
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
12746 nelberth 140
 
141
                $group_uuid = $this->params()->fromRoute('group_id');
142
                if(!$group_uuid) {
143
                    $data = [
144
                        'success'   => false,
145
                        'data'   => 'ERROR_INVALID_PARAMETER'
146
                    ];
147
 
148
                    return new JsonModel($data);
149
                }
150
 
151
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
152
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
153
 
154
                if (!$highPerformanceTeamsGroups) {
155
                    $data = [
156
                        'success' => false,
157
                        'data' => 'ERROR_RECORD_NOT_FOUND'
158
                    ];
159
 
160
                    return new JsonModel($data);
161
                }
162
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
163
 
164
                    return new JsonModel([
165
                        'success' => false,
166
                        'data' => 'ERROR_UNAUTHORIZED'
167
                    ]);
168
 
169
                }
170
                $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
171
                $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
172
                if(!$highPerformanceTeamsGroupsMember) {
173
                    return new JsonModel([
174
                        'success' => false,
175
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
176
                    ]);
177
                }
178
                if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
179
                    return new JsonModel([
180
                        'success' => false,
181
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
182
                    ]);
183
                }
184
 
185
 
12750 nelberth 186
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/goals/edit');
187
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/goals/delete');
188
                $allowGoals = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/goals/task');
12762 nelberth 189
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/goals/add');
12737 nelberth 190
 
191
                $search = $this->params()->fromQuery('search', []);
192
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
193
 
194
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
195
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
196
                $order =  $this->params()->fromQuery('order', []);
197
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
198
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
199
 
200
                $fields =  ['title'];
201
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
202
 
203
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
204
                    $order_direction = 'ASC';
205
                }
206
 
207
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
208
                $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($objective_uuid);
209
 
210
                if (!$objectives) {
211
                    $data = [
212
                        'success' => false,
213
                        'data' => 'ERROR_RECORD_NOT_FOUND'
214
                    ];
215
 
216
 
217
                        return new JsonModel($data);
218
                }
219
 
220
                if($objectives->company_id!=$currentCompany->id){
221
                        $data = [
222
                            'success' => false,
223
                            'data' => 'ERROR_UNAUTHORIZED',
224
                        ];
225
                        return new JsonModel($data);
226
                }
12750 nelberth 227
 
228
                if($objectives->high_performance_team_group_id !=$highPerformanceTeamsGroups->id){
229
                    $data = [
230
                        'success' => false,
231
                        'data' => 'ERROR_UNAUTHORIZED',
232
                    ];
233
                    return new JsonModel($data);
12923 nelberth 234
                }
12737 nelberth 235
 
236
 
237
                $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
238
                $paginator = $planningObjectivesAndGoalsGoalsMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $objectives->id);
239
 
240
                $items = [];
241
 
242
                $records = $paginator->getCurrentItems();
243
                $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
244
                foreach($records as $record)
245
                {
246
 
247
                    $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record->id);
248
 
249
                    $costGoals=0;
250
                    $indicatorGoals=0;
251
                    foreach($recordsTask as $record2){
252
                        $indicatorGoals=$indicatorGoals+$record2->indicator;
253
                        $costGoals=$costGoals+$record2->cost;
254
                    }
255
                    $countRecordsTask = count($recordsTask);
256
                    if($countRecordsTask>0){
257
                        $indicatorGoals=round($indicatorGoals/$countRecordsTask, 2);
258
                    }else{
259
                        $indicatorGoals=-1;
260
                    }
261
 
262
                    $item = [
263
                        'title' => $record->title,
264
                        'description' => $record->description,
265
                        'progress'=>$indicatorGoals,
266
                        'cost' => $costGoals,
12762 nelberth 267
 
12737 nelberth 268
                        'status'=>$record->status,
269
                        'actions' => [
12750 nelberth 270
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/goals/edit', ['group_id'=>$highPerformanceTeamsGroups->uuid,'objective_id' => $objective_uuid,'id' => $record->uuid]) : '',
271
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/goals/delete', ['group_id'=>$highPerformanceTeamsGroups->uuid,'objective_id' => $objective_uuid,'id' => $record->uuid]) : '',
272
                            'link_goals' => $allowGoals ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/goals/task', ['group_id'=>$highPerformanceTeamsGroups->uuid,'objective_id' => $objective_uuid,'goal_id' => $record->uuid]) : ''
12737 nelberth 273
                        ]
274
 
275
                    ];
276
 
277
 
278
                    array_push($items, $item);
279
                }
280
 
281
                $recordsGoals = $planningObjectivesAndGoalsGoalsMapper->fetchAll($objectives->id);
282
 
283
                $costObjective=0;
284
                $indicatorObjective=0;
285
                $contador=0;
286
                $countRecordsGoals = count($recordsGoals);
287
                if($countRecordsGoals>0){
288
                    $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
289
 
290
                    foreach($recordsGoals as $record)
291
                    {
292
                        $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record->id);
293
 
294
                        $costGoals=0;
295
                        $indicatorGoals=0;
296
                        $countRecordsTask = count($recordsTask);
297
                        if($countRecordsTask>0){
298
                            foreach($recordsTask as $record2){
299
                                $indicatorGoals=$indicatorGoals+$record2->indicator;
300
                                $costGoals=$costGoals+$record2->cost;
301
                            }
302
                            $indicatorObjective=$indicatorObjective+($indicatorGoals/$countRecordsTask);
303
                            $costObjective=$costObjective+$costGoals;
304
                            $contador++;
305
                        }
306
                    }
307
                    if($indicatorObjective==0){
308
                        $indicatorObjective=(-1);
309
                    }else{
310
                        $indicatorObjective=round($indicatorObjective/$contador,2);
311
 
312
                    }
313
                }else{
314
                    $indicatorObjective=(-2);
315
                }
316
                $date='Sin fecha';
317
                if($objectives->date!=NULL){
318
                    $dt = \DateTime::createFromFormat('Y-m-d', $objectives->date);
319
                    $date =  $dt->format('d/m/Y');
320
                }
321
 
322
 
323
 
324
                return new JsonModel([
325
                    'success' => true,
326
                    'data' => [
327
                        'items' => $items,
328
                        'total' => $paginator->getTotalItemCount(),
329
                        'objective' => [
330
                            'titleObjective' =>$objectives->title,
331
                            'descriptionObjective' =>$objectives->description,
332
                            'dateObjective' =>$date,
333
                            'costObjective' =>'$'.$costObjective,
334
                            'indicatorObjective'=>$indicatorObjective,
12762 nelberth 335
                            'link_add'=>$allowAdd ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/goals/add', ['group_id'=>$highPerformanceTeamsGroups->uuid,'objective_id' => $objective_uuid]) : '',
12752 nelberth 336
                            'goBackObjective'=> $this->url()->fromRoute('high-performance-teams/groups/view/objectives', ['group_id'=>$highPerformanceTeamsGroups->uuid]),
12737 nelberth 337
                            'statusObjective'=>$objectives->status=='a'?'LABEL_ACTIVE':'LABEL_INACTIVE',
338
 
339
                        ]
340
                    ]
341
                ]);
12746 nelberth 342
            }
12737 nelberth 343
        } else {
344
            return new JsonModel([
345
                'success' => false,
346
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
347
            ]);
348
        }
349
    }
350
    public function addAction()
351
    {
352
      $currentUserPlugin = $this->plugin('currentUserPlugin');
353
        $currentUser = $currentUserPlugin->getUser();
354
        $currentCompany = $currentUserPlugin->getCompany();
355
        $objective_uuid = $this->params()->fromRoute('objective_id');
12933 nelberth 356
 
12737 nelberth 357
        if(!$objective_uuid) {
358
            $data = [
359
                'success'   => false,
360
                'data'   => 'ERROR_INVALID_PARAMETER'
361
            ];
362
 
363
            return new JsonModel($data);
364
        }
365
 
12746 nelberth 366
        $group_uuid = $this->params()->fromRoute('group_id');
367
        if(!$group_uuid) {
368
            $data = [
369
                'success'   => false,
370
                'data'   => 'ERROR_INVALID_PARAMETER'
371
            ];
372
 
373
            return new JsonModel($data);
374
        }
375
 
376
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
377
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
378
 
379
        if (!$highPerformanceTeamsGroups) {
380
            $data = [
381
                'success' => false,
382
                'data' => 'ERROR_RECORD_NOT_FOUND'
383
            ];
384
 
385
            return new JsonModel($data);
386
        }
387
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
388
 
389
            return new JsonModel([
390
                'success' => false,
391
                'data' => 'ERROR_UNAUTHORIZED'
392
            ]);
393
 
394
        }
395
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
396
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
397
        if(!$highPerformanceTeamsGroupsMember) {
398
            return new JsonModel([
399
                'success' => false,
400
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
401
            ]);
402
        }
403
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
404
            return new JsonModel([
405
                'success' => false,
406
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
407
            ]);
408
        }
409
 
12737 nelberth 410
        $request = $this->getRequest();
411
        if($request->isPost()) {
412
            $form = new  PlanningObjectivesAndGoalsGoalsForm();
413
            $dataPost = $request->getPost()->toArray();
414
            $form->setData($dataPost);
415
 
416
            if($form->isValid()) {
417
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
418
                $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($objective_uuid);
419
 
420
                if (!$objectives) {
421
                    $data = [
422
                        'success' => false,
423
                        'data' => 'ERROR_RECORD_NOT_FOUND'
424
                    ];
425
 
426
 
427
                        return new JsonModel($data);
428
                }
429
 
430
                if($objectives->company_id!=$currentCompany->id){
431
                        $data = [
432
                            'success' => false,
433
                            'data' => 'ERROR_UNAUTHORIZED',
434
                        ];
435
                        return new JsonModel($data);
436
                }
12762 nelberth 437
                if($objectives->high_performance_team_group_id !=$highPerformanceTeamsGroups->id){
438
                    $data = [
439
                        'success' => false,
440
                        'data' => 'ERROR_UNAUTHORIZED',
441
                    ];
442
                    return new JsonModel($data);
443
            }
12737 nelberth 444
                $dataPost = (array) $form->getData();
445
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsGoals::STATUS_INACTIVE;
446
                $dataPost['objective_id']=$objectives->id;
447
 
448
                $hydrator = new ObjectPropertyHydrator();
449
                $planningObjectivesAndGoalsGoals = new PlanningObjectivesAndGoalsGoals();
450
                $hydrator->hydrate($dataPost, $planningObjectivesAndGoalsGoals);
451
 
452
                $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
453
                $result = $planningObjectivesAndGoalsGoalsMapper->insert($planningObjectivesAndGoalsGoals);
454
 
455
                if($result) {
456
                    $this->logger->info('Se agrego la meta ' . $planningObjectivesAndGoalsGoals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
457
 
458
                    $data = [
459
                        'success'   => true,
460
                        'data'   => 'LABEL_RECORD_ADDED'
461
                    ];
462
                } else {
463
                    $data = [
464
                        'success'   => false,
465
                        'data'      => $planningObjectivesAndGoalsGoalsMapper->getError()
466
                    ];
467
 
468
                }
469
 
470
                return new JsonModel($data);
471
 
472
            } else {
473
                $messages = [];
474
                $form_messages = (array) $form->getMessages();
475
                foreach($form_messages  as $fieldname => $field_messages)
476
                {
477
 
478
                    $messages[$fieldname] = array_values($field_messages);
479
                }
480
 
481
                return new JsonModel([
482
                    'success'   => false,
483
                    'data'   => $messages
484
                ]);
485
            }
486
 
487
        } else {
488
            $data = [
489
                'success' => false,
490
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
491
            ];
492
 
493
            return new JsonModel($data);
494
        }
495
 
496
        return new JsonModel($data);
497
 
498
 
499
    }
500
 
501
 
502
 
503
     public function editAction(){
504
 
505
        $currentUserPlugin = $this->plugin('currentUserPlugin');
506
        $currentUser = $currentUserPlugin->getUser();
507
        $currentCompany = $currentUserPlugin->getCompany();
508
        $request = $this->getRequest();
509
        $uuid = $this->params()->fromRoute('id');
510
 
511
 
512
        if(!$uuid) {
513
            $data = [
514
                'success'   => false,
515
                'data'   => 'ERROR_INVALID_PARAMETER'
516
            ];
517
 
518
            return new JsonModel($data);
519
        }
520
 
521
        $objective_uuid = $this->params()->fromRoute('objective_id');
522
        if(!$objective_uuid) {
523
            $data = [
524
                'success'   => false,
525
                'data'   => 'ERROR_INVALID_PARAMETER'
526
            ];
527
 
528
            return new JsonModel($data);
529
        }
12746 nelberth 530
 
531
 
12737 nelberth 532
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
533
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($objective_uuid);
534
 
535
        if (!$objectives) {
536
            $data = [
537
                'success' => false,
538
                'data' => 'ERROR_RECORD_NOT_FOUND'
539
            ];
540
 
541
 
542
                return new JsonModel($data);
543
        }
544
 
545
        if($objectives->company_id!=$currentCompany->id){
546
                $data = [
547
                    'success' => false,
548
                    'data' => 'ERROR_UNAUTHORIZED',
549
                ];
550
                return new JsonModel($data);
551
        }
552
 
553
        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
554
        $goals = $planningObjectivesAndGoalsGoalsMapper->fetchOneByUuid($uuid);
555
 
556
        if (!$goals) {
557
            $data = [
558
                'success' => false,
559
                'data' => 'ERROR_RECORD_NOT_FOUND'
560
            ];
561
 
562
            return new JsonModel($data);
563
        }
12746 nelberth 564
        $group_uuid = $this->params()->fromRoute('group_id');
565
        if(!$group_uuid) {
566
            $data = [
567
                'success'   => false,
568
                'data'   => 'ERROR_INVALID_PARAMETER'
569
            ];
570
 
571
            return new JsonModel($data);
572
        }
12737 nelberth 573
 
12746 nelberth 574
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
575
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
576
 
577
        if (!$highPerformanceTeamsGroups) {
578
            $data = [
579
                'success' => false,
580
                'data' => 'ERROR_RECORD_NOT_FOUND'
581
            ];
12737 nelberth 582
 
12746 nelberth 583
            return new JsonModel($data);
584
        }
585
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
586
 
587
            return new JsonModel([
588
                'success' => false,
589
                'data' => 'ERROR_UNAUTHORIZED'
590
            ]);
591
 
592
        }
593
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
594
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
595
        if(!$highPerformanceTeamsGroupsMember) {
596
            return new JsonModel([
597
                'success' => false,
598
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
599
            ]);
600
        }
601
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
602
            return new JsonModel([
603
                'success' => false,
604
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
605
            ]);
606
        }
607
 
12737 nelberth 608
        if($request->isPost()) {
609
            $form = new  PlanningObjectivesAndGoalsGoalsForm();
610
            $dataPost = $request->getPost()->toArray();
611
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsGoals::STATUS_INACTIVE;
612
            $form->setData($dataPost);
613
 
614
            if($form->isValid()) {
615
                $dataPost = (array) $form->getData();
616
 
617
                $hydrator = new ObjectPropertyHydrator();
618
                $hydrator->hydrate($dataPost, $goals);
619
                $result = $planningObjectivesAndGoalsGoalsMapper->update($goals);
620
 
621
                if($result) {
622
                    $this->logger->info('Se actualizo la meta ' . $goals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
623
 
624
                    $data = [
625
                        'success' => true,
626
                        'data' => 'LABEL_RECORD_UPDATED'
627
                    ];
628
                } else {
629
                    $data = [
630
                        'success'   => false,
631
                        'data'      => $planningObjectivesAndGoalsGoalsMapper->getError()
632
                    ];
633
                }
634
 
635
                return new JsonModel($data);
636
 
637
            } else {
638
                $messages = [];
639
                $form_messages = (array) $form->getMessages();
640
                foreach($form_messages  as $fieldname => $field_messages)
641
                {
642
                    $messages[$fieldname] = array_values($field_messages);
643
                }
644
 
645
                return new JsonModel([
646
                    'success'   => false,
647
                    'data'   => $messages
648
                ]);
649
            }
650
        }else if ($request->isGet()) {
651
            $hydrator = new ObjectPropertyHydrator();
652
 
653
            $data = [
654
                'success' => true,
655
                'data' => $hydrator->extract($goals)
656
            ];
657
 
658
            return new JsonModel($data);
659
        } else {
660
            $data = [
661
                'success' => false,
662
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
663
            ];
664
 
665
            return new JsonModel($data);
666
        }
667
 
668
        return new JsonModel($data);
669
 
670
    }
671
 
672
 
673
 
674
 
675
    public function deleteAction(){
676
        $currentUserPlugin = $this->plugin('currentUserPlugin');
677
        $currentCompany = $currentUserPlugin->getCompany();
678
        $currentUser = $currentUserPlugin->getUser();
679
 
680
        $request = $this->getRequest();
681
        $uuid = $this->params()->fromRoute('id');
682
 
683
 
684
        if (!$uuid) {
685
            $data = [
686
                'success' => false,
687
                'data' => 'ERROR_INVALID_PARAMETER'
688
            ];
689
 
690
            return new JsonModel($data);
691
        }
692
 
693
        $objective_uuid = $this->params()->fromRoute('objective_id');
694
        if(!$objective_uuid) {
695
            $data = [
696
                'success'   => false,
697
                'data'   => 'ERROR_INVALID_PARAMETER'
698
            ];
699
 
700
            return new JsonModel($data);
701
        }
702
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
703
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($objective_uuid);
704
 
705
        if (!$objectives) {
706
            $data = [
707
                'success' => false,
708
                'data' => 'ERROR_RECORD_NOT_FOUND'
709
            ];
710
 
711
 
712
                return new JsonModel($data);
713
        }
714
 
715
        if($objectives->company_id!=$currentCompany->id){
716
                $data = [
717
                    'success' => false,
718
                    'data' => 'ERROR_UNAUTHORIZED',
719
                ];
720
                return new JsonModel($data);
721
        }
722
 
723
 
724
        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
725
        $goals = $planningObjectivesAndGoalsGoalsMapper->fetchOneByUuid($uuid);
726
 
727
        if (!$goals) {
728
            $data = [
729
                'success' => false,
730
                'data' => 'ERROR_RECORD_NOT_FOUND'
731
            ];
732
 
733
            return new JsonModel($data);
734
        }
12746 nelberth 735
        $group_uuid = $this->params()->fromRoute('group_id');
736
        if(!$group_uuid) {
737
            $data = [
738
                'success'   => false,
739
                'data'   => 'ERROR_INVALID_PARAMETER'
740
            ];
741
 
742
            return new JsonModel($data);
743
        }
12737 nelberth 744
 
12746 nelberth 745
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
746
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
747
 
748
        if (!$highPerformanceTeamsGroups) {
749
            $data = [
750
                'success' => false,
751
                'data' => 'ERROR_RECORD_NOT_FOUND'
752
            ];
753
 
754
            return new JsonModel($data);
755
        }
756
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
757
 
758
            return new JsonModel([
759
                'success' => false,
760
                'data' => 'ERROR_UNAUTHORIZED'
761
            ]);
762
 
763
        }
764
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
765
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
766
        if(!$highPerformanceTeamsGroupsMember) {
767
            return new JsonModel([
768
                'success' => false,
769
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
770
            ]);
771
        }
772
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
773
            return new JsonModel([
774
                'success' => false,
775
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
776
            ]);
777
        }
12737 nelberth 778
 
779
        if ($request->isPost()) {
780
 
781
 
782
            $result = $planningObjectivesAndGoalsGoalsMapper->delete($goals->id);
783
            if ($result) {
784
                $this->logger->info('Se borro la meta con el titulo ' . $goals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
785
 
786
                $data = [
787
                    'success' => true,
788
                    'data' => 'LABEL_RECORD_DELETED'
789
                ];
790
            } else {
791
 
792
                $data = [
793
                    'success' => false,
794
                    'data' => $planningObjectivesAndGoalsGoalsMapper->getError()
795
                ];
796
 
797
                return new JsonModel($data);
798
            }
799
        } else {
800
            $data = [
801
                'success' => false,
802
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
803
            ];
804
 
805
            return new JsonModel($data);
806
        }
807
 
808
        return new JsonModel($data);
809
    }
810
 
811
 
812
 
813
 
814
}