Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2252 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\PlanningObjectivesAndGoalsObjectives;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Form\PlanningObjectivesAndGoalsObjectivesForm;
18
 
19
use LeadersLinked\Library\Functions;
20
 
3205 nelberth 21
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsTaskMapper;
22
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsGoalsMapper;
2252 nelberth 23
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsObjectivesMapper;
24
 
25
class PlanningObjectivesAndGoalsObjectivesController extends AbstractActionController
26
{
27
    /**
28
     *
29
     * @var AdapterInterface
30
     */
31
    private $adapter;
32
 
33
 
34
    /**
35
     *
36
     * @var AbstractAdapter
37
     */
38
    private $cache;
39
 
40
    /**
41
     *
42
     * @var  LoggerInterface
43
     */
44
    private $logger;
45
 
46
    /**
47
     *
48
     * @var array
49
     */
50
    private $config;
51
 
52
 
53
 
54
    /**
55
     *
56
     * @param AdapterInterface $adapter
57
     * @param AbstractAdapter $cache
58
     * @param LoggerInterface $logger
59
     * @param array $config
60
     */
61
    public function __construct($adapter, $cache , $logger, $config)
62
    {
63
        $this->adapter      = $adapter;
64
        $this->cache        = $cache;
65
        $this->logger       = $logger;
66
        $this->config       = $config;
67
 
68
 
69
    }
70
 
71
 
72
 
73
 
74
    public function indexAction()
75
    {
2624 efrain 76
        $currentUserPlugin = $this->plugin('currentUserPlugin');
77
        $currentUser = $currentUserPlugin->getUser();
78
        $currentCompany = $currentUserPlugin->getCompany();
2252 nelberth 79
 
2624 efrain 80
 
2252 nelberth 81
        $request = $this->getRequest();
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
            }
102
 
103
            if($isJson) {
104
 
2629 nelberth 105
 
2624 efrain 106
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
107
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'planning-objectives-and-goals/objectives/edit');
108
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'planning-objectives-and-goals/objectives/delete');
2657 nelberth 109
                $allowObjective = $acl->isAllowed($currentUser->usertype_id, 'planning-objectives-and-goals/objectives/goals');
2624 efrain 110
 
2630 nelberth 111
 
2624 efrain 112
 
2252 nelberth 113
                $search = $this->params()->fromQuery('search', []);
114
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
115
 
3334 nelberth 116
 
2252 nelberth 117
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
3337 nelberth 118
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
3333 nelberth 119
                $order =  $this->params()->fromQuery('order', []);
2252 nelberth 120
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
121
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
122
 
123
                $fields =  ['title', 'date'];
124
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
125
 
126
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
127
                    $order_direction = 'ASC';
128
                }
129
 
2624 efrain 130
 
2252 nelberth 131
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
3227 nelberth 132
                $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
133
                $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
2252 nelberth 134
                $paginator = $planningObjectivesAndGoalsObjectivesMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $currentCompany->id);
135
 
136
                $items = [];
137
 
138
                $records = $paginator->getCurrentItems();
3227 nelberth 139
 
2252 nelberth 140
                foreach($records as $record)
141
                {
3205 nelberth 142
 
143
 
144
 
3317 nelberth 145
                    $recordsGoals = $planningObjectivesAndGoalsGoalsMapper->fetchAll($record->id);
3205 nelberth 146
                    $costObjective=0;
147
                    $indicatorObjective=0;
148
                    $contador=0;
3317 nelberth 149
                    $countRecordsGoals = count($recordsGoals);
150
                    if($countRecordsGoals>0){
3206 nelberth 151
                        foreach($recordsGoals as $record2)
3205 nelberth 152
                        {
3317 nelberth 153
                            $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
3205 nelberth 154
                            $costGoals=0;
155
                            $indicatorGoals=0;
3317 nelberth 156
                            $countRecordsTask = count($recordsTask);
157
                            if($countRecordsTask>0){
3206 nelberth 158
                                foreach($recordsTask as $record3){
159
                                    $indicatorGoals=$indicatorGoals+$record3->indicator;
160
                                    $costGoals=$costGoals+$record3->cost;
3205 nelberth 161
                                }
3317 nelberth 162
                                $indicatorObjective=$indicatorObjective+($indicatorGoals/$countRecordsTask);
3205 nelberth 163
                                $costObjective=$costObjective+$costGoals;
164
                                $contador++;
165
                            }
166
                        }
167
                        if($indicatorObjective==0){
168
                            $indicatorObjective=(-1);
169
                        }else{
170
                            $indicatorObjective=round($indicatorObjective/$contador,2);
171
 
172
                        }
173
                    }else{
174
                        $indicatorObjective=(-2);
175
                    }
176
 
2653 nelberth 177
                    $date='';
178
                    if($record->date!=NULL){
179
                        $dt = \DateTime::createFromFormat('Y-m-d', $record->date);
180
                        $date =  $dt->format('d/m/Y');
181
                    }
2252 nelberth 182
                    $item = [
183
                        'title' => $record->title,
184
                        'description' => $record->description,
2653 nelberth 185
                        'date'=> $date,
3207 nelberth 186
                        'cost'=>$costObjective,
3205 nelberth 187
                        'progress'=>$indicatorObjective,
3199 nelberth 188
                        'status'=> $record->status,
2252 nelberth 189
                        'actions' => [
2624 efrain 190
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('planning-objectives-and-goals/objectives/edit', ['id' => $record->uuid]) : '',
191
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('planning-objectives-and-goals/objectives/delete', ['id' => $record->uuid]) : '',
2657 nelberth 192
                            'link_objective' => $allowObjective ? $this->url()->fromRoute('planning-objectives-and-goals/objectives/goals', ['uuid_objective' => $record->uuid]) : '',
2624 efrain 193
 
2252 nelberth 194
                        ]
195
 
196
                    ];
2638 nelberth 197
 
198
 
199
                    array_push($items, $item);
2646 nelberth 200
 
2252 nelberth 201
                }
2645 nelberth 202
 
3317 nelberth 203
                $recordsObjectivesModule = $planningObjectivesAndGoalsObjectivesMapper->fetchAll($currentCompany->id);
3230 nelberth 204
                $costModule=0;
205
                $indicatorModule=0;
3227 nelberth 206
                $contadorTotalModule=0;
3317 nelberth 207
                $countRecordsObjectives = count($recordsObjectivesModule);
208
                if($countRecordsObjectives>0){
3227 nelberth 209
 
3317 nelberth 210
                    foreach($recordsObjectivesModule as $record)
3227 nelberth 211
                    {
3235 nelberth 212
 
3317 nelberth 213
                        $recordsGoalsModule = $planningObjectivesAndGoalsGoalsMapper->fetchAll($record->id);
3227 nelberth 214
                        $costObjectiveModule=0;
215
                        $indicatorObjectiveModule=0;
216
                        $contadorModule=0;
3317 nelberth 217
                        $countRecordsGoalsModule = count($recordsGoalsModule);
218
                        if($countRecordsGoalsModule>0){
3236 nelberth 219
 
3227 nelberth 220
                            foreach($recordsGoalsModule as $record2)
221
                            {
3239 nelberth 222
 
3317 nelberth 223
                                $recordsTaskModule = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
3227 nelberth 224
                                $costGoalsModule=0;
225
                                $indicatorGoalsModule=0;
3317 nelberth 226
                                $countRecordsTaskModule = count($recordsTaskModule);
227
                                if($countRecordsTaskModule>0){
3227 nelberth 228
                                    foreach($recordsTaskModule as $record3){
3238 nelberth 229
 
3227 nelberth 230
                                        $indicatorGoalsModule=$indicatorGoalsModule+$record3->indicator;
231
                                        $costGoalsModule=$costGoalsModule+$record3->cost;
232
                                    }
3317 nelberth 233
                                    $indicatorObjectiveModule=$indicatorObjectiveModule+($indicatorGoalsModule/$countRecordsTaskModule);
3227 nelberth 234
                                    $costModule=$costModule+$costGoalsModule;
235
                                    $contadorModule++;
236
                                }
237
                            }
3231 nelberth 238
                            if($indicatorObjectiveModule>0){
3227 nelberth 239
                                $indicatorModule=$indicatorModule+($indicatorObjectiveModule/$contadorModule);
240
                                $contadorTotalModule++;
241
                            }
242
                        }
243
 
244
                    }
3231 nelberth 245
                    if($indicatorModule==0){
3232 nelberth 246
                        $indicatorModule=(-5);
3231 nelberth 247
                    }else{
248
                        $indicatorModule=round($indicatorModule/$contadorTotalModule,2);
249
                    }
3227 nelberth 250
                }else{
3229 nelberth 251
                    $indicatorModule=(-3);
3227 nelberth 252
                }
253
 
254
 
2643 nelberth 255
                return new JsonModel([
2252 nelberth 256
                    'success' => true,
257
                    'data' => [
2643 nelberth 258
                        'items' => $items,
3323 nelberth 259
                        'total' => $paginator->getTotalItemCount(),
3223 nelberth 260
                        'module'=>[
3226 nelberth 261
                            'costModule' => '$'.$costModule,
3332 nelberth 262
                            'indicatorModule'=>$indicatorModule
3223 nelberth 263
                        ]
2252 nelberth 264
                    ]
265
                ]);
2641 nelberth 266
 
2252 nelberth 267
            } else  {
268
                $formAdd = new PlanningObjectivesAndGoalsObjectivesForm();
269
                $this->layout()->setTemplate('layout/layout-backend');
270
                $viewModel = new ViewModel();
2254 nelberth 271
                $viewModel->setTemplate('leaders-linked/planning-objectives-and-goals-objectives/index.phtml');
2252 nelberth 272
                $viewModel->setVariables([
273
                    'formAdd' => $formAdd,
274
                ]);
275
                return $viewModel ;
276
            }
277
        } else {
278
            return new JsonModel([
279
                'success' => false,
280
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
281
            ]);
282
        }
283
    }
284
    public function addAction()
285
    {
286
      $currentUserPlugin = $this->plugin('currentUserPlugin');
287
        $currentUser = $currentUserPlugin->getUser();
288
        $currentCompany = $currentUserPlugin->getCompany();
289
 
290
        $request = $this->getRequest();
291
        if($request->isPost()) {
292
            $form = new  PlanningObjectivesAndGoalsObjectivesForm();
293
            $dataPost = $request->getPost()->toArray();
294
 
295
            $form->setData($dataPost);
296
 
297
            if($form->isValid()) {
298
                $dataPost = (array) $form->getData();
3219 nelberth 299
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsObjectives::STATUS_INACTIVE;
2252 nelberth 300
                $dataPost['id_company']=$currentCompany->id;
301
 
302
                $hydrator = new ObjectPropertyHydrator();
303
                $planningObjectivesAndGoalsObjectives = new PlanningObjectivesAndGoalsObjectives();
304
                $hydrator->hydrate($dataPost, $planningObjectivesAndGoalsObjectives);
305
 
306
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
307
                $result = $planningObjectivesAndGoalsObjectivesMapper->insert($planningObjectivesAndGoalsObjectives);
308
 
309
                if($result) {
310
                    $this->logger->info('Se agrego el objetivo ' . $planningObjectivesAndGoalsObjectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
311
 
312
                    $data = [
313
                        'success'   => true,
314
                        'data'   => 'LABEL_RECORD_ADDED'
315
                    ];
316
                } else {
317
                    $data = [
318
                        'success'   => false,
319
                        'data'      => $planningObjectivesAndGoalsObjectivesMapper->getError()
320
                    ];
321
 
322
                }
323
 
324
                return new JsonModel($data);
325
 
326
            } else {
327
                $messages = [];
328
                $form_messages = (array) $form->getMessages();
329
                foreach($form_messages  as $fieldname => $field_messages)
330
                {
331
 
332
                    $messages[$fieldname] = array_values($field_messages);
333
                }
334
 
335
                return new JsonModel([
336
                    'success'   => false,
337
                    'data'   => $messages
338
                ]);
339
            }
340
 
341
        } else {
342
            $data = [
343
                'success' => false,
344
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
345
            ];
346
 
347
            return new JsonModel($data);
348
        }
349
 
350
        return new JsonModel($data);
351
 
352
 
353
    }
354
 
355
 
356
 
357
     public function editAction(){
358
 
359
        $currentUserPlugin = $this->plugin('currentUserPlugin');
360
        $currentUser = $currentUserPlugin->getUser();
361
        $currentCompany = $currentUserPlugin->getCompany();
362
        $request = $this->getRequest();
363
        $uuid = $this->params()->fromRoute('id');
364
 
365
 
366
        if(!$uuid) {
367
            $data = [
368
                'success'   => false,
369
                'data'   => 'ERROR_INVALID_PARAMETER'
370
            ];
371
 
372
            return new JsonModel($data);
373
        }
374
 
375
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
376
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuid);
377
 
378
        if (!$objectives) {
379
            $data = [
380
                'success' => false,
381
                'data' => 'ERROR_RECORD_NOT_FOUND'
382
            ];
383
 
384
            return new JsonModel($data);
385
        }
386
 
387
        if ($objectives->id_company != $currentCompany->id) {
388
            return new JsonModel([
389
                'success' => false,
390
                'data' => 'ERROR_UNAUTHORIZED'
391
            ]);
392
        }
393
 
394
        if($request->isPost()) {
395
            $form = new  PlanningObjectivesAndGoalsObjectivesForm();
396
            $dataPost = $request->getPost()->toArray();
3219 nelberth 397
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsObjectives::STATUS_INACTIVE;
2252 nelberth 398
            $form->setData($dataPost);
399
 
400
            if($form->isValid()) {
401
                $dataPost = (array) $form->getData();
402
 
403
                $hydrator = new ObjectPropertyHydrator();
404
                $hydrator->hydrate($dataPost, $objectives);
405
                $result = $planningObjectivesAndGoalsObjectivesMapper->update($objectives);
406
 
407
                if($result) {
408
                    $this->logger->info('Se actualizo el objetivo ' . $objectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
409
 
410
                    $data = [
411
                        'success' => true,
412
                        'data' => 'LABEL_RECORD_UPDATED'
413
                    ];
414
                } else {
415
                    $data = [
416
                        'success'   => false,
417
                        'data'      => $planningObjectivesAndGoalsObjectivesMapper->getError()
418
                    ];
419
                }
420
 
421
                return new JsonModel($data);
422
 
423
            } else {
424
                $messages = [];
425
                $form_messages = (array) $form->getMessages();
426
                foreach($form_messages  as $fieldname => $field_messages)
427
                {
428
                    $messages[$fieldname] = array_values($field_messages);
429
                }
430
 
431
                return new JsonModel([
432
                    'success'   => false,
433
                    'data'   => $messages
434
                ]);
435
            }
436
        }else if ($request->isGet()) {
437
            $hydrator = new ObjectPropertyHydrator();
438
 
439
            $data = [
440
                'success' => true,
441
                'data' => $hydrator->extract($objectives)
442
            ];
443
 
444
            return new JsonModel($data);
445
        } else {
446
            $data = [
447
                'success' => false,
448
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
449
            ];
450
 
451
            return new JsonModel($data);
452
        }
453
 
454
        return new JsonModel($data);
455
 
456
    }
457
 
458
 
459
 
460
 
461
    public function deleteAction(){
462
        $currentUserPlugin = $this->plugin('currentUserPlugin');
463
        $currentCompany = $currentUserPlugin->getCompany();
464
        $currentUser = $currentUserPlugin->getUser();
465
 
466
        $request = $this->getRequest();
467
        $uuid = $this->params()->fromRoute('id');
468
 
469
 
470
        if (!$uuid) {
471
            $data = [
472
                'success' => false,
473
                'data' => 'ERROR_INVALID_PARAMETER'
474
            ];
475
 
476
            return new JsonModel($data);
477
        }
478
 
479
 
480
 
481
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
482
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuid);
483
 
484
        if (!$objectives) {
485
            $data = [
486
                'success' => false,
487
                'data' => 'ERROR_RECORD_NOT_FOUND'
488
            ];
489
 
490
            return new JsonModel($data);
491
        }
492
 
493
        if ($objectives->id_company != $currentCompany->id) {
494
            return new JsonModel([
495
                'success' => false,
496
                'data' => 'ERROR_UNAUTHORIZED'
497
            ]);
498
        }
499
 
500
        if ($request->isPost()) {
501
 
502
 
503
            $result = $planningObjectivesAndGoalsObjectivesMapper->delete($objectives->id);
504
            if ($result) {
505
                $this->logger->info('Se borro el objetivo con el titulo ' . $objectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
506
 
507
                $data = [
508
                    'success' => true,
509
                    'data' => 'LABEL_RECORD_DELETED'
510
                ];
511
            } else {
512
 
513
                $data = [
514
                    'success' => false,
515
                    'data' => $planningObjectivesAndGoalsObjectivesMapper->getError()
516
                ];
517
 
518
                return new JsonModel($data);
519
            }
520
        } else {
521
            $data = [
522
                'success' => false,
523
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
524
            ];
525
 
526
            return new JsonModel($data);
527
        }
528
 
529
        return new JsonModel($data);
530
    }
531
 
532
 
533
 
534
}