Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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