Proyectos de Subversion LeadersLinked - Backend

Rev

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