src/Model/DataObject/BrandExtension.php line 468

Open in your IDE?
  1. <?php
  2. namespace App\Model\DataObject;
  3. use Pimcore\Cache;
  4. use Pimcore\Model\DataObject;
  5. use Pimcore\Model\DataObject\BrandExtension\Listing;
  6. use Pimcore\Model\DataObject\AbstractObject;
  7. use Pimcore\Model\DataObject\Folder;
  8. use App\Tool\AdminStyle;
  9. use App\Model\DataObject\Product\TraitClasses\Checkoutable;
  10. use App\Tool\SizeSort;
  11. use App\Model\DataObject\AvailabilityCountry;
  12. use Pimcore\Model\DataObject\PreGetValueHookInterface;
  13. use Pimcore\Twig\Extension\Templating\PimcoreUrl;
  14. class BrandExtension extends \Pimcore\Model\DataObject\BrandExtension {
  15.     use Checkoutable;
  16.    
  17.     public function getElementAdminStyle() {
  18.         if (!$this->o_elementAdminStyle) {
  19.             $this->o_elementAdminStyle = new AdminStyle($this);
  20.         }
  21.         return $this->o_elementAdminStyle;
  22.     }
  23.     public function save() {
  24.         parent::save();
  25.         Cache::clearTag("object_" $this->internalGetBaseProduct()->getId());
  26.     }
  27.     public function isActive($inProductList false) : bool{
  28.         return $this->isPublished();
  29.     }
  30.     public function getOSDoIndexProduct(): bool{
  31.         if($this->getType() == "object" && $this->getParent() instanceof Product) {
  32.             return false;
  33.         } else {
  34.             return true;
  35.         }
  36.     }
  37.     public function getOSParentId() {
  38.         if($this->getType() == "variant" && $this->getParent()->getParent() instanceof Product) {
  39.             return $this->getParent()->getParent()->getId();
  40.         } elseif ($this->getParent() instanceof Product) {
  41.             return parent::getOSParentId();
  42.         } else {
  43.             return $this->getId();
  44.         }
  45.     }
  46.     public function getPrice(){
  47.         
  48.     }
  49.     protected $prices = array();
  50.     /**
  51.      * Returns an array on min and max price for a product based on all it's children
  52.      *
  53.      * @param string $priceType (new|old)
  54.      * @return array|int|null
  55.      */
  56.     public function getPriceRange($priceType 'new') {
  57.         if(!array_key_exists($priceType$this->prices)) {
  58.             $method $priceType == 'new' 'getPrice' 'getPriceOld';
  59.             if($this->isVariant()) {
  60.                 return $this->$method();
  61.             } else {
  62.                 $prices = array();
  63.                 $colorSizeVariants $this->internalGetSizeVariants();
  64.                 foreach($colorSizeVariants as $sizeVariants) {
  65.                     foreach($sizeVariants as $sizeVariant) {
  66.                         $prices[] = $sizeVariant->$method();
  67.                     }
  68.                 }
  69.                 if ($prices) {
  70.                     $min min($prices);
  71.                     $max max($prices);
  72.                     if($min == $max) {
  73.                         $this->prices[$priceType] = $min;
  74.                     } else {
  75.                         $this->prices[$priceType] = array('min' => $min'max' => $max);
  76.                     }
  77.                 } else {
  78.                     $this->prices[$priceType] = null;
  79.                 }
  80.             }
  81.         }
  82.         return $this->prices[$priceType];
  83.     }
  84.     /**
  85.      * @return \Pimcore\Model\Asset\Image|null
  86.      */
  87.     public function getFirstImageAsset($country null) {
  88.         
  89.         //$authNamespace = new \Zend_Session_Namespace('Zend_Auth'); 
  90.         //$country = $authNamespace->defaultUserCountry;
  91.         $images '';
  92.         if ($country && $country->getName() != "US") {    
  93.             $list = new DataObject\BrandExtension\Listing();
  94.             $list->setObjectTypes([DataObject\AbstractObject::OBJECT_TYPE_VARIANT]);
  95.             //$list->setCondition("o_parentId = :id AND BottleSize = '700 ml' AND CountryAvailabilityList LIKE :country", ["id" => $this->getId(), "country" => "%32857%"]);
  96.             $list->setCondition("o_parentId = :id AND CountryAvailabilityList LIKE :country", ["id" => $this->getId(), "country" => "%" $country->getId() . "%"]);
  97.             $list->setLimit(1);
  98.             foreach ($list as $item) {
  99.                     $images $item->getDocumentImage();
  100.             }
  101.         }
  102.     
  103.         if (!$images) {
  104.             $images $this->getDocumentImage();
  105.         }
  106.    
  107.         if ($images) {
  108.                 $firstImage $images;
  109.                 return $firstImage;
  110.             }
  111.             //elseif (\Pimcore\Tool\Frontend::getWebsiteConfig()->fallbackImage) {
  112.             //    return \Pimcore\Tool\Frontend::getWebsiteConfig()->fallbackImage;
  113.             //} 
  114.             else {
  115.                 return null;
  116.             }
  117.     /*
  118.         if ($images->items[0] && $images->items[0]->getPhotoBottle()) {
  119.             $firstImage = $images->items[0]->getPhotoBottle();
  120.             return $firstImage;
  121.         } elseif (\Pimcore\Tool\Frontend::getWebsiteConfig()->fallbackImage) {
  122.             return \Pimcore\Tool\Frontend::getWebsiteConfig()->fallbackImage;
  123.         } else {
  124.             return null;
  125.         }
  126.     */
  127.     }
  128.     
  129.     public function getAdditionalImages($type null) {
  130.         $array = [];
  131.         $additionalPhotos $this->getAdditionalPhotos();
  132.         if ($additionalPhotos) {
  133.             if ($type == null) {
  134.                 foreach($additionalPhotos as $item) {
  135.                         array_push($array$item);
  136.                 }
  137.             } else if ($type == 'mobile') {
  138.                 foreach($additionalPhotos as $item) {
  139.                     if ($item['AdditionalPhotoType']->getData() == 'mobile') {
  140.                         array_push($array$item);
  141.                     }
  142.                 }
  143.             } else if ($type == 'scale') {
  144.                 foreach($additionalPhotos as $item) {
  145.                     if ($item['AdditionalPhotoType']->getData() == 'scale') {
  146.                         array_push($array$item);
  147.                     }
  148.                 }
  149.             } else if ($type == 'angled') {
  150.                 foreach($additionalPhotos as $item) {
  151.                     if ($item['AdditionalPhotoType']->getData() != 'scale' && $item['AdditionalPhotoType']->getData() != 'mobile') {
  152.                         array_push($array$item);
  153.                     }
  154.                 }
  155.             }    
  156.             
  157.         }
  158.         
  159.         return $array;
  160.     }
  161.     /**
  162.      * returns product variant that should be used for detail link
  163.      *
  164.      * @return \Website\DefaultProduct
  165.      */
  166.     public function getLinkProduct() {
  167.         $firstSizeVariants $this->getColorVariants(true);
  168.         if(count($firstSizeVariants) == 0)
  169.         {
  170.             // no variants
  171.             return $this;
  172.         }
  173.         else
  174.         {
  175.             return $firstSizeVariants[0];
  176.         }
  177.     }
  178.    
  179.     public function getLifeStyleImages() {
  180.         $images = array();
  181.         $lfphotos $this->getLifestylePhotoList();
  182.         if ($lfphotos) {
  183.             foreach ($lfphotos as $lfphoto) {
  184.                 $images[] = $lfphoto->getPhotoLifestyle();
  185.             }
  186.         }
  187.         $variants $this->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
  188.         if($variants) {
  189.             foreach($variants as $variant) {
  190.                 $lfphotos $variant->getLifestylePhotoList();
  191.                 if ($lfphotos) {
  192.                     foreach ($lfphotos as $lfphoto) {
  193.                         $images[] = $lfphoto->getPhotoLifestyle();
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.         return $images;
  199.     }
  200.      public function getSize() {
  201.         return $this->getBottleSize();
  202.     }
  203.     // public function getName() {
  204.     //     return $this->getName();
  205.     // }
  206.     public function getSeoName() {
  207.         return $this->getName();
  208.     }
  209.     public function getCategories():array{
  210.         $ctg = array();
  211.         $types $this->getCategory();
  212.         if ($types) {
  213.             foreach ($types as $type) {
  214.                 $ctg[] = $type->getObject();
  215.             }
  216.         }
  217.         return $ctg;
  218.     }
  219.     
  220.     
  221.     //this function should be called for BrandExtension
  222.     public function getBottleSizes($availabilityCountry) {
  223.         if ($this->getDocumentImage()) $images[] = $this->getDocumentImage();
  224.         $variants $this->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
  225.         if($variants) {
  226.             foreach($variants as $variant) {
  227.                if ($variant->BottlePhoto$images[] = $variant->getDocumentImage();
  228.             }
  229.         }
  230.         if (count($images) == 0$images[] = \Pimcore\Tool\Frontend::getWebsiteConfig()->fallbackImage;
  231.         return $images;
  232.     }
  233.     public function getDocumentImagesInheritance() {
  234.     }
  235.     public function getLifestylePhotosInheritance() {
  236.     }
  237.     public function getAccoladesListInheritance() {
  238.     }
  239.     public function getLifestylePhotoListInheritance() {
  240.     }
  241.     public function getAmazonDataInheritance() {
  242.     }
  243.     public function getGS1DataInheritance() {
  244.     }
  245.     
  246.     /**
  247.     /**
  248.      * Returns concatenated category names for product
  249.      * @param bool $seo to suppress seo name, default is true and returns the seo name if available
  250.      * @return string
  251.      */
  252.     /*
  253.     public function getCategoriesText( $seo = true) {
  254.         $categories = $this->getCategories();
  255.         $categoriesArray = array();
  256.         foreach ($categories as $item) {
  257.             $categoriesArray[] = ( $item->getSeoname() && $seo) ? $item->getSeoname() : $item->getName();
  258.         }
  259.         return implode(',' , $categoriesArray);
  260.     }
  261.     public function getMainMaterialText() {
  262.         $mainMaterials = $this->getMaterialComposition() ? $this->getMaterialComposition() : array();
  263.         $mainMaterialsArray = array();
  264.         foreach ($mainMaterials as $item) {
  265.             if ($item->getPercent()) {
  266.                 $mainMaterialsArray[] = $item->getPercent() . '% ' . $item->getObject()->getName();
  267.             } else {
  268.                 $mainMaterialsArray[] = $item->getObject()->getName();
  269.             }
  270.         }
  271.         return implode(',', $mainMaterialsArray);
  272.     } */
  273.     public function getSecondaryMaterialText() {
  274.         $secondaryMaterials $this->getSecondaryMaterialComposition() ? $this->getSecondaryMaterialComposition() : array() ;
  275.         $secondaryMaterialsArray = array();
  276.         foreach ($secondaryMaterials as $item) {
  277.             if ($item->getPercent()) {
  278.                 $secondaryMaterialsArray[] = $item->getPercent() . '% ' .$item->getObject()->getName();
  279.             } else {
  280.                 $secondaryMaterialsArray[] = $item->getObject()->getName();
  281.             }
  282.         }
  283.         return implode(','$secondaryMaterialsArray);
  284.     }
  285.     public function getCanonicalId() {
  286.         if($this->isVariant() ) {
  287.             return $this->getParent()->getCanonicalId();
  288.         } else {
  289.             return $this->getId();
  290.         }
  291.     }
  292.     /**
  293.      * Returns the first size variant for all color variants (or just the color variants) of a product regardless if it's the main product or already a color variant
  294.      *
  295.      * @param $withSize switch the color with ot without the first size variant
  296.      * @return \Website\DefaultProduct[]
  297.      */
  298.     public function getColorVariants($withSize true) {
  299.         if($withSize) {
  300.             $firstSizeVariants = array();
  301.             foreach($this->internalGetFirstSizeVariants() as $id => $sizeVariant) {
  302.                 if($sizeVariant) {
  303.                     $firstSizeVariants[] = $sizeVariant;
  304.                 } else {
  305.                     $firstSizeVariants[] = AbstractObject::getById($id);
  306.                 }
  307.             }
  308.             return $firstSizeVariants;
  309.         } else {
  310.             return $this->internalGetColorVariants();
  311.         }
  312.     }
  313.     public $baseProduct null;
  314.     public $baseColorVariant null;
  315.     private $colorVariants null;
  316.     public $firstSizeVariants null;
  317.     private $sizeVariants null;
  318.     protected function fillBaseProducts() {
  319.         if(!$this->isVariant()) {
  320.             $this->baseProduct $this;
  321.             $this->baseColorVariant null;
  322.         } elseif($this->getType() == AbstractObject::OBJECT_TYPE_OBJECT) {
  323.             $this->baseProduct $this->getParent();
  324.             $this->baseColorVariant $this;
  325.         } elseif($this->getType() == AbstractObject::OBJECT_TYPE_VARIANT && $this->getParent()->getType() == AbstractObject::OBJECT_TYPE_OBJECT) {
  326.             $this->baseColorVariant $this->getParent();
  327.             $this->baseProduct $this->getParent()->getParent();
  328.         } else {
  329.             throw new \Exception("Invalid Product Tree with object " $this->getId());
  330.         }
  331.     }
  332.     protected function fillVariants() {
  333.         $this->fillBaseProducts();
  334.         $this->colorVariants $this->internalGetColorVariants(); // $this->baseProduct->getChildren(array(Object_Abstract::OBJECT_TYPE_OBJECT));
  335.         $this->firstSizeVariants = array();
  336.         if(!empty($this->colorVariants)) {
  337.             foreach($this->colorVariants as $colorVariant) {
  338.                 $childs $colorVariant->getChildren((array(AbstractObject::OBJECT_TYPE_VARIANT)));
  339.                 $this->firstSizeVariants[$colorVariant->getId()] = (isset($childs[0])) ? $childs[0] : null;
  340.             }
  341.         }
  342.     }
  343.     public function internalGetBaseProduct() {
  344.         if(empty($this->baseProduct)) {
  345.             $this->fillVariants();
  346.         }
  347.         return $this->baseProduct;
  348.     }
  349.     protected function internalGetColorVariants() {
  350.         if($this->colorVariants === null) {
  351.             $this->colorVariants $this->internalGetBaseProduct()->getChildren(array(AbstractObject::OBJECT_TYPE_OBJECT));
  352.         }
  353.         return $this->colorVariants;
  354.     }
  355.     protected function internalGetSizeVariants() {
  356.         if($this->sizeVariants === null) {
  357.             $this->sizeVariants = array();
  358.             if(!empty($this->colorVariants)) {
  359.                 foreach($this->colorVariants as $colorVariant) {
  360.                     $this->sizeVariants[$colorVariant->getId()] = $colorVariant->getChildren((array(AbstractObject::OBJECT_TYPE_VARIANT)));
  361.                 }
  362.             }
  363.         }
  364.         return $this->sizeVariants;
  365.     }
  366.     protected function internalGetFirstSizeVariants() {
  367.         if($this->firstSizeVariants === null) {
  368.             $this->fillVariants();
  369.         }
  370.         return $this->firstSizeVariants;
  371.     }
  372.     public function getBaseColorVariant() { 
  373.         if(empty($this->baseColorVariant)) {
  374.             $this->fillVariants();
  375.         }
  376.         return $this->baseColorVariant;
  377.     }
  378.     /**
  379.      * Returns size variants for a product regardless if it's the main product, a color child or a size variant
  380.      *
  381.      * @return \Website\DefaultProduct[]|null
  382.      */
  383.     public function getSizeVariants($country null) {
  384.         if(empty($this->baseProduct)) {
  385.             $this->fillBaseProducts();
  386.         }
  387.         
  388.         $list = new DataObject\BrandExtension\Listing();
  389.         if ($country != null) {
  390.                 $list->setCondition("o_parentId = :id", ["id" => $this->baseColorVariant->getId()]);
  391.         } else {
  392.             $list->setCondition("o_parentId = :id", ["id" => $this->baseColorVariant->getId()]);
  393.         }
  394.         
  395.         //$list->setObjectTypes(array(AbstractObject::OBJECT_TYPE_VARIANT));        
  396.         return SizeSort::sort($list->load());      
  397.     }
  398.     
  399.     protected $isVariant null;
  400.     /**
  401.      * Checks if a a product is a variant or the main product
  402.      *
  403.      * @return bool
  404.      */
  405.     public function isVariant() {
  406.         if($this->isVariant === null) {
  407.             if($this->getType() == "object" && $this->getParent() instanceof Folder) {
  408.                 $this->isVariant false;
  409.             } else {
  410.                 $this->isVariant true;
  411.             }
  412.         }
  413.         return $this->isVariant;
  414.     }
  415.     public function hasTechnologyAttributes() {
  416.         return $this->getMainMaterialText() || $this->getSecondaryMaterialText() || $this->getFeatures();
  417.     }
  418.     public function getFirstCategory() {
  419.         if($categories $this->getCategories()) {
  420.             foreach($categories as $cat) {
  421.                 return $cat;
  422.             }
  423.         }
  424.     }
  425.     /**
  426.      * enables inheritance for field collections, if xxxInheritance field is available and set to string 'true'
  427.      *
  428.      * @param string $key
  429.      * @return mixed|\Pimcore\Model\Object\Fieldcollection
  430.      */
  431.     public function preGetValue($key) {
  432.         if ($this->getClass()->getAllowInherit()
  433.             && AbstractObject::doGetInheritedValues()
  434.             && $this->getClass()->getFieldDefinition($key) instanceof \Pimcore\Model\DataObject\ClassDefinition\Data\Fieldcollections
  435.         ) {
  436.             $checkInheritanceKey $key "Inheritance";
  437.             if ($this->{
  438.                 'get' $checkInheritanceKey
  439.                 }() == "true"
  440.             ) {
  441.                 $parentValue $this->getValueFromParent($key);
  442.                 $data $this->$key;
  443.                 if(!$data) {
  444.                     $data $this->getClass()->getFieldDefinition($key)->preGetData($this);;
  445.                 }
  446.                 if (!$data) {
  447.                     return $parentValue;
  448.                 } else {
  449.                     $value = new \Pimcore\Model\DataObject\Fieldcollection($data->getItems());
  450.                     if (!empty($parentValue)) {
  451.                         foreach ($parentValue as $entry) {
  452.                             $value->add($entry);
  453.                         }
  454.                     }
  455.                     return $value;
  456.                 }
  457.             }
  458.         }
  459.         return parent::preGetValue($key);
  460.     }
  461. }