Proyectos de Subversion LeadersLinked - Backend

Rev

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