Proyectos de Subversion LeadersLinked - Services

Rev

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

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