Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7489 nelberth 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Authentication\AuthenticationService;
7
use Laminas\Authentication\Result as AuthResult;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Mvc\I18n\Translator;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Model\HighPerformanceTeamsGroupsViewTopic;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Form\HighPerformanceTeamsGroupsViewTopicForm;
18
use LeadersLinked\Library\Functions;
19
use LeadersLinked\Mapper\UserMapper;
20
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsViewTopicMapper;
7515 nelberth 21
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
7547 nelberth 22
 
7489 nelberth 23
use LeadersLinked\Mapper\CompanyMapper;
24
use LeadersLinked\Mapper\CompanyUserMapper;
25
 
26
 
27
 
28
class HighPerformanceTeamsGroupsViewTopicController extends AbstractActionController
29
{
30
    /**
31
     *
32
     * @var AdapterInterface
33
     */
34
    private $adapter;
35
 
36
 
37
    /**
38
     *
39
     * @var AbstractAdapter
40
     */
41
    private $cache;
42
 
43
    /**
44
     *
45
     * @var  LoggerInterface
46
     */
47
    private $logger;
48
 
49
    /**
50
     *
51
     * @var array
52
     */
53
    private $config;
54
 
55
 
56
 
57
    /**
58
     *
59
     * @param AdapterInterface $adapter
60
     * @param AbstractAdapter $cache
61
     * @param LoggerInterface $logger
62
     * @param array $config
63
     */
64
    public function __construct($adapter, $cache , $logger, $config)
65
    {
66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
 
71
 
72
    }
73
 
74
 
75
 
76
 
77
    public function indexAction()
78
    {
79
 
7544 nelberth 80
 
7489 nelberth 81
        $currentUserPlugin = $this->plugin('currentUserPlugin');
82
        $currentUser = $currentUserPlugin->getUser();
83
        $currentCompany = $currentUserPlugin->getCompany();
84
 
85
 
86
        $request = $this->getRequest();
87
        if($request->isGet()) {
88
 
89
 
90
            $headers  = $request->getHeaders();
91
 
92
            $isJson = false;
93
            if($headers->has('Accept')) {
94
                $accept = $headers->get('Accept');
95
 
96
                $prioritized = $accept->getPrioritized();
97
 
98
                foreach($prioritized as $key => $value) {
99
                    $raw = trim($value->getRaw());
100
 
101
                    if(!$isJson) {
102
                        $isJson = strpos($raw, 'json');
103
                    }
104
 
105
                }
106
            }
107
 
108
            if($isJson) {
109
 
7544 nelberth 110
                $uuid = $this->params()->fromRoute('id');
7547 nelberth 111
                if(!$uuid) {
112
                    $data = [
113
                        'success'   => false,
114
                        'data'   => 'ERROR_INVALID_PARAMETER'
115
                    ];
116
 
117
                    return new JsonModel($data);
118
                }
7549 nelberth 119
 
7547 nelberth 120
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
121
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($uuid);
122
 
123
                if (!$highPerformanceTeamsGroups) {
124
                    $data = [
125
                        'success' => false,
126
                        'data' => 'ERROR_RECORD_NOT_FOUND'
127
                    ];
128
 
129
                    return new JsonModel($data);
130
                }
7551 nelberth 131
 
7489 nelberth 132
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
7560 nelberth 133
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/topic/edit');
134
                $allowDelete = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/topic/delete');
7562 nelberth 135
                $allowView = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view');
7489 nelberth 136
 
7552 nelberth 137
 
7489 nelberth 138
                $search = $this->params()->fromQuery('search', []);
139
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
140
 
141
 
142
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
143
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
144
                $order =  $this->params()->fromQuery('order', []);
145
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
146
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
147
 
148
                $fields =  ['title', 'date'];
149
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
150
 
151
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
152
                    $order_direction = 'ASC';
153
                }
7553 nelberth 154
 
155
                $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
156
                $paginator = $highPerformanceTeamsGroupsViewTopicMapper->fetchAllByGroup($highPerformanceTeamsGroups->id);
7554 nelberth 157
 
158
                $items = [];
159
 
160
 
7558 nelberth 161
 
7555 nelberth 162
                foreach($paginator as $record)
7489 nelberth 163
                {
164
 
7569 nelberth 165
 
7489 nelberth 166
                    $item = [
167
                        'title' => $record->title,
168
                        'status'=> $record->status,
7566 nelberth 169
                        'actions' => [
7572 nelberth 170
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/view/topic/edit', ['id' => $highPerformanceTeamsGroups->uuid, 'id' => $record->uuid]) : '',
7566 nelberth 171
                            /*'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/view/topic/delete', ['id' => $highPerformanceTeamsGroups->uuid],['id' => $record->uuid]) : '',
7565 nelberth 172
                            'link_view' => $allowView ? $this->url()->fromRoute('high-performance-teams/groups/view', ['id' => $highPerformanceTeamsGroups->uuid]) : '',*/
7489 nelberth 173
                        ]
174
 
175
                    ];
176
 
177
                    array_push($items, $item);
178
 
179
                }
180
 
181
                return new JsonModel([
182
                    'success' => true,
183
                    'data' => [
7561 nelberth 184
                        'items' => $items
7489 nelberth 185
                    ]
186
                ]);
187
 
7500 nelberth 188
            }
7489 nelberth 189
        } else {
190
            return new JsonModel([
191
                'success' => false,
192
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
193
            ]);
194
        }
195
    }
196
 
197
    public function addAction()
198
    {
199
 
7514 nelberth 200
        $currentUserPlugin = $this->plugin('currentUserPlugin');
201
        $currentUser = $currentUserPlugin->getUser();
202
        $currentCompany = $currentUserPlugin->getCompany();
7499 nelberth 203
 
7489 nelberth 204
        $request = $this->getRequest();
205
        if($request->isPost()) {
206
            $form = new  HighPerformanceTeamsGroupsViewTopicForm($this->adapter);
207
            $dataPost = $request->getPost()->toArray();
208
 
209
            $form->setData($dataPost);
210
 
211
            if($form->isValid()) {
212
                $dataPost = (array) $form->getData();
213
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroupsViewTopic::STATUS_INACTIVE;
7514 nelberth 214
 
7489 nelberth 215
 
7514 nelberth 216
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
217
                $group = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($dataPost['group_id']);
218
 
219
                if (!$group) {
220
                    $data = [
221
                        'success' => false,
222
                        'data' => 'ERROR_RECORD_NOT_FOUND'
223
                    ];
224
 
225
                    return new JsonModel($data);
226
                }
227
 
228
                if ($group->company_id != $currentCompany->id) {
229
                    return new JsonModel([
230
                        'success' => false,
231
                        'data' => 'ERROR_UNAUTHORIZED'
232
                    ]);
233
                }
234
 
235
                $dataPost['group_id']=$group->id;
7515 nelberth 236
 
7489 nelberth 237
                $hydrator = new ObjectPropertyHydrator();
238
                $highPerformanceTeamsGroupsViewTopic = new HighPerformanceTeamsGroupsViewTopic();
239
                $hydrator->hydrate($dataPost, $highPerformanceTeamsGroupsViewTopic);
240
 
241
                $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
242
                $result = $highPerformanceTeamsGroupsViewTopicMapper->insert($highPerformanceTeamsGroupsViewTopic);
243
 
244
                if($result) {
245
 
7512 nelberth 246
                    $this->logger->info('Se agrego el topic del grupo ' . $highPerformanceTeamsGroupsViewTopic->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
7489 nelberth 247
 
248
                    $data = [
249
                        'success'   => true,
250
                        'data'   => 'LABEL_RECORD_ADDED'
251
                    ];
252
                } else {
253
                    $data = [
254
                        'success'   => false,
255
                        'data'      => $highPerformanceTeamsGroupsViewTopicMapper->getError()
256
                    ];
257
 
258
                }
259
 
260
                return new JsonModel($data);
261
 
262
            } else {
263
                $messages = [];
264
                $form_messages = (array) $form->getMessages();
265
                foreach($form_messages  as $fieldname => $field_messages)
266
                {
267
 
268
                    $messages[$fieldname] = array_values($field_messages);
269
                }
270
 
271
                return new JsonModel([
272
                    'success'   => false,
273
                    'data'   => $messages
274
                ]);
275
            }
276
 
277
        } else {
278
            $data = [
279
                'success' => false,
7502 nelberth 280
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
7489 nelberth 281
            ];
282
 
283
            return new JsonModel($data);
284
        }
285
 
286
        return new JsonModel($data);
287
 
288
 
289
    }
290
 
291
 
292
 
293
     public function editAction(){
294
 
295
        $currentUserPlugin = $this->plugin('currentUserPlugin');
296
        $currentUser = $currentUserPlugin->getUser();
297
        $currentCompany = $currentUserPlugin->getCompany();
298
        $request = $this->getRequest();
299
        $uuid = $this->params()->fromRoute('id');
300
 
301
 
302
        if(!$uuid) {
303
            $data = [
304
                'success'   => false,
305
                'data'   => 'ERROR_INVALID_PARAMETER'
306
            ];
307
 
308
            return new JsonModel($data);
309
        }
310
 
311
        $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
312
        $group = $highPerformanceTeamsGroupsViewTopicMapper->fetchOneByUuid($uuid);
313
 
314
        if (!$group) {
315
            $data = [
316
                'success' => false,
317
                'data' => 'ERROR_RECORD_NOT_FOUND'
318
            ];
319
 
320
            return new JsonModel($data);
321
        }
322
 
323
        if ($group->company_id != $currentCompany->id) {
324
            return new JsonModel([
325
                'success' => false,
326
                'data' => 'ERROR_UNAUTHORIZED'
327
            ]);
328
        }
329
 
330
        if($request->isPost()) {
331
            $form = new  HighPerformanceTeamsGroupsViewTopicForm($this->adapter);
332
            $dataPost = $request->getPost()->toArray();
333
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroupsViewTopic::STATUS_INACTIVE;
334
            $form->setData($dataPost);
335
 
336
            if($form->isValid()) {
337
                $dataPost = (array) $form->getData();
338
 
339
                $hydrator = new ObjectPropertyHydrator();
340
                $hydrator->hydrate($dataPost, $group);
341
                $result = $highPerformanceTeamsGroupsViewTopicMapper->update($group);
342
 
343
                if($result) {
344
                    $this->logger->info('Se actualizo el grupo de alto rendimiento ' . $group->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
345
 
346
                    $data = [
347
                        'success' => true,
348
                        'data' => 'LABEL_RECORD_UPDATED'
349
                    ];
350
                } else {
351
                    $data = [
352
                        'success'   => false,
353
                        'data'      => $highPerformanceTeamsGroupsViewTopicMapper->getError()
354
                    ];
355
                }
356
 
357
                return new JsonModel($data);
358
 
359
            } else {
360
                $messages = [];
361
                $form_messages = (array) $form->getMessages();
362
                foreach($form_messages  as $fieldname => $field_messages)
363
                {
364
                    $messages[$fieldname] = array_values($field_messages);
365
                }
366
 
367
                return new JsonModel([
368
                    'success'   => false,
369
                    'data'   => $messages
370
                ]);
371
            }
372
        }else if ($request->isGet()) {
373
            $hydrator = new ObjectPropertyHydrator();
374
 
375
            $data = [
376
                'success' => true,
377
                'data' => $hydrator->extract($group)
378
            ];
379
 
380
            return new JsonModel($data);
381
        } else {
382
            $data = [
383
                'success' => false,
384
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
385
            ];
386
 
387
            return new JsonModel($data);
388
        }
389
 
390
        return new JsonModel($data);
391
 
392
    }
393
 
394
 
395
 
396
 
397
    public function deleteAction(){
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
 
406
        if (!$uuid) {
407
            $data = [
408
                'success' => false,
409
                'data' => 'ERROR_INVALID_PARAMETER'
410
            ];
411
 
412
            return new JsonModel($data);
413
        }
414
 
415
 
416
 
417
        $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
418
        $group = $highPerformanceTeamsGroupsViewTopicMapper->fetchOneByUuid($uuid);
419
 
420
        if (!$group) {
421
            $data = [
422
                'success' => false,
423
                'data' => 'ERROR_RECORD_NOT_FOUND'
424
            ];
425
 
426
            return new JsonModel($data);
427
        }
428
 
429
        if ($group->company_id != $currentCompany->id) {
430
            return new JsonModel([
431
                'success' => false,
432
                'data' => 'ERROR_UNAUTHORIZED'
433
            ]);
434
        }
435
 
436
        if ($request->isPost()) {
437
 
438
 
439
            $result = $highPerformanceTeamsGroupsViewTopicMapper->delete($group->id);
440
            if ($result) {
441
                $this->logger->info('Se borro el grupo de alto rendimiento  ' . $group->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
442
 
443
                $data = [
444
                    'success' => true,
445
                    'data' => 'LABEL_RECORD_DELETED'
446
                ];
447
            } else {
448
 
449
                $data = [
450
                    'success' => false,
451
                    'data' => $highPerformanceTeamsGroupsViewTopicMapper->getError()
452
                ];
453
 
454
                return new JsonModel($data);
455
            }
456
        } else {
457
            $data = [
458
                'success' => false,
459
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
460
            ];
461
 
462
            return new JsonModel($data);
463
        }
464
 
465
        return new JsonModel($data);
466
    }
467
 
468
 
469
 
470
 
471
}