/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Products/ProductResource.php
<?php
namespace App\Http\Resources\Pages\Products;
use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Blog\PostCollection;
use App\Http\Resources\Pages\Catalogs\CatalogCollection;
use App\Http\Resources\Pages\Products\Benefits\BenefitCollection;
use App\Http\Resources\Pages\Products\CompositionCompatibility\CompositionCompatibilityCollection;
use App\Http\Resources\Pages\Resource\CropsCollection;
use App\Models\Page;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;
/**
* @OA\Schema(title="ProductResource",description="Product resource",
* @OA\Xml(name="ProductResource")
* )
*/
class ProductResource extends JsonResource
{
/**
* @OA\Property(property="slug", type="string", example="wuxal"),
* @OA\Property(property="type", type="string", example="product"),
* @OA\Property(property="preview_title", type="string", example="Wuxal"),
* @OA\Property(property="preview_image", type="string", example="https://uniferx-backend.test/storage/products/image.png"),
* @OA\Property(property="logo", type="string", example="https://uniferx-backend.test/storage/products/logo.png"),
* @OA\Property(property="preview_description", type="string", example="Wuxal® — is an effective suspension for foliar treatment"),
* @OA\Property(property="preview_button_text", type="string", example="View product"),
* @OA\Property(property="title", type="string", example="Wuxal"),
* @OA\Property(property="title_button", type="string", example="Find a distributor"),
* @OA\Property(property="title_button_link", type="string", example="https://uniferx-backend.test"),
* @OA\Property(property="title_image", type="string", example="https://uniferx-backend.test/storage/products/logo.png"),
* @OA\Property(property="about_tag", type="string", example="about product"),
* @OA\Property(property="about_title", type="string", example="<p>New <img src='../../storage/uploads/M34cjZyuj5tNX3RYYoOlFm0Dy5k3fnm5ejAZ3sK4.png' alt='' width='180' height='80' /> Wuxal’s are products<br />of a higher level, more than just<br />a product of fertilizer</p>"),
* @OA\Property(property="about_description", type="string", example="<p>Wuxal® — is an effective suspension for foliar treatment of plants and seed treatment, as well as a set of innovative solutions that allow you to achieve high efficiency.</p>"),
* @OA\Property(property="general_information", type="string", example="<h3>General information</h3>\n<p>These are multifunctional products for optimizing modern plant protection and nutrition technologies. Unlike microfertilizers, they are recommended as a component of tank mixtures in plant protection and nutrition systems."),
* @OA\Property(property="general_information_read_more", type="string", example="Read more"),
* @OA\Property(property="general_information_image", type="string", example="https://uniferx-backend.test/storage/products/image.png"),
* @OA\Property(property="benefits", type="array",
* @OA\Items(type="object",ref="#/components/schemas/BenefitResource"),
* ),
* @OA\Property(property="benefits_preview_video", type="string", example="https://uniferx-backend.test/storage/benefits/video.png"),
* @OA\Property(property="benefits_video", type="string", example="https://uniferx-backend.test/storage/benefits/video.mp4"),
* @OA\Property(property="composition_compatibility", type="array",
* @OA\Items(type="object",ref="#/components/schemas/CompositionCompatibilityResource"),
* ),
* @OA\Property(property="related_products", type="array",
* @OA\Items(type="object",
* @OA\Property(property="slug", type="string", example="wuxal_p_max"),
* @OA\Property(property="title", type="string", example="Wuxal"),
* @OA\Property(property="icon", type="string", example="https://uniferx-backend.test/storage/products/icon.png"),
* ),),
* @OA\Property(property="nutrition_programs", type="array",
* @OA\Items(type="object",
* @OA\Property(property="slug", type="string", example="cereals"),
* @OA\Property(property="title", type="string", example="Cereals"),
* @OA\Property(property="icon", type="string", example="https://uniferx-backend.test/storage/products/icon.png"),
* ),),
* @OA\Property(property="trial_results", type="object",
* @OA\Property(property="en", type="array",
* @OA\Items(type="object",
* @OA\Property(property="slug", type="string", example="cereals"),
* @OA\Property(property="title", type="string", example="Cereals"),
* @OA\Property(property="icon", type="string", example="https://uniferx-backend.test/storage/products/icon.png"),
* ),),
* @OA\Property(property="other", type="array",
* @OA\Items(type="object",
* @OA\Property(property="slug", type="string", example="sunflower"),
* @OA\Property(property="title", type="string", example="Sunflower"),
* @OA\Property(property="icon", type="string", example="https://uniferx-backend.test/storage/products/icon.png"),
* ),),
* )
*
*/
public string|null $language;
public string|null $country;
public bool $full;
public string|null $slug;
public string|null $type;
public string|null $preview_title;
public array|string|null $preview_image;
public array|string|null $logo;
public string|null $preview_description;
public string|null $preview_button_text;
public string|null $title;
public string|null $title_button;
public string|null $title_button_link;
public array|string|null $title_image;
public string|null $about_tag;
public string|null $about_title;
public string|null $about_description;
public string|null $general_information;
public string|null $general_information_read_more;
public array|string|null $general_information_image;
public array|object|null $benefits;
public array|string|null $benefits_preview_video;
public string|null $benefits_video;
public array|object|null $composition_compatibility;
public object|null $related_products;
public object|null $nutrition_programs;
public object|null $trial_results;
public object|null $catalogs;
public array|null $data;
public function language($value): static
{
$this->language = $value;
return $this;
}
public function country($value): static
{
$this->country = $value;
return $this;
}
public function full($value = true): static
{
$this->full = $value;
return $this;
}
public function __construct($resource, $language = null, $country = null, $full = true)
{
parent::__construct($resource);
$this->slug = $resource->slug ?? null;
$this->type = $resource->type ?? null;
$this->preview_title = $resource->preview_title ?? null;
$this->preview_image = $resource->preview_image ?? null;
$this->logo = $resource->logo ?? null;
$this->preview_description = $resource->preview_description ?? null;
$this->preview_button_text = $resource->preview_button_text ?? null;
$this->title = $resource->title ?? null;
$this->title_button = $resource->title_button ?? null;
$this->title_button_link = $resource->title_button_link ?? null;
$this->title_image = $resource->title_image ?? null;
// $this->about_tag = $resource->about_tag ?? null;
$this->about_title = $resource->about_title ?? null;
$this->about_description = $resource->about_description ?? null;
$this->general_information = $resource->general_information ?? null;
$this->general_information_read_more = $resource->general_information_read_more ?? null;
$this->general_information_image = $resource->general_information_image ?? null;
$this->benefits = $resource->benefits ?? null;
$this->benefits_preview_video = $resource->benefits_preview_video ?? null;
$this->benefits_video = $resource->benefits_video ?? null;
$this->composition_compatibility = $resource->composition_compatibility ?? null;
$this->related_products = $resource->related_products ?? null;
$this->nutrition_programs = $resource->nutrition_programs ?? null;
$this->trial_results = $resource->trial_results ?? null;
$this->catalogs = $resource->catalogs ?? null;
$this->data = $resource->data ?? null;
$this->country = $country ?? null;
$this->language = $language ?? null;
$this->full = $full ?? null;
}
public function toArray(Request $request): array
{
$generalTranslation = Page::query()->where('name', 'General Translations')->first();
// dd($generalTranslation->data);
// dd($this->nutrition_programs->count());
$data = [
'slug' => $this->slug,
'type' => $this->type,
'preview_title' => ResourceHelper::getValue($this->preview_title, $this->country, $this->language),
'preview_image' => ResourceHelper::getImage($this->preview_image, $this->country, $this->language),
'logo' => ResourceHelper::getImage($this->logo),
'preview_description' => ResourceHelper::getValue($this->preview_description, $this->country, $this->language),
'preview_button_text' => ResourceHelper::getValue($this->preview_button_text, $this->country, $this->language),
];
if ($this->full) {
if ($this->type === 'product') {
$group_slug = $this->slug;
} else {
$group_slug = $this->product->slug;
}
$select_trial_results = $this->trial_results()->whereHas('countries', function ($query) {
$query->where('code', $this->country);
})->count();
$other_trial_results = $this->trial_results()->whereHas('countries', function ($query) {
$query->where('code', '!=', $this->country);
})->get();
// dd($this->nutrition_program);
$data['group_slug'] = $group_slug;
$data['title'] = ResourceHelper::getValue($this->title, $this->country, $this->language);
$data['title_button'] = ResourceHelper::getValue($this->title_button, $this->country, $this->language);
$data['title_button_link'] = ResourceHelper::getValue($this->title_button_link, $this->country, $this->language);
$data['title_image'] = ResourceHelper::getImage($this->title_image);
// $data['about_tag'] = ResourceHelper::getValue($this->about_tag, $this->country, $this->language);
$data['about_title'] = ResourceHelper::getValue($this->about_title, $this->country, $this->language);
$data['about_description'] = ResourceHelper::getValue($this->about_description, $this->country, $this->language);
$data['general_information_title'] = ResourceHelper::getValue($this->data['general_information_title'] ?? null, $this->country, $this->language);
$data['general_information'] = ResourceHelper::getValue($this->general_information, $this->country, $this->language);
$data['general_information_read_more'] = ResourceHelper::getValue($this->general_information_read_more, $this->country, $this->language);
$data['formula_title'] = ResourceHelper::getValue($this->data['formula_title'] ?? null, $this->country, $this->language);
$data['formula_text'] = ResourceHelper::getValue($this->data['formula_text'] ?? null, $this->country, $this->language);
$data['formula_table'] = ResourceHelper::getValue($this->data['formula_table'] ?? null, $this->country, $this->language);
// $data['formula_image'] = ResourceHelper::getImage($this->general_information_image);
$data['formula_description'] = ResourceHelper::getValue($this->data['formula_description'] ?? null, $this->country, $this->language);
$data['benefits_title'] = ResourceHelper::getValue($this->data['benefits_title'] ?? null, $this->country, $this->language);
$data['benefits'] = $this->benefits ? BenefitCollection::make(collect($this->benefits)->collect())->country($this->country)->language($this->language) : null;
$data['benefits_preview_video'] = ResourceHelper::getImage($this->benefits_preview_video['preview'] ?? null);
$data['benefits_preview_text'] = ResourceHelper::getValue($this->benefits_preview_video['text'] ?? null, $this->country, $this->language);
$data['benefits_video'] = ResourceHelper::getValue($this->benefits_video, $this->country, $this->language);
$data['products_title'] = ResourceHelper::getValue($this->data['products_title'] ?? null, $this->country, $this->language);
$data['composition_compatibility_title'] = ResourceHelper::getValue($this->data['composition_compatibility_title'] ?? null, $this->country, $this->language);
$data['nutrition_programs_title'] = ResourceHelper::getValue($generalTranslation->data['nutrition_programs_title'] ?? null, $this->country, $this->language);
$data['trial_results_title'] = ResourceHelper::getValue($generalTranslation->data['trial_results_title'] ?? null, $this->country, $this->language);
$data['trial_in_country_tab'] = ResourceHelper::getValue($generalTranslation->data['trial_in_country_tab'] ?? null, $this->country, $this->language);
$data['trial_in_other_country_tab'] = ResourceHelper::getValue($generalTranslation->data['trial_in_other_country_tab'] ?? null, $this->country, $this->language);
$data['composition_compatibility'] = $this->composition_compatibility
? CompositionCompatibilityCollection::make(collect($this->composition_compatibility)->collect())->country($this->country)->language($this->language)
: null;
$data['has_related_products'] = (bool)$this->related_products->count();
$data['has_nutrition_programs'] = (bool)$this->nutrition_programs->count();
$data['has_trial_results'] = (bool)$select_trial_results;
$data['catalogs'] = $this->catalogs ? CatalogCollection::make($this->catalogs()->where('is_enabled', true)->get()->collect())->country($this->country)->language($this->language) : null;
}
return $data;
}
public static function collection($resource): PostCollection
{
return new PostCollection($resource);
}
}