/**
  * Builds the DataObject of filtered products
  *
  * @return DataList
  * 
  * @author Sebastian Diel <sdiel@πixeltricks.de>
  * @since 23.09.2014
  */
 public function buildSearchResultProducts()
 {
     $searchResultProducts = $this->searchResultProducts;
     $productsPerPage = $this->getProductsPerPageSetting();
     $SQL_start = $this->getSqlOffset();
     $searchQuery = $this->getSearchQuery();
     $searchTerms = explode(' ', $searchQuery);
     $filter = '';
     $useExtensionResults = $this->extend('updateSearchResult', $searchResultProducts, $searchQuery, $SQL_start);
     if (empty($useExtensionResults)) {
         $this->listFilters['original'] = sprintf('
            "SilvercartProduct"."SilvercartProductGroupID" IS NOT NULL AND
            "SilvercartProduct"."SilvercartProductGroupID" > 0 AND
            "SPGPL"."ID" > 0 AND
            "SilvercartProduct"."isActive" = 1 AND (
                 (
                     "SilvercartProductLanguage"."Title" LIKE \'%s%%\' OR
                     "SilvercartProductLanguage"."ShortDescription" LIKE \'%s%%\' OR
                     "SilvercartProductLanguage"."LongDescription" LIKE \'%s%%\' OR
                     "SilvercartProductLanguage"."Title" LIKE \'%%%s%%\' OR
                     "SilvercartProductLanguage"."ShortDescription" LIKE \'%%%s%%\' OR
                     "SilvercartProductLanguage"."LongDescription" LIKE \'%%%s%%\'
                 ) OR
                "SilvercartProductLanguage"."MetaKeywords" LIKE \'%%%s%%\' OR
                "SilvercartProduct"."ProductNumberShop" LIKE \'%%%s%%\' OR
                "SilvercartProduct"."EANCode" LIKE \'%%%s%%\' OR
                 STRCMP(
                     SOUNDEX("SilvercartProductLanguage"."Title"), SOUNDEX(\'%s\')
                 ) = 0
             )
             ', $searchQuery, $searchQuery, $searchQuery, $searchQuery, $searchQuery, $searchQuery, $searchQuery, $searchQuery, $searchQuery, $searchQuery);
         if (count(self::$registeredFilterPlugins) > 0) {
             foreach (self::$registeredFilterPlugins as $registeredPlugin) {
                 $pluginFilters = $registeredPlugin->filter();
                 if (is_array($pluginFilters)) {
                     $this->listFilters = array_merge($this->listFilters, $pluginFilters);
                 }
             }
         }
         $this->extend('updateListFilters', $this->listFilters, $searchTerms);
         foreach ($this->listFilters as $listFilter) {
             if (empty($filter)) {
                 $filter = $listFilter;
             } else {
                 if (strpos(trim($listFilter), 'AND') !== 0 && strpos(trim($listFilter), 'OR') !== 0) {
                     $listFilter = 'AND ' . $listFilter;
                 }
                 $filter = '(' . $filter . ') ' . $listFilter;
             }
         }
         if (SilvercartProduct::defaultSort() == 'relevance') {
             $sort = '';
         } else {
             $sort = SilvercartProduct::defaultSort();
         }
         $searchResultProductsRaw = SilvercartProduct::getProducts($filter, $sort, array(array('table' => 'SilvercartProductGroupPage_Live', 'on' => '"SPGPL"."ID" = "SilvercartProduct"."SilvercartProductGroupID"', 'alias' => 'SPGPL')));
         $searchResultProducts = new PaginatedList($searchResultProductsRaw, $this->getRequest());
         $searchResultProducts->setPageStart($SQL_start);
         $searchResultProducts->setPageLength($productsPerPage);
     }
     $this->searchResultProducts = $searchResultProducts;
     $this->totalNumberOfProducts = $searchResultProducts->count();
     $searchQueryObject = SilvercartSearchQuery::get_by_query(Convert::raw2sql($searchQuery));
     if ($searchQueryObject->Hits != $this->totalNumberOfProducts) {
         $searchQueryObject->Hits = $this->totalNumberOfProducts;
         $searchQueryObject->write();
     }
     return $this->searchResultProducts;
 }
 /**
  * Returns a number of bargain products.
  * 
  * @return SS_List
  * 
  * @author Sebastian Diel <*****@*****.**>
  * @since 27.03.2012
  */
 public function Elements()
 {
     if (is_null($this->elements)) {
         if (!$this->numberOfProductsToFetch) {
             $this->numberOfProductsToFetch = SilvercartBargainProductsWidget::$defaults['numberOfProductsToFetch'];
         }
         if (SilvercartConfig::Pricetype() == 'net') {
             $priceField = 'PriceNetAmount';
         } else {
             $priceField = 'PriceGrossAmount';
         }
         switch ($this->fetchMethod) {
             case 'sortOrderAsc':
                 $sort = "\"SilvercartProduct\".\"MSRPriceAmount\" - \"SilvercartProduct\".\"PriceGrossAmount\" ASC";
                 break;
             case 'sortOrderDesc':
                 $sort = "\"SilvercartProduct\".\"MSRPriceAmount\" - \"SilvercartProduct\".\"PriceGrossAmount\" DESC";
                 break;
             case 'random':
             default:
                 $sort = "RAND()";
         }
         $this->listFilters = array();
         if (count(self::$registeredFilterPlugins) > 0) {
             foreach (self::$registeredFilterPlugins as $registeredPlugin) {
                 $pluginFilters = $registeredPlugin->filter();
                 if (is_array($pluginFilters)) {
                     $this->listFilters = array_merge($this->listFilters, $pluginFilters);
                 }
             }
         }
         $filter = sprintf("\"SilvercartProduct\".\"MSRPriceAmount\" IS NOT NULL \n                            AND \"SilvercartProduct\".\"MSRPriceAmount\" > 0\n                            AND \"SilvercartProduct\".\"%s\" < \"SilvercartProduct\".\"MSRPriceAmount\"", $priceField);
         foreach ($this->listFilters as $listFilterIdentifier => $listFilter) {
             $filter .= ' ' . $listFilter;
         }
         $products = SilvercartProduct::getProducts($filter, $sort, null, "0," . $this->numberOfProductsToFetch);
         $this->elements = $products;
         foreach ($this->elements as $element) {
             $element->addCartFormIdentifier = $this->ID . '_' . $element->ID;
         }
     }
     return $this->elements;
 }
 /**
  * All products of this group
  * 
  * @param int    $numberOfProducts The number of products to return
  * @param string $addFilter        Optional filter to add
  * 
  * @return DataList all products of this group or FALSE
  * 
  * @author Roland Lehmann <*****@*****.**>,
  *         Sebastian Diel <*****@*****.**>
  * @since 12.10.2013
  */
 public function getRandomProducts($numberOfProducts, $addFilter = null)
 {
     $listFilters = array();
     $filter = '';
     // ----------------------------------------------------------------
     // Get products that have this group set as mirror group
     // ----------------------------------------------------------------
     $mirroredProductIdList = '';
     $mirroredProductIDs = $this->getMirroredProductIDs();
     foreach ($mirroredProductIDs as $mirroredProductID) {
         $mirroredProductIdList .= sprintf("'%s',", $mirroredProductID);
     }
     if (!empty($mirroredProductIdList)) {
         $mirroredProductIdList = substr($mirroredProductIdList, 0, -1);
     }
     // ----------------------------------------------------------------
     // Get products that have this group set as main group
     // ----------------------------------------------------------------
     if ($this->isFilteredByManufacturer()) {
         $manufacturer = SilvercartManufacturer::getByUrlSegment($this->urlParams['ID']);
         if ($manufacturer) {
             $this->addListFilter('SilvercartManufacturerID', $manufacturer->ID);
         }
     }
     if (empty($mirroredProductIdList)) {
         $listFilters['original'] = sprintf("SilvercartProductGroupID = '%s'", $this->ID);
     } else {
         $listFilters['original'] = sprintf("(SilvercartProductGroupID = '%s' OR\n                  \"SilvercartProduct\".\"ID\" IN (%s))", $this->ID, $mirroredProductIdList);
     }
     foreach ($listFilters as $listFilterIdentifier => $listFilter) {
         $filter .= ' ' . $listFilter;
     }
     if (!is_null($addFilter)) {
         $filter .= ' AND ' . $addFilter;
     }
     $sort = 'RAND()';
     $products = SilvercartProduct::getProducts($filter, $sort, null, $numberOfProducts);
     return $products;
 }
 /**
  * Returns a number of products from the chosen productgroup.
  *
  * @return ArrayList
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 15.07.2015
  */
 public function getElementsByProductGroup()
 {
     $cache = false;
     $productGroupPage = Controller::curr();
     $elements = new PaginatedList(new ArrayList());
     if (method_exists($productGroupPage, 'getProductsPerPageSetting')) {
         $elements->pageLength = $productGroupPage->getProductsPerPageSetting();
         $elements->pageStart = $productGroupPage->getSqlOffset();
     }
     $pageEnd = $elements->pageStart + $elements->pageLength;
     $elementIdx = 0;
     $products = new ArrayList();
     if (!$productGroupPage instanceof SilvercartProductGroupPage_Controller || $productGroupPage->getProducts()->count() > 0) {
         return $elements;
     }
     $pageIDsToWorkOn = $productGroupPage->getDescendantIDList();
     if (is_array($pageIDsToWorkOn) && count($pageIDsToWorkOn) > 0) {
         if (SilvercartConfig::DefaultLanguage() != i18n::get_locale()) {
             $translationGroupQuery = 'SELECT "STTG"."TranslationGroupID" FROM "SiteTree_translationgroups" AS "STTG" WHERE "STTG"."OriginalID" IN (' . implode(',', $pageIDsToWorkOn) . ')';
             $translationIDsQuery = 'SELECT "STTG2"."OriginalID" FROM "SiteTree_translationgroups" AS "STTG2" WHERE "STTG2"."TranslationGroupID" IN (' . $translationGroupQuery . ')';
             $mirrored = 'SELECT "SPGMP"."SilvercartProductID" FROM SilvercartProduct_SilvercartProductGroupMirrorPages AS "SPGMP" WHERE "SPGMP"."SilvercartProductGroupPageID" IN (' . implode(',', $pageIDsToWorkOn) . ') OR "SPGMP"."SilvercartProductGroupPageID" IN (' . $translationIDsQuery . ')';
         } else {
             $mirrored = 'SELECT "SPGMP"."SilvercartProductID" FROM SilvercartProduct_SilvercartProductGroupMirrorPages AS "SPGMP" WHERE "SPGMP"."SilvercartProductGroupPageID" IN (' . implode(',', $pageIDsToWorkOn) . ')';
         }
         $products = SilvercartProduct::getProducts('("SilvercartProduct"."SilvercartProductGroupID" IN (' . implode(',', $pageIDsToWorkOn) . ') OR "SilvercartProduct"."ID" IN (' . $mirrored . '))');
     }
     foreach ($products as $product) {
         if ($elementIdx >= $elements->pageStart && $elementIdx < $pageEnd) {
             $product->addCartFormIdentifier = $this->ID . '_' . $product->ID;
             $elements->push($product);
         }
         $elementIdx++;
     }
     $elements->totalSize = $elementIdx;
     $productGroupPage->addTotalNumberOfProducts($elements->totalSize);
     return $elements;
 }