Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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