Index: trunk/core/units/general/helpers/search_helper.php =================================================================== diff -u -N -r7391 -r7702 --- trunk/core/units/general/helpers/search_helper.php (.../search_helper.php) (revision 7391) +++ trunk/core/units/general/helpers/search_helper.php (.../search_helper.php) (revision 7702) @@ -1,8 +1,8 @@ $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]; - + return $final; } - + function getPositiveKeywords($keyword) { $keywords = $this->splitKeyword($keyword); - + $ret = Array(); foreach ($keywords as $keyword => $sign) { if ($sign == '+' || $sign == '') { @@ -35,54 +35,54 @@ } return $ret; } - + function buildWhereClause($keyword, $fields) { $keywords = $this->splitKeyword($keyword); - + $normal_conditions = Array(); $plus_conditions = Array(); $minus_conditions = Array(); - + foreach ($keywords as $keyword => $sign) { switch ($sign) { case '+': $plus_conditions[] = implode(' LIKE "%'.$keyword.'%" OR ', $fields).' LIKE "%'.$keyword.'%"'; break; - + case '-': foreach ($fields as $field) { $condition[] = $field.' NOT LIKE "%'.$keyword.'%" OR '.$field.' IS NULL'; } $minus_conditions[] = '('.implode(') AND (', $condition).')'; break; - + case '': $keyword = str_replace('"', '\"', $keyword); $normal_conditions[] = implode(' LIKE "%'.$keyword.'%" OR ', $fields).' LIKE "%'.$keyword.'%"'; break; } } - + // building where clause if ($normal_conditions) { $where_clause = '('.implode(') OR (', $normal_conditions).')'; } else { $where_clause = '1'; } - + if ($plus_conditions) { $where_clause = '('.$where_clause.') AND ('.implode(') AND (', $plus_conditions).')'; } - + if ($minus_conditions) { $where_clause = '('.$where_clause.') AND ('.implode(') AND (', $minus_conditions).')'; } - + return $where_clause; } - + /** * Returns additional information about search field * @@ -104,16 +104,16 @@ } $table_name = ($sql_filter_type == 'where') ? $table_name : ''; - + // replace wid inside table name to WID_MARK constant value $is_temp_table = preg_match('/(.*)'.TABLE_PREFIX.'ses_'.$this->Application->GetSID().'(_[\d]+){0,1}_edit_(.*)/', $table_name, $regs); if ($is_temp_table) { $table_name = $regs[1].TABLE_PREFIX.'ses_'.EDIT_MARK.'_edit_'.$regs[3]; // edit_mark will be replaced with sid[_main_wid] in AddFilters } - + return Array ('field_name' => $field_name, 'field_type' => $field_type, 'table_name' => $table_name, 'sql_filter_type' => $sql_filter_type); } - + /** * Enter description here... * @@ -129,32 +129,32 @@ $search_keyword = str_replace('*', '%', $search_keyword); $custom_filter = $this->processCustomFilters($event); - + if(!$search_keyword && $custom_filter === false) { $this->resetSearch($event); return true; } - + if ($search_keyword) { $this->processAutomaticFilters($event, $search_keyword, $custom_filter); } } - - + + 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(); - + foreach ($search_fields as $search_field) { $custom_search = isset($custom_filter[$search_field]); - + $filter_data = $this->getSearchClause($object, $search_field, $search_keyword, $custom_search); - + if ($filter_data) { $search_filter[$search_field] = $filter_data; } @@ -164,7 +164,7 @@ } $this->Application->StoreVar($event->getPrefixSpecial().'_search_filter', serialize($search_filter) ); } - + /** * Returns search clause for any particular field * @@ -177,17 +177,17 @@ function getSearchClause(&$object, $field_name, $search_keyword, $custom_search) { $search_keywords = $this->splitKeyword($search_keyword); - + extract( $this->getFieldInformation($object, $field_name) ); // see getFieldInformation for more details - + $filter_value = ''; - + // get field clause by formatter name and/or parameters $formatter = getArrayValue($object->Fields[$field_name], 'formatter'); switch ($formatter) { case 'kOptionsFormatter': $search_keys = Array(); - + if ($custom_search === false) { // if keywords passed through simple search filter (on each grid) $use_phrases = getArrayValue($object->Fields[$field_name], 'use_phrases'); @@ -271,14 +271,14 @@ break; } } - + if ($filter_value) { return Array('type' => $sql_filter_type, 'value' => $filter_value); } - + return false; } - + /** * Processes custom filters from submit * @@ -288,7 +288,7 @@ function processCustomFilters(&$event) { $grid_name = $this->Application->GetVar('grid_name'); - + // update "custom filter" with values from submit: begin $view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view'); $custom_filters = $this->Application->RecallPersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name); @@ -312,7 +312,7 @@ if (isset($custom_filter[$field_name])) { // use isset, because non-existing key will cause "php notice"! unset($custom_filter[$field_name][$filter_type]); // remove filter - + if (!$custom_filter[$field_name]) { // if no filters left for field, then delete record at all unset($custom_filter[$field_name]); @@ -325,21 +325,21 @@ } } } - + if ($custom_filter) { $custom_filters[$grid_name] = $custom_filter; } else { unset($custom_filters[$grid_name]); } // update "custom filter" with values from submit: end - + if (!$custom_filter) { // in case when no filters specified, there are nothing to process $this->Application->StorePersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name, serialize($custom_filters) ); return false; } - + $object =& $event->getObject(); // don't recall it each time in getCustomFilterSearchClause $grid_info = $this->Application->getUnitOption($event->Prefix.'.'.$grid_name, 'Grids'); foreach ($custom_filter as $field_name => $field_options) { @@ -351,12 +351,12 @@ $custom_filter[$field_name][$filter_type] = $field_options; } } - + $custom_filters[$grid_name] = $custom_filter; $this->Application->StorePersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name, serialize($custom_filters) ); return $custom_filter; } - + /** * Return numeric range filter value + checking that it's number * @@ -367,23 +367,22 @@ { return strlen($value) && is_numeric($value) ? $this->Conn->qstr($value) : false; } - + function getCustomFilterSearchClause(&$object, $field_name, $filter_type, $field_options) { - if ($filter_type == 'date_range') { - // use timestamp field, not formatted one + // this is usually used for mutlilingual fields and date fields + if (isset($field_options['grid_options']['sort_field'])) { $field_name = $field_options['grid_options']['sort_field']; } - extract( $this->getFieldInformation($object, $field_name) ); // see getFieldInformation for more details - + $filter_value = ''; - + switch ($filter_type) { case 'range': $from = $this->getRangeValue($field_options['submit_value']['from']); $to = $this->getRangeValue($field_options['submit_value']['to']); - + if ($from !== false && $to !== false) { // add range filter $filter_value = $table_name.'`'.$field_name.'` >= '.$from.' AND '.$table_name.'`'.$field_name.'` <= '.$to; @@ -397,12 +396,12 @@ $filter_value = $table_name.'`'.$field_name.'` = '.$to; } break; - + case 'float_range': // MySQL can't compare values in "float" type columns using "=" operator $from = $this->getRangeValue($field_options['submit_value']['from']); $to = $this->getRangeValue($field_options['submit_value']['to']); - + if ($from !== false && $to !== false) { // add range filter $filter_value = $table_name.'`'.$field_name.'` >= '.$from.' AND '.$table_name.'`'.$field_name.'` <= '.$to; @@ -416,11 +415,11 @@ $filter_value = 'ABS('.$table_name.'`'.$field_name.'` - '.$to.') <= 0.0001'; } break; - + case 'date_range': $from = $this->processRangeField($object, $field_name, $field_options['submit_value'], 'from'); $to = $this->processRangeField($object, $field_name, $field_options['submit_value'], 'to'); - + $day_seconds = 23 * 60 * 60 + 59 * 60 + 59; if ($from !== false && $to === false) { $from = strtotime(date('Y-m-d', $from).' 00:00:00', $from); // reset to morning @@ -430,34 +429,34 @@ $to = strtotime(date('Y-m-d', $to).' 23:59:59', $to); // reset to evening $from = $to - $day_seconds; } - + if ($from !== false && $to !== false) { $filter_value = $table_name.'`'.$field_name.'` >= '.$from.' AND '.$table_name.'`'.$field_name.'` <= '.$to; } break; - + case 'equals': - case 'options': + case 'options': $field_value = strlen($field_options['submit_value']) ? $this->Conn->qstr($field_options['submit_value']) : false; if ($field_value) { $filter_value = $table_name.'`'.$field_name.'` = '.$field_value; } break; - + case 'like': $filter_value = $this->buildWhereClause($field_options['submit_value'], Array($table_name.'`'.$field_name.'`')); - break; - + break; + default: break; } - + $field_options['sql_filter_type'] = $sql_filter_type; $field_options['value'] = $filter_value; - + return $field_options; } - + /** * Enter description here... * @@ -471,11 +470,11 @@ if (!strlen($value[$type])) { return false; } - + $lang_current =& $this->Application->recallObject('lang.current'); $options = $object->GetFieldOptions($search_field); $dt_separator = isset($options['date_time_separator']) ? $options['date_time_separator'] : ' '; - + $tmp_value = explode($dt_separator, $value[$type], 2); if (count($tmp_value) == 1) { // time is missing, then guess it and add to date @@ -495,7 +494,7 @@ } return $value_ts; } - + /** * Resets current search * @@ -509,10 +508,10 @@ $view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view'); $this->Application->RemovePersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name); } - - - - + + + + } ?> \ No newline at end of file