Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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