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
 /**
  * Searches for language items matching the given criteria.
  *
  * @param MW_Common_Criteria_Interface $search Search object
  * @param array $ref List of domains to fetch list items and referenced items for
  * @param integer &$total Number of items that are available in total
  * @return array List of items implementing MShop_Locale_Language_Item_Interface
  */
 public function searchItems(MW_Common_Criteria_Interface $search, array $ref = array(), &$total = null)
 {
     $items = array();
     $context = $this->_getContext();
     $config = $context->getConfig();
     $dbm = $context->getDatabaseManager();
     $dbname = $this->_getResourceName();
     $conn = $dbm->acquire($dbname);
     try {
         $attributes = $this->getSearchAttributes();
         $types = $this->_getSearchTypes($attributes);
         $translations = $this->_getSearchTranslations($attributes);
         $find = array(':cond', ':order', ':start', ':size');
         $replace = array($search->getConditionString($types, $translations), $search->getSortationString($types, $translations), $search->getSliceStart(), $search->getSliceSize());
         /** mshop/locale/manager/language/default/item/search
          * Retrieves the records matched by the given criteria in the database
          *
          * Fetches the records matched by the given criteria from the attribute
          * database. The records must be from one of the sites that are
          * configured via the context item. If the current site is part of
          * a tree of sites, the SELECT statement can retrieve all records
          * from the current site and the complete sub-tree of sites.
          *
          * As the records can normally be limited by criteria from sub-managers,
          * their tables must be joined in the SQL context. This is done by
          * using the "internaldeps" property from the definition of the ID
          * column of the sub-managers. These internal dependencies specify
          * the JOIN between the tables and the used columns for joining. The
          * ":joins" placeholder is then replaced by the JOIN strings from
          * the sub-managers.
          *
          * To limit the records matched, conditions can be added to the given
          * criteria object. It can contain comparisons like column names that
          * must match specific values which can be combined by AND, OR or NOT
          * operators. The resulting string of SQL conditions replaces the
          * ":cond" placeholder before the statement is sent to the database
          * server.
          *
          * If the records that are retrieved should be ordered by one or more
          * columns, the generated string of column / sort direction pairs
          * replaces the ":order" placeholder. In case no ordering is required,
          * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
          * markers is removed to speed up retrieving the records. Columns of
          * sub-managers can also be used for ordering the result set but then
          * no index can be used.
          *
          * The number of returned records can be limited and can start at any
          * number between the begining and the end of the result set. For that
          * the ":size" and ":start" placeholders are replaced by the
          * corresponding values from the criteria object. The default values
          * are 0 for the start and 100 for the size value.
          *
          * The SQL statement should conform to the ANSI standard to be
          * compatible with most relational database systems. This also
          * includes using double quotes for table and column names.
          *
          * @param string SQL statement for searching items
          * @since 2014.03
          * @category Developer
          * @see mshop/locale/manager/language/default/item/insert
          * @see mshop/locale/manager/language/default/item/update
          * @see mshop/locale/manager/language/default/item/delete
          * @see mshop/locale/manager/language/default/item/count
          */
         $path = 'mshop/locale/manager/language/default/item/search';
         $sql = $config->get($path, $path);
         $results = $this->_getSearchResults($conn, str_replace($find, $replace, $sql));
         try {
             while (($row = $results->fetch()) !== false) {
                 $items[$row['id']] = $this->_createItem($row);
             }
         } catch (Exception $e) {
             $results->finish();
             throw $e;
         }
         if ($total !== null) {
             /** mshop/locale/manager/language/default/item/count
              * Counts the number of records matched by the given criteria in the database
              *
              * Counts all records matched by the given criteria from the attribute
              * database. The records must be from one of the sites that are
              * configured via the context item. If the current site is part of
              * a tree of sites, the statement can count all records from the
              * current site and the complete sub-tree of sites.
              *
              * As the records can normally be limited by criteria from sub-managers,
              * their tables must be joined in the SQL context. This is done by
              * using the "internaldeps" property from the definition of the ID
              * column of the sub-managers. These internal dependencies specify
              * the JOIN between the tables and the used columns for joining. The
              * ":joins" placeholder is then replaced by the JOIN strings from
              * the sub-managers.
              *
              * To limit the records matched, conditions can be added to the given
              * criteria object. It can contain comparisons like column names that
              * must match specific values which can be combined by AND, OR or NOT
              * operators. The resulting string of SQL conditions replaces the
              * ":cond" placeholder before the statement is sent to the database
              * server.
              *
              * Both, the strings for ":joins" and for ":cond" are the same as for
              * the "search" SQL statement.
              *
              * Contrary to the "search" statement, it doesn't return any records
              * but instead the number of records that have been found. As counting
              * thousands of records can be a long running task, the maximum number
              * of counted records is limited for performance reasons.
              *
              * The SQL statement should conform to the ANSI standard to be
              * compatible with most relational database systems. This also
              * includes using double quotes for table and column names.
              *
              * @param string SQL statement for counting items
              * @since 2014.03
              * @category Developer
              * @see mshop/locale/manager/language/default/item/insert
              * @see mshop/locale/manager/language/default/item/update
              * @see mshop/locale/manager/language/default/item/delete
              * @see mshop/locale/manager/language/default/item/search
              */
             $path = 'mshop/locale/manager/language/default/item/count';
             $sql = $config->get($path, $path);
             $results = $this->_getSearchResults($conn, str_replace($find, $replace, $sql));
             $row = $results->fetch();
             $results->finish();
             if ($row === false) {
                 throw new MShop_Locale_Exception('No total results value found');
             }
             $total = $row['count'];
         }
         $dbm->release($conn, $dbname);
     } catch (Exception $e) {
         $dbm->release($conn, $dbname);
         throw $e;
     }
     return $items;
 }
Exemplo n.º 3
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.º 4
0
 /**
  * Search for currency items.
  *
  * @param MW_Common_Criteria_Interface $search Search object
  * @param integer &$total Number of items that are available in total
  *
  * @return array List of items implementing MShop_Locale_Item_Currency_Interface
  *
  * @throws MW_DB_Exception On failures with the db object
  * @throws MShop_Common_Exception On failures with the MW_Common_Criteria_ object
  * @throws MShop_Locale_Exception On failures with the currency item object
  */
 public function searchItems(MW_Common_Criteria_Interface $search, array $ref = array(), &$total = null)
 {
     $context = $this->_getContext();
     $config = $context->getConfig();
     $dbm = $context->getDatabaseManager();
     $dbname = $this->_getResourceName();
     $conn = $dbm->acquire($dbname);
     $items = array();
     try {
         $attributes = $this->getSearchAttributes();
         $types = $this->_getSearchTypes($attributes);
         $translations = $this->_getSearchTranslations($attributes);
         $find = array(':cond', ':order', ':start', ':size');
         $replace = array($search->getConditionString($types, $translations), $search->getSortationString($types, $translations), $search->getSliceStart(), $search->getSliceSize());
         $path = 'mshop/locale/manager/currency/default/item/search';
         $sql = $config->get($path, $path);
         $results = $this->_getSearchResults($conn, str_replace($find, $replace, $sql));
         try {
             while (($row = $results->fetch()) !== false) {
                 $items[$row['id']] = $this->_createItem($row);
             }
         } catch (Exception $e) {
             $results->finish();
             throw $e;
         }
         if ($total !== null) {
             $path = 'mshop/locale/manager/currency/default/item/count';
             $sql = $config->get($path, $path);
             $results = $this->_getSearchResults($conn, str_replace($find, $replace, $sql));
             $row = $results->fetch();
             $results->finish();
             if ($row === false) {
                 throw new MShop_Locale_Exception('No total results value found.');
             }
             $total = $row['count'];
         }
         $dbm->release($conn, $dbname);
     } catch (Exception $e) {
         $dbm->release($conn, $dbname);
         throw $e;
     }
     return $items;
 }
Exemplo n.º 5
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.º 6
0
 /**
  * Retrieves a list of nodes from the storage matching the given search criteria.
  *
  * @param MW_Common_Criteria_Interface $search Search criteria object
  * @param integer|null $id Search nodes starting at the node with the given ID
  * @return array List of nodes implementing MW_Tree_Node_Interface
  */
 public function searchNodes(MW_Common_Criteria_Interface $search, $id = null)
 {
     $left = 1;
     $right = 0x7fffffff;
     if ($id !== null) {
         $node = $this->_getNodeById($id);
         $left = $node->left;
         $right = $node->right;
     }
     $types = $this->_getSearchTypes($this->_searchConfig);
     $translations = $this->_getSearchTranslations($this->_searchConfig);
     $conditions = $search->getConditionString($types, $translations);
     $sortations = $search->getSortationString($types, $translations);
     $sql = str_replace(array(':cond', ':order'), array($conditions, $sortations), $this->_config['search']);
     $conn = $this->_dbm->acquire($this->_dbname);
     try {
         $stmt = $conn->create($sql);
         $stmt->bind(1, $left, MW_DB_Statement_Abstract::PARAM_INT);
         $stmt->bind(2, $right, MW_DB_Statement_Abstract::PARAM_INT);
         $result = $stmt->execute();
         try {
             $nodes = array();
             while (($row = $result->fetch()) !== false) {
                 $nodes[$row['id']] = $this->_createNode($row);
             }
         } catch (Exception $e) {
             $result->finish();
             throw $e;
         }
         $this->_dbm->release($conn, $this->_dbname);
     } catch (Exception $e) {
         $this->_dbm->release($conn, $this->_dbname);
         throw $e;
     }
     return $nodes;
 }
Exemplo n.º 7
0
 /**
  * Re-writes the index entries for all products that are search result of given criteria
  *
  * @param MW_Common_Criteria_Interface $search Search criteria
  * @param array $domains List of domains to be
  * @param integer $size Size of a chunk of products to handle at a time
  */
 protected function _writeIndex(MW_Common_Criteria_Interface $search, array $domains, $size)
 {
     $manager = MShop_Factory::createManager($this->_getContext(), 'product');
     $submanagers = $this->_getSubManagers();
     $start = 0;
     do {
         $search->setSlice($start, $size);
         $products = $manager->searchItems($search, $domains);
         $prodIds = array_keys($products);
         try {
             $this->begin();
             $this->deleteItems($prodIds);
             foreach ($submanagers as $submanager) {
                 $submanager->rebuildIndex($products);
             }
             $this->_saveSubProducts($products);
             $this->commit();
         } catch (Exception $e) {
             $this->_rollback();
             throw $e;
         }
         $this->_clearCache($prodIds);
         $count = count($products);
         $start += $count;
     } while ($count == $search->getSliceSize());
 }
Exemplo n.º 8
0
 /**
  * Searches for site items matching the given criteria.
  *
  * @param MW_Common_Criteria_Interface $search Search object
  * @param integer &$total Number of items that are available in total
  * @return array List of site items implementing MShop_Locale_Item_Site_Interface
  *
  * @throws MW_DB_Exception On failures with the db object
  * @throws MShop_Common_Exception On failures with the MW_Common_Criteria_ object
  * @throws MShop_Locale_Exception On failures with the site item object
  */
 public function searchItems(MW_Common_Criteria_Interface $search, array $ref = array(), &$total = null)
 {
     $items = array();
     $context = $this->_getContext();
     $dbm = $context->getDatabaseManager();
     $dbname = $this->_getResourceName();
     $conn = $dbm->acquire($dbname);
     try {
         $attributes = $this->getSearchAttributes();
         $types = $this->_getSearchTypes($attributes);
         $translations = $this->_getSearchTranslations($attributes);
         $find = array(':cond', ':order', ':start', ':size');
         $replace = array($search->getConditionString($types, $translations), $search->getSortationString($types, $translations), $search->getSliceStart(), $search->getSliceSize());
         $path = 'mshop/locale/manager/site/default/item/search';
         $sql = $context->getConfig()->get($path, $path);
         $results = $this->_getSearchResults($conn, str_replace($find, $replace, $sql));
         try {
             while (($row = $results->fetch()) !== false) {
                 $config = $row['config'];
                 if (($row['config'] = json_decode($row['config'], true)) === null) {
                     $msg = sprintf('Invalid JSON as result of search for ID "%2$s" in "%1$s": %3$s', 'mshop_locale.config', $row['id'], $config);
                     $this->_getContext()->getLogger()->log($msg, MW_Logger_Abstract::WARN);
                 }
                 $items[$row['id']] = $this->_createItem($row);
             }
         } catch (Exception $e) {
             $results->finish();
             throw $e;
         }
         if ($total !== null) {
             $path = 'mshop/locale/manager/site/default/item/count';
             $sql = $this->_getContext()->getConfig()->get($path, $path);
             $results = $this->_getSearchResults($conn, str_replace($find, $replace, $sql));
             $row = $results->fetch();
             $results->finish();
             if ($row === false) {
                 throw new MShop_Locale_Exception('No total results value found');
             }
             $total = $row['count'];
         }
         $dbm->release($conn, $dbname);
     } catch (Exception $e) {
         $dbm->release($conn, $dbname);
         throw $e;
     }
     return $items;
 }
Exemplo n.º 9
0
 /**
  * Initializes the criteria object with sortations 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 _initCriteriaSortations(MW_Common_Criteria_Interface $criteria, stdClass $params)
 {
     if (isset($params->sort) && isset($params->dir)) {
         $sortation = array();
         switch ($params->dir) {
             case 'ASC':
                 $sortation[] = $criteria->sort('+', $params->sort);
                 break;
             case 'DESC':
                 $sortation[] = $criteria->sort('-', $params->sort);
                 break;
             default:
                 throw new Controller_ExtJS_Exception(sprintf('Invalid sort direction "%1$s"', $params->sort));
         }
         $criteria->setSortations($sortation);
     }
     if ($this->_sort !== null) {
         $sort = $criteria->getSortations();
         $sort[] = $criteria->sort('+', $this->_sort);
         $criteria->setSortations($sort);
     }
 }