Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15451 | Rev 16325 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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