Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3454 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
 
18
use LeadersLinked\Library\Functions;
19
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
20
 
21
use LeadersLinked\Library\Image;
22
use LeadersLinked\Form\MyGroups\CreateForm;
23
use LeadersLinked\Model\Group;
24
use LeadersLinked\Mapper\GroupMapper;
25
use LeadersLinked\Form\MyGroup\AccessibilityForm;
26
use LeadersLinked\Form\MyGroup\CoverForm;
27
use LeadersLinked\Form\MyGroup\ExtendedForm;
28
use LeadersLinked\Form\MyGroup\GroupTypeForm;
29
use LeadersLinked\Form\MyGroup\ImageForm;
30
use LeadersLinked\Form\MyGroup\IndustryForm;
31
use LeadersLinked\Form\MyGroup\PrivacyForm;
32
use LeadersLinked\Form\MyGroup\WebsiteForm;
33
use LeadersLinked\Mapper\IndustryMapper;
34
use LeadersLinked\Mapper\GroupTypeMapper;
35
use LeadersLinked\Mapper\GroupMemberMapper;
36
use LeadersLinked\Mapper\QueryMapper;
37
use LeadersLinked\Model\GroupMember;
38
 
39
class MyGroupsController extends AbstractActionController
40
{
41
    /**
42
     *
43
     * @var AdapterInterface
44
     */
45
    private $adapter;
46
 
47
 
48
    /**
49
     *
50
     * @var AbstractAdapter
51
     */
52
    private $cache;
53
 
54
    /**
55
     *
56
     * @var  LoggerInterface
57
     */
58
    private $logger;
59
 
60
 
61
    /**
62
     *
63
     * @var array
64
     */
65
    private $config;
66
 
67
    /**
68
     *
69
     * @param AdapterInterface $adapter
70
     * @param AbstractAdapter $cache
71
     * @param LoggerInterface $logger
72
     * @param array $config
73
     */
74
    public function __construct($adapter, $cache , $logger,  $config)
75
    {
76
        $this->adapter      = $adapter;
77
        $this->cache        = $cache;
78
        $this->logger       = $logger;
79
        $this->config       = $config;
80
 
81
    }
82
 
83
    /**
84
     *
85
     * Generación del listado de perfiles
86
     * {@inheritDoc}
87
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
88
     */
89
    public function indexAction()
90
    {
91
        $currentUserPlugin = $this->plugin('currentUserPlugin');
92
        $currentUser = $currentUserPlugin->getUser();
93
 
94
        $request = $this->getRequest();
95
        if($request->isGet()) {
96
 
97
 
98
            $headers  = $request->getHeaders();
99
 
100
            $isJson = false;
101
            if($headers->has('Accept')) {
102
                $accept = $headers->get('Accept');
103
 
104
                $prioritized = $accept->getPrioritized();
105
 
106
                foreach($prioritized as $key => $value) {
107
                    $raw = trim($value->getRaw());
108
 
109
                    if(!$isJson) {
110
                        $isJson = strpos($raw, 'json');
111
                    }
112
 
113
                }
114
            }
115
 
116
            if($isJson) {
117
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
118
 
119
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
120
                $allowView = $acl->isAllowed($currentUser->usertype_id, 'group/view');
121
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'group/my-groups/edit');
122
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'group/my-groups/delete');
123
 
124
 
125
                $queryMapper = QueryMapper::getInstance($this->adapter);
126
 
127
                $select = $queryMapper->getSql()->select(GroupMapper::_TABLE);
128
                $select->columns(['id', 'uuid', 'name', 'privacy',  'image', 'status']);
129
                $select->where->equalTo('status', Group::STATUS_ACTIVE);
130
 
131
                if($search) {
132
                    $select->where->like('name', '%' . $search . '%');
133
                }
134
                $select->where->equalTo('user_id', $currentUser->id);
135
                $select->order('name ASC');
136
 
137
                $records = $queryMapper->fetchAll($select);
138
 
139
 
140
                $values = [
141
                    Group::PRIVACY_IS_PRIVATE => 'LABEL_PRIVATE',
142
                    Group::PRIVACY_IS_PUBLIC => 'LABEL_PUBLIC'
143
                ];
144
 
145
 
146
                $items = [];
147
                foreach($records as $record)
148
                {
149
 
150
                    $item = [
151
                        'name' => $record['name'],
152
                        'privacy' => $values[$record['privacy']],
153
                        'image' => $this->url()->fromRoute('storage', ['type' => 'group', 'code' => $record['uuid'], 'filename' => $record['image'] ]),
154
                        'link_view' => $allowView ? $this->url()->fromRoute('group/view', ['id' => $record['uuid'] ])  : '',
155
                        'link_edit' => $allowEdit ? $this->url()->fromRoute('group/my-groups/edit', ['id' => $record['uuid'] ])  : '',
156
                        'link_delete' => $allowDelete ? $this->url()->fromRoute('group/my-groups/delete', ['id' => $record['uuid'] ]) : '',
157
 
158
                    ];
159
 
160
                    array_push($items, $item);
161
                }
162
 
163
 
164
 
165
                $response = [
166
                    'success' => true,
167
                    'data' => $items
168
                ];
169
 
170
                return new JsonModel($response);
171
 
172
 
173
            } else {
174
                $industries = [];
175
                $industryMapper = IndustryMapper::getInstance($this->adapter);
176
                $records = $industryMapper->fetchAllActives();
177
                foreach($records as $record)
178
                {
179
                    $industries[$record->uuid] = $record->name;
180
                }
181
 
182
                $groupTypes = [];
183
                $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
184
                $records = $groupTypeMapper->fetchAllActives();
185
                foreach($records as $record)
186
                {
187
                    $groupTypes[$record->uuid] = $record->name;
188
                }
189
 
190
                $formAdd = new CreateForm($this->adapter);
191
 
192
                $this->layout()->setTemplate('layout/layout.phtml');
193
                $viewModel = new ViewModel();
194
                $viewModel->setTemplate('leaders-linked/my-groups/index.phtml');
195
                $viewModel->setVariables([
196
                    'industries' => $industries,
197
                    'types' => $groupTypes,
198
                    'formAdd' => $formAdd
199
                ]);
200
                return $viewModel ;
201
            }
202
 
203
        } else {
204
            return new JsonModel([
205
                'success' => false,
206
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
207
            ]);
208
        }
209
    }
210
 
211
    /**
212
     *
213
     * Agregar un nuevo perfil
214
     * @return \Laminas\View\Model\JsonModel
215
     */
216
    public function addAction()
217
    {
218
        $request = $this->getRequest();
219
 
220
 
221
        if($request->isPost()) {
222
            $form = new  CreateForm($this->adapter);
223
            $dataPost = $request->getPost()->toArray();
224
 
225
            $form->setData($dataPost);
226
 
227
            if($form->isValid()) {
228
 
229
 
230
                $currentUserPlugin = $this->plugin('currentUserPlugin');
231
                $currentUser = $currentUserPlugin->getUser();
232
 
233
                $dataPost = (array) $form->getData();
234
 
235
                $industryMapper = IndustryMapper::getInstance($this->adapter);
236
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
237
 
238
                $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
239
                $groupType = $groupTypeMapper->fetchOneByUuid($dataPost['type_id']);
240
 
241
 
242
                $group = new Group();
243
                $group->name = $dataPost['name'];
244
                $group->industry_id = $industry->id;
245
                $group->type_id = $groupType->id;
246
                $group->user_id = $currentUser->id;
247
 
248
                $groupMapper = GroupMapper::getInstance($this->adapter);
249
                $result = $groupMapper->insert($group );
250
 
251
                if($result) {
252
 
253
                    $groupMember = new GroupMember();
254
                    $groupMember->group_id = $group->id;
255
                    $groupMember->user_id = $group->user_id;
256
                    $groupMember->status = GroupMember::STATUS_ACCEPTED;
257
 
258
                    $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
259
                    $groupMemberMapper->insert($groupMember);
260
 
261
 
262
                    $this->logger->info('Se agrego el grupo : ' . $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
263
 
264
                    $data = [
265
                        'success'   => true,
266
                        'data'   => 'LABEL_RECORD_ADDED'
267
                    ];
268
                } else {
269
                    $data = [
270
                        'success'   => false,
271
                        'data'      => $group->getError()
272
                    ];
273
 
274
                }
275
 
276
                return new JsonModel($data);
277
 
278
            } else {
279
                $messages = [];
280
                $form_messages = (array) $form->getMessages();
281
                foreach($form_messages  as $fieldname => $field_messages)
282
                {
283
 
284
                    $messages[$fieldname] = array_values($field_messages);
285
                }
286
 
287
                return new JsonModel([
288
                    'success'   => false,
289
                    'data'   => $messages
290
                ]);
291
            }
292
 
293
        } else {
294
            $data = [
295
                'success' => false,
296
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
297
            ];
298
 
299
            return new JsonModel($data);
300
        }
301
 
302
        return new JsonModel($data);
303
    }
304
 
305
    /**
306
     *
307
     * Borrar un perfil excepto el público
308
     * @return \Laminas\View\Model\JsonModel
309
     */
310
    public function deleteAction()
311
    {
312
        $request = $this->getRequest();
313
        $id = $this->params()->fromRoute('id');
314
 
315
        if(!$id) {
316
            $data = [
317
                'success'   => false,
318
                'data'   => 'ERROR_INVALID_PARAMETER'
319
            ];
320
 
321
            return new JsonModel($data);
322
        }
323
 
324
 
325
 
326
        $groupMapper =GroupMapper::getInstance($this->adapter);
327
        $group = $groupMapper->fetchOneByUuid($id);
328
        if(!$group) {
329
            $data = [
330
                'success'   => false,
331
                'data'   => 'ERROR_RECORD_NOT_FOUND'
332
            ];
333
 
334
            return new JsonModel($data);
335
        }
336
 
337
        $currentUser = $this->plugin('currentUserPlugin');
338
        if($currentUser->getUserId() != $group->user_id) {
339
            $response = [
340
                'success' => false,
341
                'data' => 'ERROR_UNAUTHORIZED'
342
            ];
343
 
344
            return new JsonModel($response);
345
        }
346
 
347
 
348
        if($request->isPost()) {
349
            $result = $groupMapper->delete($group);
350
            if($result) {
351
                $this->logger->info('Se borro el grupo : ' .  $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
352
 
353
                $data = [
354
                    'success' => true,
355
                    'data' => 'LABEL_RECORD_DELETED'
356
                ];
357
            } else {
358
 
359
                $data = [
360
                    'success'   => false,
361
                    'data'      => $groupMapper->getError()
362
                ];
363
 
364
                return new JsonModel($data);
365
            }
366
 
367
        } else {
368
            $data = [
369
                'success' => false,
370
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
371
            ];
372
 
373
            return new JsonModel($data);
374
        }
375
 
376
        return new JsonModel($data);
377
    }
378
 
379
    /**
380
     * Presenta el perfil con las opciónes de edición de cada sección
381
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
382
     */
383
    public function editAction()
384
    {
385
        $flashMessenger = $this->plugin('FlashMessenger');
386
 
387
 
388
        $request = $this->getRequest();
389
        $id = $this->params()->fromRoute('id');
390
 
391
 
392
        if(!$id) {
393
            $flashMessenger->addErrorMessage('ERROR_INVALID_PARAMETER');
394
            return $this->redirect()->toRoute('dashboard');
395
        }
396
 
397
 
398
 
399
        $groupMapper =GroupMapper::getInstance($this->adapter);
400
        $group = $groupMapper->fetchOneByUuid($id);
401
        if(!$group) {
402
            $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
403
            return $this->redirect()->toRoute('dashboard');
404
        }
405
 
406
        $currentUser = $this->plugin('currentUserPlugin');
407
        if($currentUser->getUserId() != $group->user_id) {
408
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
409
            return $this->redirect()->toRoute('dashboard');
410
        }
411
 
412
 
413
        if($request->isGet()) {
414
 
415
            $accessibilities = [
416
                Group::ACCESSIBILITY_AUTO_JOIN => 'LABEL_AUTO_JOIN',
417
                Group::ACCESSIBILITY_REQUEST_TO_JOIN => 'LABEL_REQUEST_TO_JOIN',
418
                Group::ACCESSIBILITY_ADMIN_WILL_ADD => 'LABEL_ADMIN_WILL_ADD',
419
            ];
420
 
421
            $accessibility = $accessibilities[$group->accessibility];
422
 
423
            $privacies = [
424
                Group::PRIVACY_IS_PRIVATE => 'LABEL_PRIVATE',
425
                Group::PRIVACY_IS_PUBLIC => 'LABEL_PUBLIC'
426
            ];
427
 
428
            $privacy = $privacies[$group->privacy];
429
 
430
            $industryMapper = IndustryMapper::getInstance($this->adapter);
431
            $record = $industryMapper->fetchOne($group->industry_id);
432
 
433
            $industry = $record->name;
434
 
435
 
436
            $industries = [];
437
            $records = $industryMapper->fetchAllActives();
438
            foreach($records as $record)
439
            {
440
                $industries[$record->uuid] = $record->name;
441
            }
442
 
443
 
444
            $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
445
            $record = $groupTypeMapper->fetchOne($group->type_id);
446
 
447
            $group_type = $record->name;
448
 
449
            $types = [];
450
            $records =  $groupTypeMapper->fetchAllActives();
451
            foreach($records as $record)
452
            {
453
                $types[$record->uuid] = $record->name;
454
            }
455
 
456
 
457
 
458
            $formExtended = new ExtendedForm();
459
            $formAccessibility = new AccessibilityForm();
460
            $formPrivacy = new PrivacyForm();
461
            $formType = new GroupTypeForm($this->adapter);
462
            $formIndustry = new IndustryForm($this->adapter);
463
            $formImage = new ImageForm($this->config);
464
            $formCover = new CoverForm($this->config);
465
            $formWebsite = new WebsiteForm();
466
 
467
 
468
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
469
            $total_members = $groupMemberMapper->fetchTotalByGroupId($group->id);
470
 
471
            $image_size_cover = $this->config['leaderslinked.image_sizes.group_cover_upload'];
472
            $image_size_profile = $this->config['leaderslinked.image_sizes.group_image_upload'];
473
 
474
 
475
 
476
            $this->layout()->setTemplate('layout/layout.phtml');
477
            $viewModel = new ViewModel();
478
            $viewModel->setTemplate('leaders-linked/my-groups/edit.phtml');
479
            $viewModel->setVariables([
480
                'total_members'         => $total_members,
481
                'accessibility'         => $accessibility ,
482
                'privacy'               => $privacy,
483
                'industry'              => $industry,
484
                'group_id'              => $group->id,
485
                'group_type'            => $group_type,
486
                'group_uuid'            => $group->uuid,
487
                'name'                  => trim($group->name),
488
                'image'                 => $group->image,
489
                'cover'                 => $group->cover,
490
                'overview'              => $group->description,
491
                'website'               => $group->website,
492
 
493
                'formAccessibility'     => $formAccessibility,
494
                'formPrivacy'           => $formPrivacy,
495
                'formType'              => $formType,
496
                'formIndustry'          => $formIndustry,
497
                'formExtended'          => $formExtended,
498
                'formWebsite'           => $formWebsite,
499
                'formImage'             => $formImage,
500
                'formCover'             => $formCover,
501
                'image_size_cover'      => $image_size_cover,
502
                'image_size_profile'    => $image_size_profile,
503
 
504
 
505
                'industries'            => $industries,
506
                'types'                 => $types,
507
                'privacies'             => $privacies,
508
                'accessibilities'       => $accessibilities
509
            ]);
510
            return $viewModel ;
511
 
512
        } else {
513
            $data = [
514
                'success' => false,
515
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
516
            ];
517
 
518
            return new JsonModel($data);
519
        }
520
 
521
        return new JsonModel($data);
522
    }
523
 
524
 
525
    /**
526
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
527
     * @return \Laminas\View\Model\JsonModel
528
     */
529
    public function extendedAction()
530
    {
531
 
532
        $user_group_id = $this->params()->fromRoute('id');
533
        $groupMapper =GroupMapper::getInstance($this->adapter);
534
 
535
        $group = $groupMapper->fetchOneByUuid($user_group_id);
536
        if(!$group) {
537
            $response = [
538
                'success' => false,
539
                'data' => 'ERROR_INVALID_PARAMETER'
540
            ];
541
 
542
            return new JsonModel($response);
543
 
544
        }
545
 
546
        $currentUser = $this->plugin('currentUserPlugin');
547
        if($currentUser->getUserId() != $group->user_id) {
548
            $response = [
549
                'success' => false,
550
                'data' => 'ERROR_UNAUTHORIZED'
551
            ];
552
 
553
            return new JsonModel($response);
554
        }
555
 
556
 
557
 
558
        $request = $this->getRequest();
559
        if($request->isGet()) {
560
            $data = [
561
                'success' => true,
562
                'data' => $group->description,
563
            ];
564
 
565
            return new JsonModel($data);
566
 
567
 
568
        } else if($request->isPost()) {
569
 
570
 
571
            $form = new ExtendedForm();
572
            $dataPost = $request->getPost()->toArray();
573
 
574
            $form->setData($dataPost);
575
 
576
            if($form->isValid()) {
577
                $dataPost = (array) $form->getData();
578
 
579
                $hydrator = new ObjectPropertyHydrator();
580
                $hydrator->hydrate($dataPost, $group);
581
 
582
                $groupMapper->updateExtended($group);
583
 
584
                $this->logger->info('Se actualizo las descripción del grupo : '.  $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
585
 
586
                return new JsonModel([
587
                    'success'   => true,
588
                    'data' => $group->description,
589
                ]);
590
 
591
            } else {
592
                $messages = [];
593
                $form_messages = (array) $form->getMessages();
594
                foreach($form_messages  as $fieldname => $field_messages)
595
                {
596
                    $messages[$fieldname] = array_values($field_messages);
597
                }
598
 
599
                return new JsonModel([
600
                    'success'   => false,
601
                    'data'   => $messages
602
                ]);
603
            }
604
        }
605
 
606
 
607
        $data = [
608
            'success' => false,
609
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
610
        ];
611
 
612
 
613
        return new JsonModel($data);
614
    }
615
 
616
 
617
 
618
    public function imageAction()
619
    {
620
        $user_group_id    = $this->params()->fromRoute('id');
621
        $operation          = $this->params()->fromRoute('operation');
622
 
623
        $groupMapper =GroupMapper::getInstance($this->adapter);
624
 
625
        $group = $groupMapper->fetchOneByUuid($user_group_id);
626
        if(!$group) {
627
            $response = [
628
                'success' => false,
629
                'data' => 'ERROR_INVALID_PARAMETER'
630
            ];
631
 
632
            return new JsonModel($response);
633
 
634
        }
635
 
636
        $currentUser = $this->plugin('currentUserPlugin');
637
        $currentUser = $currentUser->getUser();
638
 
639
        if($currentUser->id != $group->user_id) {
640
            $response = [
641
                'success' => false,
642
                'data' => 'ERROR_UNAUTHORIZED'
643
            ];
644
 
645
            return new JsonModel($response);
646
        }
647
 
648
 
649
 
650
        $request = $this->getRequest();
651
        if($request->isPost()) {
652
            $target_path = $this->config['leaderslinked.fullpath.group'] . DIRECTORY_SEPARATOR . $group->uuid;
653
 
654
 
655
            if($operation == 'delete') {
656
                $this->logger->info('Se borro el image del grupo : ' .  $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
657
                if($group->image) {
658
                    if(!Image::delete($target_path, $group->image)) {
659
                        return new JsonModel([
660
                            'success'   => false,
661
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
662
                        ]);
663
                    }
664
                }
665
 
666
 
667
                $group->image = '';
668
                if(!$groupMapper->updateImage($group)) {
669
                    return new JsonModel([
670
                        'success'   => false,
671
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
672
                    ]);
673
                }
674
 
675
            } else {
676
                $form = new ImageForm($this->config);
677
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
678
 
679
                $form->setData($data);
680
 
681
                if($form->isValid()) {
682
 
683
                    $files = $request->getFiles()->toArray();
684
                    if(!empty($files['image']['error'])) {
685
 
686
                        return new JsonModel([
687
                            'success'   => false,
688
                            'data'   =>  'ERROR_UPLOAD_FILE'
689
                        ]);
690
 
691
 
692
                    }
693
 
694
                    if($group->image) {
695
                        if(!Image::delete($target_path, $group->image)) {
696
                            return new JsonModel([
697
                                'success'   => false,
698
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
699
                            ]);
700
                        }
701
                    }
702
 
703
 
704
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.group_image_size']);
705
                    $source             = $files['image']['tmp_name'];
706
                    $target_filename    = 'group-image-' . uniqid() . '.png';
707
                    $crop_to_dimensions = true;
708
 
709
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
710
                        return new JsonModel([
711
                            'success'   => false,
712
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
713
                        ]);
714
                    }
715
 
716
                    $group->image = $target_filename;
717
                    if(!$groupMapper->updateImage($group)) {
718
                        return new JsonModel([
719
                            'success'   => false,
720
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
721
                        ]);
722
                    }
723
 
724
                    $this->logger->info('Se actualizo el image del grupo : ' . $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
725
 
726
                } else {
727
                    $messages = [];
728
                    $form_messages = (array) $form->getMessages();
729
                    foreach($form_messages  as $fieldname => $field_messages)
730
                    {
731
                        $messages[$fieldname] = array_values($field_messages);
732
                    }
733
 
734
                    return new JsonModel([
735
                        'success'   => false,
736
                        'data'   => $messages
737
                    ]);
738
                }
739
            }
740
            return new JsonModel([
741
                'success'   => true,
742
                'data' => $this->url()->fromRoute('storage', ['type' => 'group', 'code' => $group->uuid, 'filename' => $group->image])
743
 
744
            ]);
745
        }
746
 
747
 
748
        $data = [
749
            'success' => false,
750
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
751
        ];
752
 
753
 
754
        return new JsonModel($data);
755
    }
756
 
757
 
758
    public function coverAction()
759
    {
760
        $user_group_id  = $this->params()->fromRoute('id');
761
        $operation      = $this->params()->fromRoute('operation');
762
 
763
        $groupMapper = GroupMapper::getInstance($this->adapter);
764
 
765
        $group = $groupMapper->fetchOneByUuid($user_group_id);
766
        if(!$group) {
767
            $response = [
768
                'success' => false,
769
                'data' => 'ERROR_INVALID_PARAMETER'
770
            ];
771
 
772
            return new JsonModel($response);
773
 
774
        }
775
 
776
        $currentUser = $this->plugin('currentUserPlugin');
777
        if($currentUser->getUserId() != $group->user_id) {
778
            $response = [
779
                'success' => false,
780
                'data' => 'ERROR_UNAUTHORIZED'
781
            ];
782
 
783
            return new JsonModel($response);
784
        }
785
 
786
 
787
 
788
        $request = $this->getRequest();
789
        if($request->isPost()) {
790
            $target_path = $this->config['leaderslinked.fullpath.group'] . DIRECTORY_SEPARATOR . $group->uuid;
791
 
792
 
793
            if($operation == 'delete') {
794
                if($group->cover) {
795
                    if(!Image::delete($target_path, $group->cover)) {
796
                        return new JsonModel([
797
                            'success'   => false,
798
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
799
                        ]);
800
                    }
801
                }
802
 
803
                $this->logger->info('Se borro el cover del grupo : ' . $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
804
 
805
                $group->cover = '';
806
                if(!$groupMapper->updateCover($group)) {
807
                    return new JsonModel([
808
                        'success'   => false,
809
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
810
                    ]);
811
                }
812
 
813
            } else {
814
                $form = new CoverForm($this->config);
815
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
816
 
817
                $form->setData($data);
818
 
819
                if($form->isValid()) {
820
 
821
                    $files = $request->getFiles()->toArray();
822
                    if(!empty($files['cover']['error'])) {
823
 
824
                        return new JsonModel([
825
                            'success'   => false,
826
                            'data'   =>  'ERROR_UPLOAD_FILE'
827
                        ]);
828
 
829
 
830
                    }
831
 
832
                    if($group->cover) {
833
                        if(!Image::delete($target_path, $group->cover)) {
834
                            return new JsonModel([
835
                                'success'   => false,
836
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
837
                            ]);
838
                        }
839
                    }
840
 
841
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.group_cover_size']);
842
                    $source             = $files['cover']['tmp_name'];
843
                    $target_filename    = 'group-cover-' . uniqid() . '.png';
844
                    $crop_to_dimensions = false;
845
 
846
                    if(!Image::uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions)) {
847
                        return new JsonModel([
848
                            'success'   => false,
849
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
850
                        ]);
851
                    }
852
 
853
 
854
 
855
                    $group->cover = $target_filename;
856
                    if(!$groupMapper->updateCover($group)) {
857
                        return new JsonModel([
858
                            'success'   => false,
859
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
860
                        ]);
861
                    }
862
 
863
                    $this->logger->info('Se actualizo el cover del grupo :  ' . $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
864
 
865
                } else {
866
                    $messages = [];
867
                    $form_messages = (array) $form->getMessages();
868
                    foreach($form_messages  as $fieldname => $field_messages)
869
                    {
870
                        $messages[$fieldname] = array_values($field_messages);
871
                    }
872
 
873
                    return new JsonModel([
874
                        'success'   => false,
875
                        'data'   => $messages
876
                    ]);
877
                }
878
            }
879
            return new JsonModel([
880
                'success'   => true,
881
                'data' => $this->url()->fromRoute('storage', ['type' => 'group-cover', 'code' => $group->uuid, 'filename' => $group->cover])
882
 
883
            ]);
884
        }
885
 
886
 
887
        $data = [
888
            'success' => false,
889
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
890
        ];
891
 
892
 
893
        return new JsonModel($data);
894
    }
895
 
896
    public function industryAction()
897
    {
898
        $id = $this->params()->fromRoute('id');
899
        if(!$id) {
900
            $response = [
901
                'success' => false,
902
                'data' => 'ERROR_INVALID_PARAMETER'
903
            ];
904
 
905
            return new JsonModel($response);
906
        }
907
 
908
        $groupMapper = GroupMapper::getInstance($this->adapter);
909
        $group = $groupMapper->fetchOneByUuid($id);
910
        if(!$group) {
911
            $response = [
912
                'success' => false,
913
                'data' => 'ERROR_RECORD_NOT_FOUND'
914
            ];
915
 
916
            return new JsonModel($response);
917
        }
918
 
919
        $currentUserPlugin = $this->plugin('currentUserPlugin');
920
        $currentUser = $currentUserPlugin->getUser();
921
        if($currentUser->id != $group->user_id) {
922
            $response = [
923
                'success' => false,
924
                'data' => 'ERROR_UNAUTHORIZED'
925
            ];
926
 
927
            return new JsonModel($response);
928
        }
929
 
930
 
931
 
932
        $request = $this->getRequest();
933
        if($request->isGet()) {
934
            $industryMapper = IndustryMapper::getInstance($this->adapter);
935
            $industry = $industryMapper->fetchOne($group->industry_id);
936
 
937
 
938
 
939
            $data = [
940
                'success' => true,
941
                'data' => $industry->uuid,
942
            ];
943
 
944
            return new JsonModel($data);
945
 
946
 
947
        } else if($request->isPost()) {
948
 
949
 
950
            $form = new IndustryForm($this->adapter);
951
            $dataPost = $request->getPost()->toArray();
952
 
953
            $form->setData($dataPost);
954
 
955
            if($form->isValid()) {
956
                $dataPost = (array) $form->getData();
957
 
958
                $industryMapper = IndustryMapper::getInstance($this->adapter);
959
                $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);
960
 
961
                $group->industry_id = $industry->id;
962
                $groupMapper->updateIndustry($group);
963
 
964
                $this->logger->info('Se actualizo la industria del grupo : ' . $group->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
965
 
966
 
967
 
968
                return new JsonModel([
969
                    'success'   => true,
970
                    'data' =>  $industry->name,
971
 
972
                ]);
973
 
974
            } else {
975
                $messages = [];
976
                $form_messages = (array) $form->getMessages();
977
                foreach($form_messages  as $fieldname => $field_messages)
978
                {
979
                    $messages[$fieldname] = array_values($field_messages);
980
                }
981
 
982
                return new JsonModel([
983
                    'success'   => false,
984
                    'data'   => $messages
985
                ]);
986
            }
987
        }
988
 
989
 
990
        $data = [
991
            'success' => false,
992
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
993
        ];
994
 
995
 
996
        return new JsonModel($data);
997
    }
998
 
999
    public function websiteAction()
1000
    {
1001
        $id = $this->params()->fromRoute('id');
1002
        if(!$id) {
1003
            $response = [
1004
                'success' => false,
1005
                'data' => 'ERROR_INVALID_PARAMETER'
1006
            ];
1007
 
1008
            return new JsonModel($response);
1009
        }
1010
 
1011
        $groupMapper = GroupMapper::getInstance($this->adapter);
1012
        $group = $groupMapper->fetchOneByUuid($id);
1013
        if(!$group) {
1014
            $response = [
1015
                'success' => false,
1016
                'data' => 'ERROR_RECORD_NOT_FOUND'
1017
            ];
1018
 
1019
            return new JsonModel($response);
1020
        }
1021
 
1022
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1023
        $currentUser = $currentUserPlugin->getUser();
1024
        if($currentUser->id != $group->user_id) {
1025
            $response = [
1026
                'success' => false,
1027
                'data' => 'ERROR_UNAUTHORIZED'
1028
            ];
1029
 
1030
            return new JsonModel($response);
1031
        }
1032
 
1033
 
1034
        $request = $this->getRequest();
1035
        if($request->isGet()) {
1036
            $data = [
1037
                'success' => true,
1038
                'data' => $group->website,
1039
            ];
1040
 
1041
            return new JsonModel($data);
1042
 
1043
 
1044
        } else if($request->isPost()) {
1045
 
1046
 
1047
            $form = new WebsiteForm();
1048
            $dataPost = $request->getPost()->toArray();
1049
 
1050
            $form->setData($dataPost);
1051
 
1052
            if($form->isValid()) {
1053
                $dataPost = (array) $form->getData();
1054
 
1055
                $hydrator = new ObjectPropertyHydrator();
1056
                $hydrator->hydrate($dataPost, $group);
1057
 
1058
                $groupMapper->updateWebsite($group);
1059
 
1060
                $this->logger->info('Se actualizo el website de la empresa ' . $group->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1061
 
1062
                return new JsonModel([
1063
                    'success'   => true,
1064
                    'data' => $group->website,
1065
                ]);
1066
 
1067
            } else {
1068
                $messages = [];
1069
                $form_messages = (array) $form->getMessages();
1070
                foreach($form_messages  as $fieldname => $field_messages)
1071
                {
1072
                    $messages[$fieldname] = array_values($field_messages);
1073
                }
1074
 
1075
                return new JsonModel([
1076
                    'success'   => false,
1077
                    'data'   => $messages
1078
                ]);
1079
            }
1080
        }
1081
 
1082
 
1083
        $data = [
1084
            'success' => false,
1085
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1086
        ];
1087
 
1088
 
1089
        return new JsonModel($data);
1090
    }
1091
 
1092
    public function accessibilityAction()
1093
    {
1094
        $id = $this->params()->fromRoute('id');
1095
        if(!$id) {
1096
            $response = [
1097
                'success' => false,
1098
                'data' => 'ERROR_INVALID_PARAMETER'
1099
            ];
1100
 
1101
            return new JsonModel($response);
1102
        }
1103
 
1104
        $groupMapper = GroupMapper::getInstance($this->adapter);
1105
        $group = $groupMapper->fetchOneByUuid($id);
1106
        if(!$group) {
1107
            $response = [
1108
                'success' => false,
1109
                'data' => 'ERROR_RECORD_NOT_FOUND'
1110
            ];
1111
 
1112
            return new JsonModel($response);
1113
        }
1114
 
1115
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1116
        $currentUser = $currentUserPlugin->getUser();
1117
        if($currentUser->id != $group->user_id) {
1118
            $response = [
1119
                'success' => false,
1120
                'data' => 'ERROR_UNAUTHORIZED'
1121
            ];
1122
 
1123
            return new JsonModel($response);
1124
        }
1125
 
1126
 
1127
        $request = $this->getRequest();
1128
        if($request->isGet()) {
1129
            $data = [
1130
                'success' => true,
1131
                'data' => $group->accessibility,
1132
            ];
1133
 
1134
            return new JsonModel($data);
1135
 
1136
 
1137
        } else if($request->isPost()) {
1138
 
1139
 
1140
            $form = new AccessibilityForm();
1141
            $dataPost = $request->getPost()->toArray();
1142
 
1143
            $form->setData($dataPost);
1144
 
1145
            if($form->isValid()) {
1146
                $dataPost = (array) $form->getData();
1147
 
1148
                $hydrator = new ObjectPropertyHydrator();
1149
                $hydrator->hydrate($dataPost, $group);
1150
 
1151
                $groupMapper->updateAccessibility($group);
1152
 
1153
                 $values = [
1154
                    Group::ACCESSIBILITY_AUTO_JOIN => 'LABEL_AUTO_JOIN',
1155
                    Group::ACCESSIBILITY_REQUEST_TO_JOIN => 'LABEL_REQUEST_TO_JOIN',
1156
                    Group::ACCESSIBILITY_ADMIN_WILL_ADD => 'LABEL_ADMIN_WILL_ADD',
1157
                ];
1158
 
1159
                $this->logger->info('Se actualizo el accesibilidad de la empresa ' . $group->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1160
 
1161
                return new JsonModel([
1162
                    'success'   => true,
1163
                    'data' => $values[$group->accessibility]
1164
                 ]);
1165
 
1166
            } else {
1167
                $messages = [];
1168
                $form_messages = (array) $form->getMessages();
1169
                foreach($form_messages  as $fieldname => $field_messages)
1170
                {
1171
                    $messages[$fieldname] = array_values($field_messages);
1172
                }
1173
 
1174
                return new JsonModel([
1175
                    'success'   => false,
1176
                    'data'   => $messages
1177
                ]);
1178
            }
1179
        }
1180
 
1181
 
1182
        $data = [
1183
            'success' => false,
1184
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1185
        ];
1186
 
1187
 
1188
        return new JsonModel($data);
1189
    }
1190
 
1191
    public function privacyAction()
1192
    {
1193
        $id = $this->params()->fromRoute('id');
1194
        if(!$id) {
1195
            $response = [
1196
                'success' => false,
1197
                'data' => 'ERROR_INVALID_PARAMETER'
1198
            ];
1199
 
1200
            return new JsonModel($response);
1201
        }
1202
 
1203
        $groupMapper = GroupMapper::getInstance($this->adapter);
1204
        $group = $groupMapper->fetchOneByUuid($id);
1205
        if(!$group) {
1206
            $response = [
1207
                'success' => false,
1208
                'data' => 'ERROR_RECORD_NOT_FOUND'
1209
            ];
1210
 
1211
            return new JsonModel($response);
1212
        }
1213
 
1214
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1215
        $currentUser = $currentUserPlugin->getUser();
1216
        if($currentUser->id != $group->user_id) {
1217
            $response = [
1218
                'success' => false,
1219
                'data' => 'ERROR_UNAUTHORIZED'
1220
            ];
1221
 
1222
            return new JsonModel($response);
1223
        }
1224
 
1225
 
1226
        $request = $this->getRequest();
1227
        if($request->isGet()) {
1228
            $data = [
1229
                'success' => true,
1230
                'data' => $group->privacy,
1231
            ];
1232
 
1233
            return new JsonModel($data);
1234
 
1235
 
1236
        } else if($request->isPost()) {
1237
 
1238
 
1239
            $form = new PrivacyForm();
1240
            $dataPost = $request->getPost()->toArray();
1241
 
1242
            $form->setData($dataPost);
1243
 
1244
            if($form->isValid()) {
1245
                $dataPost = (array) $form->getData();
1246
 
1247
                $hydrator = new ObjectPropertyHydrator();
1248
                $hydrator->hydrate($dataPost, $group);
1249
 
1250
                $groupMapper->updatePrivacy($group);
1251
 
1252
                $values = [
1253
                    Group::PRIVACY_IS_PRIVATE => 'LABEL_PRIVATE',
1254
                    Group::PRIVACY_IS_PUBLIC => 'LABEL_PUBLIC'
1255
                ];
1256
 
1257
 
1258
                $this->logger->info('Se actualizo la privacidad de la empresa ' . $group->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1259
 
1260
                return new JsonModel([
1261
                    'success'   => true,
1262
                    'data' => $values[$group->privacy]
1263
                ]);
1264
 
1265
            } else {
1266
                $messages = [];
1267
                $form_messages = (array) $form->getMessages();
1268
                foreach($form_messages  as $fieldname => $field_messages)
1269
                {
1270
                    $messages[$fieldname] = array_values($field_messages);
1271
                }
1272
 
1273
                return new JsonModel([
1274
                    'success'   => false,
1275
                    'data'   => $messages
1276
                ]);
1277
            }
1278
        }
1279
 
1280
 
1281
        $data = [
1282
            'success' => false,
1283
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1284
        ];
1285
 
1286
 
1287
        return new JsonModel($data);
1288
    }
1289
 
1290
 
1291
 
1292
    public function typeAction()
1293
    {
1294
        $id = $this->params()->fromRoute('id');
1295
        if(!$id) {
1296
            $response = [
1297
                'success' => false,
1298
                'data' => 'ERROR_INVALID_PARAMETER'
1299
            ];
1300
 
1301
            return new JsonModel($response);
1302
        }
1303
 
1304
        $groupMapper = GroupMapper::getInstance($this->adapter);
1305
        $group = $groupMapper->fetchOneByUuid($id);
1306
        if(!$group) {
1307
            $response = [
1308
                'success' => false,
1309
                'data' => 'ERROR_RECORD_NOT_FOUND'
1310
            ];
1311
 
1312
            return new JsonModel($response);
1313
        }
1314
 
1315
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1316
        $currentUser = $currentUserPlugin->getUser();
1317
        if($currentUser->id != $group->user_id) {
1318
            $response = [
1319
                'success' => false,
1320
                'data' => 'ERROR_UNAUTHORIZED'
1321
            ];
1322
 
1323
            return new JsonModel($response);
1324
        }
1325
 
1326
 
1327
 
1328
        $request = $this->getRequest();
1329
        if($request->isGet()) {
1330
 
1331
            $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
1332
            $groupType = $groupTypeMapper->fetchOne($group->type_id);
1333
 
1334
            $data = [
1335
                'success' => true,
1336
                'data' => $groupType->uuid,
1337
            ];
1338
 
1339
            return new JsonModel($data);
1340
 
1341
 
1342
        } else if($request->isPost()) {
1343
 
1344
 
1345
            $form = new GroupTypeForm($this->adapter);
1346
            $dataPost = $request->getPost()->toArray();
1347
 
1348
            $form->setData($dataPost);
1349
 
1350
            if($form->isValid()) {
1351
                $dataPost = (array) $form->getData();
1352
 
1353
                $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
1354
                $groupType = $groupTypeMapper->fetchOneByUuid($dataPost['type_id']);
1355
 
1356
                $group->type_id = $groupType->id;
1357
                $groupMapper->updateGroupType($group);
1358
 
1359
                $this->logger->info('Se actualizo el tipo del grupo : ' . $group->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1360
 
1361
                $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
1362
                $groupType = $groupTypeMapper->fetchOne($group->type_id);
1363
 
1364
                return new JsonModel([
1365
                    'success'   => true,
1366
                    'data' =>  $groupType->name,
1367
 
1368
                ]);
1369
 
1370
            } else {
1371
                $messages = [];
1372
                $form_messages = (array) $form->getMessages();
1373
                foreach($form_messages  as $fieldname => $field_messages)
1374
                {
1375
                    $messages[$fieldname] = array_values($field_messages);
1376
                }
1377
 
1378
                return new JsonModel([
1379
                    'success'   => false,
1380
                    'data'   => $messages
1381
                ]);
1382
            }
1383
        }
1384
 
1385
 
1386
        $data = [
1387
            'success' => false,
1388
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1389
        ];
1390
 
1391
 
1392
        return new JsonModel($data);
1393
    }
1394
 
1395
 
1396
 
1397
 
1398
 
1399
 
1400
 
1401
}