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