Proyectos de Subversion LeadersLinked - Backend

Rev

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