Proyectos de Subversion LeadersLinked - Backend

Rev

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