Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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