Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16701 | Rev 16768 | 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;
16766 efrain 8
use LeadersLinked\Cache\CacheInterface;
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
     *
26
     * @var AdapterInterface
27
     */
28
    private $adapter;
29
 
30
    /**
31
     *
16766 efrain 32
     * @var CacheInterface
15451 efrain 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
16766 efrain 51
     *@param CacheInterface $cache
15451 efrain 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', []);
16766 efrain 92
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
15451 efrain 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);
16766 efrain 101
                $order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
15451 efrain 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
 
16325 efrain 131
                        case MyCoachCategory::PRIVACY_PUBLIC :
15451 efrain 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
                        'status' => $record->status,
152
                        'privacy' => $privacy,
153
                        'actions' => [
154
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('my-coach/categories/edit', ['id' => $record->uuid]) : '',
155
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('my-coach/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;
15831 efrain 172
                $form = new  MyCoachCategoryForm($allowPrivacyPublic);
15451 efrain 173
 
174
 
175
                $this->layout()->setTemplate('layout/layout-backend');
176
                $viewModel = new ViewModel();
177
                $viewModel->setTemplate('leaders-linked/my-coach-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()
16701 efrain 191
    {
192
 
193
        $currentNetworkPlugin  = $this->plugin('currentNetworkPlugin');
194
        $currentNetwork        = $currentNetworkPlugin->getNetwork();
195
 
196
 
15451 efrain 197
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
198
        $currentCompany     = $currentUserPlugin->getCompany();
199
        $currentUser        = $currentUserPlugin->getUser();
200
        $request            = $this->getRequest();
201
 
202
        if($request->isPost()) {
203
            $dataPost = $request->getPost()->toArray();
204
 
205
            $allowPrivacyPublic = $currentCompany->default_for_network == Company::DEFAULT_FOR_NETWORK_YES;
15831 efrain 206
            $form = new  MyCoachCategoryForm($allowPrivacyPublic);
15451 efrain 207
            $form->setData($dataPost);
208
 
209
            if($form->isValid()) {
210
 
211
 
212
                $category =  new MyCoachCategory();
213
 
214
                $dataPost = (array) $form->getData();
215
                $hydrator = new ObjectPropertyHydrator();
216
                $hydrator->hydrate($dataPost, $category);
217
 
218
                $category->company_id = $currentCompany->id;
16701 efrain 219
                $category->network_id = $currentNetwork->id;
15451 efrain 220
 
221
                $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
222
 
223
 
224
                if($myCoachCategoryMapper->insert($category)) {
225
 
226
                    $this->logger->info('Se agrego la categoria ' . $category->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
227
 
228
                    $data = [
229
                        'success'   => true,
230
                        'data'   => 'LABEL_RECORD_ADDED'
231
                    ];
232
                } else {
233
                    $data = [
234
                        'success'   => false,
235
                        'data'      => $myCoachCategoryMapper->getError()
236
                    ];
237
 
238
                }
239
 
240
                return new JsonModel($data);
241
 
242
            } else {
243
                $messages = [];
244
                $form_messages = (array) $form->getMessages();
245
                foreach ($form_messages as $fieldname => $field_messages) {
246
 
247
                    $messages[$fieldname] = array_values($field_messages);
248
                }
249
 
250
                return new JsonModel([
251
                    'success' => false,
252
                    'data' => $messages
253
                ]);
254
 
255
            }
256
 
257
        } else {
258
            $data = [
259
                'success' => false,
260
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
261
            ];
262
 
263
            return new JsonModel($data);
264
        }
265
 
266
        return new JsonModel($data);
267
 
268
 
269
    }
270
 
271
    public function editAction()
272
    {
273
        $request = $this->getRequest();
274
        $currentUserPlugin = $this->plugin('currentUserPlugin');
275
        $currentCompany = $currentUserPlugin->getCompany();
276
        $currentUser = $currentUserPlugin->getUser();
277
 
278
        $request = $this->getRequest();
279
        $uuid = $this->params()->fromRoute('id');
280
 
281
        if (!$uuid) {
282
            $data = [
283
                'success' => false,
284
                'data' => 'ERROR_INVALID_PARAMETER'
285
            ];
286
 
287
            return new JsonModel($data);
288
        }
289
 
290
        $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
291
        $category = $myCoachCategoryMapper->fetchOneByUuid($uuid);
292
 
293
        if (!$category) {
294
            $data = [
295
                'success' => false,
296
                'data' => 'ERROR_RECORD_NOT_FOUND'
297
            ];
298
 
299
            return new JsonModel($data);
300
        }
301
 
302
        if($category->company_id != $currentCompany->id) {
303
            $response = [
304
                'success' => false,
305
                'data' =>  'ERROR_UNAUTHORIZED'
306
            ];
307
 
308
            return new JsonModel($response);
309
        }
310
 
311
 
312
        if ($request->isPost()) {
313
            $dataPost = $request->getPost()->toArray();
314
 
315
            $allowPrivacyPublic = $currentCompany->default_for_network == Company::DEFAULT_FOR_NETWORK_YES;
15831 efrain 316
            $form = new  MyCoachCategoryForm($allowPrivacyPublic);
15451 efrain 317
            $form->setData($dataPost);
318
 
319
 
320
 
321
            if ($form->isValid()) {
322
                $dataPost = (array) $form->getData();
323
 
324
                $hydrator = new ObjectPropertyHydrator();
325
                $hydrator->hydrate($dataPost, $category);
326
 
327
                if($myCoachCategoryMapper->update($category)) {
328
 
329
 
330
                   $this->logger->info('Se actualizo la categoria ' . $category->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
331
 
332
                   $data = [
333
                       'success'   => true,
334
                       'data'   => 'LABEL_RECORD_UPDATED'
335
                   ];
336
               } else {
337
                   $data = [
338
                       'success'   => false,
339
                       'data'      => $myCoachCategoryMapper->getError()
340
                   ];
341
 
342
               }
343
 
344
                return new JsonModel($data);
345
            } else {
346
                $messages = [];
347
                $form_messages = (array) $form->getMessages();
348
                foreach ($form_messages as $fieldname => $field_messages) {
349
                    $messages[$fieldname] = array_values($field_messages);
350
                }
351
 
352
                return new JsonModel([
353
                    'success' => false,
354
                    'data' => $messages
355
                ]);
356
            }
357
        } else if ($request->isGet()) {
358
 
359
            $hydrator = new ObjectPropertyHydrator();
360
 
361
            $data = [
362
                'success' => true,
363
                'data' => [
364
                    'name' => $category->name,
365
                    'status' => $category->status,
366
                    'privacy' => $category->privacy,
367
                ]
368
            ];
369
 
370
            return new JsonModel($data);
371
        }
372
 
373
 
374
        $data = [
375
            'success' => false,
376
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
377
        ];
378
 
379
        return new JsonModel($data);
380
 
381
    }
382
 
383
    public function deleteAction()
384
    {
385
        $request = $this->getRequest();
386
        $currentUserPlugin = $this->plugin('currentUserPlugin');
387
        $currentCompany = $currentUserPlugin->getCompany();
388
        $currentUser = $currentUserPlugin->getUser();
389
 
390
        $request = $this->getRequest();
391
        $uuid = $this->params()->fromRoute('id');
392
 
393
        $myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
394
        $category = $myCoachCategoryMapper->fetchOneByUuid($uuid);
395
 
396
        if (!$category) {
397
            $data = [
398
                'success' => false,
399
                'data' => 'ERROR_RECORD_NOT_FOUND'
400
            ];
401
 
402
            return new JsonModel($data);
403
        }
404
 
405
        if($category->company_id != $currentCompany->id) {
406
            $response = [
407
                'success' => false,
408
                'data' =>  'ERROR_UNAUTHORIZED'
409
            ];
410
 
411
            return new JsonModel($response);
412
        }
413
 
414
 
415
        if ($request->isPost()) {
416
 
417
            if ($myCoachCategoryMapper->delete($category->id)) {
418
                $this->logger->info('Se borro la categoria ' . $category->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
419
 
420
                $data = [
421
                    'success' => true,
422
                    'data' => 'LABEL_RECORD_DELETED'
423
                ];
424
            } else {
425
 
426
                $data = [
427
                    'success' => false,
428
                    'data' => $myCoachCategoryMapper->getError()
429
                ];
430
 
431
 
432
            }
433
        } else {
434
            $data = [
435
                'success' => false,
436
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
437
            ];
438
 
439
 
440
        }
441
 
442
 
443
        return new JsonModel($data);
444
 
445
    }
446
}