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