/**
  * Find all products we need to list.
  * @return array
  */
 protected function findProducts($arrCacheIds = null)
 {
     $arrColumns = array();
     $arrCategories = $this->findCategories();
     //Get filters and sorting values
     list($arrValues, $strWhere, $strSorting) = $this->getFiltersAndSorting();
     //Handle no values
     if (!is_array($arrValues)) {
         $arrValues = array();
     }
     //Add categories to query
     $arrColumns[] = Product_Model::getTable() . ".id IN( SELECT pid FROM tl_iso_product_category WHERE page_id IN (" . implode(',', $arrCategories) . "))";
     //Get only cache IDs
     if (!empty($arrCacheIds) && is_array($arrCacheIds)) {
         $arrColumns[] = Product_Model::getTable() . ".id IN (" . implode(',', $arrCacheIds) . ")";
     }
     // Apply new/old product filter
     if ($this->iso_newFilter == 'show_new') {
         $arrColumns[] = Product_Model::getTable() . ".dateAdded>=" . Isotope::getConfig()->getNewProductLimit();
     } elseif ($this->iso_newFilter == 'show_old') {
         $arrColumns[] = Product_Model::getTable() . ".dateAdded<" . Isotope::getConfig()->getNewProductLimit();
     }
     if ($this->iso_list_where != '') {
         $arrColumns[] = Haste::getInstance()->call('replaceInsertTags', $this->iso_list_where);
     }
     //Add where query from filters/sorting
     if ($strWhere != '') {
         $arrColumns[] = $strWhere;
     }
     //Calculate the total on the query
     $intTotal = static::countPublishedBy($arrColumns, $arrValues);
     //Generate pagination and get offset
     $offset = $this->generatePagination($intTotal);
     //Build options
     $arrOptions = array('offset' => $offset, 'limit' => $this->numberOfItems && $this->perPage ? min($this->numberOfItems, $this->perPage) : ($this->perPage ?: $this->numberOfItems), 'order' => $strSorting);
     // Temporary fix for category sorting values
     $arrColumns[] = "c.page_id IN (" . implode(',', $arrCategories) . ")";
     //Run query
     $objProducts = Product_Model::findPublishedBy($arrColumns, $arrValues, $arrOptions);
     return null === $objProducts ? array() : $objProducts->getModels();
 }
 public function modifyDC(&$arrDca = null)
 {
     // get the product
     switch ($this->iso_direct_checkout_product_mode) {
         case 'product_type':
             if (($objTypes = FieldPaletteModel::findByPidAndTableAndField($this->objModule->id, 'tl_module', 'iso_direct_checkout_product_types')) !== null) {
                 while ($objTypes->next()) {
                     $arrColumns = array('type=?');
                     $arrValues = array($objTypes->iso_direct_checkout_product_type);
                     if ($this->iso_listingSortField) {
                         $arrSorting = array($this->iso_listingSortField => $this->iso_listingSortDirection == 'DESC' ? Sort::descending() : Sort::ascending());
                     } else {
                         $arrSorting = array();
                     }
                     $objProducts = Product::findPublishedBy($arrColumns, $arrValues, array('sorting' => $arrSorting));
                     if ($objProducts->count() > 0) {
                         $objProduct = $objProducts->current();
                         $this->arrProducts[] = array('product' => $objProduct, 'useQuantity' => $objTypes->iso_use_quantity);
                         $this->addProductFields($objProduct, $objTypes->iso_use_quantity, $objTypes->iso_addSubscriptionCheckbox, $arrDca);
                     }
                 }
             }
             break;
         default:
             if (($objProducts = FieldPaletteModel::findByPidAndTableAndField($this->objModule->id, 'tl_module', 'iso_direct_checkout_products')) !== null) {
                 while ($objProducts->next()) {
                     $objProduct = Product::findByPk($objProducts->iso_direct_checkout_product);
                     $this->arrProducts[] = array('product' => $objProduct, 'useQuantity' => $objProducts->iso_use_quantity);
                     $this->addProductFields($objProduct, $objProducts->iso_use_quantity, $objProducts->iso_addSubscriptionCheckbox, $arrDca);
                 }
             }
             break;
     }
     // add address fields
     \Controller::loadDataContainer('tl_iso_address');
     \System::loadLanguageFile('tl_iso_address');
     $arrAddressFields = deserialize(Config::findByPk($this->iso_config_id)->address_fields, true);
     // add billing address fields
     foreach ($arrAddressFields as $strName => $arrAddressField) {
         $arrData = $GLOBALS['TL_DCA']['tl_iso_address']['fields'][$strName];
         if (!is_array($arrData) || $arrAddressField['billing'] == 'disabled') {
             continue;
         }
         $arrData['eval']['mandatory'] = $arrAddressField['billing'] == 'mandatory';
         $this->arrBillingAddressFields[] = $strName;
         $this->addEditableField($strName, $arrData);
     }
     if ($this->iso_use_notes) {
         $this->addEditableField('notes', array('label' => &$GLOBALS['TL_LANG']['MSC']['iso_note'], 'exclude' => true, 'inputType' => 'textarea', 'eval' => array('tl_class' => 'clr w50'), 'sql' => "text NULL"));
     }
     $this->addEditableField('shippingaddress', array('label' => array($GLOBALS['TL_LANG']['MSC']['differentShippingAddress'], $GLOBALS['TL_LANG']['MSC']['differentShippingAddress']), 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => true)));
     // add shipping address fields
     $arrShippingAddressFields = array();
     foreach ($arrAddressFields as $strName => $arrAddressField) {
         $arrData = $GLOBALS['TL_DCA']['tl_iso_address']['fields'][$strName];
         if (!is_array($arrData) || $arrAddressField['shipping'] == 'disabled') {
             continue;
         }
         $arrData['eval']['mandatory'] = $arrAddressField['shipping'] == 'mandatory';
         $this->addEditableField('shippingaddress_' . $strName, $arrData);
         $arrShippingAddressFields[] = 'shippingaddress_' . $strName;
     }
     $this->dca['palettes']['__selector__'][] = 'shippingaddress';
     $this->dca['subpalettes']['shippingaddress'] = implode(',', $arrShippingAddressFields);
     $this->arrShippingAddressFields = $arrShippingAddressFields;
 }
 /**
  * Generate ajax
  */
 public function generateAjax()
 {
     if (!\Environment::get('isAjaxRequest')) {
         return;
     }
     // todo: Use the current filters too...
     if ($this->iso_searchAutocomplete && \Input::get('iso_autocomplete') == $this->id) {
         include_once TL_ROOT . '/system/modules/isotope_direct/config/stopwords.php';
         $arrWhere = array("c.page_id IN (" . implode(',', array_map('intval', $this->findCategories())) . ")");
         $arrValues = array();
         $keywords = explode(' ', \Input::get('query'));
         for ($i = 0; $i < count($keywords); $i++) {
             $strTerm = trim($keywords[$i]);
             if (empty($strTerm) || in_array(strtolower($strTerm), array_map('strtolower', $GLOBALS['KEYWORD_STOP_WORDS'])) || in_array(strtolower($strTerm), array_map('strtolower', $GLOBALS['KEYWORD_STOP_WORDS']))) {
                 continue;
             }
             $arrWhere[] = Product_Model::getTable() . "." . $this->iso_searchAutocomplete . " REGEXP ?";
             $arrValues[] = $strTerm;
         }
         if ($this->iso_list_where != '') {
             $arrWhere[] = Haste::getInstance()->call('replaceInsertTags', $this->iso_list_where);
         }
         $objProducts = Product_Model::findPublishedBy($arrWhere, $arrValues, array('order' => "c.sorting"));
         if (null === $objProducts) {
             $objResponse = new JsonResponse(array('suggestions' => array()));
             $objResponse->send();
         }
         $objResponse = new JsonResponse(array('suggestions' => array_values(array_map('html_entity_decode', $objProducts->fetchEach($this->iso_searchAutocomplete)))));
         $objResponse->send();
     }
 }