Proyectos de Subversion LeadersLinked - Services

Rev

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