<?php
namespace App\Model;
use App\Helpers\PageObjectRelationHelper;
use Google\Service\Apigee\Resource\Projects;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ProductCategory\Listing;
class ProductCategory extends \Pimcore\Model\DataObject\ProductCategory
{
public function page()
{
return (new PageObjectRelationHelper())->getProductCategoryPageFromProductCategoryObject($this);
}
/**
* @return false|string
*
* Returns the thumbnail of the object. We use this instead of the direct .items twig function so we can
* centrally define what image will be used as a thumbnail and what fallback rules are used.
*/
public function renderThumbnail($thumbnail = "default", $options = [])
{
return $this->getThumbnailImage()?->getThumbnail($thumbnail)?->getHtml($options);
}
public function getThumbnailImage()
{
if (isset($this->examples)
&& count($this->examples->getItems())) {
if ($image = $this->examples->getItems()[0] ?? null) {
return $image->getImage();
}
}
}
public function formatPropertyItems(): array
{
$output = [];
if ($this->getProperty_items()) {
foreach ($this->getProperty_items() as $property) {
/** @var Property $propertyObject */
$propertyObject = $property['property_object']->getData();
$output[] = [
'title' => $propertyObject->getTitle(),
'icon' => $propertyObject->getIcon(),
'text' => $property['property_value']->getData(),
];
}
}
return $output;
}
/**
* @return array
*/
public function mediaGalleryItems(): array
{
$output = [];
if ($this->examples) {
foreach ($this->getExamples()->getItems() as $hotspotimage) {
if ($hotspotimage) {
$output[] = $hotspotimage->getImage();
}
}
}
return $output;
}
public function projects($limit = 3)
{
$def = $this->getClass()->getFieldDefinition("projects");
$refKey = $def->getOwnerFieldName();
$refId = $def->getOwnerClassId();
$projectIds = [];
$projectRelation = $this->getRelationData($refKey, false, $refId);
if (isset($projectRelation) && !empty($projectRelation)) {
foreach ($projectRelation as $projectRelationData) {
if ($projectRelationData['published'] == "1"
&& $projectRelationData['type'] == "object"
&& $projectRelationData['subtype'] == 'Project') {
$projectIds[] = $projectRelationData['id'];
}
}
}
if (empty($projectIds)) {
return [];
}
$listing = new DataObject\Project\Listing();
$listing->setCondition("o_id IN (?)", [$projectIds]);
$listing->setLimit($limit);
return $listing->getData();
}
public function hasProjects(): bool
{
return boolval(count($this->projects()));
}
public function projectsWithLocale(string $locale, int $limit = 3): array
{
/** @var Project $project */
foreach ($this->projects(999) as $project) {
if ($project->page()) {
$projectsInLocale[] = $project;
}
}
if (!isset($projectsInLocale)) {
return [];
}
return array_splice($projectsInLocale, $limit) ?? [];
}
public function hasProjectsWithLocale(string $locale): bool
{
return boolval($this->projectsWithLocale($locale));
}
public function getTopCategory()
{
// retrieve the top-most category of this category, it is possible that this category has no parents (ie. is the root category)
// in that case return self.
$category = function($object) use(&$category){
// if main category below folder named products
if($object->getParent()->getType() == "folder"){
return $object;
}
return $category($object->getParent());
};
return $category($this);
}
}