Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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