Proyectos de Subversion LeadersLinked - Backend

Rev

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