Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7017 | Rev 7047 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6001 efrain 1
<?php
6257 anderson 2
 
6001 efrain 3
/**
4
 *
5
 * Controlador: Mis Perfiles
6
 *
7
 */
6257 anderson 8
 
6001 efrain 9
declare(strict_types=1);
10
 
11
namespace LeadersLinked\Controller;
12
 
13
use Laminas\Db\Adapter\AdapterInterface;
6849 efrain 14
 
6001 efrain 15
use Laminas\Mvc\Controller\AbstractActionController;
16
use Laminas\Log\LoggerInterface;
17
use Laminas\View\Model\ViewModel;
18
use Laminas\View\Model\JsonModel;
6056 efrain 19
use LeadersLinked\Library\Functions;
20
use LeadersLinked\Library\Image;
6001 efrain 21
use LeadersLinked\Mapper\KnowledgeAreaCategoryMapper;
22
use LeadersLinked\Mapper\KnowledgeAreaCategoryUserMapper;
6056 efrain 23
use LeadersLinked\Mapper\UserMapper;
6001 efrain 24
use LeadersLinked\Model\KnowledgeAreaCategory;
6056 efrain 25
use LeadersLinked\Model\KnowledgeAreaCategoryUser;
26
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCreateForm;
27
use LeadersLinked\Mapper\KnowledgeAreaContentMapper;
28
use LeadersLinked\Model\KnowledgeAreaContent;
29
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaEditForm;
30
use LeadersLinked\Mapper\CommentMapper;
31
use LeadersLinked\Form\KnowledgeArea\CommentForm;
32
use LeadersLinked\Model\Comment;
33
use LeadersLinked\Mapper\ContentReactionMapper;
34
use LeadersLinked\Model\ContentReaction;
6001 efrain 35
 
36
class KnowledgeAreaController extends AbstractActionController
37
{
38
    /**
39
     *
6866 efrain 40
     * @var \Laminas\Db\Adapter\AdapterInterface
6001 efrain 41
     */
42
    private $adapter;
6866 efrain 43
 
6001 efrain 44
    /**
45
     *
6866 efrain 46
     * @var \LeadersLinked\Cache\CacheInterface
6001 efrain 47
     */
6866 efrain 48
    private $cache;
49
 
50
 
51
    /**
52
     *
53
     * @var \Laminas\Log\LoggerInterface
54
     */
6001 efrain 55
    private $logger;
6866 efrain 56
 
6001 efrain 57
    /**
58
     *
59
     * @var array
60
     */
61
    private $config;
6866 efrain 62
 
63
 
6001 efrain 64
    /**
65
     *
6866 efrain 66
     * @var \Laminas\Mvc\I18n\Translator
67
     */
68
    private $translator;
69
 
70
 
71
    /**
72
     *
73
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
74
     * @param \LeadersLinked\Cache\CacheInterface $cache
75
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
6001 efrain 76
     * @param array $config
6866 efrain 77
     * @param \Laminas\Mvc\I18n\Translator $translator
6001 efrain 78
     */
6866 efrain 79
    public function __construct($adapter, $cache, $logger, $config, $translator)
6001 efrain 80
    {
81
        $this->adapter      = $adapter;
6866 efrain 82
        $this->cache        = $cache;
6001 efrain 83
        $this->logger       = $logger;
84
        $this->config       = $config;
6866 efrain 85
        $this->translator   = $translator;
6257 anderson 86
    }
6001 efrain 87
 
88
    /**
89
     *
90
     * Generación del listado de perfiles
91
     * {@inheritDoc}
92
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
93
     */
94
    public function indexAction()
95
    {
96
        $currentUserPlugin = $this->plugin('currentUserPlugin');
97
        $currentUser = $currentUserPlugin->getUser();
6257 anderson 98
 
6001 efrain 99
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
100
        $currentNetwork = $currentNetworkPlugin->getNetwork();
6257 anderson 101
 
6001 efrain 102
        $request = $this->getRequest();
6257 anderson 103
        if ($request->isGet()) {
104
 
6056 efrain 105
            $isJson = false;
6257 anderson 106
 
6056 efrain 107
            $headers  = $request->getHeaders();
6257 anderson 108
            if ($headers->has('Accept')) {
6056 efrain 109
                $accept = $headers->get('Accept');
6257 anderson 110
 
6056 efrain 111
                $prioritized = $accept->getPrioritized();
6257 anderson 112
 
113
                foreach ($prioritized as $key => $value) {
6056 efrain 114
                    $raw = trim($value->getRaw());
6257 anderson 115
 
116
                    if (!$isJson) {
6056 efrain 117
                        $isJson = strpos($raw, 'json');
118
                    }
119
                }
120
            }
6257 anderson 121
 
7016 efrain 122
           // $isJson = true;
6257 anderson 123
            if ($isJson) {
124
 
6056 efrain 125
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
7016 efrain 126
                $allowAdd       = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/add') ? 1 : 0;
6056 efrain 127
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/edit');
128
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/delete');
129
                $allowView = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/view');
6257 anderson 130
 
131
 
132
 
6749 efrain 133
                $category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
134
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
6056 efrain 135
                $page   = intval($this->params()->fromQuery('start', 1), 10);
6257 anderson 136
 
6056 efrain 137
                $order_field        = 'added_on';
138
                $order_direction    = 'asc';
139
                $records_x_page     = 12;
140
 
6257 anderson 141
 
142
 
6056 efrain 143
                $category_with_edition_ids = [];
7016 efrain 144
                $category_with_edition_uuids = [];
6056 efrain 145
                $category_ids = [];
146
                $categories = [];
6257 anderson 147
 
7016 efrain 148
 
149
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 150
 
6056 efrain 151
                $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
152
                $records =  $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
6257 anderson 153
                foreach ($records as $record) {
154
                    if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
155
 
6056 efrain 156
                        array_push($category_with_edition_ids, $record->category_id);
7016 efrain 157
 
158
                        $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
159
                        if($knowledgeAreaCategory) {
160
                            array_push($category_with_edition_uuids, $knowledgeAreaCategory->uuid);
161
                        }
162
 
6056 efrain 163
                    }
6257 anderson 164
 
6056 efrain 165
                    array_push($category_ids, $record->category_id);
166
                }
6257 anderson 167
 
6056 efrain 168
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 169
 
170
                if ($category_ids) {
6056 efrain 171
                    $records =  $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
6257 anderson 172
                    foreach ($records as $record) {
173
                        $categories[$record->id] = [
6069 efrain 174
                            'uuid' => $record->uuid,
175
                            'name' => $record->name,
176
                        ];
6056 efrain 177
                    }
178
                }
6257 anderson 179
 
6056 efrain 180
                $records =  $knowledgeAreaCategoryMapper->fetchAllPublicByNetworkId($currentNetwork->id);
6257 anderson 181
                foreach ($records as $record) {
182
                    if (!isset($categories[$record->id])) {
183
 
184
                        $categories[$record->id] = [
6069 efrain 185
                            'uuid' => $record->uuid,
186
                            'name' => $record->name,
187
                        ];
6056 efrain 188
                    }
189
                }
190
 
6257 anderson 191
 
192
 
6056 efrain 193
                $categories = array_values($categories);
6257 anderson 194
                usort($categories, function ($a, $b) {
6056 efrain 195
                    return $a['name'] <=> $b['name'];
196
                });
6257 anderson 197
 
198
 
199
                if ($category_filter_id) {
6056 efrain 200
                    $categoryFilter = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_filter_id);
6257 anderson 201
                    if ($categoryFilter) {
202
                        $category_ids = [$categoryFilter->id];
6056 efrain 203
                    } else {
204
                        $category_ids = [];
205
                    }
206
                }
6257 anderson 207
 
6056 efrain 208
                $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
209
                $paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds($category_ids,  $search, $page, $records_x_page, $order_field, $order_direction);
6257 anderson 210
 
211
 
6056 efrain 212
                $items = [];
213
                $records = $paginator->getCurrentItems();
6257 anderson 214
                foreach ($records as $record) {
215
 
216
                    if (!isset($categories[$record->category_id])) {
217
                        $category = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
218
                        if ($category) {
219
                            $categories[$category->id] = [
6069 efrain 220
                                'uuid' =>  $category->uuid,
221
                                'name' =>  $category->name,
222
                            ];
223
                        }
224
                    }
6056 efrain 225
 
6257 anderson 226
 
227
 
6056 efrain 228
                    $description = strip_tags($record->description);
6257 anderson 229
                    if (strlen($description) > 120) {
6056 efrain 230
                        $description = substr($description, 0, 120) . '...';
231
                    }
6257 anderson 232
 
6056 efrain 233
                    $item = [
234
                        'image' => $this->url()->fromRoute('storage', ['type' => 'knowledge-area', 'code' => $record->uuid,  'filename' => $record->image]),
235
                        'title' => $record->title,
236
                        'description' => $description,
237
                        'category' => $categories[$record->category_id]['name'],
6257 anderson 238
                        'link_view' => $allowView ?  $this->url()->fromRoute('knowledge-area/view', ['id' => $record->uuid]) : '',
239
 
6056 efrain 240
                    ];
241
 
6257 anderson 242
 
243
 
244
                    if (in_array($record->category_id, $category_with_edition_ids)) {
245
                        $item['link_edit'] = $allowEdit ?  $this->url()->fromRoute('knowledge-area/edit', ['id' => $record->uuid]) : '';
246
                        $item['link_delete'] = $allowDelete ? $this->url()->fromRoute('knowledge-area/delete', ['id' => $record->uuid]) : '';
6056 efrain 247
                    }
6257 anderson 248
 
6056 efrain 249
                    array_push($items, $item);
250
                }
6257 anderson 251
 
7016 efrain 252
 
253
 
254
                $image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
255
 
6056 efrain 256
                return new JsonModel([
257
                    'success' => true,
258
                    'data' => [
259
                        'items' => $items,
260
                        'total' => $paginator->getTotalItemCount(),
261
                        'page' => $paginator->getCurrentPageNumber(),
7016 efrain 262
                        'total_pages' => $paginator->getPages()->pageCount,
263
                        'categories' => $categories,
264
                        'categories_with_edition' => $category_with_edition_uuids,
7017 efrain 265
                        'link_add' => $allowAdd ? $this->url()->fromRoute('knowledge-area/add') : '',
7016 efrain 266
                        'image_size' => $image_size,
7018 efrain 267
                        'content_edit' => count($category_with_edition_ids) > 0,
7016 efrain 268
 
6056 efrain 269
                    ]
270
                ]);
271
            } else {
6257 anderson 272
 
273
 
6056 efrain 274
                $category_with_edition_ids = [];
275
                $category_ids = [];
6257 anderson 276
 
277
 
6056 efrain 278
                $categories = [];
6257 anderson 279
 
280
 
6056 efrain 281
                $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
6257 anderson 282
                $records =  $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
283
                foreach ($records as $record) {
284
                    if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
285
 
6056 efrain 286
                        array_push($category_with_edition_ids, $record->category_id);
287
                    }
6257 anderson 288
 
6056 efrain 289
                    array_push($category_ids, $record->category_id);
290
                }
6257 anderson 291
 
6056 efrain 292
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 293
 
294
                if ($category_ids) {
6056 efrain 295
                    $records =  $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
6257 anderson 296
                    foreach ($records as $record) {
297
                        if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
298
 
299
                            $categories[$record->id] = [
300
                                'uuid' => $record->uuid,
301
                                'name' => $record->name,
6056 efrain 302
                            ];
303
                        }
304
                    }
305
                }
6257 anderson 306
 
6056 efrain 307
                $records =  $knowledgeAreaCategoryMapper->fetchAllPublicByNetworkId($currentNetwork->id);
6257 anderson 308
                foreach ($records as $record) {
309
                    if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
310
 
311
                        if (!isset($categories[$record->id])) {
312
 
313
                            $categories[$record->id] = [
6056 efrain 314
                                'uuid' => $record->uuid,
315
                                'name' => $record->name,
316
                            ];
317
                        }
318
                    }
319
                }
6257 anderson 320
 
321
 
6056 efrain 322
                $image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
6257 anderson 323
 
324
 
6056 efrain 325
                $categories = array_values($categories);
6257 anderson 326
                usort($categories, function ($a, $b) {
327
                    return $a['name'] <=> $b['name'];
6056 efrain 328
                });
6257 anderson 329
 
330
 
6056 efrain 331
                $formAdd = new KnowledgeAreaCreateForm($this->adapter, $category_with_edition_ids);
332
                $formEdit = new KnowledgeAreaEditForm($this->adapter, $category_with_edition_ids);
6257 anderson 333
 
334
 
6056 efrain 335
                $this->layout()->setTemplate('layout/layout.phtml');
336
                $viewModel = new ViewModel();
337
                $viewModel->setTemplate('leaders-linked/knowledge-area/index.phtml');
338
                $viewModel->setVariables([
6257 anderson 339
                    'categories' => $categories,
6056 efrain 340
                    'formAdd' => $formAdd,
341
                    'formEdit' => $formEdit,
342
                    'image_size' => $image_size,
343
                    'content_edit' => count($category_with_edition_ids) > 0,
344
                ]);
6257 anderson 345
                return $viewModel;
6056 efrain 346
            }
347
        } else {
348
            return new JsonModel([
349
                'success' => false,
350
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
351
            ]);
352
        }
353
    }
6257 anderson 354
 
6056 efrain 355
    public function addAction()
356
    {
357
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
358
        $currentUser        = $currentUserPlugin->getUser();
6257 anderson 359
 
6056 efrain 360
        $request            = $this->getRequest();
6257 anderson 361
 
362
        if ($request->isPost()) {
6056 efrain 363
            $category_with_edition_ids = [];
6001 efrain 364
            $category_ids = [];
6257 anderson 365
 
366
 
6001 efrain 367
            $categories = [];
6257 anderson 368
 
369
 
6001 efrain 370
            $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
6056 efrain 371
            $records =  $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
6257 anderson 372
            foreach ($records as $record) {
373
                if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
374
 
6056 efrain 375
                    array_push($category_with_edition_ids, $record->category_id);
376
                }
6257 anderson 377
 
6001 efrain 378
                array_push($category_ids, $record->category_id);
379
            }
6257 anderson 380
 
6001 efrain 381
            $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 382
 
383
            if ($category_ids) {
6001 efrain 384
                $records =  $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
6257 anderson 385
                foreach ($records as $record) {
386
                    if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
387
 
388
                        $categories[$record->id] = [
6056 efrain 389
                            'uuid' => $record->uuid,
390
                            'name' => $record->name,
6001 efrain 391
                        ];
392
                    }
393
                }
394
            }
6056 efrain 395
 
6257 anderson 396
 
397
 
6056 efrain 398
            $categories = array_values($categories);
6257 anderson 399
            usort($categories, function ($a, $b) {
6056 efrain 400
                return $a['name'] <=> $b['name'];
401
            });
6257 anderson 402
 
403
 
6056 efrain 404
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
405
            $form = new KnowledgeAreaCreateForm($this->adapter, $category_with_edition_ids);
6257 anderson 406
 
6056 efrain 407
            $form->setData($dataPost);
6257 anderson 408
 
409
            if ($form->isValid()) {
6056 efrain 410
                $dataPost = (array) $form->getData();
6257 anderson 411
 
6056 efrain 412
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
413
                $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($dataPost['category_id']);
6257 anderson 414
 
415
 
6056 efrain 416
                $knowledgeAreaContent = new KnowledgeAreaContent();
417
                $knowledgeAreaContent->network_id = $knowledgeAreaCategory->network_id;
418
                $knowledgeAreaContent->company_id = $knowledgeAreaCategory->company_id;
419
                $knowledgeAreaContent->category_id = $knowledgeAreaCategory->id;
420
                $knowledgeAreaContent->user_id = $currentUser->id;
421
                $knowledgeAreaContent->title = $dataPost['title'];
422
                $knowledgeAreaContent->description = $dataPost['description'];
423
                $knowledgeAreaContent->link = $dataPost['link'];
6257 anderson 424
 
6056 efrain 425
                $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
6257 anderson 426
                if ($knowledgeAreaContentMapper->insert($knowledgeAreaContent)) {
6056 efrain 427
                    $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOne($knowledgeAreaContent->id);
6257 anderson 428
 
6056 efrain 429
                    $target_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
430
                    list($target_width, $target_height) = explode('x', $target_size);
6257 anderson 431
 
432
 
6056 efrain 433
                    $target_path = $this->config['leaderslinked.fullpath.knowledge_area']  . $knowledgeAreaContent->uuid;
6257 anderson 434
                    if (!file_exists($target_path)) {
6056 efrain 435
                        mkdir($target_path, 0755, true);
436
                    }
6257 anderson 437
 
438
 
6056 efrain 439
                    $files = $this->getRequest()->getFiles()->toArray();
6257 anderson 440
 
441
 
442
                    if (isset($files['image']) && empty($files['image']['error'])) {
6056 efrain 443
                        $tmp_filename  = $files['image']['tmp_name'];
444
                        $filename      = explode('.',  $files['image']['name']);
6257 anderson 445
                        $filename       = Functions::normalizeString(uniqid() . '-' . $filename[0] . '.png');
446
 
6685 efrain 447
                        //$crop_to_dimensions = true;
6257 anderson 448
 
6685 efrain 449
                        if(Image::uploadFile($tmp_filename, $target_path, $filename)) {
450
                            //  if (Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
6056 efrain 451
                            $knowledgeAreaContent->image = $filename;
452
                            $knowledgeAreaContentMapper->update($knowledgeAreaContent);
453
                        }
454
                    }
6257 anderson 455
 
456
                    if (isset($files['attachment']) && empty($files['attachment']['error'])) {
6056 efrain 457
                        $tmp_filename   = $files['attachment']['tmp_name'];
458
                        $filename       = Functions::normalizeString($files['attachment']['name']);
459
                        $destination      = $target_path  . DIRECTORY_SEPARATOR . $filename;
6257 anderson 460
 
461
 
462
                        if (move_uploaded_file($tmp_filename, $destination)) {
6056 efrain 463
                            $knowledgeAreaContent->attachment = $filename;
464
                            $knowledgeAreaContentMapper->update($knowledgeAreaContent);
465
                        }
466
                    }
6257 anderson 467
 
468
 
6056 efrain 469
                    $this->logger->info('Se agrego el contenido ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
6257 anderson 470
 
6056 efrain 471
                    $data = [
472
                        'success'   => true,
473
                        'data'   => 'LABEL_RECORD_ADDED'
474
                    ];
475
                } else {
476
                    $data = [
477
                        'success'   => false,
478
                        'data'      => $knowledgeAreaContentMapper->getError()
479
                    ];
480
                }
6257 anderson 481
 
6056 efrain 482
                return new JsonModel($data);
483
            } else {
484
                $messages = [];
485
                $form_messages = (array) $form->getMessages();
6257 anderson 486
                foreach ($form_messages  as $fieldname => $field_messages) {
487
 
6056 efrain 488
                    $messages[$fieldname] = array_values($field_messages);
489
                }
6257 anderson 490
 
6056 efrain 491
                return new JsonModel([
492
                    'success'   => false,
493
                    'data'   => $messages
494
                ]);
495
            }
496
        } else {
497
            $data = [
498
                'success' => false,
499
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
500
            ];
6257 anderson 501
 
6056 efrain 502
            return new JsonModel($data);
503
        }
6257 anderson 504
 
6056 efrain 505
        return new JsonModel($data);
506
    }
6257 anderson 507
 
6056 efrain 508
    public function deleteAction()
509
    {
510
        $currentUserPlugin = $this->plugin('currentUserPlugin');
511
        $currentUser    = $currentUserPlugin->getUser();
512
 
6257 anderson 513
 
6056 efrain 514
        $request    = $this->getRequest();
515
        $id         = $this->params()->fromRoute('id');
6257 anderson 516
 
517
 
518
 
6056 efrain 519
        $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
520
        $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 521
        if (!$knowledgeAreaContent) {
6056 efrain 522
            return new JsonModel([
523
                'success'   => false,
524
                'data'   => 'ERROR_RECORD_NOT_FOUND'
525
            ]);
526
        }
6257 anderson 527
 
528
 
529
 
6056 efrain 530
        $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
531
        $knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
6257 anderson 532
 
6056 efrain 533
        $ok = false;
6257 anderson 534
        if ($knowledgeAreaCategoryUser) {
535
 
536
            if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_EDITOR) {
6056 efrain 537
                $ok = $knowledgeAreaContent->user_id == $currentUser->id;
538
            }
6257 anderson 539
 
540
            if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR) {
6056 efrain 541
                $ok = true;
542
            }
543
        }
6257 anderson 544
 
545
        if (!$ok) {
6056 efrain 546
            return new JsonModel([
547
                'success'   => false,
548
                'data'   => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
549
            ]);
550
        }
6257 anderson 551
 
552
        if ($request->isPost()) {
553
 
6056 efrain 554
            $result =  $knowledgeAreaContentMapper->delete($knowledgeAreaContent);
6257 anderson 555
            if ($result) {
6056 efrain 556
                $this->logger->info('Se borro el cotenido : ' .  $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
6257 anderson 557
 
558
                if ($knowledgeAreaContent->image) {
559
 
6056 efrain 560
                    $target_path = $this->config['leaderslinked.fullpath.knowledge_area']  . $knowledgeAreaContent->uuid;
6257 anderson 561
                    if (file_exists($target_path)) {
6056 efrain 562
                        Functions::rmDirRecursive($target_path);
563
                    }
564
                }
6257 anderson 565
 
6056 efrain 566
                $data = [
567
                    'success' => true,
568
                    'data' => 'LABEL_RECORD_DELETED'
569
                ];
570
            } else {
6257 anderson 571
 
6056 efrain 572
                $data = [
573
                    'success'   => false,
574
                    'data'      => $knowledgeAreaContentMapper->getError()
575
                ];
6257 anderson 576
 
6056 efrain 577
                return new JsonModel($data);
578
            }
579
        } else {
580
            $data = [
581
                'success' => false,
582
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
583
            ];
6257 anderson 584
 
6056 efrain 585
            return new JsonModel($data);
586
        }
6257 anderson 587
 
6056 efrain 588
        return new JsonModel($data);
589
    }
6257 anderson 590
 
591
 
6056 efrain 592
    public function editAction()
593
    {
594
        $currentUserPlugin = $this->plugin('currentUserPlugin');
595
        $currentUser    = $currentUserPlugin->getUser();
6257 anderson 596
 
6056 efrain 597
        $request    = $this->getRequest();
598
        $id    = $this->params()->fromRoute('id');
6257 anderson 599
 
6056 efrain 600
        $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
601
        $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 602
        if (!$knowledgeAreaContent) {
6056 efrain 603
            return new JsonModel([
604
                'success'   => false,
605
                'data'   => 'ERROR_RECORD_NOT_FOUND'
606
            ]);
607
        }
6257 anderson 608
 
609
 
610
 
6056 efrain 611
        $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
612
        $knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
6257 anderson 613
 
6056 efrain 614
        $ok = false;
6257 anderson 615
        if ($knowledgeAreaCategoryUser) {
616
 
617
            if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_EDITOR) {
6056 efrain 618
                $ok = $knowledgeAreaContent->user_id == $currentUser->id;
619
            }
6257 anderson 620
 
621
            if ($knowledgeAreaCategoryUser->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR) {
6056 efrain 622
                $ok = true;
623
            }
624
        }
6257 anderson 625
 
626
        if (!$ok) {
6056 efrain 627
            return new JsonModel([
628
                'success'   => false,
629
                'data'   => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
630
            ]);
631
        }
6257 anderson 632
 
633
        if ($request->isGet()) {
634
 
635
 
6056 efrain 636
            $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 637
            $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($knowledgeAreaContent->category_id);
638
 
639
 
640
 
6056 efrain 641
            $data = [
642
                'success' => true,
643
                'data' => [
644
                    'category_id' => $knowledgeAreaCategory->uuid,
645
                    'title' => $knowledgeAreaContent->title,
646
                    'description' => $knowledgeAreaContent->description,
647
                    'link' => $knowledgeAreaContent->link,
648
                ]
649
            ];
6257 anderson 650
 
6056 efrain 651
            return new JsonModel($data);
6257 anderson 652
        } else if ($request->isPost()) {
6056 efrain 653
            $category_with_edition_ids = [];
654
            $category_ids = [];
6257 anderson 655
 
656
 
6056 efrain 657
            $categories = [];
6257 anderson 658
 
659
 
6056 efrain 660
            $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
661
            $records =  $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
6257 anderson 662
            foreach ($records as $record) {
663
                if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
664
 
6056 efrain 665
                    array_push($category_with_edition_ids, $record->category_id);
666
                }
6257 anderson 667
 
6056 efrain 668
                array_push($category_ids, $record->category_id);
669
            }
6257 anderson 670
 
6056 efrain 671
            $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 672
 
673
            if ($category_ids) {
6056 efrain 674
                $records =  $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
6257 anderson 675
                foreach ($records as $record) {
676
                    if ($record->status == KnowledgeAreaCategory::STATUS_ACTIVE) {
677
 
678
                        $categories[$record->id] = [
6001 efrain 679
                            'uuid' => $record->uuid,
680
                            'name' => $record->name,
681
                        ];
682
                    }
683
                }
684
            }
6257 anderson 685
 
686
 
687
 
6001 efrain 688
            $categories = array_values($categories);
6257 anderson 689
            usort($categories, function ($a, $b) {
6056 efrain 690
                return $a['name'] <=> $b['name'];
6001 efrain 691
            });
6257 anderson 692
 
693
 
6056 efrain 694
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
695
            $form = new KnowledgeAreaEditForm($this->adapter, $category_with_edition_ids);
696
            $form->setData($dataPost);
6257 anderson 697
 
698
            if ($form->isValid()) {
6056 efrain 699
                $dataPost = (array) $form->getData();
6257 anderson 700
 
701
 
6056 efrain 702
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 703
                $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($dataPost['category_id']);
704
 
705
 
706
 
707
                $knowledgeAreaContent->category_id = $knowledgeAreaCategory->id;
6056 efrain 708
                $knowledgeAreaContent->title = $dataPost['title'];
709
                $knowledgeAreaContent->description = $dataPost['description'];
6001 efrain 710
 
6257 anderson 711
 
712
                if ($knowledgeAreaContentMapper->update($knowledgeAreaContent)) {
713
 
6056 efrain 714
                    $target_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
715
                    list($target_width, $target_height) = explode('x', $target_size);
6257 anderson 716
 
717
 
6056 efrain 718
                    $target_path = $this->config['leaderslinked.fullpath.knowledge_area']  . $knowledgeAreaContent->uuid;
6257 anderson 719
                    if (!file_exists($target_path)) {
6056 efrain 720
                        mkdir($target_path, 0755, true);
721
                    }
6257 anderson 722
 
723
 
6056 efrain 724
                    $files = $this->getRequest()->getFiles()->toArray();
6257 anderson 725
 
726
 
727
                    if (isset($files['image']) && empty($files['image']['error'])) {
728
 
729
                        if ($knowledgeAreaContent->image) {
6056 efrain 730
                            @unlink($target_path  . DIRECTORY_SEPARATOR . $knowledgeAreaContent->image);
731
                        }
6257 anderson 732
 
6056 efrain 733
                        $tmp_filename  = $files['image']['tmp_name'];
734
                        $filename      = explode('.',  $files['image']['name']);
6257 anderson 735
                        $filename       = Functions::normalizeString(uniqid() . '-' . $filename[0] . '.png');
736
 
6685 efrain 737
                        // $crop_to_dimensions = true;
6257 anderson 738
 
6685 efrain 739
                        if(Image::uploadFile($tmp_filename, $target_path, $filename)) {
740
                            // if (Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
6056 efrain 741
                            $knowledgeAreaContent->image = $filename;
742
                            $knowledgeAreaContentMapper->update($knowledgeAreaContent);
743
                        }
744
                    }
6257 anderson 745
 
746
                    if (isset($files['attachment']) && empty($files['attachment']['error'])) {
6056 efrain 747
                        $tmp_filename   = $files['attachment']['tmp_name'];
748
                        $filename       = Functions::normalizeString($files['attachment']['name']);
749
                        $destination      = $target_path  . DIRECTORY_SEPARATOR . $filename;
6257 anderson 750
 
751
                        if ($knowledgeAreaContent->attachment) {
6056 efrain 752
                            @unlink($target_path  . DIRECTORY_SEPARATOR . $knowledgeAreaContent->attachment);
753
                        }
6257 anderson 754
 
755
 
756
                        if (move_uploaded_file($tmp_filename, $destination)) {
6056 efrain 757
                            $knowledgeAreaContent->attachment = $filename;
758
                            $knowledgeAreaContentMapper->update($knowledgeAreaContent);
759
                        }
760
                    }
6257 anderson 761
 
762
 
6056 efrain 763
                    $this->logger->info('Se edito el contenido ' . $knowledgeAreaContent->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
6257 anderson 764
 
6056 efrain 765
                    $data = [
766
                        'success'   => true,
767
                        'data'   => 'LABEL_RECORD_UPDATED'
768
                    ];
769
                } else {
770
                    $data = [
771
                        'success'   => false,
772
                        'data'      => $knowledgeAreaContentMapper->getError()
773
                    ];
774
                }
6257 anderson 775
 
6056 efrain 776
                return new JsonModel($data);
777
            } else {
778
                $messages = [];
779
                $form_messages = (array) $form->getMessages();
6257 anderson 780
                foreach ($form_messages  as $fieldname => $field_messages) {
781
 
6056 efrain 782
                    $messages[$fieldname] = array_values($field_messages);
783
                }
6257 anderson 784
 
6056 efrain 785
                return new JsonModel([
786
                    'success'   => false,
787
                    'data'   => $messages
788
                ]);
789
            }
790
        } else {
791
            $data = [
792
                'success' => false,
793
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
794
            ];
6257 anderson 795
 
6056 efrain 796
            return new JsonModel($data);
797
        }
6257 anderson 798
 
6056 efrain 799
        return new JsonModel($data);
800
    }
6257 anderson 801
 
802
 
6056 efrain 803
    public function viewAction()
804
    {
805
        $currentUserPlugin = $this->plugin('currentUserPlugin');
806
        $currentUser    = $currentUserPlugin->getUser();
6257 anderson 807
 
6056 efrain 808
        $request    = $this->getRequest();
809
        $id    = $this->params()->fromRoute('id');
6257 anderson 810
 
6056 efrain 811
        $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
812
        $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 813
        if (!$knowledgeAreaContent) {
6056 efrain 814
            return new JsonModel([
815
                'success'   => false,
816
                'data'   => 'ERROR_RECORD_NOT_FOUND'
817
            ]);
818
        }
6257 anderson 819
 
820
 
821
 
822
 
823
        if ($request->isGet()) {
6056 efrain 824
            $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
6257 anderson 825
            $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($knowledgeAreaContent->category_id);
826
 
827
 
828
 
6056 efrain 829
            $ok = false;
6257 anderson 830
            if ($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_COMPANY) {
831
 
6056 efrain 832
                $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
833
                $knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaContent->category_id, $currentUser->id);
6257 anderson 834
 
835
 
836
                if ($knowledgeAreaCategoryUser) {
6056 efrain 837
                    $ok = true;
838
                }
839
            }
6257 anderson 840
            if ($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_PUBLIC) {
6056 efrain 841
                $ok = true;
842
            }
6257 anderson 843
 
844
            if (!$ok) {
6056 efrain 845
                return new JsonModel([
846
                    'success'   => false,
847
                    'data'   => 'ERROR_KNOWLEDGE_AREA_YOU_DO_NOT_HAVE_PERMISSION'
848
                ]);
849
            }
6257 anderson 850
 
6056 efrain 851
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
852
            $contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
6257 anderson 853
 
854
 
855
 
6001 efrain 856
            $this->layout()->setTemplate('layout/layout.phtml');
857
            $viewModel = new ViewModel();
6056 efrain 858
            $viewModel->setTemplate('leaders-linked/knowledge-area/view.phtml');
6001 efrain 859
            $viewModel->setVariables([
6056 efrain 860
                'category' => $knowledgeAreaCategory->name,
861
                'title' => $knowledgeAreaContent->title,
862
                'description' => $knowledgeAreaContent->description,
863
                'link' => $knowledgeAreaContent->link,
864
                'image' =>  $this->url()->fromRoute('storage', ['type' => 'knowledge-area', 'code' =>  $knowledgeAreaContent->uuid,  'filename' =>  $knowledgeAreaContent->image]),
865
                'attachment' =>  $knowledgeAreaContent->attachment ? $this->url()->fromRoute('storage', ['type' => 'knowledge-area', 'code' =>  $knowledgeAreaContent->uuid,  'filename' =>  $knowledgeAreaContent->attachment]) : '',
866
                'reaction' => $contentReaction ? $contentReaction->reaction : '',
867
                'routeComments' => $this->url()->fromRoute('knowledge-area/comments', ['id' => $knowledgeAreaContent->uuid]),
868
                'routeCommentAdd' => $this->url()->fromRoute('knowledge-area/comments/add', ['id' => $knowledgeAreaContent->uuid]),
869
                'routeSaveReaction' => $this->url()->fromRoute('knowledge-area/save-reaction', ['id' => $knowledgeAreaContent->uuid]),
870
                'routeDeleteReaction' => $this->url()->fromRoute('knowledge-area/delete-reaction', ['id' => $knowledgeAreaContent->uuid]),
6001 efrain 871
            ]);
6257 anderson 872
            return $viewModel;
6056 efrain 873
        } else {
874
            $data = [
875
                'success' => false,
876
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
877
            ];
6257 anderson 878
 
6056 efrain 879
            return new JsonModel($data);
880
        }
6257 anderson 881
 
6056 efrain 882
        return new JsonModel($data);
883
    }
884
 
6257 anderson 885
 
6056 efrain 886
    public function addCommentAction()
887
    {
888
        $currentUserPlugin = $this->plugin('currentUserPlugin');
889
        $currentUser    = $currentUserPlugin->getUser();
6257 anderson 890
 
6056 efrain 891
        $id = $this->params()->fromRoute('id');
6257 anderson 892
 
6056 efrain 893
        $request = $this->getRequest();
894
        if ($request->isPost()) {
6257 anderson 895
 
6056 efrain 896
            $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
897
            $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 898
            if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
6056 efrain 899
                return new JsonModel([
900
                    'success'   => false,
901
                    'data'   => 'ERROR_RECORD_NOT_FOUND'
902
                ]);
903
            }
904
 
6265 anderson 905
 
906
 
907
 
6056 efrain 908
            $dataPost = $request->getPost()->toArray();
909
            $form = new CommentForm();
910
            $form->setData($dataPost);
6257 anderson 911
 
6056 efrain 912
            if ($form->isValid()) {
913
                $currentUserPlugin = $this->plugin('currentUserPlugin');
914
                $currentUser = $currentUserPlugin->getUser();
6257 anderson 915
 
6056 efrain 916
                $dataPost = (array) $form->getData();
6257 anderson 917
 
6056 efrain 918
                $comment = new Comment();
919
                $comment->network_id = $currentUser->network_id;
920
                $comment->comment = $dataPost['comment'];
921
                $comment->user_id = $currentUser->id;
922
                $comment->knowledge_area_id = $knowledgeAreaContent->id;
923
                $comment->relational = Comment::RELATIONAL_KNOWLEDGE_AREA;
6257 anderson 924
 
6056 efrain 925
                $commentMapper = CommentMapper::getInstance($this->adapter);
6388 efrain 926
                $now = $commentMapper->getDatebaseNow();
927
 
6056 efrain 928
                if ($commentMapper->insert($comment)) {
6257 anderson 929
 
6056 efrain 930
                    $total_comments = $commentMapper->fetchCountCommentByKnowledgeAreaId($comment->knowledge_area_id);
6257 anderson 931
 
6056 efrain 932
                    $knowledgeAreaContent->total_comments = $total_comments;
933
                    $knowledgeAreaContentMapper->update($knowledgeAreaContent);
6257 anderson 934
 
6056 efrain 935
                    $response = [
936
                        'success'   => true,
937
                        'data'   => $this->renderComment($comment->id, $now),
938
                        'total_comments' => $total_comments
939
                    ];
6257 anderson 940
 
6056 efrain 941
                    return new JsonModel($response);
942
                } else {
6257 anderson 943
 
6056 efrain 944
                    $response = [
945
                        'success'   => false,
946
                        'data'   => $commentMapper->getError()
947
                    ];
6257 anderson 948
 
6056 efrain 949
                    return new JsonModel($response);
950
                }
951
            } else {
952
                $message = '';;
953
                $form_messages = (array) $form->getMessages();
954
                foreach ($form_messages  as $fieldname => $field_messages) {
955
                    foreach ($field_messages as $key => $value) {
956
                        $message = $value;
957
                    }
958
                }
6257 anderson 959
 
6056 efrain 960
                $response = [
961
                    'success'   => false,
962
                    'data'   => $message
963
                ];
6257 anderson 964
 
6056 efrain 965
                return new JsonModel($response);
966
            }
6001 efrain 967
        } else {
6056 efrain 968
            $response = [
6001 efrain 969
                'success' => false,
970
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
6056 efrain 971
            ];
6257 anderson 972
 
6056 efrain 973
            return new JsonModel($response);
6001 efrain 974
        }
975
    }
6257 anderson 976
 
977
 
978
 
6056 efrain 979
    public function deleteCommentAction()
980
    {
981
        $currentUserPlugin = $this->plugin('currentUserPlugin');
982
        $currentUser    = $currentUserPlugin->getUser();
6257 anderson 983
 
6056 efrain 984
        $request = $this->getRequest();
985
        if ($request->isPost()) {
986
 
987
            $id = $this->params()->fromRoute('id');
988
            $comment = $this->params()->fromRoute('comment');
6257 anderson 989
 
990
 
6056 efrain 991
            $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
992
            $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
993
 
6257 anderson 994
 
995
 
996
            if ($knowledgeAreaContent && $knowledgeAreaContent->network_id == $currentUser->network_id) {
997
 
6056 efrain 998
                $commentMapper = CommentMapper::getInstance($this->adapter);
999
                $comment = $commentMapper->fetchOneByUuid($comment);
1000
 
6257 anderson 1001
 
6056 efrain 1002
                if ($comment && $comment->knowledge_area_id == $knowledgeAreaContent->id && $comment->user_id == $currentUser->id) {
6257 anderson 1003
 
6056 efrain 1004
                    $comment->status = Comment::STATUS_DELETED;
6257 anderson 1005
 
6056 efrain 1006
                    if ($commentMapper->update($comment)) {
6257 anderson 1007
 
6056 efrain 1008
                        $total_comments = $commentMapper->fetchCountCommentByKnowledgeAreaId($knowledgeAreaContent->id);
1009
                        $knowledgeAreaContent->total_comments = $total_comments;
1010
                        $knowledgeAreaContentMapper->update($knowledgeAreaContent);
1011
 
1012
                        $response = [
1013
                            'success' => true,
1014
                            'data' => 'LABEL_COMMENT_WAS_DELETED',
1015
                            'total_comments' => $total_comments
1016
                        ];
1017
                    } else {
1018
                        $response = [
1019
                            'success' => false,
1020
                            'data' => $commentMapper->getError()
1021
                        ];
1022
                    }
1023
                } else {
1024
                    $response = [
1025
                        'success' => false,
1026
                        'data' => 'ERROR_COMMENT_NOT_FOUND'
1027
                    ];
1028
                }
1029
            } else {
1030
                $response = [
1031
                    'success' => false,
1032
                    'data' => 'ERROR_COMMENT_NOT_FOUND'
1033
                ];
1034
            }
1035
        } else {
1036
            $response = [
1037
                'success' => false,
1038
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1039
            ];
1040
        }
6257 anderson 1041
 
6056 efrain 1042
        return new JsonModel($response);
1043
    }
6257 anderson 1044
 
1045
 
1046
 
6056 efrain 1047
    public function saveReactionAction()
1048
    {
6257 anderson 1049
 
6056 efrain 1050
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1051
        $currentUser    = $currentUserPlugin->getUser();
6257 anderson 1052
 
6056 efrain 1053
        $id = $this->params()->fromRoute('id');
1054
        $reaction  = $this->params()->fromPost('reaction');
6257 anderson 1055
 
6056 efrain 1056
        $request = $this->getRequest();
1057
        if ($request->isPost()) {
1058
 
1059
            $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
1060
            $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 1061
            if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
6056 efrain 1062
                return new JsonModel([
1063
                    'success'   => false,
1064
                    'data'   => 'ERROR_RECORD_NOT_FOUND'
1065
                ]);
1066
            }
6257 anderson 1067
 
6056 efrain 1068
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1069
            $contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
6257 anderson 1070
 
6056 efrain 1071
            if ($contentReaction) {
1072
                $contentReaction->reaction = $reaction;
6257 anderson 1073
 
6056 efrain 1074
                $result = $contentReactionMapper->update($contentReaction);
1075
            } else {
1076
                $contentReaction = new ContentReaction();
1077
                $contentReaction->user_id = $currentUser->id;
1078
                $contentReaction->knowledge_area_id = $knowledgeAreaContent->id;
1079
                $contentReaction->relational = ContentReaction::RELATIONAL_KNOWLEDGE_AREA;
1080
                $contentReaction->reaction = $reaction;
6257 anderson 1081
 
6056 efrain 1082
                $result = $contentReactionMapper->insert($contentReaction);
1083
            }
6257 anderson 1084
 
1085
 
1086
 
6056 efrain 1087
            if ($result) {
6257 anderson 1088
 
6521 efrain 1089
                $reactions = $contentReactionMapper->fetchCountByKnowledgeAreaId($knowledgeAreaContent->id);
6056 efrain 1090
                $response = [
1091
                    'success' => true,
1092
                    'data' => [
1093
                        'reactions' => $reactions
1094
                    ]
1095
                ];
1096
            } else {
1097
                $response = [
1098
                    'success' => false,
1099
                    'data' => $contentReactionMapper->getError()
1100
                ];
1101
            }
1102
            return new JsonModel($response);
1103
        }
6257 anderson 1104
 
6056 efrain 1105
        $response = [
1106
            'success' => false,
1107
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1108
        ];
1109
        return new JsonModel($response);
1110
    }
6257 anderson 1111
 
6056 efrain 1112
    public function deleteReactionAction()
1113
    {
1114
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1115
        $currentUser    = $currentUserPlugin->getUser();
6257 anderson 1116
 
6056 efrain 1117
        $id = $this->params()->fromRoute('id');
6257 anderson 1118
 
6056 efrain 1119
        $request = $this->getRequest();
1120
        if ($request->isPost()) {
1121
 
6257 anderson 1122
 
6056 efrain 1123
            $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
1124
            $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 1125
            if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
6056 efrain 1126
                return new JsonModel([
1127
                    'success'   => false,
1128
                    'data'   => 'ERROR_RECORD_NOT_FOUND'
1129
                ]);
1130
            }
6257 anderson 1131
 
6056 efrain 1132
            $contentReactionMapper = ContentReactionMapper::getInstance($this->adapter);
1133
            $contentReaction = $contentReactionMapper->fetchOneByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id);
6257 anderson 1134
 
6056 efrain 1135
            if (!$contentReaction) {
1136
                $response = [
1137
                    'success' => false,
1138
                    'data' => 'ERROR_DUPLICATE_ACTION'
1139
                ];
1140
                return new JsonModel($response);
1141
            }
6257 anderson 1142
 
6056 efrain 1143
            if ($contentReactionMapper->deleteByKnowledgeAreaIdAndUserId($knowledgeAreaContent->id, $currentUser->id)) {
6521 efrain 1144
                $reactions = $contentReactionMapper->fetchCountByKnowledgeAreaId($knowledgeAreaContent->id);
6257 anderson 1145
 
6056 efrain 1146
                $response = [
1147
                    'success' => true,
1148
                    'data' => [
1149
                        'reactions' => $reactions
1150
                    ]
1151
                ];
1152
            } else {
1153
                $response = [
1154
                    'success' => false,
1155
                    'data' => $contentReactionMapper->getError()
1156
                ];
1157
            }
1158
            return new JsonModel($response);
1159
        }
6257 anderson 1160
 
6056 efrain 1161
        $response = [
1162
            'success' => false,
1163
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1164
        ];
1165
        return new JsonModel($response);
1166
    }
6257 anderson 1167
 
6056 efrain 1168
    public function commentsAction()
1169
    {
1170
        $id = $this->params()->fromRoute('id');
6257 anderson 1171
 
6056 efrain 1172
        $request = $this->getRequest();
1173
        if ($request->isGet()) {
1174
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1175
            $currentUser = $currentUserPlugin->getUser();
1176
 
1177
            $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
6388 efrain 1178
            $now = $knowledgeAreaContentMapper->getDatebaseNow();
1179
 
6056 efrain 1180
            $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOneByUuid($id);
6257 anderson 1181
            if (!$knowledgeAreaContent || $knowledgeAreaContent->network_id != $currentUser->network_id) {
6056 efrain 1182
                return new JsonModel([
1183
                    'success'   => false,
1184
                    'data'   => 'ERROR_RECORD_NOT_FOUND'
1185
                ]);
1186
            }
6257 anderson 1187
 
6056 efrain 1188
            $commentMapper = CommentMapper::getInstance($this->adapter);
1189
            $records = $commentMapper->fetchAllPublishedByKnowledgeAreaId($knowledgeAreaContent->id);
6257 anderson 1190
 
6056 efrain 1191
            $comments = [];
1192
            foreach ($records as $record) {
1193
                $comment = $this->renderComment($record->id, $now);
1194
                array_push($comments, $comment);
1195
            }
6257 anderson 1196
 
6056 efrain 1197
            $response = [
1198
                'success' => true,
1199
                'data' => $comments
1200
            ];
6257 anderson 1201
 
6056 efrain 1202
            return new JsonModel($response);
1203
        } else {
6257 anderson 1204
 
1205
 
1206
 
6056 efrain 1207
            $response = [
1208
                'success' => false,
1209
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1210
            ];
6257 anderson 1211
 
1212
 
6056 efrain 1213
            return new JsonModel($response);
1214
        }
1215
    }
1216
 
6257 anderson 1217
 
1218
 
1219
 
6056 efrain 1220
    private function renderComment($comment_id, $now)
1221
    {
1222
        $item = [];
6257 anderson 1223
 
6056 efrain 1224
        $commentMapper = CommentMapper::getInstance($this->adapter);
1225
        $record = $commentMapper->fetchOne($comment_id);
6257 anderson 1226
 
6056 efrain 1227
        $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
1228
        $knowledgeAreaContent = $knowledgeAreaContentMapper->fetchOne($record->knowledge_area_id);
6257 anderson 1229
 
6056 efrain 1230
        if ($record) {
1231
            $userMapper = UserMapper::getInstance($this->adapter);
6257 anderson 1232
 
6056 efrain 1233
            $user = $userMapper->fetchOne($record->user_id);
6257 anderson 1234
 
6056 efrain 1235
            $item['unique'] = uniqid();
1236
            $item['user_image'] = $this->url()->fromRoute('storage', ['type' => 'user',  'code' => $user->uuid, 'filename' =>  $user->image]);
1237
            $item['user_url'] = $this->url()->fromRoute('profile/view', ['id' => $user->uuid]);
1238
            $item['user_name'] = $user->first_name . ' ' . $user->last_name;
1239
            $item['time_elapsed'] = Functions::timeAgo($record->added_on, $now);
6258 anderson 1240
            $item['comment'] = $record->comment;
6056 efrain 1241
            $item['link_delete'] = $this->url()->fromRoute('knowledge-area/comments/delete', ['id' => $knowledgeAreaContent->uuid, 'comment' => $record->uuid]);
1242
        }
1243
        return $item;
1244
    }
6001 efrain 1245
}