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;
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
 
80
 
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
 
110
 
111
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
112
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/edit');
113
                $allowDelete = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/delete');
114
                $allowView = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view');
115
 
116
 
117
                $search = $this->params()->fromQuery('search', []);
118
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
119
 
120
 
121
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
122
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
123
                $order =  $this->params()->fromQuery('order', []);
124
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
125
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
126
 
127
                $fields =  ['title', 'date'];
128
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
129
 
130
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
131
                    $order_direction = 'ASC';
132
                }
133
 
134
                $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
135
                $paginator = $highPerformanceTeamsGroupsViewTopicMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $currentCompany->id);
136
 
137
                $items = [];
138
 
139
                $records = $paginator->getCurrentItems();
140
 
141
 
142
                foreach($records as $record)
143
                {
144
 
145
 
146
 
147
                    $item = [
148
                        'title' => $record->title,
149
                        'description' => $record->description,
150
                        'status'=> $record->status,
151
                        'actions' => [
152
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/edit', ['id' => $record->uuid]) : '',
153
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/delete', ['id' => $record->uuid]) : '',
154
                            'link_view' => $allowView ? $this->url()->fromRoute('high-performance-teams/groups/view', ['id' => $record->uuid]) : '',
155
                        ]
156
 
157
                    ];
158
 
159
                    array_push($items, $item);
160
 
161
                }
162
 
163
                return new JsonModel([
164
                    'success' => true,
165
                    'data' => [
166
                        'items' => $items,
167
                        'total' => $paginator->getTotalItemCount(),
168
                    ]
169
                ]);
170
 
7500 nelberth 171
            }
7489 nelberth 172
        } else {
173
            return new JsonModel([
174
                'success' => false,
175
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
176
            ]);
177
        }
178
    }
179
 
180
    public function addAction()
181
    {
182
 
7514 nelberth 183
        $currentUserPlugin = $this->plugin('currentUserPlugin');
184
        $currentUser = $currentUserPlugin->getUser();
185
        $currentCompany = $currentUserPlugin->getCompany();
7499 nelberth 186
 
7489 nelberth 187
        $request = $this->getRequest();
188
        if($request->isPost()) {
189
            $form = new  HighPerformanceTeamsGroupsViewTopicForm($this->adapter);
190
            $dataPost = $request->getPost()->toArray();
191
 
192
            $form->setData($dataPost);
193
 
194
            if($form->isValid()) {
195
                $dataPost = (array) $form->getData();
196
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroupsViewTopic::STATUS_INACTIVE;
7514 nelberth 197
 
7489 nelberth 198
 
7514 nelberth 199
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
200
                $group = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($dataPost['group_id']);
201
 
202
                if (!$group) {
203
                    $data = [
204
                        'success' => false,
205
                        'data' => 'ERROR_RECORD_NOT_FOUND'
206
                    ];
207
 
208
                    return new JsonModel($data);
209
                }
210
 
211
                if ($group->company_id != $currentCompany->id) {
212
                    return new JsonModel([
213
                        'success' => false,
214
                        'data' => 'ERROR_UNAUTHORIZED'
215
                    ]);
216
                }
217
 
218
                $dataPost['group_id']=$group->id;
7515 nelberth 219
 
7489 nelberth 220
                $hydrator = new ObjectPropertyHydrator();
221
                $highPerformanceTeamsGroupsViewTopic = new HighPerformanceTeamsGroupsViewTopic();
222
                $hydrator->hydrate($dataPost, $highPerformanceTeamsGroupsViewTopic);
223
 
224
                $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
225
                $result = $highPerformanceTeamsGroupsViewTopicMapper->insert($highPerformanceTeamsGroupsViewTopic);
226
 
227
                if($result) {
228
 
7512 nelberth 229
                    $this->logger->info('Se agrego el topic del grupo ' . $highPerformanceTeamsGroupsViewTopic->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
7489 nelberth 230
 
231
                    $data = [
232
                        'success'   => true,
233
                        'data'   => 'LABEL_RECORD_ADDED'
234
                    ];
235
                } else {
236
                    $data = [
237
                        'success'   => false,
238
                        'data'      => $highPerformanceTeamsGroupsViewTopicMapper->getError()
239
                    ];
240
 
241
                }
242
 
243
                return new JsonModel($data);
244
 
245
            } else {
246
                $messages = [];
247
                $form_messages = (array) $form->getMessages();
248
                foreach($form_messages  as $fieldname => $field_messages)
249
                {
250
 
251
                    $messages[$fieldname] = array_values($field_messages);
252
                }
253
 
254
                return new JsonModel([
255
                    'success'   => false,
256
                    'data'   => $messages
257
                ]);
258
            }
259
 
260
        } else {
261
            $data = [
262
                'success' => false,
7502 nelberth 263
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
7489 nelberth 264
            ];
265
 
266
            return new JsonModel($data);
267
        }
268
 
269
        return new JsonModel($data);
270
 
271
 
272
    }
273
 
274
 
275
 
276
     public function editAction(){
277
 
278
        $currentUserPlugin = $this->plugin('currentUserPlugin');
279
        $currentUser = $currentUserPlugin->getUser();
280
        $currentCompany = $currentUserPlugin->getCompany();
281
        $request = $this->getRequest();
282
        $uuid = $this->params()->fromRoute('id');
283
 
284
 
285
        if(!$uuid) {
286
            $data = [
287
                'success'   => false,
288
                'data'   => 'ERROR_INVALID_PARAMETER'
289
            ];
290
 
291
            return new JsonModel($data);
292
        }
293
 
294
        $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
295
        $group = $highPerformanceTeamsGroupsViewTopicMapper->fetchOneByUuid($uuid);
296
 
297
        if (!$group) {
298
            $data = [
299
                'success' => false,
300
                'data' => 'ERROR_RECORD_NOT_FOUND'
301
            ];
302
 
303
            return new JsonModel($data);
304
        }
305
 
306
        if ($group->company_id != $currentCompany->id) {
307
            return new JsonModel([
308
                'success' => false,
309
                'data' => 'ERROR_UNAUTHORIZED'
310
            ]);
311
        }
312
 
313
        if($request->isPost()) {
314
            $form = new  HighPerformanceTeamsGroupsViewTopicForm($this->adapter);
315
            $dataPost = $request->getPost()->toArray();
316
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroupsViewTopic::STATUS_INACTIVE;
317
            $form->setData($dataPost);
318
 
319
            if($form->isValid()) {
320
                $dataPost = (array) $form->getData();
321
 
322
                $hydrator = new ObjectPropertyHydrator();
323
                $hydrator->hydrate($dataPost, $group);
324
                $result = $highPerformanceTeamsGroupsViewTopicMapper->update($group);
325
 
326
                if($result) {
327
                    $this->logger->info('Se actualizo el grupo de alto rendimiento ' . $group->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
328
 
329
                    $data = [
330
                        'success' => true,
331
                        'data' => 'LABEL_RECORD_UPDATED'
332
                    ];
333
                } else {
334
                    $data = [
335
                        'success'   => false,
336
                        'data'      => $highPerformanceTeamsGroupsViewTopicMapper->getError()
337
                    ];
338
                }
339
 
340
                return new JsonModel($data);
341
 
342
            } else {
343
                $messages = [];
344
                $form_messages = (array) $form->getMessages();
345
                foreach($form_messages  as $fieldname => $field_messages)
346
                {
347
                    $messages[$fieldname] = array_values($field_messages);
348
                }
349
 
350
                return new JsonModel([
351
                    'success'   => false,
352
                    'data'   => $messages
353
                ]);
354
            }
355
        }else if ($request->isGet()) {
356
            $hydrator = new ObjectPropertyHydrator();
357
 
358
            $data = [
359
                'success' => true,
360
                'data' => $hydrator->extract($group)
361
            ];
362
 
363
            return new JsonModel($data);
364
        } else {
365
            $data = [
366
                'success' => false,
367
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
368
            ];
369
 
370
            return new JsonModel($data);
371
        }
372
 
373
        return new JsonModel($data);
374
 
375
    }
376
 
377
 
378
 
379
 
380
    public function deleteAction(){
381
        $currentUserPlugin = $this->plugin('currentUserPlugin');
382
        $currentCompany = $currentUserPlugin->getCompany();
383
        $currentUser = $currentUserPlugin->getUser();
384
 
385
        $request = $this->getRequest();
386
        $uuid = $this->params()->fromRoute('id');
387
 
388
 
389
        if (!$uuid) {
390
            $data = [
391
                'success' => false,
392
                'data' => 'ERROR_INVALID_PARAMETER'
393
            ];
394
 
395
            return new JsonModel($data);
396
        }
397
 
398
 
399
 
400
        $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
401
        $group = $highPerformanceTeamsGroupsViewTopicMapper->fetchOneByUuid($uuid);
402
 
403
        if (!$group) {
404
            $data = [
405
                'success' => false,
406
                'data' => 'ERROR_RECORD_NOT_FOUND'
407
            ];
408
 
409
            return new JsonModel($data);
410
        }
411
 
412
        if ($group->company_id != $currentCompany->id) {
413
            return new JsonModel([
414
                'success' => false,
415
                'data' => 'ERROR_UNAUTHORIZED'
416
            ]);
417
        }
418
 
419
        if ($request->isPost()) {
420
 
421
 
422
            $result = $highPerformanceTeamsGroupsViewTopicMapper->delete($group->id);
423
            if ($result) {
424
                $this->logger->info('Se borro el grupo de alto rendimiento  ' . $group->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
425
 
426
                $data = [
427
                    'success' => true,
428
                    'data' => 'LABEL_RECORD_DELETED'
429
                ];
430
            } else {
431
 
432
                $data = [
433
                    'success' => false,
434
                    'data' => $highPerformanceTeamsGroupsViewTopicMapper->getError()
435
                ];
436
 
437
                return new JsonModel($data);
438
            }
439
        } else {
440
            $data = [
441
                'success' => false,
442
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
443
            ];
444
 
445
            return new JsonModel($data);
446
        }
447
 
448
        return new JsonModel($data);
449
    }
450
 
451
 
452
 
453
 
454
}