_parseQuery($this->_filter['query']); if ($this->_matches) { foreach (array_keys($this->_matches) as $key) { $this->_filter['query'] = str_replace($key, ' ', $this->_filter['query']); } } return $this; } /** * @param string $query * @return App_ECommerce_Product_Search_Obj */ protected function _parseQuery($query) { if (!$this->_matches) { $query = ' ' . trim($query) . ' '; $matches = array(); if (preg_match_all($this->_pattern, $query, $matches)) { foreach($matches[1] as $key => $value) { $value = rtrim($value, '.'); $value = str_replace(',', '', $value); $this->_matches[$matches[0][$key]] = $value; } } if ($this->_matches) { $compareFunction = function ($a, $b) { $aLength = strlen($a); $bLength = strlen($b); if ($aLength == $bLength) { return 0; } return ($aLength > $bLength) ? -1 : 1; }; uasort($this->_matches, $compareFunction); } } return $this; } protected function _where(Zend_Db_Select $select) { $select->where('`' . $this->_tableAlias . '`.`enabled` = ?', 'y'); if ($this->_matches) { $where = array(); foreach ($this->_matches as $value) { $value = str_replace(array('_', '%'), array('\\_', '\\%'), $value); $where[] = '`' . $this->_tableAlias . '`.`price` LIKE ' . $this->_db->quote('%' . $value . '%'); $where[] = '`' . $this->_tableAlias . '`.`weight` LIKE ' . $this->_db->quote('%' . $value . '%'); } $where = '(' . implode(' OR ', $where) . ')'; $select->where($where); } App_ECommerce_Product_Abstract_Obj::_where($select); $this->_whereHasEnabledCategory($select); return $this; } protected function _whereHasEnabledCategory(Zend_Db_Select $select) { $subSelect = $this->_db->select(); $subSelect->from($this->_getPair('Product2Category', 'p2c'), new Zend_Db_Expr('1')); $subSelect->join($this->_getPair('ProductCategory', 'pc'), '`pc`.`id` = `p2c`.`categoryId`', []); $subSelect->where('`p2c`.`productId` = ' . $this->_quoteField('id')); $subSelect->where('`pc`.`enabled` = ?', 'y'); $subSelect->limit(1); $select->where('EXISTS(' . $subSelect . ')'); return $this; } protected function _prepareSearchList(array &$list, array $providerOptions) { if ($this->_matches) { $this->_filter['query'] .= ' ' . implode(' ', array_keys($this->_matches)); } parent::_prepareSearchList($list, $providerOptions); $this->_markNumbers($list); return $this; } protected function _markNumbers(array &$list) { if (!$this->_matches) { return $this; } require_once 'lib/Smarty/app_plugins/modifier.money.php'; $highlightFields = array('price', 'weight'); // prepare highlight patterns foreach ($this->_matches as &$value) { $divisional = ''; if (false !== ($dotPos = strpos($value, '.'))) { $integer = substr($value, 0, $dotPos); $divisional = substr($value, $dotPos + 1); } else { $integer = $value; } $integer = implode(',?', str_split($integer)); $value = $integer . (($divisional) ? ('.' . $divisional) : ''); } unset($value); // highlight numbers foreach ($list as &$row) { foreach ($highlightFields as $field) { $row[$field] = smarty_modifier_money($row[$field]); $fieldValue = $row[$field]; foreach ($this->_matches as $value) { $row[$field] = preg_replace( array('/\s/', '/(' . $value . ')/i'), array(' ', '$1'), $row[$field] ); if ($fieldValue != $row[$field]) { break; } } } } return $this; } }