Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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