Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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