Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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