Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 345 Rev 788
Línea 429... Línea 429...
429
        ]);
429
        ]);
430
    }
430
    }
Línea 431... Línea 431...
431
    
431
    
432
    public function imageAction()
432
    public function imageAction()
-
 
433
    {
433
    {
434
        // Obtener el usuario actual
434
        $currentUserPlugin = $this->plugin('currentUserPlugin');
435
        $currentUserPlugin = $this->plugin('currentUserPlugin');
435
        $currentUser = $currentUserPlugin->getUser();
-
 
Línea -... Línea 436...
-
 
436
        $currentUser = $currentUserPlugin->getUser();
-
 
437
        
Línea -... Línea 438...
-
 
438
        // Obtener el operation
Línea -... Línea 439...
-
 
439
        $operation = $this->params()->fromRoute('operation');
-
 
440
        
-
 
441
        $request = $this->getRequest();
-
 
442
        
-
 
443
        if(!$request->isPost() || !$request->isGet()) {
-
 
444
            return new JsonModel([
-
 
445
                'success' => false,
-
 
446
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
447
            ]);
-
 
448
        }
Línea 436... Línea -...
436
        $operation = $this->params()->fromRoute('operation');
-
 
437
        
449
 
438
        
-
 
439
        
-
 
440
        
-
 
441
        $request = $this->getRequest();
-
 
442
        if($request->isGet()) {
-
 
443
            
-
 
444
            $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
445
            $currentUser = $currentUserPlugin->getUser();
-
 
446
            
450
        $userMapper = UserMapper::getInstance($this->adapter);
447
            $userMapper = UserMapper::getInstance($this->adapter);
-
 
Línea 448... Línea 451...
448
            
451
        $storage = Storage::getInstance($this->config, $this->adapter);
449
          
452
        $target_path = $storage->getPathUser();
450
            $storage = Storage::getInstance($this->config, $this->adapter);
453
        
451
            $image = $storage->getUserImage($currentUser);
454
        if($request->isGet()) {
452
 
455
            $image = $storage->getUserImage($currentUser);
453
            
456
            
454
            return new JsonModel([
457
            return new JsonModel([
455
                'success' => true,
-
 
456
                'data' => $image,
-
 
457
            ]);
-
 
458
            
-
 
459
            
-
 
460
        } else  if($request->isPost()) {
-
 
461
            $image = Image::getInstance($this->config);
458
                'success' => true,
-
 
459
                'data' => $image,
-
 
460
            ]);
-
 
461
        } 
-
 
462
        
462
            $target_path = $image->getStorage()->getPathUser();
463
        if($request->isPost()) {
-
 
464
            if($operation == 'delete') {
-
 
465
                // Si el usuario no tiene image
463
            
466
                if(!$currentUser->image) {
464
            
467
                    return new JsonModel([
465
            $userMapper = UserMapper::getInstance($this->adapter);
468
                        'success'   => false,
466
 
469
                        'data'   =>  'ERROR_RECORD_NOT_FOUND'
467
            if($operation == 'delete') {
470
                    ]);
468
                $this->logger->info('Se borro el image  del usuario ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
471
                }
469
                
-
 
470
                if($currentUser->image) {
472
 
471
                    if(!$image->getStorage()->deleteFile($target_path ,$currentUser->uuid, $currentUser->image)) {
473
                // Si no se puede borrar el archivo
Línea -... Línea 474...
-
 
474
                if(!$storage->deleteFile($target_path ,$currentUser->uuid, $currentUser->image)) {
472
                        return new JsonModel([
475
                    return new JsonModel([
473
                            'success'   => false,
476
                        'success'   => false,
474
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
477
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
475
                        ]);
478
                    ]);
476
                    }
479
                }
477
                }
480
                
478
                
481
                // Actualizar el usuario
479
                $currentUser->image = '';
482
                $currentUser->image = '';
-
 
483
                if(!$userMapper->update($currentUser)) {
480
                if(!$userMapper->update($currentUser)) {
484
                    return new JsonModel([
481
                    return new JsonModel([
485
                        'success'   => false,
482
                        'success'   => false,
486
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
483
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
487
                    ]);
484
                    ]);
488
                }
Línea 485... Línea 489...
485
                }
489
 
Línea 486... Línea 490...
486
                
490
                $this->logger->info('Se borro el image  del usuario ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
487
                
-
 
488
                
-
 
489
            } else {
-
 
490
                $form = new ChangeImageForm($this->config);
-
 
491
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
492
                
491
            }
493
                $form->setData($data);
492
 
494
                
-
 
495
                if($form->isValid()) {
-
 
496
                    
-
 
497
                    $files = $request->getFiles()->toArray();
-
 
498
                    if(!empty($files['image']['error'])) {
-
 
Línea 499... Línea -...
499
                        
-
 
500
                        return new JsonModel([
-
 
501
                            'success'   => false,
-
 
502
                            'data'   =>  'ERROR_UPLOAD_FILE'
-
 
503
                        ]);
493
            if($operation == 'upload') {
504
                        
-
 
505
                        
-
 
506
                    }
-
 
507
                    
494
                $form = new ChangeImageForm($this->config);
508
 
-
 
509
                    if($currentUser->image) {
-
 
510
                       
-
 
511
                        if(!$image->getStorage()->deleteFile($target_path, $currentUser->uuid, $currentUser->image)) {
-
 
512
                           return new JsonModel([
-
 
513
                                'success'   => false,
495
                $data 	= array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
514
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
515
                            ]);
-
 
516
                        }
-
 
517
                    }
-
 
518
                    
-
 
519
                    
-
 
520
                    list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
-
 
521
                    
-
 
522
           
496
                
Línea -... Línea 497...
-
 
497
                $form->setData($data);
-
 
498
                
-
 
499
                if(!$form->isValid()) {
-
 
500
                    $messages = [];
-
 
501
                    $form_messages = (array) $form->getMessages();
Línea 523... Línea 502...
523
                    $target_filename    = 'user-' . uniqid() . '.png';
502
 
524
                    $source             = $files['image']['tmp_name'];
503
                    foreach($form_messages  as $fieldname => $field_messages)
-
 
504
                    {
-
 
505
                        $messages[$fieldname] = array_values($field_messages);
-
 
506
                    }
-
 
507
                    
-
 
508
                    return new JsonModel([
-
 
509
                        'success'   => false,
525
                    $unlink_source      = false;
510
                        'data'   => $messages
-
 
511
                    ]);
-
 
512
                }
-
 
513
                    
-
 
514
                $storage->setFiles($request->getFiles()->toArray());
-
 
515
                if(!$storage->setCurrentFilename('image')) {
526
                    $crop_to_dimensions = true;
516
                    $this->logger->err('Error al subir la imagen del usuario', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
527
                    if(!$image->uploadProcessChangeSize($source, $target_path, $currentUser->uuid, $target_filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
517
                    return new JsonModel([
528
                        return new JsonModel([
518
                        'success'   => false,
529
                            'success'   => false,
519
                        'data'   =>  'ERROR_UPLOAD_FILE'
530
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
531
                        ]);
-
 
532
                    }
-
 
533
                    
-
 
534
                    
-
 
535
                    $currentUser->image = $target_filename;
-
 
536
                    if(!$userMapper->updateImage($currentUser)) {
-
 
537
                           
-
 
538
                        return new JsonModel([
-
 
539
                            'success'   => false,
-
 
540
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
541
                        ]);
-
 
542
                    } else {
-
 
543
                        $unlink_source      = true;
-
 
544
                        $target_filename    = 'user-profile-' . uniqid() . '.png';
-
 
545
                        
-
 
546
                        if(!$image->uploadProcessChangeSize($source, $target_path, $currentUser->uuid, $target_filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
-
 
547
                            return new JsonModel([
-
 
548
                                'success'   => false,
-
 
549
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
550
                            ]);
-
 
551
                        }
-
 
552
                       
-
 
553
                        
-
 
554
                        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
-
 
555
                        $userProfile = $userProfileMapper->fetchOnePublicByUserId($currentUser->id);
-
 
556
                        
-
 
557
                        if($userProfile) {
-
 
558
                            $userProfile->image = $currentUser->image;
-
 
559
                            $userProfileMapper->updateImage($userProfile);
-
 
560
                        }
-
 
561
                        
-
 
562
                    }
520
                    ]);
-
 
521
                }
563
                    
522
                
-
 
523
                // Si el usuario tiene image se borra
-
 
524
                if($currentUser->image) {
-
 
525
                    // Si el usuario tiene image, se borra
-
 
526
                    if(!$storage->deleteFile($target_path, $currentUser->uuid, $currentUser->image)) {
-
 
527
                        $this->logger->err('Error al borrar la imagen del usuario', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
528
                        return new JsonModel([
-
 
529
                            'success'   => false,
-
 
530
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
531
                        ]);
564
                    
532
                    }
565
                    
533
                }
-
 
534
                
-
 
535
                // Obtener el tamaño de la imagen
-
 
536
                list( $target_width, $target_height ) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
-
 
537
                
-
 
538
                // Obtener el archivo temporal
-
 
539
                $source_filename = $storage->getTmpFilename();
-
 
540
                $target_filename = $storage->getFilename();
-
 
541
 
566
                    $this->logger->info('Se actualizo el image del usuario', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
542
                if(!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
-
 
543
                    $this->logger->err('Error al subir la imagen del usuario', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
544
                    return new JsonModel([
-
 
545
                        'success'   => false,
-
 
546
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
547
                    ]);
-
 
548
                }
-
 
549
                
-
 
550
                
-
 
551
                $currentUser->image = $target_filename;
567
                    
552
                if(!$userMapper->updateImage($currentUser)) {
568
                } else {
553
                    return new JsonModel([
-
 
554
                        'success'   => false,
-
 
555
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
556
                    ]);
-
 
557
                }
-
 
558
                
-
 
559
                if(!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
-
 
560
                    $this->logger->err('Error al subir la imagen del usuario', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
561
                    return new JsonModel([
-
 
562
                        'success'   => false,
-
 
563
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
569
                    $messages = [];
564
                    ]);
Línea 570... Línea -...
570
                    $form_messages = (array) $form->getMessages();
-
 
571
                    foreach($form_messages  as $fieldname => $field_messages)
-
 
Línea 572... Línea 565...
572
                    {
565
                }
573
                        $messages[$fieldname] = array_values($field_messages);
566
                
574
                    }
567
                $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
575
                    
-
 
576
                    return new JsonModel([
568
                $userProfile = $userProfileMapper->fetchOnePublicByUserId($currentUser->id);
577
                        'success'   => false,
569
                
578
                        'data'   => $messages
-
 
579
                    ]);
-
 
580
                }
-
 
581
            }
-
 
582
            
-
 
583
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
584
            
-
 
585
            
-
 
586
            return new JsonModel([
-
 
587
                'success'   => true,
570
                if($userProfile) {
Línea 588... Línea 571...
588
                'data' =>  $storage->getUserImage(currentUser)
571
                    $userProfile->image = $currentUser->image;