Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1664 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;
1992 nelberth 15
use LeadersLinked\Model\PlanningObjectivesAndGoals;
1664 nelberth 16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1815 nelberth 17
use LeadersLinked\Form\PlanningObjectivesAndGoalsForm;
1664 nelberth 18
 
2002 nelberth 19
use LeadersLinked\Library\Functions;
20
 
1740 nelberth 21
use LeadersLinked\Mapper\PlanningObjectivesAndGoalsMapper;
22
 
1664 nelberth 23
class PlanningObjectivesAndGoalsController extends AbstractActionController
24
{
25
    /**
26
     *
27
     * @var AdapterInterface
28
     */
29
    private $adapter;
30
 
31
 
32
    /**
33
     *
34
     * @var AbstractAdapter
35
     */
36
    private $cache;
37
 
38
    /**
39
     *
40
     * @var  LoggerInterface
41
     */
42
    private $logger;
43
 
44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
49
 
50
 
2076 nelberth 51
 
1664 nelberth 52
    /**
53
     *
54
     * @param AdapterInterface $adapter
55
     * @param AbstractAdapter $cache
56
     * @param LoggerInterface $logger
57
     * @param array $config
58
     */
59
    public function __construct($adapter, $cache , $logger, $config)
60
    {
61
        $this->adapter      = $adapter;
62
        $this->cache        = $cache;
63
        $this->logger       = $logger;
64
        $this->config       = $config;
65
 
66
 
67
    }
68
 
1740 nelberth 69
 
70
 
71
 
1664 nelberth 72
    public function indexAction()
73
    {
74
 
75
        $request = $this->getRequest();
76
        if($request->isGet()) {
77
 
78
 
1740 nelberth 79
            $headers  = $request->getHeaders();
1664 nelberth 80
 
1740 nelberth 81
            $isJson = false;
82
            if($headers->has('Accept')) {
83
                $accept = $headers->get('Accept');
84
 
85
                $prioritized = $accept->getPrioritized();
86
 
87
                foreach($prioritized as $key => $value) {
88
                    $raw = trim($value->getRaw());
89
 
90
                    if(!$isJson) {
91
                        $isJson = strpos($raw, 'json');
92
                    }
93
 
94
                }
95
            }
96
 
97
            if($isJson) {
98
 
99
 
100
                $search = $this->params()->fromQuery('search', []);
101
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
102
 
103
                $page               = intval($this->params()->fromQuery('start', 1), 10);
104
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
105
                $order =  $this->params()->fromQuery('order', []);
106
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
107
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
108
 
109
                $fields =  ['title', 'date'];
110
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
111
 
112
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
113
                    $order_direction = 'ASC';
114
                }
2076 nelberth 115
                $currentUserPlugin = $this->plugin('currentUserPlugin');
2078 nelberth 116
                $currentCompany = $currentUserPlugin->getCompany()->id;
2012 nelberth 117
 
1740 nelberth 118
                $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
2078 nelberth 119
                $paginator = $PlanningObjectivesAndGoalsMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $currentCompany);
2053 nelberth 120
 
121
                $items = [];
2055 nelberth 122
 
2054 nelberth 123
                $records = $paginator->getCurrentItems();
124
 
1740 nelberth 125
                foreach($records as $record)
126
                {
127
 
128
                    $item = [
129
                        'title' => $record->title,
1782 nelberth 130
                        'description' => $record->description,
1831 nelberth 131
                        'progress'=> '90%',
2115 nelberth 132
                        'date'=>$record->date,
2064 nelberth 133
                        'actions' => [
134
                            'link_edit' => $this->url()->fromRoute('planning-objectives-and-goals/edit', ['id' => $record->uuid]),
2102 nelberth 135
                            'link_delete' => $this->url()->fromRoute('planning-objectives-and-goals/delete', ['id' => $record->uuid]),
136
                            'code'=> $record->uuid,
2064 nelberth 137
                        ]
1782 nelberth 138
 
1740 nelberth 139
                    ];
140
 
141
                    array_push($items, $item);
142
                }
143
 
144
                return new JsonModel([
145
                    'success' => true,
146
                    'data' => [
147
                        'items' => $items,
148
                        'total' => $paginator->getTotalItemCount(),
149
                    ]
150
                ]);
1744 nelberth 151
            } else  {
1815 nelberth 152
                $formAdd = new PlanningObjectivesAndGoalsForm();
1752 nelberth 153
                $this->layout()->setTemplate('layout/layout-backend');
1744 nelberth 154
                $viewModel = new ViewModel();
155
                $viewModel->setTemplate('leaders-linked/planning-objectives-and-goals/index.phtml');
1818 nelberth 156
                $viewModel->setVariables([
157
                    'formAdd' => $formAdd,
158
                ]);
1744 nelberth 159
                return $viewModel ;
160
            }
1664 nelberth 161
        } else {
162
            return new JsonModel([
163
                'success' => false,
164
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1954 nelberth 165
            ]);
1664 nelberth 166
        }
167
    }
1925 nelberth 168
    public function addAction()
1995 nelberth 169
    {
2076 nelberth 170
      $currentUserPlugin = $this->plugin('currentUserPlugin');
171
        $currentUser = $currentUserPlugin->getUser();
172
        $currentCompany = $currentUserPlugin->getCompany()->id;
2008 nelberth 173
 
1943 nelberth 174
        $request = $this->getRequest();
175
        if($request->isPost()) {
176
            $form = new  PlanningObjectivesAndGoalsForm();
177
            $dataPost = $request->getPost()->toArray();
178
 
179
            $form->setData($dataPost);
180
 
181
            if($form->isValid()) {
182
                $dataPost = (array) $form->getData();
1986 nelberth 183
                $dataPost['status']='a';
2076 nelberth 184
                $dataPost['id_company']=$currentCompany;
2011 nelberth 185
 
1943 nelberth 186
                $hydrator = new ObjectPropertyHydrator();
187
                $PlanningObjectivesAndGoals = new PlanningObjectivesAndGoals();
188
                $hydrator->hydrate($dataPost, $PlanningObjectivesAndGoals);
189
 
190
                $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
191
                $result = $PlanningObjectivesAndGoalsMapper->insert($PlanningObjectivesAndGoals);
192
 
193
                if($result) {
2076 nelberth 194
                    $this->logger->info('Se agrego el objetivo ' . $PlanningObjectivesAndGoals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1943 nelberth 195
 
196
                    $data = [
197
                        'success'   => true,
198
                        'data'   => 'LABEL_RECORD_ADDED'
199
                    ];
200
                } else {
201
                    $data = [
202
                        'success'   => false,
2005 nelberth 203
                        'data'      => $PlanningObjectivesAndGoalsMapper->getError()
1943 nelberth 204
                    ];
205
 
206
                }
207
 
208
                return new JsonModel($data);
209
 
210
            } else {
211
                $messages = [];
212
                $form_messages = (array) $form->getMessages();
213
                foreach($form_messages  as $fieldname => $field_messages)
214
                {
215
 
216
                    $messages[$fieldname] = array_values($field_messages);
217
                }
218
 
219
                return new JsonModel([
220
                    'success'   => false,
221
                    'data'   => $messages
222
                ]);
223
            }
224
 
225
        } else {
226
            $data = [
227
                'success' => false,
228
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
229
            ];
230
 
231
            return new JsonModel($data);
232
        }
1959 nelberth 233
 
1943 nelberth 234
        return new JsonModel($data);
235
 
2003 nelberth 236
 
1920 nelberth 237
    }
2088 nelberth 238
 
239
 
240
 
1922 nelberth 241
     public function editAction(){
2088 nelberth 242
 
243
        $currentUserPlugin = $this->plugin('currentUserPlugin');
244
        $currentUser = $currentUserPlugin->getUser();
245
        $currentCompany = $currentUserPlugin->getCompany()->id;
246
        $request = $this->getRequest();
247
        $uuid = $this->params()->fromRoute('id');
248
 
1920 nelberth 249
 
2088 nelberth 250
        if(!$uuid) {
251
            $data = [
252
                'success'   => false,
253
                'data'   => 'ERROR_INVALID_PARAMETER'
254
            ];
255
 
256
            return new JsonModel($data);
257
        }
2078 nelberth 258
 
2088 nelberth 259
        $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
260
        $objectivesMapper = $PlanningObjectivesAndGoalsMapper->fetchOneByUuid($uuid);
261
 
262
        if (!$objectivesMapper) {
263
            $data = [
264
                'success' => false,
265
                'data' => 'ERROR_RECORD_NOT_FOUND'
266
            ];
267
 
268
            return new JsonModel($data);
269
        }
270
 
271
        if ($objectivesMapper->id_company != $currentCompany) {
272
            return new JsonModel([
273
                'success' => false,
274
                'data' => 'ERROR_UNAUTHORIZED'
275
            ]);
276
        }
277
 
278
        if($request->isPost()) {
279
            $form = new  PlanningObjectivesAndGoalsForm();
280
            $dataPost = $request->getPost()->toArray();
281
 
282
            $form->setData($dataPost);
283
 
284
            if($form->isValid()) {
285
                $dataPost = (array) $form->getData();
286
 
287
                $hydrator = new ObjectPropertyHydrator();
288
                $hydrator->hydrate($dataPost, $objectivesMapper);
289
                $result = $PlanningObjectivesAndGoalsMapper->update($objectivesMapper);
290
 
291
                if($result) {
292
                    $this->logger->info('Se actualizo el objetivo ' . $objectivesMapper->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
293
 
294
                    $data = [
295
                        'success' => true,
296
                        'data' => 'LABEL_RECORD_UPDATED'
297
                    ];
298
                } else {
299
                    $data = [
300
                        'success'   => false,
301
                        'data'      => $PlanningObjectivesAndGoalsMapper->getError()
302
                    ];
303
                }
304
 
305
                return new JsonModel($data);
306
 
307
            } else {
308
                $messages = [];
309
                $form_messages = (array) $form->getMessages();
310
                foreach($form_messages  as $fieldname => $field_messages)
311
                {
312
                    $messages[$fieldname] = array_values($field_messages);
313
                }
314
 
315
                return new JsonModel([
316
                    'success'   => false,
317
                    'data'   => $messages
318
                ]);
319
            }
320
        } else {
321
            $data = [
322
                'success' => false,
323
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
324
            ];
325
 
326
            return new JsonModel($data);
327
        }
2081 nelberth 328
 
2088 nelberth 329
        return new JsonModel($data);
2080 nelberth 330
 
2088 nelberth 331
 
332
 
333
    }
334
 
335
 
336
 
337
 
338
    public function deleteAction(){
2076 nelberth 339
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2078 nelberth 340
        $currentCompany = $currentUserPlugin->getCompany()->id;
2076 nelberth 341
        $currentUser = $currentUserPlugin->getUser();
2072 nelberth 342
 
343
        $request = $this->getRequest();
344
        $uuid = $this->params()->fromRoute('id');
2082 nelberth 345
 
2072 nelberth 346
 
347
        if (!$uuid) {
348
            $data = [
349
                'success' => false,
350
                'data' => 'ERROR_INVALID_PARAMETER'
351
            ];
352
 
353
            return new JsonModel($data);
354
        }
355
 
2083 nelberth 356
 
357
 
358
        $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
359
        $objectivesMapper = $PlanningObjectivesAndGoalsMapper->fetchOneByUuid($uuid);
2084 nelberth 360
 
2078 nelberth 361
        if (!$objectivesMapper) {
2072 nelberth 362
            $data = [
363
                'success' => false,
364
                'data' => 'ERROR_RECORD_NOT_FOUND'
365
            ];
366
 
367
            return new JsonModel($data);
368
        }
2087 nelberth 369
 
370
        if ($objectivesMapper->id_company != $currentCompany) {
2072 nelberth 371
            return new JsonModel([
372
                'success' => false,
373
                'data' => 'ERROR_UNAUTHORIZED'
374
            ]);
2084 nelberth 375
        }
2086 nelberth 376
 
2072 nelberth 377
        if ($request->isPost()) {
378
 
379
 
2078 nelberth 380
            $result = $PlanningObjectivesAndGoalsMapper->delete($objectivesMapper->id);
2072 nelberth 381
            if ($result) {
2084 nelberth 382
                $this->logger->info('Se borro el objetivo con el titulo ' . $objectivesMapper->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
2072 nelberth 383
 
384
                $data = [
385
                    'success' => true,
386
                    'data' => 'LABEL_RECORD_DELETED'
387
                ];
388
            } else {
389
 
390
                $data = [
391
                    'success' => false,
2078 nelberth 392
                    'data' => $PlanningObjectivesAndGoalsMapper->getError()
2072 nelberth 393
                ];
394
 
395
                return new JsonModel($data);
396
            }
397
        } else {
398
            $data = [
399
                'success' => false,
400
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
401
            ];
402
 
403
            return new JsonModel($data);
404
        }
405
 
406
        return new JsonModel($data);
1920 nelberth 407
    }
1740 nelberth 408
 
1920 nelberth 409
 
1740 nelberth 410
 
1664 nelberth 411
}