Пример #1
0
 protected function hf_multiple(Utils_RecordBrowser_CritsSingle $crit, $callback, $field_def = null)
 {
     $sql = array();
     $vals = array();
     $field_sql_id = $this->get_field_sql($crit->get_field());
     $operator = self::transform_meta_operators_to_sql($crit->get_operator());
     $raw_sql_val = $crit->get_raw_sql_value();
     $value = $crit->get_value();
     $negation = $crit->get_negation();
     if ($operator == '!=') {
         $operator = '=';
         $negation = !$negation;
     }
     if (is_array($value)) {
         // for empty array it will give empty result
         $sql[] = 'false';
     } else {
         $value = array($value);
     }
     foreach ($value as $w) {
         $vv = explode('::', $w, 2);
         if (isset($vv[1]) && is_callable($vv)) {
             $sql[] = 'true';
             continue;
         }
         $args = array($field_sql_id, $operator, $w, $raw_sql_val, $field_def);
         list($sql2, $vals2) = call_user_func_array($callback, $args);
         if ($sql2) {
             $sql[] = $sql2;
             $vals = array_merge($vals, $vals2);
         }
     }
     $sql_str = implode(' OR ', $sql);
     if ($sql_str && $negation) {
         $sql_str = "NOT ({$sql_str})";
     }
     return array($sql_str, $vals);
 }
Пример #2
0
 public function build_from_array($crits)
 {
     $CRITS = array(new Utils_RecordBrowser_Crits());
     if (!$crits) {
         return $CRITS[0];
     }
     $CRITS_cnt = 1;
     /** @var Utils_RecordBrowser_Crits $current_crit */
     $current_crit = $CRITS[0];
     $or_started = $group_or = false;
     $group_or_cnt = null;
     foreach ($crits as $k => $v) {
         if ($k == '') {
             continue;
         }
         // initiate key modifiers for each crit
         $negative = $noquotes = $or_start = $or = $group_or_start = false;
         // default operator
         $operator = '=';
         // parse and remove modifiers
         while (($k[0] < 'a' || $k[0] > 'z') && ($k[0] < 'A' || $k[0] > 'Z') && $k[0] != ':') {
             if ($k[0] == '!') {
                 $negative = true;
             }
             if ($k[0] == '"') {
                 $noquotes = true;
             }
             if ($k[0] == '(') {
                 $or_start = true;
             }
             if ($k[0] == '|') {
                 $or = true;
             }
             if ($k[0] == '<') {
                 $operator = '<';
             }
             if ($k[0] == '>') {
                 $operator = '>';
             }
             if ($k[0] == '~') {
                 $operator = 'LIKE';
             }
             if ($k[0] == '^') {
                 $group_or_start = true;
             }
             // parse >= and <=
             if ($k[1] == '=' && $operator != 'LIKE') {
                 $operator .= '=';
                 $k = substr($k, 2);
             } else {
                 $k = substr($k, 1);
             }
             if (!isset($k[0])) {
                 trigger_error('Invalid criteria in build query: missing word. Crits:' . print_r($crits, true), E_USER_ERROR);
             }
         }
         $new_crit = new Utils_RecordBrowser_CritsSingle($k, $operator, $v);
         if ($noquotes) {
             $new_crit->set_raw_sql_value();
         }
         if ($negative) {
             $new_crit->set_negation();
         }
         if ($group_or_start) {
             $or_started = false;
             // group or takes precedence
             if ($group_or) {
                 $CRITS_cnt = $group_or_cnt + 1;
                 // return to group or crit
                 $current_crit = $CRITS[$group_or_cnt];
                 // get grouping crit
             } else {
                 $CC = new Utils_RecordBrowser_Crits();
                 $group_or_cnt = $CRITS_cnt;
                 $CRITS[$CRITS_cnt++] = $CC;
                 $current_crit->_and($CC);
                 $current_crit = $CC;
                 $group_or = true;
             }
             $CC = new Utils_RecordBrowser_Crits();
             $CRITS[$CRITS_cnt++] = $CC;
             $current_crit->_or($CC);
             $current_crit = $CC;
         }
         if ($or_start) {
             if ($or_started) {
                 $CRITS_cnt -= 1;
                 // pop current one
                 $current_crit = $CRITS[$CRITS_cnt - 1];
                 // get grouping crit
             }
             $CC = new Utils_RecordBrowser_Crits($new_crit);
             $CRITS[$CRITS_cnt++] = $CC;
             $current_crit->_and($CC);
             $current_crit = $CC;
             $or_started = true;
             continue;
         }
         if ($or) {
             $current_crit->_or($new_crit);
         } else {
             if ($or_started) {
                 $CRITS_cnt -= 1;
                 // pop current one
                 $current_crit = $CRITS[$CRITS_cnt - 1];
                 // get grouping crit
                 $or_started = false;
             }
             $current_crit->_and($new_crit);
         }
     }
     return $CRITS[0];
 }
Пример #3
0
 protected function validate_single(Utils_RecordBrowser_CritsSingle $crits, $record)
 {
     $id = isset($record['id']) ? $record['id'] : '';
     list($field, $subfield) = Utils_RecordBrowser_CritsSingle::parse_subfield($crits->get_field());
     $field = ltrim(Utils_RecordBrowserCommon::get_field_id($field), '_');
     $subfield = ltrim(Utils_RecordBrowserCommon::get_field_id($subfield), '_');
     $r_val = isset($record[$field]) ? $record[$field] : '';
     $crit_value = $crits->get_value();
     $field_definition = $this->get_field_definition($field);
     if ($subfield && $field_definition) {
         $sub_tab = isset($field_definition['ref_table']) ? $field_definition['ref_table'] : false;
         if ($sub_tab) {
             if (is_array($r_val)) {
                 foreach ($r_val as $k => $v) {
                     $r_val[$k] = Utils_RecordBrowserCommon::get_value($sub_tab, $v, $subfield);
                 }
             } else {
                 if ($r_val) {
                     $r_val = Utils_RecordBrowserCommon::get_value($sub_tab, $r_val, $subfield);
                 } else {
                     $r_val = '';
                 }
                 if (substr($r_val, 0, 2) == '__') {
                     $r_val = Utils_RecordBrowserCommon::decode_multi($r_val);
                 }
                 // FIXME need better check
             }
         }
     }
     $k = strtolower($field);
     $record[$k] = $r_val;
     $result = false;
     $transform_date = false;
     if ($k == 'created_on') {
         $transform_date = 'timestamp';
     } elseif ($k == 'edited_on') {
         $details = Utils_RecordBrowserCommon::get_record_info($this->tab, $id);
         $record[$k] = $details['edited_on'];
         $transform_date = 'timestamp';
     } elseif ($field_definition) {
         $type = $field_definition['type'];
         if ($type == 'timestamp') {
             $transform_date = 'timestamp';
         } elseif ($type == 'date') {
             $transform_date = 'date';
         }
     }
     if ($transform_date == 'timestamp' && $crit_value) {
         $crit_value = Base_RegionalSettingsCommon::reg2time($crit_value, false);
         $crit_value = date('Y-m-d H:i:s', $crit_value);
     } else {
         if ($transform_date == 'date' && $crit_value) {
             $crit_value = Base_RegionalSettingsCommon::reg2time($crit_value, false);
             $crit_value = date('Y-m-d', $crit_value);
         }
     }
     $vv = explode('::', $crit_value, 2);
     if (isset($vv[1]) && is_callable($vv)) {
         $result = call_user_func_array($vv, array($this->tab, &$record, $k, $crits));
     } else {
         if (is_array($record[$k])) {
             if ($crit_value) {
                 $result = in_array($crit_value, $record[$k]);
             } else {
                 $result = empty($record[$k]);
             }
             if ($crits->get_operator() == '!=') {
                 $result = !$result;
             }
         } else {
             switch ($crits->get_operator()) {
                 case '>':
                     $result = $record[$k] > $crit_value;
                     break;
                 case '>=':
                     $result = $record[$k] >= $crit_value;
                     break;
                 case '<':
                     $result = $record[$k] < $crit_value;
                     break;
                 case '<=':
                     $result = $record[$k] <= $crit_value;
                     break;
                 case '!=':
                     $result = $record[$k] != $crit_value;
                     break;
                 case '=':
                     $result = $record[$k] == $crit_value;
                     break;
                 case 'LIKE':
                     $result = self::check_like_match($record[$k], $crit_value);
                     break;
                 case 'NOT LIKE':
                     $result = !self::check_like_match($record[$k], $crit_value);
                     break;
             }
         }
     }
     if ($crits->get_negation()) {
         $result = !$result;
     }
     if (!$result) {
         $this->issues[] = $k;
     }
     return $result;
 }
Пример #4
0
 protected function build_single_crit_to_words(Utils_RecordBrowser_CritsSingle $crits)
 {
     $value = $crits->get_value();
     $operator = $crits->get_operator();
     list($field, $subfield) = Utils_RecordBrowser_CritsSingle::parse_subfield($crits->get_field());
     $negation = $crits->get_negation();
     $field_definition = $this->get_field_definition($field);
     $subquery_generated = false;
     if ($subfield) {
         $tab2 = isset($field_definition['ref_table']) ? $field_definition['ref_table'] : false;
         $single_tab = !($tab2 == '__RECORDSETS__' || count(explode(',', $tab2)) > 1);
         if ($tab2 && $single_tab) {
             $cb = new self($tab2);
             $cb->enable_html_decoration($this->html_decoration);
             $value = $cb->to_words(new Utils_RecordBrowser_CritsSingle($subfield, $operator, $value, $negation, $crits->get_raw_sql_value()));
             $subquery_generated = true;
         }
     }
     if (!is_array($value)) {
         $value = array($value);
     }
     foreach ($value as $k => $v) {
         if (is_bool($v)) {
             $value[$k] = $v ? __('true') : __('false');
         } elseif ($v === '' || $v === null) {
             $value[$k] = __('empty');
         } else {
             if ($field == ':Created_on' || $field == ':Edited_on') {
                 if (isset(Utils_RecordBrowserCommon::$date_values[$v])) {
                     $value[$k] = Utils_RecordBrowserCommon::$date_values[$v];
                 } else {
                     $value[$k] = Base_RegionalSettingsCommon::time2reg($v);
                 }
             } elseif ($field == ':Created_by') {
                 if (is_numeric($v)) {
                     $value[$k] = Base_UserCommon::get_user_login($v);
                 }
             } elseif ($field_definition) {
                 $vv = explode('::', $v, 2);
                 if (!(isset($vv[1]) && is_callable($vv)) && (is_numeric($v) || $field_definition['commondata'] || !isset($field_definition['ref_table']))) {
                     $new_val = Utils_RecordBrowserCommon::get_val($this->tab, $field, array($field => $v), true);
                     if ($new_val) {
                         $value[$k] = $new_val;
                     }
                 }
             }
         }
         if ($this->html_decoration) {
             if (!$subquery_generated) {
                 $value[$k] = '<strong>' . $value[$k] . '</strong>';
             }
         }
     }
     if ($operator == '!=') {
         $negation ^= $operator == '!=';
         $operator = '=';
     }
     switch ($field) {
         case ':Fav':
             $field = __('Favorite status');
             break;
         case ':Recent':
             $field = __('Recently viewed');
             break;
         case ':Sub':
             $field = __('Subscription status');
             break;
         case ':Created_by':
             $field = __('Created by');
             break;
         case ':Created_on':
             $field = __('Created on');
             break;
         case ':Edited_on':
             $field = __('Edited on');
             break;
         case 'id':
             $field = __('ID');
             break;
         default:
             if ($field_definition) {
                 $field = _V($field_definition['name']);
             }
     }
     if ($this->html_decoration) {
         $field = "<strong>{$field}</strong>";
     }
     if ($subquery_generated) {
         $operand = __('is set to record where');
     } else {
         switch ($operator) {
             case '<':
                 $operand = $negation ? __('is not smaller than') : __('is smaller than');
                 break;
             case '<=':
                 $operand = $negation ? __('is not smaller or equal to') : __('is smaller or equal to');
                 break;
             case '>':
                 $operand = $negation ? __('is not greater than') : __('is greater than');
                 break;
             case '>=':
                 $operand = $negation ? __('is not greater than') : __('is greater or equal to');
                 break;
             case 'LIKE':
                 $operand = $negation ? __('is not like') : __('is like');
                 break;
             case 'NOT LIKE':
                 $operand = $negation ? __('is like') : __('is not like');
                 break;
             default:
                 $operand = $negation ? __('is not equal to') : __('is equal to');
         }
     }
     $value_str = implode(' ' . __('or') . ' ', $value);
     if (count($value) > 1) {
         $value_str = "({$value_str})";
     }
     $ret = "{$field} {$operand} {$value_str}";
     if (!$this->html_decoration) {
         $ret = html_entity_decode($ret);
     }
     return array('str' => $ret, 'multiple' => false);
 }