Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16785 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\JsonModel;
12
use Laminas\View\Model\ViewModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
15
use LeadersLinked\Mapper\PlanningGoalMapper;
16
use LeadersLinked\Model\PlanningGoal;
17
use LeadersLinked\Form\Planning\PlanningGoalForm;
18
use LeadersLinked\Form\Planning\PlanningGoalHeaderForm;
19
use LeadersLinked\Mapper\PlanningPeriodMapper;
20
use LeadersLinked\Model\PlanningObjective;
21
use LeadersLinked\Mapper\PlanningObjectiveMapper;
22
 
23
class PlanningGoalController extends AbstractActionController {
24
 
25
    /**
26
     *
27
     * @var \Laminas\Db\Adapter\AdapterInterface
28
     */
29
    private $adapter;
30
 
31
    /**
32
     *
33
     * @var \LeadersLinked\Cache\CacheInterface
34
     */
35
    private $cache;
36
 
37
 
38
    /**
39
     *
40
     * @var \Laminas\Log\LoggerInterface
41
     */
42
    private $logger;
43
 
44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
49
 
50
 
51
    /**
52
     *
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
63
     * @param array $config
64
     * @param \Laminas\Mvc\I18n\Translator $translator
65
     */
66
    public function __construct($adapter, $cache, $logger, $config, $translator)
67
    {
68
        $this->adapter      = $adapter;
69
        $this->cache        = $cache;
70
        $this->logger       = $logger;
71
        $this->config       = $config;
72
        $this->translator   = $translator;
73
    }
74
 
75
    public function indexAction() {
76
 
77
 
78
        $currentUserPlugin = $this->plugin('currentUserPlugin');
79
        $currentUser = $currentUserPlugin->getUser();
80
        $currentCompany = $currentUserPlugin->getCompany();
81
 
82
 
83
        $request = $this->getRequest();
84
        if($request->isGet()) {
85
 
86
 
87
            $headers  = $request->getHeaders();
88
 
89
            $isJson = false;
90
            if($headers->has('Accept')) {
91
                $accept = $headers->get('Accept');
92
 
93
                $prioritized = $accept->getPrioritized();
94
 
95
                foreach($prioritized as $key => $value) {
96
                    $raw = trim($value->getRaw());
97
 
98
                    if(!$isJson) {
99
                        $isJson = strpos($raw, 'json');
100
                    }
101
 
102
                }
103
            }
104
 
105
            if($isJson) {
106
                $link_add = '';
107
 
108
                $period_id = Functions::sanitizeFilterString($this->params()->fromQuery('period_id'));
109
                $objective_id = Functions::sanitizeFilterString($this->params()->fromQuery('objective_id'));
110
 
111
                if(empty($period_id)) {
112
 
113
                    return new JsonModel([
114
                        'success' => true,
115
                        'data' => [
116
                            'items' => [],
117
                            'total' => 0,
118
                            'link_add' => '',
119
                        ]
120
                    ]);
121
 
122
                }
123
 
124
 
125
                $planningPeriodMapper = PlanningPeriodMapper::getInstance($this->adapter);
126
                $planningPeriod = $planningPeriodMapper->fetchOneByUuid($period_id);
127
 
128
                if (!$planningPeriod) {
129
                    $data = [
130
                        'success' => false,
131
                        'data' => 'ERROR_RECORD_NOT_FOUND'
132
                    ];
133
 
134
                    return new JsonModel($data);
135
                }
136
 
137
                if ($planningPeriod->company_id != $currentCompany->id) {
138
                    return new JsonModel([
139
                        'success' => false,
140
                        'data' => 'ERROR_UNAUTHORIZED'
141
                    ]);
142
                }
143
 
144
 
145
                $objectives = [];
146
                $planningObjectiveMapper = PlanningObjectiveMapper::getInstance($this->adapter);
147
 
148
                if(empty($objective_id)) {
149
                    $planningObjective = null;
150
 
151
 
152
                    $records = $planningObjectiveMapper->fetchAllActiveByCompanyIdAndPeriodId($planningPeriod->company_id, $planningPeriod->id);
153
                    foreach($records as $record)
154
                    {
155
                        if(empty($planningObjective)) {
156
                            $planningObjective = $record;
157
                        }
158
 
159
                        $objectives[ $record->uuid ] = $record->title;
160
                    }
161
 
162
                } else {
163
 
164
 
165
                    $planningObjective = $planningObjectiveMapper->fetchOneByUuid($objective_id);
166
 
167
                    if (!$planningObjective) {
168
                        $data = [
169
                            'success' => false,
170
                            'data' => 'ERROR_RECORD_NOT_FOUND'
171
                        ];
172
 
173
                        return new JsonModel($data);
174
                    }
175
 
176
                    if ($planningObjective->period_id != $planningPeriod->id) {
177
                        return new JsonModel([
178
                            'success' => false,
179
                            'data' => 'ERROR_UNAUTHORIZED'
180
                        ]);
181
                    }
182
 
183
                }
184
 
185
                if(empty($planningObjective)) {
186
                    return new JsonModel([
187
                        'success' => true,
188
                        'data' => [
189
                            'items'         => [],
190
                            'total'         => 0,
191
                            'link_add'      => '',
192
                            'objectives'    => $objectives,
193
                        ]
194
                    ]);
195
                }
196
 
197
 
198
 
199
                $link_add = $this->url()->fromRoute('planning/goals/add', ['objective_id' => $planningObjective->uuid ]);
200
 
201
 
202
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
203
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'planning/goals/add');
204
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'planning/goals/edit');
205
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'planning/goals/delete');
206
 
207
                $search = $this->params()->fromQuery('search', []);
208
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
209
 
210
 
211
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
212
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
213
                $order =  $this->params()->fromQuery('order', []);
214
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
215
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
216
 
217
                $fields =  ['title', 'start', 'end', 'progress'];
218
 
219
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
220
 
221
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
222
                    $order_direction = 'ASC';
223
                }
224
 
225
                $planningGoalMapper = PlanningGoalMapper::getInstance($this->adapter);
226
                $paginator = $planningGoalMapper->fetchAllDataTableByCompanyIdAndPeriodId($planningPeriod->company_id, $planningPeriod->id, $planningObjective->id, $search, $page, $records_x_page, $order_field, $order_direction);
227
 
228
 
229
                $items = [];
230
                $records = $paginator->getCurrentItems();
231
 
232
                foreach($records as $record)
233
                {
234
                    $dtStart = \DateTime::createFromFormat('Y-m-d', $record->start);
235
                    $dtEnd = \DateTime::createFromFormat('Y-m-d', $record->end);
236
 
237
                    $item = [
238
                        'title'         => $record->title,
239
                        'description'   => $record->description,
240
                        'status'        => $record->status,
241
                        'start'         => $dtStart->format('d/m/Y'),
242
                        'end'           => $dtEnd->format('d/m/Y'),
243
                        'progress'      => $record->progress ? $record->progress : 0,
244
                        'actions' => [
245
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('planning/goals/edit', ['id' => $record->uuid]) : '',
246
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('planning/goals/delete', ['id' => $record->uuid]) : '',
247
                            //'link_goal' => $allowGoal ? $this->url()->fromRoute('planning/goals/goals', ['goal_id' => $record->uuid]) : '',
248
                            //'link_goal_report' => $allowGoalReport ? $this->url()->fromRoute('planning/goals/report', ['id' => $record->uuid]) : '',
249
                        ]
250
                    ];
251
                    array_push($items, $item);
252
                }
253
 
254
 
255
                return new JsonModel([
256
                    'success' => true,
257
                    'data' => [
258
                        'items'         => $items,
259
                        'total'         => $paginator->getTotalItemCount(),
260
                        'link_add'      => $allowAdd ? $link_add : '',
261
                        'objectives'    => $objectives,
262
 
263
                    ]
264
                ]);
265
 
266
            } else  {
267
                $form = new PlanningGoalForm();
268
                $formFilter = new PlanningGoalHeaderForm($this->adapter, $currentCompany->id);
269
 
270
                $this->layout()->setTemplate('layout/layout-backend');
271
                $viewModel = new ViewModel();
272
                $viewModel->setTemplate('leaders-linked/planning/goals');
273
                $viewModel->setVariables([
274
                    'form' => $form,
275
                    'formFilter' => $formFilter,
276
                ]);
277
                return $viewModel ;
278
            }
279
        } else {
280
            return new JsonModel([
281
                'success' => false,
282
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
283
            ]);
284
        }
285
    }
286
 
287
 
288
 
289
    public function addAction()
290
    {
291
 
292
        $currentUserPlugin = $this->plugin('currentUserPlugin');
293
        $currentUser = $currentUserPlugin->getUser();
294
        $currentCompany = $currentUserPlugin->getCompany();
295
 
296
        $request = $this->getRequest();
297
        if($request->isPost()) {
298
            $objective_id = Functions::sanitizeFilterString($this->params()->fromRoute('objective_id'));
299
 
300
            $planningObjectiveMapper = PlanningObjectiveMapper::getInstance($this->adapter);
301
            $planningObjective = $planningObjectiveMapper->fetchOneByUuid($objective_id);
302
 
303
            if (!$planningObjective) {
304
                $data = [
305
                    'success' => false,
306
                    'data' => 'ERROR_RECORD_NOT_FOUND'
307
                ];
308
 
309
                return new JsonModel($data);
310
            }
311
 
312
            if ($planningObjective->company_id != $currentCompany->id) {
313
                return new JsonModel([
314
                    'success' => false,
315
                    'data' => 'ERROR_UNAUTHORIZED'
316
                ]);
317
            }
318
 
319
 
320
 
321
            $form = new  PlanningGoalForm();
322
            $dataPost = $request->getPost()->toArray();
323
 
324
            if(!empty($dataPost['date_range'])) {
325
                $s = explode('-', $dataPost['date_range']);
326
                $start = trim($s[0]);
327
                $end = trim($s[1]);
328
 
329
                $dtStart = \DateTime::createFromFormat('d/m/Y', $start);
330
                $dtEnd  = \DateTime::createFromFormat('d/m/Y', $end);
331
                if(!$dtStart &&  !$dtEnd) {
332
                    $dataPost['date_range'] = '';
333
                }
334
            }
335
 
336
 
337
 
338
            $form->setData($dataPost);
339
 
340
            if($form->isValid()) {
341
                $dataPost = (array) $form->getData();
342
                $dataPost['status'] =  empty($dataPost['status']) ? PlanningGoal::STATUS_INACTIVE : $dataPost['status'];
343
 
344
 
345
                $hydrator = new ObjectPropertyHydrator();
346
                $planningGoal = new PlanningGoal();
347
                $planningGoal->company_id = $planningObjective->company_id;
348
                $planningGoal->period_id = $planningObjective->period_id;
349
                $planningGoal->objective_id = $planningObjective->id;
350
 
351
                $hydrator->hydrate($dataPost, $planningGoal);
352
 
353
 
354
 
355
                $planningGoalMapper = PlanningGoalMapper::getInstance($this->adapter);
356
                $result = $planningGoalMapper->insert($planningGoal);
357
 
358
                if($result) {
359
                    $this->logger->info('Se agrego el meta la planificación : ' . $planningGoal->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
360
 
361
                    $data = [
362
                        'success'   => true,
363
                        'data'   => 'LABEL_RECORD_ADDED'
364
                    ];
365
                } else {
366
                    $data = [
367
                        'success'   => false,
368
                        'data'      => $planningGoalMapper->getError()
369
                    ];
370
 
371
                }
372
 
373
                return new JsonModel($data);
374
 
375
            } else {
376
                $messages = [];
377
                $form_messages = (array) $form->getMessages();
378
                foreach($form_messages  as $fieldname => $field_messages)
379
                {
380
 
381
                    $messages[$fieldname] = array_values($field_messages);
382
                }
383
 
384
                return new JsonModel([
385
                    'success'   => false,
386
                    'data'   => $messages
387
                ]);
388
            }
389
 
390
        } else {
391
            $data = [
392
                'success' => false,
393
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
394
            ];
395
 
396
            return new JsonModel($data);
397
        }
398
 
399
        return new JsonModel($data);
400
 
401
 
402
    }
403
 
404
 
405
 
406
    public function editAction()
407
    {
408
 
409
        $currentUserPlugin = $this->plugin('currentUserPlugin');
410
        $currentUser = $currentUserPlugin->getUser();
411
        $currentCompany = $currentUserPlugin->getCompany();
412
 
413
 
414
        $request = $this->getRequest();
415
        $uuid = $this->params()->fromRoute('id');
416
 
417
 
418
        if(!$uuid) {
419
            $data = [
420
                'success'   => false,
421
                'data'   => 'ERROR_INVALID_PARAMETER'
422
            ];
423
 
424
            return new JsonModel($data);
425
        }
426
 
427
        $planningGoalMapper = PlanningGoalMapper::getInstance($this->adapter);
428
        $planningGoal = $planningGoalMapper->fetchOneByUuid($uuid);
429
 
430
        if (!$planningGoal) {
431
            $data = [
432
                'success' => false,
433
                'data' => 'ERROR_RECORD_NOT_FOUND'
434
            ];
435
 
436
            return new JsonModel($data);
437
        }
438
 
439
        if ($planningGoal->company_id != $currentCompany->id) {
440
            return new JsonModel([
441
                'success' => false,
442
                'data' => 'ERROR_UNAUTHORIZED'
443
            ]);
444
        }
445
 
446
        if($request->isPost()) {
447
            $form = new  PlanningGoalForm();
448
            $dataPost = $request->getPost()->toArray();
449
            $dataPost['status'] =  empty($dataPost['status']) ? PlanningGoal::STATUS_INACTIVE : $dataPost['status'];
450
            if(empty($dataPost['date_range'])) {
451
                $dataPost['start'] = date('Y-m-d');
452
                $dataPost['end'] = date('Y-m-d');
453
            } else {
454
 
455
                $s = explode('-', $dataPost['date_range']);
456
                $start = trim($s[0]);
457
                $end = trim($s[1]);
458
 
459
                $dtStart = \DateTime::createFromFormat('d/m/Y', $start);
460
                $dtEnd  = \DateTime::createFromFormat('d/m/Y', $end);
461
                if($dtStart && $dtEnd) {
462
                    $dataPost['start'] = $dtStart->format('Y-m-d');
463
                    $dataPost['end'] = $dtEnd->format('Y-m-d');
464
 
465
                } else {
466
                    $dataPost['start'] = date('Y-m-d');
467
                    $dataPost['end'] = date('Y-m-d');
468
                }
469
            }
470
 
471
 
472
            $form->setData($dataPost);
473
 
474
            if($form->isValid()) {
475
                $dataPost = (array) $form->getData();
476
 
477
                $hydrator = new ObjectPropertyHydrator();
478
                $hydrator->hydrate($dataPost, $planningGoal);
479
                $result = $planningGoalMapper->update($planningGoal);
480
 
481
                if($result) {
482
                    $this->logger->info('Se actualizo el meta la planificación : ' . $planningGoal->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
483
 
484
                    $data = [
485
                        'success' => true,
486
                        'data' => 'LABEL_RECORD_UPDATED'
487
                    ];
488
                } else {
489
                    $data = [
490
                        'success'   => false,
491
                        'data'      => $planningGoalMapper->getError()
492
                    ];
493
                }
494
 
495
                return new JsonModel($data);
496
 
497
            } else {
498
                $messages = [];
499
                $form_messages = (array) $form->getMessages();
500
                foreach($form_messages  as $fieldname => $field_messages)
501
                {
502
                    $messages[$fieldname] = array_values($field_messages);
503
                }
504
 
505
                return new JsonModel([
506
                    'success'   => false,
507
                    'data'   => $messages
508
                ]);
509
            }
510
        }else if ($request->isGet()) {
511
 
512
            $data = [
513
                'success' => true,
514
                'data' => [
515
                    'title' => $planningGoal->title,
516
                    'description' => $planningGoal->description,
517
                    'start' => $planningGoal->start,
518
                    'end' => $planningGoal->end,
519
                    'status' => $planningGoal->status,
520
                    'progress' => $planningGoal->progress,
521
                    'budget' => $planningGoal->budget,
522
                    'cost' => $planningGoal->cost,
523
                    'hours' => $planningGoal->hours,
524
                ]
525
            ];
526
 
527
            return new JsonModel($data);
528
        } else {
529
            $data = [
530
                'success' => false,
531
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
532
            ];
533
 
534
            return new JsonModel($data);
535
        }
536
 
537
        return new JsonModel($data);
538
 
539
    }
540
 
541
 
542
 
543
 
544
    public function deleteAction()
545
    {
546
        $currentUserPlugin = $this->plugin('currentUserPlugin');
547
        $currentCompany = $currentUserPlugin->getCompany();
548
        $currentUser = $currentUserPlugin->getUser();
549
 
550
        $request = $this->getRequest();
551
        $uuid = $this->params()->fromRoute('id');
552
 
553
 
554
        if(!$uuid) {
555
            $data = [
556
                'success'   => false,
557
                'data'   => 'ERROR_INVALID_PARAMETER'
558
            ];
559
 
560
            return new JsonModel($data);
561
        }
562
 
563
        $planningGoalMapper = PlanningGoalMapper::getInstance($this->adapter);
564
        $planningGoal = $planningGoalMapper->fetchOneByUuid($uuid);
565
 
566
        if (!$planningGoal) {
567
            $data = [
568
                'success' => false,
569
                'data' => 'ERROR_RECORD_NOT_FOUND'
570
            ];
571
 
572
            return new JsonModel($data);
573
        }
574
 
575
        if ($planningGoal->company_id != $currentCompany->id) {
576
            return new JsonModel([
577
                'success' => false,
578
                'data' => 'ERROR_UNAUTHORIZED'
579
            ]);
580
        }
581
 
582
 
583
        if ($request->isPost()) {
584
 
585
 
586
            $result = $planningGoalMapper->delete($planningGoal);
587
            if ($result) {
588
                $this->logger->info('Se borro el meta de la planificación :' . $planningGoal->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
589
 
590
                $data = [
591
                    'success' => true,
592
                    'data' => 'LABEL_RECORD_DELETED'
593
                ];
594
            } else {
595
 
596
                $data = [
597
                    'success' => false,
598
                    'data' => $planningGoalMapper->getError()
599
                ];
600
 
601
                return new JsonModel($data);
602
            }
603
        } else {
604
            $data = [
605
                'success' => false,
606
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
607
            ];
608
 
609
            return new JsonModel($data);
610
        }
611
 
612
        return new JsonModel($data);
613
    }
614
}