ldc-doux/app/Http/Requests/StoreProductRequest.php
2022-01-10 17:03:36 +01:00

197 lines
6.8 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class StoreProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
try {
$tabFour = array(
'id' => 0,
'name' => 'Four',
'url' => '/assets/images/four.png',
'time' => $this->time_cook0,
'temp' => $this->temp_cook0,
'status' => $this->is_activated0 ? true : false
);
$tabFriteuse = array(
'id' => 1,
'name' => 'Friteuse',
'url' => '/assets/images/friteuse.png',
'time' => $this->time_cook1,
'temp' => $this->temp_cook1,
'status' => $this->is_activated1 ? true : false
);
$tabPoele = array(
'id' => 2,
'name' => 'Poêle',
'url' => '/assets/images/poele.png',
'time' => $this->time_cook2,
'temp' => $this->temp_cook2,
'status' => $this->is_activated2 ? true : false
);
$tabCocotte = array(
'id' => 3,
'name' => 'Cocotte',
'url' => '/assets/images/cocotte.png',
'time' => $this->time_cook3,
'temp' => $this->temp_cook3,
'status' => $this->is_activated3 ? true : false
);
$tabPlancha = array(
'id' => 4,
'name' => 'Plancha',
'url' => '/assets/images/plancha.png',
'time' => $this->time_cook4,
'temp' => $this->temp_cook4,
'status' => $this->is_activated4 ? true : false
);
$tabBainMarie = array(
'id' => 5,
'name' => 'Bain Marie',
'url' => '/assets/images/bainmarie.png',
'time' => $this->time_cook5,
'temp' => $this->temp_cook5,
'status' => $this->is_activated5 ? true : false
);
$tabBarbecue = array(
'id' => 6,
'name' => 'Barbecue',
'url' => '/assets/images/barbecue.png',
'time' => $this->time_cook6,
'temp' => $this->temp_cook6,
'status' => $this->is_activated6 ? true : false
);
$tabMicroOndes = array(
'id' => 7,
'name' => 'Micro Ondes',
'url' => '/assets/images/microondes.png',
'time' => $this->time_cook7,
'temp' => $this->temp_cook7,
'status' => $this->is_activated7 ? true : false
);
$tabCuisson = json_encode([
'four' => $tabFour,
'friteuse' => $tabFriteuse,
'poele' => $tabPoele,
'cocotte' => $tabCocotte,
'plancha' => $tabPlancha,
'bainmarie' => $tabBainMarie,
'barbecue' => $tabBarbecue,
'microondes' => $tabMicroOndes
]);
$urlPlusProduit = "";
if ($this->pictogramme0) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme0->getClientOriginalName();
}
$tabPlusProduit1 = array(
'id' => 0,
'name' => $this->content0,
'url' => $urlPlusProduit,
);
$urlPlusProduit = "";
if ($this->pictogramme1) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme1->getClientOriginalName();
}
$tabPlusProduit2 = array(
'id' => 1,
'name' => $this->content1,
'url' => $urlPlusProduit,
);
$urlPlusProduit = "";
if ($this->pictogramme2) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme2->getClientOriginalName();
}
$tabPlusProduit3 = array(
'id' => 2,
'name' => $this->content2,
'url' => $urlPlusProduit,
);
$tabPlusProduit = json_encode([$tabPlusProduit1, $tabPlusProduit2, $tabPlusProduit3]);
$this->merge([
'slug' => Str::slug($this->name),
'gamme_id' => $this->gamme_id === '0' ? null : $this->gamme_id,
'pictos' => $tabCuisson,
'plusproduit' => $tabPlusProduit,
'is_published' => $this->is_published ? true : false,
'is_on_top' => $this->is_on_top ? true : false,
'is_food_service' => $this->is_food_service ? true : false,
'is_halal' => $this->is_halal ? true : false,
]);
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
} catch (InvalidFormatException $e) {
// On fait rien
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'gamme_id' => 'sometimes|nullable|exists:gammes,id',
'name' => 'required|max:255',
'slug' => 'required|unique:products',
'description' => 'required|min:20',
'picture' => 'required|image|mimes:png|max:1024|dimensions:width=509,height=509',
'alt_text_picture' => 'required|max:255',
'pictos' => 'sometimes|nullable',
'plusproduit' => 'sometimes|nullable',
'pictogramme0' => 'nullable|image|mimes:png|max:1024|dimensions:width=141,height=100',
'pictogramme1' => 'nullable|image|mimes:png|max:1024|dimensions:width=141,height=100',
'pictogramme2' => 'nullable|image|mimes:png|max:1024|dimensions:width=141,height=100',
'is_published' => 'nullable|boolean',
'is_on_top' => 'nullable|boolean',
'is_food_service' => 'nullable|boolean',
'is_halal' => 'nullable|boolean',
// 'seo_title' => 'required|max:255',
// 'seo_description' => 'required|max:255',
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'slug' => trans('libellé')
];
}
}