Proyectos de Subversion LeadersLinked - Backend

Rev

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

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