From 8ef48797c33624ff3d8cad2439847cba9bde5670 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Tue, 13 Mar 2018 06:55:44 -0300 Subject: [PATCH] added location images endpoints POST /api/v1/summits/{id}/locations/{location_id}/images Content Type multipart/form-data' * file (required) * name (required|string|max:255) * description (required|string) PUT /api/v1/summits/{id}/locations/{location_id}/images/{image_id} Content Type multipart/form-data' * file (sometimes) * name (sometimes|string|max:255) * description (sometimes|string) * order (sometimes|integer|ming:1) GET /api/v1/summits/{id}/locations/{location_id}/images/{image_id} DELETE /api/v1/summits/{id}/locations/{location_id}/images/{image_id} Change-Id: I71d0b234d267aed0791dfafa0c91e842f5064aca --- .../OAuth2SummitLocationsApiController.php | 645 ++++++++++++------ app/Http/routes.php | 10 + .../Factories/SummitLocationBannerFactory.php | 9 +- .../Locations/SummitGeoLocatedLocation.php | 53 +- app/Services/Model/ILocationService.php | 33 + app/Services/Model/LocationService.php | 285 +++++++- resources/lang/en/not_found_errors.php | 8 +- resources/lang/en/validation_errors.php | 6 + 8 files changed, 827 insertions(+), 222 deletions(-) diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php index 649bf5ad..453c81ab 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php @@ -33,6 +33,7 @@ use models\summit\ISummitRepository; use models\summit\Summit; use models\summit\SummitAirport; use models\summit\SummitExternalLocation; +use models\summit\SummitGeoLocatedLocation; use models\summit\SummitHotel; use models\summit\SummitVenue; use models\summit\SummitVenueRoom; @@ -768,217 +769,6 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController } } - /** - * @param $summit_id - * @param $location_id - * @param $map_id - * @return mixed - */ - public function getLocationMap($summit_id, $location_id, $map_id){ - try { - - $expand = Request::input('expand', ''); - $relations = Request::input('relations', ''); - $relations = !empty($relations) ? explode(',', $relations) : []; - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) return $this->error404(); - - $location = $summit->getLocation($location_id); - if (is_null($location)) { - return $this->error404(); - } - - if (!Summit::isPrimaryLocation($location)) { - return $this->error404(); - } - - $map = $location->getMap($map_id); - if (is_null($map)) { - return $this->error404(); - } - - return $this->ok(SerializerRegistry::getInstance()->getSerializer($map)->serialize($expand,[], $relations)); - - } catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - /** - * @param LaravelRequest $request - * @param $summit_id - * @param $location_id - * @return mixed - */ - public function addLocationMap(LaravelRequest $request, $summit_id, $location_id){ - - try { - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) return $this->error404(); - - $file = $request->file('file'); - if(is_null($file)) - throw new ValidationException('file is required.'); - - $metadata = $request->all(); - - $rules = SummitLocationImageValidationRulesFactory::build(); - // Creates a Validator instance and validates the data. - $validation = Validator::make($metadata, $rules); - - if ($validation->fails()) { - $messages = $validation->messages()->toArray(); - - return $this->error412 - ( - $messages - ); - } - - $map = $this->location_service->addLocationMap - ( - $summit, - $location_id, - HTMLCleaner::cleanData - ( - $metadata, ['description'] - ), - $file - ); - - return $this->created(SerializerRegistry::getInstance()->getSerializer($map)->serialize()); - } - catch (EntityNotFoundException $ex1) { - Log::warning($ex1); - return $this->error404(); - } - catch(ValidationException $ex2) - { - Log::warning($ex2); - return $this->error412(array($ex2->getMessage())); - } - catch(\HTTP401UnauthorizedException $ex3) - { - Log::warning($ex3); - return $this->error401(); - } - catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - - /** - * @param LaravelRequest $request - * @param $summit_id - * @param $location_id - * @param $map_id - * @return mixed - */ - public function updateLocationMap(LaravelRequest $request, $summit_id, $location_id, $map_id){ - - try { - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) return $this->error404(); - - $content_type = $request->headers->has('Content-Type') ? strtolower( $request->headers->get('Content-Type')) : null; - - if (false !== $pos = strpos($content_type, ';')) { - $content_type = substr($content_type, 0, $pos); - } - - if(!strstr($content_type, 'multipart/form-data')) - return $this->error403(); - - $multiPartRequestParser = new ParseMultiPartFormDataInputStream(); - $input = $multiPartRequestParser->getInput(); - $metadata = $input['parameters']; - $files = $input['files']; - $file = null; - - if(count($files) > 0) - $file = $files[0]; - - $rules = SummitLocationImageValidationRulesFactory::build(true); - // Creates a Validator instance and validates the data. - $validation = Validator::make($metadata, $rules); - - if ($validation->fails()) { - $messages = $validation->messages()->toArray(); - - return $this->error412 - ( - $messages - ); - } - - $map = $this->location_service->updateLocationMap - ( - $summit, - $location_id, - $map_id, - HTMLCleaner::cleanData - ( - $metadata, ['description'] - ), - $file - ); - - return $this->updated(SerializerRegistry::getInstance()->getSerializer($map)->serialize()); - } - catch (EntityNotFoundException $ex1) { - Log::warning($ex1); - return $this->error404(); - } - catch(ValidationException $ex2) - { - Log::warning($ex2); - return $this->error412(array($ex2->getMessage())); - } - catch(\HTTP401UnauthorizedException $ex3) - { - Log::warning($ex3); - return $this->error401(); - } - catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - - /** - * @param $summit_id - * @param $location_id - * @param $map_id - * @return mixed - */ - public function deleteLocationMap($summit_id, $location_id, $map_id){ - try { - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) return $this->error404(); - $this->location_service->deleteLocationMap($summit, $location_id, $map_id); - return $this->deleted(); - } - catch (EntityNotFoundException $ex1) { - Log::warning($ex1); - return $this->error404(); - } - catch(ValidationException $ex2) - { - Log::warning($ex2); - return $this->error412(array($ex2->getMessage())); - } - catch(\HTTP401UnauthorizedException $ex3) - { - Log::warning($ex3); - return $this->error401(); - } - catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - /** * @param $summit_id * @return mixed @@ -2023,4 +1813,437 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController return $this->error500($ex); } } + + /** + * Location Maps endpoints + */ + + + /** + * @param $summit_id + * @param $location_id + * @param $map_id + * @return mixed + */ + public function getLocationMap($summit_id, $location_id, $map_id){ + try { + + $expand = Request::input('expand', ''); + $relations = Request::input('relations', ''); + $relations = !empty($relations) ? explode(',', $relations) : []; + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $location = $summit->getLocation($location_id); + if (is_null($location)) { + return $this->error404(); + } + + if (!Summit::isPrimaryLocation($location)) { + return $this->error404(); + } + + $map = $location->getMap($map_id); + if (is_null($map)) { + return $this->error404(); + } + + return $this->ok(SerializerRegistry::getInstance()->getSerializer($map)->serialize($expand,[], $relations)); + + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + /** + * @param LaravelRequest $request + * @param $summit_id + * @param $location_id + * @return mixed + */ + public function addLocationMap(LaravelRequest $request, $summit_id, $location_id){ + + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $file = $request->file('file'); + if(is_null($file)) + throw new ValidationException('file is required.'); + + $metadata = $request->all(); + + $rules = SummitLocationImageValidationRulesFactory::build(); + // Creates a Validator instance and validates the data. + $validation = Validator::make($metadata, $rules); + + if ($validation->fails()) { + $messages = $validation->messages()->toArray(); + + return $this->error412 + ( + $messages + ); + } + + $map = $this->location_service->addLocationMap + ( + $summit, + $location_id, + HTMLCleaner::cleanData + ( + $metadata, ['description'] + ), + $file + ); + + return $this->created(SerializerRegistry::getInstance()->getSerializer($map)->serialize()); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch(ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412(array($ex2->getMessage())); + } + catch(\HTTP401UnauthorizedException $ex3) + { + Log::warning($ex3); + return $this->error401(); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param LaravelRequest $request + * @param $summit_id + * @param $location_id + * @param $map_id + * @return mixed + */ + public function updateLocationMap(LaravelRequest $request, $summit_id, $location_id, $map_id){ + + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $content_type = $request->headers->has('Content-Type') ? strtolower( $request->headers->get('Content-Type')) : null; + + if (false !== $pos = strpos($content_type, ';')) { + $content_type = substr($content_type, 0, $pos); + } + + if(!strstr($content_type, 'multipart/form-data')) + return $this->error403(); + + $multiPartRequestParser = new ParseMultiPartFormDataInputStream(); + $input = $multiPartRequestParser->getInput(); + $metadata = $input['parameters']; + $files = $input['files']; + $file = null; + + if(count($files) > 0) + $file = $files[0]; + + $rules = SummitLocationImageValidationRulesFactory::build(true); + // Creates a Validator instance and validates the data. + $validation = Validator::make($metadata, $rules); + + if ($validation->fails()) { + $messages = $validation->messages()->toArray(); + + return $this->error412 + ( + $messages + ); + } + + $map = $this->location_service->updateLocationMap + ( + $summit, + $location_id, + $map_id, + HTMLCleaner::cleanData + ( + $metadata, ['description'] + ), + $file + ); + + return $this->updated(SerializerRegistry::getInstance()->getSerializer($map)->serialize()); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch(ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412(array($ex2->getMessage())); + } + catch(\HTTP401UnauthorizedException $ex3) + { + Log::warning($ex3); + return $this->error401(); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @param $location_id + * @param $map_id + * @return mixed + */ + public function deleteLocationMap($summit_id, $location_id, $map_id){ + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + $this->location_service->deleteLocationMap($summit, $location_id, $map_id); + return $this->deleted(); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch(ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412(array($ex2->getMessage())); + } + catch(\HTTP401UnauthorizedException $ex3) + { + Log::warning($ex3); + return $this->error401(); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * Location Images endpoints + */ + + /** + * @param $summit_id + * @param $location_id + * @param $image_id + * @return mixed + */ + public function getLocationImage($summit_id, $location_id, $image_id){ + try { + + $expand = Request::input('expand', ''); + $relations = Request::input('relations', ''); + $relations = !empty($relations) ? explode(',', $relations) : []; + + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $location = $summit->getLocation($location_id); + if (is_null($location)) { + return $this->error404(); + } + + if(!$location instanceof SummitGeoLocatedLocation){ + return $this->error404(); + } + + $image = $location->getImage($image_id); + if (is_null($image)) { + return $this->error404(); + } + + return $this->ok(SerializerRegistry::getInstance()->getSerializer($image)->serialize($expand,[], $relations)); + + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + /** + * @param LaravelRequest $request + * @param $summit_id + * @param $location_id + * @return mixed + */ + public function addLocationImage(LaravelRequest $request, $summit_id, $location_id){ + + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $file = $request->file('file'); + if(is_null($file)) + throw new ValidationException('file is required.'); + + $metadata = $request->all(); + + $rules = SummitLocationImageValidationRulesFactory::build(); + // Creates a Validator instance and validates the data. + $validation = Validator::make($metadata, $rules); + + if ($validation->fails()) { + $messages = $validation->messages()->toArray(); + + return $this->error412 + ( + $messages + ); + } + + $image = $this->location_service->addLocationImage + ( + $summit, + $location_id, + HTMLCleaner::cleanData + ( + $metadata, ['description'] + ), + $file + ); + + return $this->created(SerializerRegistry::getInstance()->getSerializer($image)->serialize()); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch(ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412(array($ex2->getMessage())); + } + catch(\HTTP401UnauthorizedException $ex3) + { + Log::warning($ex3); + return $this->error401(); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param LaravelRequest $request + * @param $summit_id + * @param $location_id + * @param $image_id + * @return mixed + */ + public function updateLocationImage(LaravelRequest $request, $summit_id, $location_id, $image_id){ + + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $content_type = $request->headers->has('Content-Type') ? strtolower( $request->headers->get('Content-Type')) : null; + + if (false !== $pos = strpos($content_type, ';')) { + $content_type = substr($content_type, 0, $pos); + } + + if(!strstr($content_type, 'multipart/form-data')) + return $this->error403(); + + $multiPartRequestParser = new ParseMultiPartFormDataInputStream(); + $input = $multiPartRequestParser->getInput(); + $metadata = $input['parameters']; + $files = $input['files']; + $file = null; + + if(count($files) > 0) + $file = $files[0]; + + $rules = SummitLocationImageValidationRulesFactory::build(true); + // Creates a Validator instance and validates the data. + $validation = Validator::make($metadata, $rules); + + if ($validation->fails()) { + $messages = $validation->messages()->toArray(); + + return $this->error412 + ( + $messages + ); + } + + $image = $this->location_service->updateLocationImage() + ( + $summit, + $location_id, + $image_id, + HTMLCleaner::cleanData + ( + $metadata, ['description'] + ), + $file + ); + + return $this->updated(SerializerRegistry::getInstance()->getSerializer($image)->serialize()); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch(ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412(array($ex2->getMessage())); + } + catch(\HTTP401UnauthorizedException $ex3) + { + Log::warning($ex3); + return $this->error401(); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @param $location_id + * @param $image_id + * @return mixed + */ + public function deleteLocationImage($summit_id, $location_id, $image_id){ + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + $this->location_service->deleteLocationImage($summit, $location_id, $image_id); + return $this->deleted(); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch(ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412(array($ex2->getMessage())); + } + catch(\HTTP401UnauthorizedException $ex3) + { + Log::warning($ex3); + return $this->error401(); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index ff0f68a1..724cb820 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -361,6 +361,16 @@ Route::group([ }); }); + // locations images + Route::group(['prefix' => 'images'], function () { + Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@addLocationImage']); + Route::group(['prefix' => '{image_id}'], function () { + Route::get('', 'OAuth2SummitLocationsApiController@getLocationImage'); + Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@updateLocationImage']); + Route::delete('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@deleteLocationImage']); + }); + }); + Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@updateLocation']); Route::delete('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@deleteLocation']); Route::get('/events/published','OAuth2SummitLocationsApiController@getLocationPublishedEvents')->where('location_id', 'tbd|[0-9]+'); diff --git a/app/Models/Foundation/Summit/Factories/SummitLocationBannerFactory.php b/app/Models/Foundation/Summit/Factories/SummitLocationBannerFactory.php index 29a4178a..4fc17922 100644 --- a/app/Models/Foundation/Summit/Factories/SummitLocationBannerFactory.php +++ b/app/Models/Foundation/Summit/Factories/SummitLocationBannerFactory.php @@ -132,7 +132,14 @@ final class SummitLocationBannerFactory * @param array $data * @return ScheduledSummitLocationBanner|SummitLocationBanner */ - public static function populate(Summit $summit, SummitAbstractLocation $location, SummitLocationBanner $banner, array $data){ + public static function populate + ( + Summit $summit, + SummitAbstractLocation $location, + SummitLocationBanner $banner, + array $data + ) + { if($banner instanceof ScheduledSummitLocationBanner){ return self::populateScheduledSummitLocationBanner($summit, $location, $banner, $data); diff --git a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php index dfed7b2a..39af8637 100644 --- a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php +++ b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php @@ -386,7 +386,9 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation $res = $this->images ->matching ( - Criteria::create()->where(Criteria::expr()->eq("id", $image_id)) + Criteria::create() + ->where(Criteria::expr()->eq("id", $image_id)) + ->andWhere(Criteria::expr()->eq("class_name", SummitLocationImage::TypeImage)) )->first(); return $res === false ? null : $res; @@ -441,6 +443,17 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation $image->setLocation($this); } + + /** + * @param SummitLocationImage $image + * @return $this + */ + public function removeImage(SummitLocationImage $image){ + $this->images->removeElement($image); + $image->ClearLocation(); + return $this; + } + /** * @param SummitLocationImage $map * @param $new_order @@ -450,7 +463,7 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation $maps = $this->getMaps(); $maps = array_slice($maps,0, count($maps), false); $max_order = count($maps); - $former_order = 1; + $former_order = 1; foreach ($maps as $m){ if($m->getId() == $map->getId()) break; @@ -476,4 +489,40 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation } } + /** + * @param SummitLocationImage $image + * @param $new_order + * @throws ValidationException + */ + public function recalculateImageOrder(SummitLocationImage $image, $new_order){ + + $images = $this->getImages(); + $images = array_slice($images,0, count($images), false); + $max_order = count($images); + $former_order = 1; + + foreach ($images as $i){ + if($i->getId() == $image->getId()) break; + $former_order++; + } + + if($new_order > $max_order) + throw new ValidationException(sprintf("max order is %s", $max_order)); + + unset($images[$former_order - 1]); + + $images = array_merge + ( + array_slice($images, 0, $new_order -1 , true) , + [$image] , + array_slice($images, $new_order -1 , count($images), true) + ); + + $order = 1; + foreach($images as $i){ + $i->setOrder($order); + $order++; + } + } + } \ No newline at end of file diff --git a/app/Services/Model/ILocationService.php b/app/Services/Model/ILocationService.php index c9e184de..8c919722 100644 --- a/app/Services/Model/ILocationService.php +++ b/app/Services/Model/ILocationService.php @@ -180,4 +180,37 @@ interface ILocationService */ public function deleteLocationMap(Summit $summit, $location_id, $map_id); + /** + * @param Summit $summit + * @param int $location_id + * @param array $metadata + * @param $file + * @return SummitLocationImage + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function addLocationImage(Summit $summit, $location_id, array $metadata, UploadedFile $file); + + /** + * @param Summit $summit + * @param int $location_id + * @param int $image_id + * @param array $metadata + * @param $file + * @return SummitLocationImage + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function updateLocationImage(Summit $summit, $location_id, $image_id, array $metadata, UploadedFile $file); + + /** + * @param Summit $summit + * @param int $location_id + * @param int $image_id + * @return SummitAbstractLocation + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function deleteLocationImage(Summit $summit, $location_id, $image_id); + } \ No newline at end of file diff --git a/app/Services/Model/LocationService.php b/app/Services/Model/LocationService.php index 7253f8ed..b9dcbd08 100644 --- a/app/Services/Model/LocationService.php +++ b/app/Services/Model/LocationService.php @@ -1214,7 +1214,7 @@ final class LocationService implements ILocationService throw new EntityNotFoundException ( trans( - 'not_found_errors.LocationService.addLocationMap.LocationNotFound', + 'not_found_errors.LocationService.updateLocationMap.LocationNotFound', [ 'location_id' => $location_id, ] @@ -1226,7 +1226,7 @@ final class LocationService implements ILocationService throw new EntityNotFoundException ( trans( - 'not_found_errors.LocationService.addLocationMap.LocationNotFound', + 'not_found_errors.LocationService.updateLocationMap.LocationNotFound', [ 'location_id' => $location_id, ] @@ -1240,7 +1240,7 @@ final class LocationService implements ILocationService throw new EntityNotFoundException ( trans( - 'not_found_errors.LocationService.addLocationMap.MapNotFound', + 'not_found_errors.LocationService.updateLocationMap.MapNotFound', [ 'map_id' => $map_id, 'location_id' => $location_id, @@ -1254,7 +1254,7 @@ final class LocationService implements ILocationService throw new ValidationException ( trans( - 'validation_errors.LocationService.addLocationMap.FileNotAllowedExtension', + 'validation_errors.LocationService.updateLocationMap.FileNotAllowedExtension', [ 'allowed_extensions' => implode(", ", $allowed_extensions), ] @@ -1267,7 +1267,7 @@ final class LocationService implements ILocationService ( trans ( - 'validation_errors.LocationService.addLocationMap.FileMaxSize', + 'validation_errors.LocationService.updateLocationMap.FileMaxSize', [ 'max_file_size' => (($max_file_size / 1024) / 1024) ] @@ -1353,8 +1353,8 @@ final class LocationService implements ILocationService ( 'not_found_errors.LocationService.deleteLocationMap.MapNotFound', [ - 'location_id' => $location_id, - 'banner_id' => $map_id, + 'location_id' => $location_id, + 'map_id' => $map_id, ] ) ); @@ -1375,4 +1375,275 @@ final class LocationService implements ILocationService }); } + + /** + * @param Summit $summit + * @param int $location_id + * @param array $metadata + * @param $file + * @return SummitLocationImage + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function addLocationImage(Summit $summit, $location_id, array $metadata, UploadedFile $file) + { + $image = $this->tx_service->transaction(function () use ($summit, $location_id, $metadata, $file) { + $max_file_size = config('file_upload.max_file_upload_size') ; + $allowed_extensions = ['png','jpg','jpeg','gif','pdf']; + $location = $summit->getLocation($location_id); + + if (is_null($location)) { + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.addLocationImage.LocationNotFound', + [ + 'location_id' => $location_id, + ] + ) + ); + } + + if(!$location instanceof SummitGeoLocatedLocation){ + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.addLocationImage.LocationNotFound', + [ + 'location_id' => $location_id, + ] + ) + ); + } + + if(!in_array($file->extension(), $allowed_extensions)){ + throw new ValidationException + ( + trans( + 'validation_errors.LocationService.addLocationImage.FileNotAllowedExtension', + [ + 'allowed_extensions' => implode(", ", $allowed_extensions), + ] + ) + ); + } + + if($file->getSize() > $max_file_size) + { + throw new ValidationException + ( + trans + ( + 'validation_errors.LocationService.addLocationImage.FileMaxSize', + [ + 'max_file_size' => (($max_file_size/1024)/1024) + ] + ) + ); + } + + $uploader = new FileUploader($this->folder_repository); + $pic = $uploader->build($file, sprintf('summits/%s/locations/%s/images/', $location->getSummitId(), $location->getId()), true); + $image = SummitLocationImageFactory::buildImage($metadata); + $image->setPicture($pic); + $location->addImage($image); + return $image; + }); + + Event::fire + ( + new LocationImageInserted + ( + $image->getId(), + $image->getLocationId(), + $image->getLocation()->getSummitId(), + $image->getClassName() + ) + ); + + return $image; + } + + /** + * @param Summit $summit + * @param int $location_id + * @param int $image_id + * @param array $metadata + * @param $file + * @return SummitLocationImage + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function updateLocationImage(Summit $summit, $location_id, $image_id, array $metadata, UploadedFile $file) + { + return $this->tx_service->transaction(function () use ($summit, $location_id, $image_id, $metadata, $file) { + $max_file_size = config('file_upload.max_file_upload_size') ; + $allowed_extensions = ['png','jpg','jpeg','gif','pdf']; + $location = $summit->getLocation($location_id); + + if (is_null($location)) { + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.updateLocationImage.LocationNotFound', + [ + 'location_id' => $location_id, + ] + ) + ); + } + + if(!$location instanceof SummitGeoLocatedLocation){ + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.updateLocationImage.LocationNotFound', + [ + 'location_id' => $location_id, + ] + ) + ); + } + + $image = $location->getImage($image_id); + + if (is_null($image)) { + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.updateLocationImage.ImageNotFound', + [ + 'image_id' => $image, + 'location_id' => $location_id, + ] + ) + ); + } + + if(!is_null($file)) { + if (!in_array($file->extension(), $allowed_extensions)) { + throw new ValidationException + ( + trans( + 'validation_errors.LocationService.updateLocationImage.FileNotAllowedExtension', + [ + 'allowed_extensions' => implode(", ", $allowed_extensions), + ] + ) + ); + } + + if ($file->getSize() > $max_file_size) { + throw new ValidationException + ( + trans + ( + 'validation_errors.LocationService.updateLocationImage.FileMaxSize', + [ + 'max_file_size' => (($max_file_size / 1024) / 1024) + ] + ) + ); + } + + $uploader = new FileUploader($this->folder_repository); + $pic = $uploader->build($file, sprintf('summits/%s/locations/%s/images/', $location->getSummitId(), $location->getId()), true); + $image->setPicture($pic); + } + + $image = SummitLocationImageFactory::populate($image, $metadata); + + if (isset($metadata['order']) && intval($metadata['order']) != $image->getOrder()) { + // request to update order + $location->recalculateImageOrder($image, intval($metadata['order'])); + } + + Event::fire + ( + new LocationImageUpdated + ( + $image->getId(), + $image->getLocationId(), + $image->getLocation()->getSummitId(), + $image->getClassName() + ) + ); + + return $image; + }); + } + + /** + * @param Summit $summit + * @param int $location_id + * @param int $image_id + * @return SummitAbstractLocation + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function deleteLocationImage(Summit $summit, $location_id, $image_id) + { + return $this->tx_service->transaction(function () use ($summit, $location_id, $image_id) { + + $location = $summit->getLocation($location_id); + + if(is_null($location)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.deleteLocationImage.LocationNotFound', + [ + 'summit_id' => $summit->getId(), + 'location_id' => $location_id, + ] + ) + ); + } + + if(!$location instanceof SummitGeoLocatedLocation){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.deleteLocationImage.LocationNotFound', + [ + 'summit_id' => $summit->getId(), + 'location_id' => $location_id, + ] + ) + ); + } + + $image = $location->getImage($image_id); + + if(is_null($image)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.deleteLocationImage.ImageNotFound', + [ + 'location_id' => $location_id, + 'image_id' => $image_id, + ] + ) + ); + } + + Event::fire + ( + new LocationImageDeleted + ( + $image->getId(), + $image->getLocationId(), + $image->getLocation()->getSummitId(), + $image->getClassName() + ) + ); + + $location->removeImage($image); + + }); + } } \ No newline at end of file diff --git a/resources/lang/en/not_found_errors.php b/resources/lang/en/not_found_errors.php index 4def83c3..ea3ead9f 100644 --- a/resources/lang/en/not_found_errors.php +++ b/resources/lang/en/not_found_errors.php @@ -32,7 +32,13 @@ return [ 'LocationService.updateLocationBanner.LocationNotFound' => 'location :location_id not found on summit :summit_id', 'LocationService.updateLocationBanner.BannerNotFound'=> 'banner :banner_id not found on location :location_id', 'LocationService.addLocationMap.LocationNotFound' => 'location :location_id not found on summit :summit_id', - 'LocationService.addLocationMap.MapNotFound' => 'map :map_id does not belongs to location :location_id', + 'LocationService.updateLocationMap.LocationNotFound' => 'location :location_id not found on summit :summit_id', + 'LocationService.updateLocationMap.MapNotFound' => 'map :map_id does not belongs to location :location_id', 'LocationService.deleteLocationMap.LocationNotFound' => 'location :location_id not found on summit :summit_id', 'LocationService.deleteLocationMap.MapNotFound' => 'map :map_id not found on location :location_id', + 'LocationService.addLocationImage.LocationNotFound' => 'location :location_id not found on summit :summit_id', + 'LocationService.updateLocationImage.LocationNotFound' => 'location :location_id not found on summit :summit_id', + 'LocationService.updateLocationImage.ImageNotFound' => 'image :image_id does not belongs to location :location_id', + 'LocationService.deleteLocationImage.LocationNotFound' => 'location :location_id not found on summit :summit_id', + 'LocationService.deleteLocationImage.ImageNotFound' => 'image :image_id not found on location :location_id', ]; \ No newline at end of file diff --git a/resources/lang/en/validation_errors.php b/resources/lang/en/validation_errors.php index e858ae3d..557f344d 100644 --- a/resources/lang/en/validation_errors.php +++ b/resources/lang/en/validation_errors.php @@ -45,4 +45,10 @@ return [ 'LocationService.addLocationBanner.InvalidClassName' => 'invalid class name', 'LocationService.addLocationMap.FileNotAllowedExtension' => 'file extension is not allowed (:allowed_extensions)', 'LocationService.addLocationMap.FileMaxSize' => 'file exceeds max_file_size (:max_file_size MB)', + 'LocationService.updateLocationMap.FileNotAllowedExtension' => 'file extension is not allowed (:allowed_extensions)', + 'LocationService.updateLocationMap.FileMaxSize' => 'file exceeds max_file_size (:max_file_size MB)', + 'LocationService.addLocationImage.FileNotAllowedExtension' => 'file extension is not allowed (:allowed_extensions)', + 'LocationService.addLocationImage.FileMaxSize' => 'file exceeds max_file_size (:max_file_size MB)', + 'LocationService.updateLocationImage.FileNotAllowedExtension' => 'file extension is not allowed (:allowed_extensions)', + 'LocationService.updateLocationImage.FileMaxSize' => 'file exceeds max_file_size (:max_file_size MB)', ]; \ No newline at end of file