Proyectos de Subversion LeadersLinked - Services

Rev

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

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