src/Model/ProductCategory.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Model;
  3. use App\Helpers\PageObjectRelationHelper;
  4. use Google\Service\Apigee\Resource\Projects;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Model\DataObject\ProductCategory\Listing;
  7. class ProductCategory extends \Pimcore\Model\DataObject\ProductCategory
  8. {
  9.     public function page()
  10.     {
  11.         return (new PageObjectRelationHelper())->getProductCategoryPageFromProductCategoryObject($this);
  12.     }
  13.     /**
  14.      * @return false|string
  15.      *
  16.      * Returns the thumbnail of the object. We use this instead of the direct .items twig function so we can
  17.      * centrally define what image will be used as a thumbnail and what fallback rules are used.
  18.      */
  19.     public function renderThumbnail($thumbnail "default"$options = [])
  20.     {
  21.         return $this->getThumbnailImage()?->getThumbnail($thumbnail)?->getHtml($options);
  22.     }
  23.     public function getThumbnailImage()
  24.     {
  25.         if (isset($this->examples)
  26.             && count($this->examples->getItems())) {
  27.             if ($image $this->examples->getItems()[0] ?? null) {
  28.                 return $image->getImage();
  29.             }
  30.         }
  31.     }
  32.     public function formatPropertyItems(): array
  33.     {
  34.         $output = [];
  35.         if ($this->getProperty_items()) {
  36.             foreach ($this->getProperty_items() as $property) {
  37.                 /** @var Property $propertyObject */
  38.                 $propertyObject $property['property_object']->getData();
  39.                 $output[] = [
  40.                     'title' => $propertyObject->getTitle(),
  41.                     'icon' => $propertyObject->getIcon(),
  42.                     'text' => $property['property_value']->getData(),
  43.                 ];
  44.             }
  45.         }
  46.         return $output;
  47.     }
  48.     /**
  49.      * @return array
  50.      */
  51.     public function mediaGalleryItems(): array
  52.     {
  53.         $output = [];
  54.         if ($this->examples) {
  55.             foreach ($this->getExamples()->getItems() as $hotspotimage) {
  56.                 if ($hotspotimage) {
  57.                     $output[] = $hotspotimage->getImage();
  58.                 }
  59.             }
  60.         }
  61.         return $output;
  62.     }
  63.     public function projects($limit 3)
  64.     {
  65.         $def $this->getClass()->getFieldDefinition("projects");
  66.         $refKey $def->getOwnerFieldName();
  67.         $refId $def->getOwnerClassId();
  68.         $projectIds = [];
  69.         $projectRelation $this->getRelationData($refKeyfalse$refId);
  70.         if (isset($projectRelation) && !empty($projectRelation)) {
  71.             foreach ($projectRelation as $projectRelationData) {
  72.                 if ($projectRelationData['published'] == "1"
  73.                     && $projectRelationData['type'] == "object"
  74.                     && $projectRelationData['subtype'] == 'Project') {
  75.                     $projectIds[] = $projectRelationData['id'];
  76.                 }
  77.             }
  78.         }
  79.         if (empty($projectIds)) {
  80.             return [];
  81.         }
  82.         $listing = new DataObject\Project\Listing();
  83.         $listing->setCondition("o_id IN (?)", [$projectIds]);
  84.         $listing->setLimit($limit);
  85.         return $listing->getData();
  86.     }
  87.     public function hasProjects(): bool
  88.     {
  89.         return boolval(count($this->projects()));
  90.     }
  91.     public function projectsWithLocale(string $localeint $limit 3): array
  92.     {
  93.         /** @var Project $project */
  94.         foreach ($this->projects(999) as $project) {
  95.             if ($project->page()) {
  96.                 $projectsInLocale[] = $project;
  97.             }
  98.         }
  99.         if (!isset($projectsInLocale)) {
  100.             return [];
  101.         }
  102.         return array_splice($projectsInLocale$limit) ?? [];
  103.     }
  104.     public function hasProjectsWithLocale(string $locale): bool
  105.     {
  106.         return boolval($this->projectsWithLocale($locale));
  107.     }
  108.     public function getTopCategory()
  109.     {
  110.         // retrieve the top-most category of this category, it is possible that this category has no parents (ie. is the root category)
  111.         // in that case return self.
  112.         $category = function($object) use(&$category){
  113.             // if main category below folder named products
  114.             if($object->getParent()->getType() == "folder"){
  115.                 return $object;
  116.             }
  117.             return $category($object->getParent());
  118.         };
  119.         return $category($this);
  120.     }
  121. }