Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | Comparar con el anterior | Ultima modificación | Ver Log |

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