Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
9760 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;
16768 efrain 9
 
9760 nelberth 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\HighPerformanceTeamsGroups;
16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
9841 nelberth 17
use LeadersLinked\Form\CreateFeedForm;
9760 nelberth 18
use LeadersLinked\Library\Functions;
19
use LeadersLinked\Mapper\UserMapper;
20
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
21
use LeadersLinked\Mapper\CompanyMapper;
9776 nelberth 22
use LeadersLinked\Mapper\TopicMapper;
9861 nelberth 23
use LeadersLinked\Model\Feed;
9778 nelberth 24
use LeadersLinked\Mapper\FeedMapper;
9760 nelberth 25
 
26
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
27
use LeadersLinked\Form\HighPerformanceTeamsGroupsMembersForm;
28
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
9766 nelberth 29
class HighPerformanceTeamsGroupsViewForoArticlesController extends AbstractActionController
9760 nelberth 30
{
31
    /**
32
     *
16769 efrain 33
     * @var \Laminas\Db\Adapter\AdapterInterface
9760 nelberth 34
     */
35
    private $adapter;
36
 
37
    /**
38
     *
16769 efrain 39
     * @var \LeadersLinked\Cache\CacheInterface
9760 nelberth 40
     */
16769 efrain 41
    private $cache;
42
 
43
 
44
    /**
45
     *
46
     * @var \Laminas\Log\LoggerInterface
47
     */
9760 nelberth 48
    private $logger;
49
 
50
    /**
51
     *
52
     * @var array
53
     */
54
    private $config;
55
 
16769 efrain 56
 
9760 nelberth 57
    /**
58
     *
16769 efrain 59
     * @var \Laminas\Mvc\I18n\Translator
60
     */
61
    private $translator;
62
 
63
 
64
    /**
65
     *
66
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
67
     * @param \LeadersLinked\Cache\CacheInterface $cache
68
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
9760 nelberth 69
     * @param array $config
16769 efrain 70
     * @param \Laminas\Mvc\I18n\Translator $translator
9760 nelberth 71
     */
16769 efrain 72
    public function __construct($adapter, $cache, $logger, $config, $translator)
9760 nelberth 73
    {
16769 efrain 74
        $this->adapter      = $adapter;
75
        $this->cache        = $cache;
76
        $this->logger       = $logger;
77
        $this->config       = $config;
78
        $this->translator   = $translator;
9760 nelberth 79
    }
80
 
81
 
82
 
83
    public function indexAction()
84
    {
85
 
86
 
87
        $currentUserPlugin = $this->plugin('currentUserPlugin');
88
        $currentUser = $currentUserPlugin->getUser();
89
        $currentCompany = $currentUserPlugin->getCompany();
15351 efrain 90
 
91
 
92
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
93
        $network = $currentNetworkPlugin->getNetwork();
94
 
11085 nelberth 95
        $group_uuid =  $this->params()->fromRoute('group_id');
96
        $category_uuid =  $this->params()->fromRoute('category_id');
9760 nelberth 97
 
98
 
99
        $request = $this->getRequest();
100
        if($request->isGet()) {
101
 
102
 
103
            $headers  = $request->getHeaders();
104
 
105
            $isJson = false;
106
            if($headers->has('Accept')) {
107
                $accept = $headers->get('Accept');
108
 
109
                $prioritized = $accept->getPrioritized();
110
 
111
                foreach($prioritized as $key => $value) {
112
                    $raw = trim($value->getRaw());
113
 
114
                    if(!$isJson) {
115
                        $isJson = strpos($raw, 'json');
116
                    }
117
 
118
                }
119
            }
9775 nelberth 120
 
9776 nelberth 121
            if(!$group_uuid) {
122
                $data = [
123
                    'success'   => false,
124
                    'data'   => 'ERROR_INVALID_PARAMETER'
125
                ];
126
 
127
                return new JsonModel($data);
128
            }
129
 
130
            if(!$category_uuid) {
131
                $data = [
132
                    'success'   => false,
133
                    'data'   => 'ERROR_INVALID_PARAMETER'
134
                ];
135
 
136
                return new JsonModel($data);
137
            }
138
 
9775 nelberth 139
            $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
140
            $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
11189 nelberth 141
            $this->layout()->setTemplate('layout/layout-backend');
142
            $viewModel = new ViewModel();
9775 nelberth 143
            if (!$highPerformanceTeamsGroups) {
11189 nelberth 144
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
145
                return $viewModel;
9775 nelberth 146
            }
10377 nelberth 147
            if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
11189 nelberth 148
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
149
                return $viewModel;
10377 nelberth 150
 
151
            }
152
            $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
153
            $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
9776 nelberth 154
            if(!$highPerformanceTeamsGroupsMember) {
11189 nelberth 155
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
156
                return $viewModel;
9776 nelberth 157
            }
158
            if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
11189 nelberth 159
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
160
                return $viewModel;
9776 nelberth 161
            }
10377 nelberth 162
 
11190 nelberth 163
            $topicMapper = TopicMapper::getInstance($this->adapter);
11192 nelberth 164
            $topic = $topicMapper->fetchOneByUuidAndGroupId($category_uuid,$highPerformanceTeamsGroups->id);
10377 nelberth 165
 
11190 nelberth 166
            if(!$topic){
167
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
168
                return $viewModel;
169
            }
9775 nelberth 170
 
9760 nelberth 171
            if($isJson) {
172
 
9784 nelberth 173
 
9760 nelberth 174
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
9905 nelberth 175
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/foro/categories/articles/edit');
176
                $allowDelete = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/foro/categories/articles/delete');
177
                $allowView = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/foro/categories/articles/view');
9760 nelberth 178
 
9776 nelberth 179
 
9760 nelberth 180
 
181
                $search = $this->params()->fromQuery('search', []);
16766 efrain 182
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
9760 nelberth 183
 
184
 
185
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
186
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
187
                $order =  $this->params()->fromQuery('order', []);
188
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 189
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
9760 nelberth 190
 
9886 nelberth 191
                $fields =  ['title', 'added_on'];
192
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'added_on';
9760 nelberth 193
 
194
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
9888 nelberth 195
                    $order_direction = 'DESC';
9760 nelberth 196
                }
9785 nelberth 197
 
11190 nelberth 198
 
9785 nelberth 199
 
9787 nelberth 200
 
201
 
202
                $feedMapper = FeedMapper::getInstance($this->adapter);
9792 nelberth 203
 
9789 nelberth 204
                $paginator = $feedMapper->fetchAllDataTableForo($search, $page, $records_x_page, $order_field, $order_direction, $topic->id);
9794 nelberth 205
 
9760 nelberth 206
                $items = [];
207
 
208
                $records = $paginator->getCurrentItems();
209
 
210
 
211
                foreach($records as $record)
212
                {
213
 
214
 
9877 nelberth 215
                    $userMapper = UserMapper::getInstance($this->adapter);
216
                    $user = $userMapper->fetchOne($record->user_id);
9919 nelberth 217
                    if($record->user_id!=$currentUser->id){
10380 nelberth 218
                        $contentAllowDelete=$allowDelete;
219
                        $contentAllowEdit=$allowEdit;
9919 nelberth 220
                        $allowDelete=false;
221
                        $allowEdit=false;
222
                    }
10389 nelberth 223
                    if($highPerformanceTeamsGroupsMember->type!=HighPerformanceTeamsGroupsMembers::TYPE_USER){
10377 nelberth 224
                        $allowDelete=true;
225
                    }
9883 nelberth 226
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
9760 nelberth 227
                    $item = [
228
                        'title' => $record->title,
9877 nelberth 229
                        'author' => [
230
                            'user_name' => $user->first_name . ' ' . $user->last_name,
15351 efrain 231
                            'user_url' => 'https://'. $network->main_hostname . '/profile/view/' . $user->uuid,
9877 nelberth 232
                            'user_image' => $this->url()->fromRoute('storage', ['code' => $user->uuid, 'type' => 'user', 'filename' => $user->image]),
233
                        ],
9903 nelberth 234
                        'added_on'=> $dt->format('d/m/Y'),
9760 nelberth 235
                        'actions' => [
11085 nelberth 236
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/view/foro/categories/articles/edit', ['group_id' => $group_uuid,'category_id' => $category_uuid, 'article_id' => $record->uuid]) : '',
237
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/view/foro/categories/articles/delete', ['group_id' => $group_uuid,'category_id' => $category_uuid, 'article_id' => $record->uuid]) : '',
238
                            'link_view' => $allowView ? $this->url()->fromRoute('high-performance-teams/groups/view/foro/categories/articles/view', ['group_id' => $group_uuid,'category_id' => $category_uuid, 'article_id' => $record->uuid]) : '',
9760 nelberth 239
                        ]
240
 
241
                    ];
10380 nelberth 242
                    if($record->user_id!=$currentUser->id){
243
                        $allowDelete=$contentAllowDelete;
244
                        $allowEdit=$contentAllowEdit;
245
                    }
9760 nelberth 246
 
247
                    array_push($items, $item);
248
 
249
                }
250
 
251
                return new JsonModel([
252
                    'success' => true,
253
                    'data' => [
254
                        'items' => $items,
255
                        'total' => $paginator->getTotalItemCount(),
256
                    ]
257
                ]);
258
 
259
            } else  {
9841 nelberth 260
                $formAdd = new CreateFeedForm($this->adapter);
11189 nelberth 261
 
9768 nelberth 262
                $viewModel->setTemplate('leaders-linked/high-performance-teams-groups-view-foro-articles/index.phtml');
9760 nelberth 263
                $viewModel->setVariables([
9770 nelberth 264
                    'formAdd' => $formAdd,
9772 nelberth 265
                    'group_uuid'=>$group_uuid,
9775 nelberth 266
                    'group_title'=>$highPerformanceTeamsGroups->title,
12327 nelberth 267
                    'category_uuid'=>$category_uuid,
268
                    'topic_title'=>$topic->title
9760 nelberth 269
                ]);
270
                return $viewModel ;
271
            }
272
        } else {
273
            return new JsonModel([
274
                'success' => false,
275
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
276
            ]);
277
        }
278
    }
279
 
280
    public function addAction()
281
    {
9850 nelberth 282
 
9852 nelberth 283
 
9847 nelberth 284
        $currentUserPlugin = $this->plugin('currentUserPlugin');
9760 nelberth 285
        $currentUser = $currentUserPlugin->getUser();
286
        $currentCompany = $currentUserPlugin->getCompany();
11085 nelberth 287
        $group_uuid =  $this->params()->fromRoute('group_id');
288
        $category_uuid =  $this->params()->fromRoute('category_id');
9760 nelberth 289
 
290
        $request = $this->getRequest();
291
        if($request->isPost()) {
292
 
9847 nelberth 293
 
294
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
295
 
296
            $form = new CreateFeedForm($this->adapter);
297
 
9760 nelberth 298
            $form->setData($dataPost);
9859 nelberth 299
 
9760 nelberth 300
            if($form->isValid()) {
9860 nelberth 301
 
9847 nelberth 302
 
303
                $hydrator = new ObjectPropertyHydrator();
304
                $feed = new Feed();
305
                $hydrator->hydrate($dataPost, $feed);
306
 
9862 nelberth 307
 
9847 nelberth 308
 
9855 nelberth 309
 
10379 nelberth 310
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
311
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
312
 
313
                if (!$highPerformanceTeamsGroups) {
314
                    $data = [
315
                        'success' => false,
316
                        'data' => 'ERROR_RECORD_NOT_FOUND'
317
                    ];
318
 
319
                    return new JsonModel($data);
320
                }
321
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
9847 nelberth 322
 
10379 nelberth 323
                    return new JsonModel([
324
                        'success' => false,
325
                        'data' => 'ERROR_UNAUTHORIZED'
326
                    ]);
327
 
328
                }
329
                $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
330
                $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
331
                if(!$highPerformanceTeamsGroupsMember) {
332
                    return new JsonModel([
333
                        'success' => false,
334
                        'data' => 'ERROR_RECORD_NOT_FOUND'
335
                    ]);
336
                }
337
 
9847 nelberth 338
 
339
                    $highPerformanceTeamsGroupsMemberMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
340
                    $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMemberMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
341
                    if($highPerformanceTeamsGroupsMember) {
342
                        if($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN ){
343
                            $id =  $highPerformanceTeamsGroups->id;
344
                        }else{
345
 
346
                            return new JsonModel([
347
                                'success' => false,
348
                                'data' => 'ERROR_UNAUTHORIZED'
349
                            ]);
350
                        }
351
 
352
                    } else {
353
 
354
                        return new JsonModel([
355
                            'success' => false,
356
                            'data' => 'ERROR_RECORD_NOT_FOUND'
357
                        ]);
358
                    }
359
 
9864 nelberth 360
 
9847 nelberth 361
 
362
                        $topicMapper = TopicMapper::getInstance($this->adapter);
11192 nelberth 363
                        $topic = $topicMapper->fetchOneByUuidAndGroupId($category_uuid,$highPerformanceTeamsGroups->id);
9847 nelberth 364
                        if($topic){
365
                            $feed->topic_id = $topic->id;
366
                        }else{
367
                            return new JsonModel([
368
                                'success' => false,
369
                                'data' => 'ERROR_RECORD_NOT_FOUND'
370
                            ]);
371
                        }
9760 nelberth 372
 
373
 
9865 nelberth 374
 
9760 nelberth 375
 
9847 nelberth 376
                $feed->company_id       = $currentCompany->id;
377
                $feed->group_id         = null;
378
                $feed->high_performance_group_id = $id;
379
                $feed->user_id          = $currentUser->id;
380
                $feed->type             = Feed::TYPE_HPTG;
381
                $feed->posted_or_shared = Feed::POSTED;
382
                $feed->shared_with      = Feed::SHARE_WITH_CONNECTIONS;
383
                $feed->total_comments   = 0;
384
                $feed->total_shared     = 0;
385
 
386
 
9866 nelberth 387
 
388
                $feedMapper = FeedMapper::getInstance($this->adapter);
389
                $result = $feedMapper->insert($feed);
390
 
9867 nelberth 391
 
9847 nelberth 392
                if($result) {
9868 nelberth 393
 
394
                    $this->logger->info('Se agrego el articulo del foro con el titulo ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
9870 nelberth 395
 
396
                    $response = [
9760 nelberth 397
                        'success'   => true,
398
                        'data'   => 'LABEL_RECORD_ADDED'
399
                    ];
9847 nelberth 400
 
9760 nelberth 401
                } else {
9847 nelberth 402
                    $response = [
9760 nelberth 403
                        'success'   => false,
9847 nelberth 404
                        'data'   => $feedMapper->getError()
9760 nelberth 405
                    ];
406
                }
407
            } else {
408
                $messages = [];
409
                $form_messages = (array) $form->getMessages();
410
                foreach($form_messages  as $fieldname => $field_messages)
411
                {
412
                    $messages[$fieldname] = array_values($field_messages);
413
                }
414
 
9847 nelberth 415
                $response = [
9760 nelberth 416
                    'success'   => false,
417
                    'data'   => $messages
9847 nelberth 418
                ];
9760 nelberth 419
            }
420
 
421
        } else {
9847 nelberth 422
            $response = [
9760 nelberth 423
                'success' => false,
9850 nelberth 424
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
9760 nelberth 425
            ];
426
        }
427
 
9847 nelberth 428
        return new JsonModel($response);
9760 nelberth 429
    }
430
 
431
 
432
     public function editAction(){
433
 
434
        $currentUserPlugin = $this->plugin('currentUserPlugin');
435
        $currentUser = $currentUserPlugin->getUser();
436
        $currentCompany = $currentUserPlugin->getCompany();
437
        $request = $this->getRequest();
9915 nelberth 438
 
11085 nelberth 439
        $group_uuid =  $this->params()->fromRoute('group_id');
440
        $category_uuid =  $this->params()->fromRoute('category_id');
9760 nelberth 441
 
11085 nelberth 442
        $article_uuid =  $this->params()->fromRoute('article_id');
9760 nelberth 443
 
9915 nelberth 444
        if(!$group_uuid) {
9760 nelberth 445
            $data = [
446
                'success'   => false,
447
                'data'   => 'ERROR_INVALID_PARAMETER'
448
            ];
449
 
450
            return new JsonModel($data);
451
        }
452
 
9915 nelberth 453
        if(!$category_uuid) {
454
            $data = [
455
                'success'   => false,
456
                'data'   => 'ERROR_INVALID_PARAMETER'
457
            ];
458
 
459
            return new JsonModel($data);
460
        }
461
 
462
        if(!$article_uuid) {
463
            $data = [
464
                'success'   => false,
465
                'data'   => 'ERROR_INVALID_PARAMETER'
466
            ];
467
 
468
            return new JsonModel($data);
469
        }
470
 
9760 nelberth 471
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
9915 nelberth 472
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
9760 nelberth 473
 
9915 nelberth 474
        if (!$highPerformanceTeamsGroups) {
9760 nelberth 475
            $data = [
476
                'success' => false,
477
                'data' => 'ERROR_RECORD_NOT_FOUND'
478
            ];
479
 
480
            return new JsonModel($data);
481
        }
10384 nelberth 482
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
483
 
484
            return new JsonModel([
485
                'success' => false,
486
                'data' => 'ERROR_UNAUTHORIZED'
487
            ]);
488
 
489
        }
490
 
491
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
492
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
9915 nelberth 493
        if(!$highPerformanceTeamsGroupsMember) {
9760 nelberth 494
            return new JsonModel([
495
                'success' => false,
9915 nelberth 496
                'data' => 'ERROR_RECORD_NOT_FOUND'
9760 nelberth 497
            ]);
9915 nelberth 498
        }
499
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
500
            return new JsonModel([
501
                'success' => false,
502
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
503
            ]);
504
        }
10388 nelberth 505
        if($highPerformanceTeamsGroupsMember->user_id!=$currentUser->id){
506
 
507
                return new JsonModel([
508
                    'success' => false,
509
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
510
                ]);
511
 
10384 nelberth 512
        }
10388 nelberth 513
 
10384 nelberth 514
 
9915 nelberth 515
        $feedMapper = FeedMapper::getInstance($this->adapter);
516
        $feed = $feedMapper->fetchOneByUuid($article_uuid);
517
 
9760 nelberth 518
        if($request->isPost()) {
9918 nelberth 519
 
520
            $form = new  CreateFeedForm($this->adapter);
9760 nelberth 521
            $dataPost = $request->getPost()->toArray();
522
            $form->setData($dataPost);
523
 
524
            if($form->isValid()) {
525
                $dataPost = (array) $form->getData();
526
 
527
                $hydrator = new ObjectPropertyHydrator();
9918 nelberth 528
                $hydrator->hydrate($dataPost, $feed);
529
                $result = $feedMapper->update($feed);
9760 nelberth 530
 
531
                if($result) {
9918 nelberth 532
                    $this->logger->info('Se actualizo el articulo del foro con el titulo ' . $feed->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
9760 nelberth 533
 
534
                    $data = [
535
                        'success' => true,
536
                        'data' => 'LABEL_RECORD_UPDATED'
537
                    ];
538
                } else {
539
                    $data = [
540
                        'success'   => false,
9918 nelberth 541
                        'data'      => $feedMapper->getError()
9760 nelberth 542
                    ];
543
                }
544
 
545
                return new JsonModel($data);
546
 
547
            } else {
548
                $messages = [];
549
                $form_messages = (array) $form->getMessages();
550
                foreach($form_messages  as $fieldname => $field_messages)
551
                {
552
                    $messages[$fieldname] = array_values($field_messages);
553
                }
554
 
555
                return new JsonModel([
556
                    'success'   => false,
557
                    'data'   => $messages
558
                ]);
559
            }
560
        }else if ($request->isGet()) {
9915 nelberth 561
 
562
 
563
 
9760 nelberth 564
            $hydrator = new ObjectPropertyHydrator();
565
 
566
            $data = [
567
                'success' => true,
9915 nelberth 568
                'data' => $hydrator->extract($feed)
9760 nelberth 569
            ];
570
 
571
            return new JsonModel($data);
572
        } else {
573
            $data = [
574
                'success' => false,
575
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
576
            ];
577
 
578
            return new JsonModel($data);
579
        }
580
 
581
        return new JsonModel($data);
582
 
583
    }
584
 
585
 
586
 
587
 
9904 nelberth 588
    public function deleteAction()
589
    {
9760 nelberth 590
        $currentUserPlugin = $this->plugin('currentUserPlugin');
9904 nelberth 591
        $currentUser = $currentUserPlugin->getUser();
9760 nelberth 592
        $currentCompany = $currentUserPlugin->getCompany();
11085 nelberth 593
        $group_uuid =  $this->params()->fromRoute('group_id');
9904 nelberth 594
 
9760 nelberth 595
        $request = $this->getRequest();
10384 nelberth 596
 
597
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
598
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
599
 
600
        if (!$highPerformanceTeamsGroups) {
601
            $data = [
602
                'success' => false,
603
                'data' => 'ERROR_RECORD_NOT_FOUND'
604
            ];
605
 
606
            return new JsonModel($data);
607
        }
608
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
609
 
610
            return new JsonModel([
611
                'success' => false,
612
                'data' => 'ERROR_UNAUTHORIZED'
613
            ]);
614
 
615
        }
616
 
617
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
618
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
619
        if(!$highPerformanceTeamsGroupsMember) {
620
            return new JsonModel([
621
                'success' => false,
622
                'data' => 'ERROR_RECORD_NOT_FOUND'
623
            ]);
624
        }
625
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
626
            return new JsonModel([
627
                'success' => false,
628
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
629
            ]);
630
        }
10387 nelberth 631
        if($highPerformanceTeamsGroupsMember->user_id!=$currentUser->id){
10385 nelberth 632
 
10388 nelberth 633
            if($highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
10385 nelberth 634
                return new JsonModel([
635
                    'success' => false,
636
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
637
                ]);
638
            }
639
 
10384 nelberth 640
        }
10385 nelberth 641
 
10384 nelberth 642
 
10385 nelberth 643
 
644
 
9904 nelberth 645
        if($request->isPost()) {
9760 nelberth 646
 
9904 nelberth 647
 
11085 nelberth 648
            $article_uuid =  $this->params()->fromRoute('article_id');
9904 nelberth 649
 
650
            $feedMapper = FeedMapper::getInstance($this->adapter);
651
            $feed = $feedMapper->fetchOneByUuid($article_uuid);
652
            if(!$feed) {
653
                $response = [
654
                    'success' => false,
655
                    'data' => 'ERROR_POST_NOT_FOUND'
656
                ];
657
                return new JsonModel($response);
658
            }
659
 
660
 
10384 nelberth 661
 
9904 nelberth 662
            $feedMapper = FeedMapper::getInstance($this->adapter);
663
            $feed->status = Feed::STATUS_DELETED;
664
            if($feedMapper->update($feed)) {
665
                $response = [
9760 nelberth 666
                    'success' => true,
9904 nelberth 667
                    'data' => 'LABEL_FEED_WAS_DELETED'
9760 nelberth 668
                ];
9904 nelberth 669
 
9760 nelberth 670
            } else {
9904 nelberth 671
                $response = [
9760 nelberth 672
                    'success' => false,
9904 nelberth 673
                    'data' => $feedMapper->getError()
9760 nelberth 674
                ];
9904 nelberth 675
            }
9760 nelberth 676
 
9904 nelberth 677
 
678
            return new JsonModel($response);
679
 
9760 nelberth 680
        } else {
9904 nelberth 681
            $response = [
9760 nelberth 682
                'success' => false,
683
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
684
            ];
685
        }
9904 nelberth 686
 
687
        return new JsonModel($response);
9760 nelberth 688
    }
689
 
690
 
9904 nelberth 691
 
9760 nelberth 692
 
693
 
694
}