Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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