use HasTranslation on Encarts Model,
Trait for CastsEnums and HasTranslations compatibility Slides create picture storage
This commit is contained in:
parent
0b516617c9
commit
592846b56c
7 changed files with 73 additions and 8 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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é'));
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
});
|
||||
|
|
|
|||
42
app/Traits/CastsEnumsHasTranslations.php
Normal file
42
app/Traits/CastsEnumsHasTranslations.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||||
use Spatie\Translatable\HasTranslations;
|
||||
|
||||
trait CastsEnumsHasTranslations {
|
||||
|
||||
use CastsEnums, HasTranslations
|
||||
{
|
||||
CastsEnums::getAttributeValue as enumGetAttributeValue;
|
||||
CastsEnums::setAttribute as enumSetAttribute;
|
||||
HasTranslations::getAttributeValue as transGetAttributeValue;
|
||||
HasTranslations::setAttribute as transSetAttribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually mix getAttributeValue from CastsEnums and HasTranslations
|
||||
*/
|
||||
public function getAttributeValue($key)
|
||||
{
|
||||
if ($this->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);
|
||||
}
|
||||
}
|
||||
|
|
@ -93,7 +93,7 @@ return [
|
|||
|
|
||||
*/
|
||||
|
||||
'fallback_locale' => 'en',
|
||||
'fallback_locale' => 'fr',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@
|
|||
<div class="form-group">
|
||||
<label for="picture">{{ __('Image') }} <small class="text-muted">- JPEG/PNG, 2Mo max. Dimensions {{ Config::get('bo.encart_image_maxwidth') }} px par {{ Config::get('bo.encart_image_maxheight') }} px</small></label></br>
|
||||
<input type="file" id="picture" name="picture" accept="image/png, image/jpeg">
|
||||
@if ($encart->picture !== null)
|
||||
@if (!empty($encart->picture))
|
||||
<br />
|
||||
<br />
|
||||
<img src="{{$encart->picture}}" alt="{{$encart->title}}" style="max-height: 100px;">
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue