Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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