/**
  * Find all products we need to list.
  * @param   array|null
  * @return  array
  */
 protected function findProducts($arrCacheIds = null)
 {
     $arrIds = array(0);
     $objProduct = Product::findAvailableByIdOrAlias(Input::getAutoItem('product'));
     if (null === $objProduct) {
         return array();
     }
     $objRelated = RelatedProduct::findByProductAndCategories($objProduct, $this->iso_related_categories);
     if (null !== $objRelated) {
         while ($objRelated->next()) {
             $ids = trimsplit(',', $objRelated->products);
             if (!empty($ids) && is_array($ids)) {
                 $arrIds = array_unique(array_merge($arrIds, $ids));
             }
         }
     }
     $objProducts = Product::findAvailableByIds($arrIds, array('order' => \Database::getInstance()->findInSet(Product::getTable() . '.id', $arrIds)));
     return null === $objProducts ? array() : $objProducts->getModels();
 }
 /**
  * Find all products we need to list.
  * @param   array|null
  * @return  array
  */
 protected function findProducts()
 {
     $arrProducts = deserialize($this->iso_products);
     $arrSorting = $this->getSorting();
     $objProducts = Product::findAvailableByIds($arrProducts, array('sorting' => $arrSorting));
     return null === $objProducts ? array() : $objProducts->getModels();
 }
Example #3
0
 /**
  * Compile product list.
  *
  * This function is specially designed so you can keep it in your child classes and only override findProducts().
  * You will automatically gain product caching (see class property), grid classes, pagination and more.
  */
 protected function compile()
 {
     // return message if no filter is set
     if ($this->iso_emptyFilter && !\Input::get('isorc') && !\Input::get('keywords')) {
         $this->Template->message = $this->replaceInsertTags($this->iso_noFilter);
         $this->Template->type = 'noFilter';
         $this->Template->products = array();
         return;
     }
     global $objPage;
     $intPage = $this->iso_category_scope == 'article' ? $GLOBALS['ISO_CONFIG']['current_article']['pid'] : $objPage->id;
     $arrProducts = null;
     $arrCacheIds = null;
     // Try to load the products from cache
     if ($this->blnCacheProducts && ($objCache = ProductCache::findForPageAndModule($intPage, $this->id)) !== null) {
         $arrCacheIds = $objCache->getProductIds();
         // Use the cache if keywords match. Otherwise we will use the product IDs as a "limit" for findProducts()
         if ($objCache->keywords == \Input::get('keywords')) {
             $arrCacheIds = $this->generatePagination($arrCacheIds);
             $objProducts = Product::findAvailableByIds($arrCacheIds, array('order' => \Database::getInstance()->findInSet(Product::getTable() . '.id', $arrCacheIds)));
             $arrProducts = null === $objProducts ? array() : $objProducts->getModels();
             // Cache is wrong, drop everything and run findProducts()
             if (count($arrProducts) != count($arrCacheIds)) {
                 $arrCacheIds = null;
                 $arrProducts = null;
             }
         }
     }
     if (!is_array($arrProducts)) {
         // Display "loading products" message and add cache flag
         if ($this->blnCacheProducts) {
             $blnCacheMessage = (bool) $this->iso_productcache[$intPage][(int) \Input::get('isorc')];
             if ($blnCacheMessage && !\Input::get('buildCache')) {
                 // Do not index or cache the page
                 $objPage->noSearch = 1;
                 $objPage->cache = 0;
                 $this->Template = new \Isotope\Template('mod_iso_productlist_caching');
                 $this->Template->message = $GLOBALS['TL_LANG']['MSC']['productcacheLoading'];
                 return;
             }
             // Start measuring how long it takes to load the products
             $start = microtime(true);
             // Load products
             $arrProducts = $this->findProducts($arrCacheIds);
             // Decide if we should show the "caching products" message the next time
             $end = microtime(true) - $start;
             $this->blnCacheProducts = $end > 1 ? true : false;
             $arrCacheMessage = $this->iso_productcache;
             if ($blnCacheMessage != $this->blnCacheProducts) {
                 $arrCacheMessage[$intPage][(int) \Input::get('isorc')] = $this->blnCacheProducts;
                 \Database::getInstance()->prepare("UPDATE tl_module SET iso_productcache=? WHERE id=?")->execute(serialize($arrCacheMessage), $this->id);
             }
             // Do not write cache if table is locked. That's the case if another process is already writing cache
             if (ProductCache::isWritable()) {
                 \Database::getInstance()->lockTables(array(ProductCache::getTable() => 'WRITE', 'tl_iso_product' => 'READ'));
                 $arrIds = array();
                 foreach ($arrProducts as $objProduct) {
                     $arrIds[] = $objProduct->id;
                 }
                 // Delete existing cache if necessary
                 ProductCache::deleteForPageAndModuleOrExpired($intPage, $this->id);
                 $objCache = ProductCache::createForPageAndModule($intPage, $this->id);
                 $objCache->expires = $this->getProductCacheExpiration();
                 $objCache->setProductIds($arrIds);
                 $objCache->save();
                 \Database::getInstance()->unlockTables();
             }
         } else {
             $arrProducts = $this->findProducts();
         }
         if (!empty($arrProducts)) {
             $arrProducts = $this->generatePagination($arrProducts);
         }
     }
     // No products found
     if (!is_array($arrProducts) || empty($arrProducts)) {
         $this->compileEmptyMessage();
         return;
     }
     $arrBuffer = array();
     $arrDefaultOptions = $this->getDefaultProductOptions();
     /** @var \Isotope\Model\Product\Standard $objProduct */
     foreach ($arrProducts as $objProduct) {
         $arrConfig = array('module' => $this, 'template' => $this->iso_list_layout ?: $objProduct->getRelated('type')->list_template, 'gallery' => $this->iso_gallery ?: $objProduct->getRelated('type')->list_gallery, 'buttons' => deserialize($this->iso_buttons, true), 'useQuantity' => $this->iso_use_quantity, 'jumpTo' => $this->findJumpToPage($objProduct));
         if (\Environment::get('isAjaxRequest') && \Input::post('AJAX_MODULE') == $this->id && \Input::post('AJAX_PRODUCT') == $objProduct->getProductId()) {
             $objResponse = new HtmlResponse($objProduct->generate($arrConfig));
             $objResponse->send();
         }
         $objProduct->mergeRow($arrDefaultOptions);
         // Must be done after setting options to generate the variant config into the URL
         if ($this->iso_jump_first && \Haste\Input\Input::getAutoItem('product', false, true) == '') {
             \Controller::redirect($objProduct->generateUrl($arrConfig['jumpTo']));
         }
         $arrCSS = deserialize($objProduct->cssID, true);
         $arrBuffer[] = array('cssID' => $arrCSS[0] != '' ? ' id="' . $arrCSS[0] . '"' : '', 'class' => trim('product ' . ($objProduct->isNew() ? 'new ' : '') . $arrCSS[1]), 'html' => $objProduct->generate($arrConfig), 'product' => $objProduct);
     }
     // HOOK: to add any product field or attribute to mod_iso_productlist template
     if (isset($GLOBALS['ISO_HOOKS']['generateProductList']) && is_array($GLOBALS['ISO_HOOKS']['generateProductList'])) {
         foreach ($GLOBALS['ISO_HOOKS']['generateProductList'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $arrBuffer = $objCallback->{$callback}[1]($arrBuffer, $arrProducts, $this->Template, $this);
         }
     }
     RowClass::withKey('class')->addCount('product_')->addEvenOdd('product_')->addFirstLast('product_')->addGridRows($this->iso_cols)->addGridCols($this->iso_cols)->applyTo($arrBuffer);
     $this->Template->products = $arrBuffer;
 }
 /**
  * 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));
 }