Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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