Proyectos de Subversion LeadersLinked - Services

Rev

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

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