Index: branches/5.2.x/core/units/helpers/search_helper.php =================================================================== diff -u -N -r14585 -r14598 --- branches/5.2.x/core/units/helpers/search_helper.php (.../search_helper.php) (revision 14585) +++ branches/5.2.x/core/units/helpers/search_helper.php (.../search_helper.php) (revision 14598) @@ -1,6 +1,6 @@ Application->ConfigValue('CheckStopWords')) { + if ( $this->Application->ConfigValue('CheckStopWords') ) { $keyword_after_remove = $this->_removeStopWords($keyword); - if ($keyword_after_remove) { + if ( $keyword_after_remove ) { // allow to search through stop word grid $keyword = $keyword_after_remove; } } + $final = Array (); $quotes_re = '/([+\-]?)"(.*?)"/'; $no_quotes_re = '/([+\-]?)([^ ]+)/'; - $quoted_kw = preg_match_all($quotes_re, $keyword, $res); - foreach ($res[2] as $index => $kw) $final[$kw] = $res[1][$index]; + preg_match_all($quotes_re, $keyword, $res); + foreach ($res[2] as $index => $kw) { + $final[$kw] = $res[1][$index]; + } + $keyword = preg_replace($quotes_re, '', $keyword); - $not_quoted_kw = preg_match_all($no_quotes_re, $keyword, $res); - foreach ($res[2] as $index => $kw) $final[$kw] = $res[1][$index]; + preg_match_all($no_quotes_re, $keyword, $res); + foreach ($res[2] as $index => $kw) { + $final[$kw] = $res[1][$index]; + } + return $final; } @@ -73,9 +81,7 @@ { $keywords = $this->splitKeyword( $this->transformWildcards($keyword) ); - $normal_conditions = Array(); - $plus_conditions = Array(); - $minus_conditions = Array(); + $normal_conditions = $plus_conditions = $minus_conditions = Array(); foreach ($keywords as $keyword => $sign) { $keyword = $this->Conn->escape($keyword); @@ -86,6 +92,8 @@ break; case '-': + $condition = Array (); + foreach ($fields as $field) { $condition[] = $field . " NOT LIKE '%" . $keyword . "%' OR " . $field . ' IS NULL'; } @@ -129,6 +137,7 @@ $sql_filter_type = $object->isVirtualField($field_name) ? 'having' : 'where'; $field_options = $object->GetFieldOptions($field_name); + $table_name = ''; $field_type = isset($field_options['type']) ? $field_options['type'] : 'string'; if (preg_match('/(.*)\.(.*)/', $field_name, $regs)) { @@ -177,39 +186,51 @@ } /** - * Enter description here... + * Performs new search on a given grid * * @param kEvent $event + * @return void + * @access public */ - function performSearch(&$event) + public function performSearch(&$event) { $object =& $event->getObject(); + /* @var $object kDBItem */ // process search keyword - $search_keyword = $this->Application->GetVar( $event->getPrefixSpecial(true).'_search_keyword'); - $this->Application->StoreVar( $event->getPrefixSpecial().'_search_keyword', $search_keyword); + $search_keyword = $this->Application->GetVar($event->getPrefixSpecial(true) . '_search_keyword'); + $this->Application->StoreVar($event->getPrefixSpecial() . '_search_keyword', $search_keyword); $custom_filter = $this->processCustomFilters($event); - if(!$search_keyword && $custom_filter === false) { + if ( !$search_keyword && $custom_filter === false ) { $this->resetSearch($event); - return true; + return ; } - if ($search_keyword) { + if ( $search_keyword ) { $this->processAutomaticFilters($event, $search_keyword, $custom_filter); } } - + /** + * Creates filtering sql clauses based on given search restrictions + * + * @param kEvent $event + * @param string $search_keyword + * @param Array $custom_filter + * @return void + */ function processAutomaticFilters(&$event, $search_keyword, $custom_filter) { $grid_name = $this->Application->GetVar('grid_name'); $grids = $this->Application->getUnitOption($event->Prefix, 'Grids'); $search_fields = array_keys($grids[$grid_name]['Fields']); $search_filter = Array(); + $object =& $event->getObject(); + /* @var $object kDBItem */ foreach ($search_fields as $search_field) { $custom_search = isset($custom_filter[$search_field]); @@ -585,6 +606,25 @@ } break; + case 'multioptions': + $field_value = strlen($field_options['submit_value']) ? $this->Conn->escape($field_options['submit_value']) : false; + + if ($field_value) { + $field_options2 = $object->GetFieldOptions($field_name); + $multiple = array_key_exists('multiple', $field_options2) && $field_options2['multiple']; + + $field_value = explode('|', substr($field_value, 1, -1)); + $field_value = array_map(Array (&$this->Conn, 'escape'), $field_value); + + if ($multiple) { + $filter_value = $table_name.'`'.$field_name.'` LIKE "%|' . implode('|%" OR ' . $table_name.'`'.$field_name.'` LIKE "%|', $field_value) . '|%"'; + } + else { + $filter_value = $table_name.'`'.$field_name.'` IN ('.implode(',', $field_value).')'; + } + } + break; + case 'like': $filter_value = $this->buildWhereClause($field_options['submit_value'], Array($table_name.'`'.$field_name.'`')); break; @@ -609,7 +649,7 @@ */ function processRangeField(&$object, $search_field, $value, $type) { - if (!strlen($value[$type])) { + if ( !strlen($value[$type]) ) { return false; } @@ -618,9 +658,9 @@ $value[$type] = trim($value[$type], $dt_separator); // trim any $tmp_value = explode($dt_separator, $value[$type], 2); - if (count($tmp_value) == 1) { + if ( count($tmp_value) == 1 ) { $time_format = $this->_getInputTimeFormat($options); - if ($time_format) { + if ( $time_format ) { // time is missing, but time format available -> guess time and add to date $time = ($type == 'from') ? adodb_mktime(0, 0, 0) : adodb_mktime(23, 59, 59); $time = adodb_date($time_format, $time); @@ -629,6 +669,7 @@ } $formatter =& $this->Application->recallObject($options['formatter']); + /* @var $formatter kFormatter */ $value_ts = $formatter->Parse($value[$type], $search_field, $object); @@ -650,11 +691,13 @@ */ function _getInputTimeFormat($field_options) { - if (array_key_exists('input_time_format', $field_options)) { + if ( array_key_exists('input_time_format', $field_options) ) { return $field_options['input_time_format']; } $lang_current =& $this->Application->recallObject('lang.current'); + /* @var $lang_current LanguagesItem */ + return $lang_current->GetDBField('InputTimeFormat'); } @@ -683,37 +726,46 @@ function SetComplexFilter(&$event, &$type_clauses, $types, $except_types) { $includes_or_filter =& $this->Application->makeClass('kMultipleFilter', Array (kDBList::FLT_TYPE_OR)); + /* @var $includes_or_filter kMultipleFilter */ + $excepts_and_filter =& $this->Application->makeClass('kMultipleFilter', Array (kDBList::FLT_TYPE_AND)); + /* @var $excepts_and_filter kMultipleFilter */ $includes_or_filter_h =& $this->Application->makeClass('kMultipleFilter', Array (kDBList::FLT_TYPE_OR)); + /* @var $includes_or_filter_h kMultipleFilter */ + $excepts_and_filter_h =& $this->Application->makeClass('kMultipleFilter', Array (kDBList::FLT_TYPE_AND)); + /* @var $excepts_and_filter_h kMultipleFilter */ - if ($types) { + if ( $types ) { $types = explode(',', $types); + foreach ($types as $type) { $type = trim($type); - if (isset($type_clauses[$type])) { - if ($type_clauses[$type]['having_filter']) { - $includes_or_filter_h->addFilter('filter_'.$type, $type_clauses[$type]['include']); - } else { - $includes_or_filter->addFilter('filter_'.$type, $type_clauses[$type]['include']); + if ( isset($type_clauses[$type]) ) { + if ( $type_clauses[$type]['having_filter'] ) { + $includes_or_filter_h->addFilter('filter_' . $type, $type_clauses[$type]['include']); } + else { + $includes_or_filter->addFilter('filter_' . $type, $type_clauses[$type]['include']); + } } } } - if ($except_types) { + if ( $except_types ) { $except_types = explode(',', $except_types); foreach ($except_types as $type) { $type = trim($type); - if (isset($type_clauses[$type])) { - if ($type_clauses[$type]['having_filter']) { - $excepts_and_filter_h->addFilter('filter_'.$type, $type_clauses[$type]['except']); - } else { - $excepts_and_filter->addFilter('filter_'.$type, $type_clauses[$type]['except']); + if ( isset($type_clauses[$type]) ) { + if ( $type_clauses[$type]['having_filter'] ) { + $excepts_and_filter_h->addFilter('filter_' . $type, $type_clauses[$type]['except']); } + else { + $excepts_and_filter->addFilter('filter_' . $type, $type_clauses[$type]['except']); + } } } }