Proyectos de Subversion LeadersLinked - Services

Rev

Rev 777 | Rev 779 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 777 Rev 778
Línea 547... Línea 547...
547
 
547
 
548
 
548
 
-
 
549
 
549
 
550
    public function imageAction()
550
    public function imageAction()
551
    {
551
    {
552
        // Obtener el usuario actual
-
 
553
        $currentUserPlugin = $this->plugin('currentUserPlugin');
552
        $currentUserPlugin = $this->plugin('currentUserPlugin');
554
        $currentUser = $currentUserPlugin->getUser();
553
        $currentUser = $currentUserPlugin->getUser();
555
        
-
 
556
        // Obtener el id del grupo
554
 
557
        $user_group_id    = $this->params()->fromRoute('id');
Línea -... Línea 558...
-
 
558
        
555
        $user_group_id    = $this->params()->fromRoute('id');
559
        // Instanciar el mapper de grupos
556
 
560
        $groupMapper = GroupMapper::getInstance($this->adapter);
557
        $groupMapper = GroupMapper::getInstance($this->adapter);
561
 
558
 
562
        // Obtener el grupo por el id del grupo y el id de la red del usuario actual
559
        $group = $groupMapper->fetchOneByUuidAndNetworkId($user_group_id, $currentUser->network_id);
563
        $group = $groupMapper->fetchOneByUuidAndNetworkId($user_group_id, $currentUser->network_id);
560
        if (!$group) {
564
        if (!$group) {
561
            $response = [
-
 
562
                'success' => false,
-
 
563
                'data' => 'ERROR_INVALID_PARAMETER'
565
            return new JsonModel([
Línea 564... Línea -...
564
            ];
-
 
565
 
566
                'success' => false,
566
            return new JsonModel($response);
567
                'data' => 'ERROR_INVALID_PARAMETER'
567
        }
568
            ]);
568
 
569
        }
569
 
570
 
570
        if ($currentUser->id != $group->user_id) {
-
 
571
            $response = [
-
 
572
                'success' => false,
571
        if ($currentUser->id != $group->user_id) {
Línea -... Línea 572...
-
 
572
            return new JsonModel([
573
                'data' => 'ERROR_UNAUTHORIZED'
573
                'success' => false,
574
            ];
-
 
575
 
-
 
576
            return new JsonModel($response);
-
 
Línea -... Línea 574...
-
 
574
                'data' => 'ERROR_UNAUTHORIZED'
-
 
575
            ]);
577
        }
576
        }
-
 
577
 
578
 
578
        // Obtener el request
-
 
579
        $request = $this->getRequest();
-
 
580
 
-
 
581
        // Si el request es post
-
 
582
        if (!$request->isPost()) {
-
 
583
            return new JsonModel([
Línea 579... Línea 584...
579
        $request = $this->getRequest();
584
                'success' => false,
580
        
585
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
581
        if ($request->isPost()) {
586
            ]);
582
            $operation = $this->params()->fromRoute('operation');
587
        }
-
 
588
 
-
 
589
        // Obtener el operation
583
 
590
        $operation = $this->params()->fromRoute('operation');
584
            $storage = Storage::getInstance($this->config, $this->adapter);
591
 
585
            $target_path = $storage->getPathGroup();
592
        // Obtener el path de los archivos de los grupos
586
 
593
        $storage = Storage::getInstance($this->config, $this->adapter);
587
            if ($operation == 'delete') {
-
 
588
                $this->logger->info('Se borro el image del grupo : ' .  $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
594
        $target_path = $storage->getPathGroup();
589
                if ($group->image) {
595
 
Línea -... Línea 596...
-
 
596
        if ($operation == 'delete') {
-
 
597
            // Si el grupo no tiene image
-
 
598
            if (!$group->image) {
-
 
599
                return new JsonModel([
-
 
600
                    'success' => false,
-
 
601
                    'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
602
                ]);
Línea -... Línea 603...
-
 
603
            }
590
 
604
 
591
                    if (!$storage->deleteFile($target_path, $group->uuid, $group->image)) {
605
            // Si no se puede borrar el archivo
592
                        return new JsonModel([
606
            if (!$storage->deleteFile($target_path, $group->uuid, $group->image)) {
593
                            'success'   => false,
607
                return new JsonModel([
594
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
608
                    'success'   => false,
595
                        ]);
609
                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
596
                    }
610
                ]);
597
                }
-
 
598
 
-
 
599
 
-
 
600
                $group->image = '';
-
 
601
                if (!$groupMapper->updateImage($group)) {
-
 
602
                    return new JsonModel([
611
            }
603
                        'success'   => false,
612
 
604
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
605
                    ]);
613
            // Actualizar el grupo
606
                }
-
 
607
            } else {
-
 
608
                $form = new ImageForm($this->config);
-
 
609
                $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
610
 
-
 
611
                $form->setData($data);
-
 
612
 
-
 
613
                if ($form->isValid()) {
-
 
614
 
614
            $group->image = '';
615
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.group_image_size']);
-
 
616
 
-
 
617
 
-
 
618
                    $storage->setFiles($request->getFiles()->toArray());
-
 
619
                    if (!$storage->setCurrentFilename('image')) {
-
 
620
                        return new JsonModel([
-
 
621
                            'success'   => false,
615
            if (!$groupMapper->updateImage($group)) {
622
                            'data'   =>  'ERROR_UPLOAD_FILE'
616
                return new JsonModel([
623
                        ]);
617
                    'success'   => false,
624
                    }
618
                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
625
 
-
 
626
                    $source_filename    = $storage->getTmpFilename();
-
 
627
                    $filename           = 'group-image-' . uniqid() . '.png';
-
 
628
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_GROUP, $group->uuid, $filename);
619
                ]);
629
 
-
 
630
                    if ($storage->uploadImageWithOutChangeSize($source_filename, $target_filename, $target_width, $target_height)) {
-
 
631
 
-
 
632
                        if ($group->image) {
-
 
633
                            $target_path = $storage->getPathGroup();
-
 
634
                            $storage->deleteFile($target_path, $group->uuid, $group->image);
-
 
635
                        }
620
            }
636
 
-
 
637
                        $group->image = $filename;
-
 
638
 
621
 
639
                        $this->logger->info('Se guardo correctamente la imagen' . $filename . ' del grupo : ' . $group->name . ' en la ruta ' . $target_filename, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
-
 
640
 
622
            // Si se borro la imagen
641
                        if (!$groupMapper->updateImage($group)) {
623
            $this->logger->info('Se borro el image del grupo : ' .  $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
642
                            return new JsonModel([
624
        }
643
                                'success'   => false,
-
 
644
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
645
                            ]);
-
 
646
                        }
625
 
647
                    }
-
 
648
 
626
        if ($operation == 'upload') {
649
                    $this->logger->info('Se actualizo el image del grupo : ' . $group->name, ['user_id' => $group->user_id, 'ip' => Functions::getUserIP()]);
627
            $form = new ImageForm($this->config);
650
                } else {
-
 
651
                    $messages = [];
628
            $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
629
 
-
 
630
            $form->setData($data);
-
 
631
 
-
 
632
            if (!$form->isValid()) {
-
 
633
                $messages = [];
652
                    $form_messages = (array) $form->getMessages();
634
                $form_messages = (array) $form->getMessages();
Línea -... Línea 635...
-
 
635
 
-
 
636
                foreach ($form_messages  as $fieldname => $field_messages) {
Línea -... Línea 637...
-
 
637
                    $messages[$fieldname] = array_values($field_messages);
-
 
638
                }
Línea -... Línea 639...
-
 
639
 
-
 
640
                return new JsonModel([
653
                    foreach ($form_messages  as $fieldname => $field_messages) {
641
                    'success'   => false,
654
                        $messages[$fieldname] = array_values($field_messages);
642
                    'data'   => $messages
655
                    }
643
                ]);
-
 
644
            }
-
 
645
 
Línea -... Línea 646...
-
 
646
            // Obtener el tamaño de la imagen
-
 
647
            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.group_image_size']);
-
 
648
 
-
 
649
            // Setear los archivos
-
 
650
            $storage->setFiles($request->getFiles()->toArray());
-
 
651
 
-
 
652
            // Setear el archivo actual
-
 
653
            if (!$storage->setCurrentFilename('image')) {
-
 
654
                return new JsonModel([
656
 
655
                    'success'   => false,
657
                    return new JsonModel([
656
                    'data'   =>  'ERROR_UPLOAD_FILE'
Línea -... Línea 657...
-
 
657
                ]);
-
 
658
            }
-
 
659
 
-
 
660
            // Obtener la información del archivo
Línea 658... Línea 661...
658
                        'success'   => false,
661
            $source_filename    = $storage->getTmpFilename();
-
 
662
            $filename           = 'group-image-' . uniqid() . '.png';
-
 
663
            $target_filename    = $storage->composePathToFilename(Storage::TYPE_GROUP, $group->uuid, $filename);
-
 
664
 
659
                        'data'   => $messages
665
            if (!$storage->uploadImageWithOutChangeSize($source_filename, $target_filename, $target_width, $target_height)) {
660
                    ]);
666
                return new JsonModel([
-
 
667
                    'success'   => false,
661
                }
668
                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
Línea -... Línea 669...
-
 
669
                ]);
Línea 662... Línea 670...
662
            }
670
            }
-
 
671
 
-
 
672
            // Si el grupo tiene image, se borra
-
 
673
            if ($group->image) {
-
 
674
                $storage->deleteFile($target_path, $group->uuid, $group->image);
663
 
675
            }
Línea 664... Línea 676...
664
 
676
 
665
 
677
            // Actualizar el grupo