Files
getgreen-backend/app/Http/Requests/Dashboard/Product/Update.php
2026-04-28 17:53:06 +05:00

234 lines
5.1 KiB
PHP
Executable File

<?php
namespace App\Http\Requests\Dashboard\Product;
use App\Support\Uploads;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use App\Api\ImageResize;
use App\Models\Product;
use Illuminate\Support\Facades\Storage;
class Update extends FormRequest
{
public function rules()
{
if ($this->isMethod('get')) {
return [];
}
return [
'name' => 'array',
'name.*' => 'required',
'body' => 'array',
'body.*' => 'required',
'short_body' => 'array',
'short_body.*' => 'required|max:300',
'price' => 'required|numeric',
'price_discount' => 'nullable',
'brand_id' => 'required',
'category_id' => 'required|integer|exists:categories,id',
'popular' => 'nullable',
"calc" => [],
'leader_of_sales' => 'nullable',
'article_number' => 'required|unique:products,article_number,' . $this->product->id . ',id,deleted_at,NULL'
];
}
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
if ($this->isMethod('post')) {
$validator->sometimes('price', 'gt:price_discount', function ($input) {
return $input->price_discount > 0;
});
}
return $validator;
}
/**
* @return mixed
*/
public function getColors()
{
return $this->get('colors');
}
/**
* @return string
*/
public function getPoster(Product $product): string
{
if ($this->hasFile('poster')) {
Storage::delete($product->poster);
$folder = "uploads/posters/" . Carbon::now()->format('Y/m/d');
return Uploads::store($this->file('poster'), $folder);
}
return $product->poster;
}
/**
* @return string
*/
public function getCalc(Product $product): string|null
{
if ($this->hasFile('calc')) {
if ($product->calc) {
Storage::delete($product->calc);
}
$folder = "uploads/calc/" . Carbon::now()->format('Y/m/d');
return Uploads::store($this->file('calc'), $folder);
}
return $product->calc;
}
public function getDataSheet(Product $product)
{
if ($this->hasFile('data_sheet')) {
if ($product->data_sheet) {
Storage::delete($product->data_sheet);
}
$folder = "uploads/datasheet/" . Carbon::now()->format('Y/m/d');
return Uploads::store($this->file('data_sheet'), $folder);
}
return $product->data_sheet;
}
/**
* @return string
*/
public function getPosterThumb(Product $product): string
{
if ($this->hasFile('poster')) {
Storage::delete($product->poster_thumb);
$tempPath = Uploads::store($this->file('poster'), 'temp', 'public');
$resizer = new ImageResize();
return $resizer->resize($tempPath, 322, 'posters', true);
}
return $product->poster_thumb;
}
/**
* @return array
*/
public function getName(): array
{
return $this->get('name');
}
/**
* @return array
*/
public function getBody(): array
{
return $this->get('body');
}
public function getPrice()
{
return $this->get('price');
}
public function getPriceDiscount()
{
if ($this->get('price_discount')) {
return $this->get('price_discount');
}
return 0;
}
/**
* @return int
*/
public function getCategoryID()
{
return $this->get('category_id');
}
/**
* @return string
*/
public function getSlug(): string
{
if ($this->get('slug')) {
return (string) str_slug($this->get('slug'));
}
return (string) str_slug($this->get('name')['ru']);
}
/**
* @return bool
*/
public function getPublished()
{
if ($this->get('published') == 'true') {
return true;
}
return false;
}
/**
* @return bool
*/
public function getAvailable()
{
if ($this->get('available') == 'true') {
return true;
}
return false;
}
/**
* @return int|null
*/
public function getBrandID()
{
if ($this->get('brand_id') != 0) {
return (int) $this->get('brand_id');
}
return null;
}
/**
* @return int|null
*/
public function getMeasurementId()
{
if ($this->get('measurement_id') != 0) {
return (int) $this->get('measurement_id');
}
return null;
}
public function getPopular(): bool
{
return $this->get('popular');
}
public function getLeaderOfSales(): bool
{
if ($this->has('leader_of_sales')) {
if ($this->get('leader_of_sales') == 'true') {
return true;
}
}
return false;
}
}