/**
  * Getter for the language object for an object that has translations
  * I impemented it a a static method because it would be redundantly declared
  * in any multilanguage DataObject
  *
  * @param HasManyList $componentset has_many relation to be searched for the right translation
  * @param string      $locale       locale eg. de_DE, en_NZ, ...
  *
  * @return DataObject|false
  * 
  * @author Roland Lehmann <*****@*****.**>
  * @since 03.01.2012
  */
 public static function getLanguage($componentset, $locale = false)
 {
     $lang = false;
     if ($locale == false) {
         $locale = Translatable::get_current_locale();
     }
     if ($componentset->find('Locale', $locale)) {
         $lang = $componentset->find('Locale', $locale);
     } elseif (SilvercartConfig::useDefaultLanguageAsFallback()) {
         if ($componentset->find('Locale', SilvercartConfig::DefaultLanguage())) {
             $lang = $componentset->find('Locale', SilvercartConfig::DefaultLanguage());
         }
     }
     return $lang;
 }
 /**
  * Link to this product.
  * The link is in context of the current controller. If the current 
  * controller does not match some related product criteria (mirrored product 
  * group, translation of a mirrored product group or translation of main
  * group) the main group will be used as context.
  *
  * @return string URL of $this
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Roland Lehmann <*****@*****.**>,
  *         Ramon Kupper <*****@*****.**>
  * @since 03.03.2015
  */
 public function Link()
 {
     $link = '';
     if (Controller::curr() instanceof SilvercartProductGroupPage_Controller && !Controller::curr() instanceof SilvercartSearchResultsPage_Controller && $this->SilvercartProductGroupMirrorPages()->find('ID', Controller::curr()->data()->ID)) {
         $link = $this->buildLink(Controller::curr(), $this->title2urlSegment());
     } elseif (Controller::curr() instanceof SilvercartProductGroupPage_Controller && Translatable::get_current_locale() != SilvercartConfig::DefaultLanguage()) {
         Translatable::disable_locale_filter();
         if ($this->SilvercartProductGroupMirrorPages()->find('ID', Controller::curr()->getTranslation(SilvercartConfig::DefaultLanguage())->ID)) {
             $link = $this->buildLink(Controller::curr(), $this->title2urlSegment());
         }
         Translatable::enable_locale_filter();
     }
     if (empty($link) && $this->SilvercartProductGroup()) {
         $link = $this->buildLink($this->SilvercartProductGroup(), $this->title2urlSegment());
     }
     return $link;
 }
 /**
  * returns the context locale as string e.g. en_US
  * if no locale is given Silvercarts default language is used
  *
  * @return string
  */
 public function getCurrentLocale()
 {
     if (is_null($this->currentLocale)) {
         $currentLocale = $this->getCliArg('locale');
         if (is_null($currentLocale)) {
             $currentLocale = SilvercartConfig::DefaultLanguage();
         }
         $this->setCurrentLocale($currentLocale);
     }
     return $this->currentLocale;
 }
 /**
  * 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;
 }