Exemplo n.º 1
0
 /**
  * Adds the conditions for the selected attributes to the given search filter.
  *
  * @param array $params Associative list of parameters that should be used for filtering
  * @param MW_Common_Criteria_Interface $filter Criteria object for searching
  */
 protected function _addAttributeFilterByParam(array $params, MW_Common_Criteria_Interface $filter)
 {
     $attrids = isset($params['f_attrid']) ? (array) $params['f_attrid'] : array();
     if (!empty($attrids)) {
         $func = $filter->createFunction('catalog.index.attributeaggregate', array(array_keys($attrids)));
         $expr = array($filter->getConditions(), $filter->compare('==', $func, count($attrids)));
         $filter->setConditions($filter->combine('&&', $expr));
     }
 }
Exemplo n.º 2
0
 /**
  * Returns the search result of the statement combined with the given criteria.
  *
  * @param MW_DB_Connection_Interface $conn Database connection
  * @param MW_Common_Criteria_Interface $search Search criteria
  * @param string $cfgPathSearch Path to SQL statement in configuration for searching
  * @param string $cfgPathCount Path to SQL statement in configuration for counting
  * @param string[] $required Additional search keys to add conditions for even if no conditions are available
  * @param integer|null $total Contains the number of all records matching the criteria if not null
  * @param integer $sitelevel Constant from MShop_Locale_Manager_Abstract for defining which site IDs should be used for searching
  * @return MW_DB_Result_Interface SQL result object for accessing the found records
  * @throws MShop_Exception if no number of all matching records is available
  */
 protected function _searchItems(MW_DB_Connection_Interface $conn, MW_Common_Criteria_Interface $search, $cfgPathSearch, $cfgPathCount, array $required, &$total = null, $sitelevel = MShop_Locale_Manager_Abstract::SITE_ONE, array $plugins = array())
 {
     $joins = $cond = array();
     $sep = $this->_getKeySeparator();
     $conditions = $search->getConditions();
     $attributes = $this->getSearchAttributes();
     $siteIds = $this->_getSiteIds($sitelevel);
     $keys = $this->_getCriteriaKeyList($search, $required);
     $basekey = array_shift($required);
     foreach ($keys as $key) {
         if ($key !== $basekey) {
             $joins = array_merge($joins, $this->_getJoins($attributes, $key));
         }
         $name = $key . $sep . 'siteid';
         if (isset($attributes[$name])) {
             $cond[] = $search->compare('==', $name, $siteIds);
         }
     }
     if ($conditions !== null) {
         $cond[] = $conditions;
     }
     $search = clone $search;
     $search->setConditions($search->combine('&&', $cond));
     $types = $this->_getSearchTypes($attributes);
     $translations = $this->_getSearchTranslations($attributes);
     $find = array(':joins', ':cond', ':start', ':size');
     $replace = array(implode("\n", array_unique($joins)), $search->getConditionString($types, $translations, $plugins), $search->getSliceStart(), $search->getSliceSize());
     if (count($search->getSortations()) > 0) {
         $keys[] = 'orderby';
         $find[] = ':order';
         $replace[] = $search->getSortationString($types, $translations);
     }
     if ($total !== null) {
         $sql = new MW_Template_SQL($this->_context->getConfig()->get($cfgPathCount, $cfgPathCount));
         $sql->replace($find, $replace)->enable($keys);
         $time = microtime(true);
         $stmt = $conn->create($sql->str());
         $results = $stmt->execute();
         $row = $results->fetch();
         $results->finish();
         $this->_context->getLogger()->log(__METHOD__ . '(' . (microtime(true) - $time) * 1000 . 'ms): SQL statement: ' . $stmt, MW_Logger_Abstract::DEBUG);
         if ($row === false) {
             throw new MShop_Exception(sprintf('Total results value not found'));
         }
         $total = (int) $row['count'];
     }
     $sql = new MW_Template_SQL($this->_context->getConfig()->get($cfgPathSearch, $cfgPathSearch));
     $sql->replace($find, $replace)->enable($keys);
     $time = microtime(true);
     $stmt = $conn->create($sql->str());
     $results = $stmt->execute();
     $this->_context->getLogger()->log(__METHOD__ . '(' . (microtime(true) - $time) * 1000 . 'ms): SQL statement: ' . $stmt, MW_Logger_Abstract::DEBUG);
     return $results;
 }
Exemplo n.º 3
0
 /**
  * Returns the given search filter with the conditions attached for filtering texts.
  *
  * @param MW_Common_Criteria_Interface $search Criteria object used for product search
  * @param string $input Search string entered by the user
  * @param string $listtype List type of the text associated to the product, usually "default"
  * @return MW_Common_Criteria_Interface Criteria object containing the conditions for searching
  */
 public function addProductFilterText(MW_Common_Criteria_Interface $search, $input, $listtype = 'default')
 {
     $langid = $this->_getContext()->getLocale()->getLanguageId();
     $expr = array($search->compare('>', $search->createFunction('catalog.index.text.relevance', array($listtype, $langid, $input)), 0));
     $expr[] = $search->getConditions();
     $search->setConditions($search->combine('&&', $expr));
     return $search;
 }
Exemplo n.º 4
0
 /**
  * Returns a node and its descendants depending on the given resource.
  *
  * @param integer|null $id Retrieve nodes starting from the given ID
  * @param integer $level One of the level constants from MW_Tree_Manager_Abstract
  * * @param MW_Common_Criteria_Interface|null $condition Optional criteria object with conditions
  * @return MW_Tree_Node_Interface Node, maybe with subnodes
  */
 public function getNode($id = null, $level = MW_Tree_Manager_Abstract::LEVEL_TREE, MW_Common_Criteria_Interface $condition = null)
 {
     if ($id === null) {
         if (($node = $this->_getRootNode()) === null) {
             throw new MW_Tree_Exception('No root node available');
         }
         if ($level === MW_Tree_Manager_Abstract::LEVEL_ONE) {
             return $node;
         }
     } else {
         $node = $this->_getNodeById($id);
         if ($level === MW_Tree_Manager_Abstract::LEVEL_ONE) {
             return $node;
         }
     }
     $id = $node->getId();
     $numlevel = $this->_getLevelFromConstant($level);
     $search = $this->createSearch();
     if ($condition !== null) {
         $expr = array($search->getConditions(), $condition->getConditions());
         $search->setConditions($search->combine('&&', $expr));
     }
     $types = $this->_getSearchTypes($this->_searchConfig);
     $translations = $this->_getSearchTranslations($this->_searchConfig);
     $conditions = $search->getConditionString($types, $translations);
     $conn = $this->_dbm->acquire($this->_dbname);
     try {
         $stmt = $conn->create(str_replace(':cond', $conditions, $this->_config['get']));
         $stmt->bind(1, $id, MW_DB_Statement_Abstract::PARAM_INT);
         $stmt->bind(2, $numlevel, MW_DB_Statement_Abstract::PARAM_INT);
         $result = $stmt->execute();
         if (($row = $result->fetch()) === false) {
             throw new MW_Tree_Exception(sprintf('No node with ID "%1$d" found', $id));
         }
         $node = $this->_createNode($row);
         $this->_createTree($result, $node);
         $this->_dbm->release($conn, $this->_dbname);
     } catch (Exception $e) {
         $this->_dbm->release($conn, $this->_dbname);
         throw $e;
     }
     return $node;
 }
Exemplo n.º 5
0
 /**
  * Initializes the criteria object with conditions based on the given parameter.
  *
  * @param MW_Common_Criteria_Interface $criteria Criteria object
  * @param stdClass $params Object that may contain the properties "condition", "sort", "dir", "start" and "limit"
  */
 private function _initCriteriaConditions(MW_Common_Criteria_Interface $criteria, stdClass $params)
 {
     if (isset($params->condition) && is_object($params->condition)) {
         $existing = $criteria->getConditions();
         $criteria->setConditions($criteria->toConditions((array) $params->condition));
         $expr = array($criteria->getConditions(), $existing);
         $criteria->setConditions($criteria->combine('&&', $expr));
     }
 }