Beispiel #1
0
 /**
  * Add the collection to a template
  * @param   object
  * @param   array
  */
 public function addToTemplate(\Isotope\Template $objTemplate, array $arrConfig = array())
 {
     $arrGalleries = array();
     $arrItems = $this->addItemsToTemplate($objTemplate, $arrConfig['sorting']);
     $objTemplate->collection = $this;
     $objTemplate->config = $this->getRelated('config_id') || Isotope::getConfig();
     $objTemplate->surcharges = \Isotope\Frontend::formatSurcharges($this->getSurcharges());
     $objTemplate->subtotal = Isotope::formatPriceWithCurrency($this->getSubtotal());
     $objTemplate->total = Isotope::formatPriceWithCurrency($this->getTotal());
     $objTemplate->generateAttribute = function ($strAttribute, ProductCollectionItem $objItem) {
         if (!$objItem->hasProduct()) {
             return '';
         }
         $objAttribute = $GLOBALS['TL_DCA']['tl_iso_product']['attributes'][$strAttribute];
         if (!$objAttribute instanceof IsotopeAttribute) {
             throw new \InvalidArgumentException($strAttribute . ' is not a valid attribute');
         }
         return $objAttribute->generate($objItem->getProduct());
     };
     $objTemplate->getGallery = function ($strAttribute, ProductCollectionItem $objItem) use($arrConfig, &$arrGalleries) {
         if (!$objItem->hasProduct()) {
             return new \Isotope\Model\Gallery\Standard();
         }
         $strCacheKey = 'product' . $objItem->product_id . '_' . $strAttribute;
         $arrConfig['jumpTo'] = $objItem->getRelated('jumpTo');
         if (!isset($arrGalleries[$strCacheKey])) {
             $arrGalleries[$strCacheKey] = Gallery::createForProductAttribute($objItem->getProduct(), $strAttribute, $arrConfig);
         }
         return $arrGalleries[$strCacheKey];
     };
     // !HOOK: allow overriding of the template
     if (isset($GLOBALS['ISO_HOOKS']['addCollectionToTemplate']) && is_array($GLOBALS['ISO_HOOKS']['addCollectionToTemplate'])) {
         foreach ($GLOBALS['ISO_HOOKS']['addCollectionToTemplate'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $objCallback->{$callback}[1]($objTemplate, $arrItems, $this);
         }
     }
 }
 public function addToTemplate($intProductType, Order $objOrder, \Template $objTemplate, array $arrConfig = array())
 {
     $arrGalleries = array();
     // FIX - call to custom function since addItemsToTemplate isn't static
     $arrItems = static::addItemsToTemplate($intProductType, $objOrder, $objTemplate, $arrConfig['sorting']);
     $objTemplate->id = $objOrder->id;
     $objTemplate->collection = $objOrder;
     $objTemplate->config = $objOrder->getRelated('config_id') || Isotope::getConfig();
     $objTemplate->surcharges = Frontend::formatSurcharges($objOrder->getSurcharges());
     $objTemplate->subtotal = Isotope::formatPriceWithCurrency($objOrder->getSubtotal());
     $objTemplate->total = Isotope::formatPriceWithCurrency($objOrder->getTotal());
     $objTemplate->tax_free_subtotal = Isotope::formatPriceWithCurrency($objOrder->getTaxFreeSubtotal());
     $objTemplate->tax_free_total = Isotope::formatPriceWithCurrency($objOrder->getTaxFreeTotal());
     $objTemplate->hasAttribute = function ($strAttribute, ProductCollectionItem $objItem) {
         if (!$objItem->hasProduct()) {
             return false;
         }
         $objProduct = $objItem->getProduct();
         return in_array($strAttribute, $objProduct->getAttributes()) || in_array($strAttribute, $objProduct->getVariantAttributes());
     };
     $objTemplate->generateAttribute = function ($strAttribute, ProductCollectionItem $objItem, array $arrOptions = array()) {
         if (!$objItem->hasProduct()) {
             return '';
         }
         $objAttribute = $GLOBALS['TL_DCA']['tl_iso_product']['attributes'][$strAttribute];
         if (!$objAttribute instanceof IsotopeAttribute) {
             throw new \InvalidArgumentException($strAttribute . ' is not a valid attribute');
         }
         return $objAttribute->generate($objItem->getProduct(), $arrOptions);
     };
     $objTemplate->getGallery = function ($strAttribute, ProductCollectionItem $objItem) use($arrConfig, &$arrGalleries) {
         if (!$objItem->hasProduct()) {
             return new Standard();
         }
         $strCacheKey = 'product' . $objItem->product_id . '_' . $strAttribute;
         $arrConfig['jumpTo'] = $objItem->getRelated('jumpTo');
         if (!isset($arrGalleries[$strCacheKey])) {
             $arrGalleries[$strCacheKey] = Gallery::createForProductAttribute($objItem->getProduct(), $strAttribute, $arrConfig);
         }
         return $arrGalleries[$strCacheKey];
     };
     // !HOOK: allow overriding of the template
     if (isset($GLOBALS['ISO_HOOKS']['addCollectionToTemplate']) && is_array($GLOBALS['ISO_HOOKS']['addCollectionToTemplate'])) {
         foreach ($GLOBALS['ISO_HOOKS']['addCollectionToTemplate'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $objCallback->{$callback}[1]($objTemplate, $arrItems, $objOrder);
         }
     }
 }
Beispiel #3
0
 /**
  * Generate a product template
  *
  * @param array $arrConfig
  *
  * @return string
  */
 public function generate(array $arrConfig)
 {
     $this->strFormId = ($arrConfig['module'] instanceof \ContentElement ? 'cte' : 'fmd') . $arrConfig['module']->id . '_product_' . $this->getProductId();
     $objProduct = $this->validateVariant();
     // A variant has been loaded, generate the variant
     if ($objProduct->id != $this->id) {
         return $objProduct->generate($arrConfig);
     }
     $arrGalleries = array();
     /** @type Template|object $objTemplate */
     $objTemplate = new Template($arrConfig['template']);
     $objTemplate->setData($this->arrData);
     $objTemplate->product = $this;
     $objTemplate->config = $arrConfig;
     $objTemplate->generateAttribute = function ($strAttribute, array $arrOptions = array()) use($objProduct) {
         $objAttribute = $GLOBALS['TL_DCA']['tl_iso_product']['attributes'][$strAttribute];
         if (!$objAttribute instanceof IsotopeAttribute) {
             throw new \InvalidArgumentException($strAttribute . ' is not a valid attribute');
         }
         return $objAttribute->generate($objProduct, $arrOptions);
     };
     $objTemplate->generatePrice = function () use($objProduct) {
         $objPrice = $objProduct->getPrice();
         /** @type ProductType $objType */
         $objType = $objProduct->getRelated('type');
         if (null === $objPrice) {
             return '';
         }
         return $objPrice->generate($objType->showPriceTiers(), 1, $objProduct->getOptions());
     };
     $objTemplate->getGallery = function ($strAttribute) use($objProduct, $arrConfig, &$arrGalleries) {
         if (!isset($arrGalleries[$strAttribute])) {
             $arrGalleries[$strAttribute] = Gallery::createForProductAttribute($objProduct, $strAttribute, $arrConfig);
         }
         return $arrGalleries[$strAttribute];
     };
     $arrVariantOptions = array();
     $arrProductOptions = array();
     $arrAjaxOptions = array();
     foreach (array_unique(array_merge($this->getAttributes(), $this->getVariantAttributes())) as $attribute) {
         $arrData = $GLOBALS['TL_DCA']['tl_iso_product']['fields'][$attribute];
         if ($arrData['attributes']['customer_defined'] || $arrData['attributes']['variant_option']) {
             $strWidget = $this->generateProductOptionWidget($attribute, $arrVariantOptions, $arrAjaxOptions);
             if ($strWidget != '') {
                 $arrProductOptions[$attribute] = array_merge($arrData, array('name' => $attribute, 'html' => $strWidget));
             }
         }
     }
     $arrButtons = array();
     // !HOOK: retrieve buttons
     if (isset($GLOBALS['ISO_HOOKS']['buttons']) && is_array($GLOBALS['ISO_HOOKS']['buttons'])) {
         foreach ($GLOBALS['ISO_HOOKS']['buttons'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $arrButtons = $objCallback->{$callback}[1]($arrButtons);
         }
         $arrButtons = array_intersect_key($arrButtons, array_flip($arrConfig['buttons']));
     }
     if (\Input::post('FORM_SUBMIT') == $this->getFormId() && !$this->doNotSubmit) {
         foreach ($arrButtons as $button => $data) {
             if (isset($_POST[$button])) {
                 if (isset($data['callback'])) {
                     $objCallback = \System::importStatic($data['callback'][0]);
                     $objCallback->{$data['callback'][1]}($this, $arrConfig);
                 }
                 break;
             }
         }
     }
     RowClass::withKey('rowClass')->addCustom('product_option')->addFirstLast()->addEvenOdd()->applyTo($arrProductOptions);
     $objTemplate->buttons = $arrButtons;
     $objTemplate->useQuantity = $arrConfig['useQuantity'];
     $objTemplate->minimum_quantity = $this->getMinimumQuantity();
     $objTemplate->raw = $this->arrData;
     $objTemplate->raw_options = $this->getConfiguration();
     $objTemplate->configuration = $this->getConfiguration();
     $objTemplate->href = $this->generateUrl($arrConfig['jumpTo']);
     $objTemplate->label_detail = $GLOBALS['TL_LANG']['MSC']['detailLabel'];
     $objTemplate->options = $arrProductOptions;
     $objTemplate->hasOptions = !empty($arrProductOptions);
     $objTemplate->enctype = $this->hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
     $objTemplate->formId = $this->getFormId();
     $objTemplate->action = ampersand(\Environment::get('request'), true);
     $objTemplate->formSubmit = $this->getFormId();
     $objTemplate->product_id = $this->getProductId();
     $objTemplate->module_id = $arrConfig['module']->id;
     $GLOBALS['AJAX_PRODUCTS'][] = array('formId' => $this->getFormId(), 'attributes' => $arrAjaxOptions);
     // !HOOK: alter product data before output
     if (isset($GLOBALS['ISO_HOOKS']['generateProduct']) && is_array($GLOBALS['ISO_HOOKS']['generateProduct'])) {
         foreach ($GLOBALS['ISO_HOOKS']['generateProduct'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $objCallback->{$callback}[1]($objTemplate, $this);
         }
     }
     return $objTemplate->parse();
 }
 /**
  * Alter the options and set an image as label
  *
  * @param IsotopeProduct|Product\Standard $objProduct
  *
  * @return array|mixed
  *
  * @throws \InvalidArgumentException when optionsSource=product but product is null
  * @throws \UnexpectedValueException for unknown optionsSource
  */
 public function getOptionsForWidget(IsotopeProduct $objProduct = null)
 {
     // Skip in the back end or without a given product
     if (TL_MODE == 'BE' || null === $objProduct) {
         return parent::getOptionsForWidget($objProduct);
     }
     // Fetch all product's variants and make the accessible via the attribute value in an array
     $objVariants = Product::findAvailableByIds($objProduct->getVariantIds());
     if (null === $objVariants) {
         return parent::getOptionsForWidget($objProduct);
     }
     /** @var IsotopeProduct[] $arrVariants */
     $arrVariants = array_combine($objVariants->fetchEach($this->field_name), $objVariants->getModels());
     // Alter the options
     return array_map(function ($arrOption) use($arrVariants) {
         // Skip if option has no associated product variant
         if (!array_key_exists($arrOption['value'], $arrVariants)) {
             return $arrOption;
         }
         /** @var Gallery|Gallery\Standard $objGallery */
         $objGallery = Gallery::createForProductAttribute($arrVariants[$arrOption['value']], 'images', array('gallery' => $this->radioImageGallery));
         // Wrap label in span with css class
         $arrOption['label'] = sprintf('<span class="attribute-label">%s</span>', $arrOption['label']);
         // Add image to label
         $arrOption['label'] .= PHP_EOL . $objGallery->generateMainImage();
         return $arrOption;
     }, parent::getOptionsForWidget($objProduct));
 }