From 592846b56c1ea237a37a75f9b22a2c23c412a552 Mon Sep 17 00:00:00 2001 From: Olivier Legat Date: Sat, 22 Jan 2022 18:32:04 +0100 Subject: [PATCH] use HasTranslation on Encarts Model, Trait for CastsEnums and HasTranslations compatibility Slides create picture storage --- app/Http/Controllers/Bo/EncartsController.php | 4 ++ app/Http/Controllers/Bo/SlidesController.php | 7 +++- app/Models/Encart.php | 16 +++++-- app/Traits/CastsEnumsHasTranslations.php | 42 +++++++++++++++++++ config/app.php | 2 +- database/factories/EncartFactory.php | 8 +++- resources/views/back/encarts/edit.blade.php | 2 +- 7 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 app/Traits/CastsEnumsHasTranslations.php diff --git a/app/Http/Controllers/Bo/EncartsController.php b/app/Http/Controllers/Bo/EncartsController.php index 2343715..6cc4f17 100644 --- a/app/Http/Controllers/Bo/EncartsController.php +++ b/app/Http/Controllers/Bo/EncartsController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bo; use App\Http\Controllers\Controller; use App\Http\Requests\UpdateEncartRequest; +use App\Lib\Utils; use App\Models\Encart; class EncartsController extends Controller @@ -29,6 +30,9 @@ class EncartsController extends Controller public function update(Encart $encart, UpdateEncartRequest $request) { $encart->fill($request->validated()); + if (is_a($request->picture,"SplFileInfo")) { + $encart->picture = Utils::storeImage($request->picture, $encart->getTable()); + } if (!$encart->saveOrFail()) { return redirect()->route('bo.encarts.edit', $encart->id) diff --git a/app/Http/Controllers/Bo/SlidesController.php b/app/Http/Controllers/Bo/SlidesController.php index 529b2b8..d168b98 100644 --- a/app/Http/Controllers/Bo/SlidesController.php +++ b/app/Http/Controllers/Bo/SlidesController.php @@ -46,6 +46,9 @@ class SlidesController extends Controller $slide = new Slide($request->validated()); $slide->order = $this->getLastOrder(); + if (is_a($request->picture,"SplFileInfo")) { + $slide->picture = Utils::storeImage($request->picture, $slide->getTable()); + } $slide->saveOrFail(); @@ -87,7 +90,9 @@ class SlidesController extends Controller public function update(StoreSlideRequest $request, Slide $slide) { $slide->fill($request->validated()); - $slide->picture = Utils::storeImage($request->picture, $slide->getTable()); + if (is_a($request->picture,"SplFileInfo")) { + $slide->picture = Utils::storeImage($request->picture, $slide->getTable()); + } if (!$slide->saveOrFail()) { return redirect()->route('bo.slides.edit', $slide->id)->with('error', trans('La modification a échoué')); diff --git a/app/Models/Encart.php b/app/Models/Encart.php index ad4a3cb..c997309 100644 --- a/app/Models/Encart.php +++ b/app/Models/Encart.php @@ -4,15 +4,15 @@ namespace App\Models; use App\Enums\Page; use App\Lib\Utils; +use App\Traits\CastsEnumsHasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\File; -use Kwaadpepper\Enum\Traits\CastsEnums; class Encart extends Model { use HasFactory; - use CastsEnums; + use CastsEnumsHasTranslations; public $enumCasts = [ 'page' => Page::class @@ -26,16 +26,24 @@ class Encart extends Model 'alt_text_picture', ]; + public $translatable = [ + 'title', + 'subtitle', + 'text', + 'picture', + 'alt_text_picture', + ]; + protected static function boot() { static::creating(function (Encart $encart) { - if ($encart->picture) { + if (is_a($encart->picture,"SplFileInfo")) { $encart->picture = Utils::storeImage($encart->picture, $encart->getTable()); } }); static::updating(function (Encart $encart) { - if ($encart->picture) { + if (is_a($encart->picture,"SplFileInfo")) { $encart->picture = Utils::storeImage($encart->picture, $encart->getTable()); } }); diff --git a/app/Traits/CastsEnumsHasTranslations.php b/app/Traits/CastsEnumsHasTranslations.php new file mode 100644 index 0000000..bc934a7 --- /dev/null +++ b/app/Traits/CastsEnumsHasTranslations.php @@ -0,0 +1,42 @@ +hasEnumCast($key)) { + return $this->enumGetAttributeValue($key); + } + + return $this->transGetAttributeValue($key); + } + + /** + * Manually mix setAttribute from CastsEnums and HasTranslations + */ + public function setAttribute($key, $value) + { + if ($this->hasEnumCast($key)) { + return $this->enumSetAttribute($key, $value); + + } + + return $this->transSetAttribute($key,$value); + } +} diff --git a/config/app.php b/config/app.php index 5ccb2b5..e1f87fb 100644 --- a/config/app.php +++ b/config/app.php @@ -93,7 +93,7 @@ return [ | */ - 'fallback_locale' => 'en', + 'fallback_locale' => 'fr', /* |-------------------------------------------------------------------------- diff --git a/database/factories/EncartFactory.php b/database/factories/EncartFactory.php index 186323c..6ba1d58 100644 --- a/database/factories/EncartFactory.php +++ b/database/factories/EncartFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Lib\Utils; use App\Models\Encart; use Illuminate\Database\Eloquent\Factories\Factory; use SplFileInfo; @@ -23,11 +24,16 @@ class EncartFactory extends Factory */ public function definition() { + + $upload = new SplFileInfo(resource_path('assets/images/prez-img.jpg')); + $encart = new Encart(); + $imgpath = Utils::storeImage($upload, $encart->getTable()); + return [ 'title' => $this->faker->text(5), 'subtitle' => $this->faker->sentence(7, false), 'text' => $this->faker->paragraph(10, false), - 'picture' => new SplFileInfo(resource_path('assets/images/prez-img.jpg')), + 'picture' => $imgpath, 'alt_text_picture' => $this->faker->sentence(10), ]; } diff --git a/resources/views/back/encarts/edit.blade.php b/resources/views/back/encarts/edit.blade.php index 4f32498..caf64c2 100644 --- a/resources/views/back/encarts/edit.blade.php +++ b/resources/views/back/encarts/edit.blade.php @@ -36,7 +36,7 @@

- @if ($encart->picture !== null) + @if (!empty($encart->picture))

{{$encart->title}}