Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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