Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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