Exemple #1
0
 /**
  * Формирование Database_Query_Builder в части филтрации документов
  * 
  * @param array $filters
  * @param array $t
  * @param Database_Query_Builder $query
  * @return Database_Query_Builder
  */
 protected function _fetch_filters(array $filters, &$t, &$query)
 {
     if (empty($filters)) {
         return $query;
     }
     $field_names = array_flip($this->get_field_names());
     $ds_fields = $this->get_fields();
     $sys_fields = $this->get_system_fields();
     foreach ($filters as $pos => $data) {
         $params = array();
         $field = $data['field'];
         if (!empty($data['params'])) {
             parse_str($data['params'], $params);
         }
         $condition = $data['condition'];
         $type = $data['type'];
         $invert = !empty($data['invert']);
         $value = Arr::get($data, 'value');
         if ($value !== NULL and $type != self::VALUE_PLAIN) {
             $ctx = Context::instance();
             $request = Request::initial();
             switch ($type) {
                 case self::VALUE_BEHAVIOR:
                     if ($ctx->behavior_router() instanceof Behavior_Route) {
                         $value = $ctx->behavior_router()->param($value);
                         break;
                     }
                 case self::VALUE_GET:
                     $value = $request->query($value);
                     break;
                 case self::VALUE_POST:
                     $value = $request->post($value);
                     break;
                 default:
                     $value = $ctx->get($value);
                     break;
             }
         }
         if ($value === NULL) {
             continue;
         }
         $field_id = strpos($field, '$') == 1 ? Context::instance()->get(substr($field, 1)) : $field;
         if (isset($sys_fields[$field_id])) {
             $field = $sys_fields[$field_id];
         } else {
             if (isset($ds_fields[$field_id])) {
                 $field = $ds_fields[$field_id];
             } else {
                 if (isset($field_names[$field_id])) {
                     $field = $ds_fields[$field_names[$field_id]];
                 } else {
                     $field = NULL;
                 }
             }
         }
         if (!$field instanceof DataSource_Hybrid_Field) {
             continue;
         }
         if (!isset($t[$field->ds_id])) {
             $query->join('dshybrid_' . $field->ds_id, 'dfilter' . $pos)->on('dfilter' . $pos . '.id', '=', 'ds.id');
             $t[$field->ds_id] = TRUE;
         }
         $in = FALSE;
         switch ($condition) {
             case self::COND_EQ:
                 $value = explode(',', $value);
                 if ($value[0] == '*') {
                     break;
                 } elseif (count($value) > 1) {
                     $in = TRUE;
                 } else {
                     $value = $value[0];
                 }
                 break;
             case self::COND_CONTAINS:
                 $value = explode(',', $value);
                 $in = TRUE;
                 break;
             case self::COND_BTW:
                 $value = explode(',', $value, 2);
                 if (count($value) != 2) {
                     break;
                 }
                 break;
             default:
                 $value = $value;
         }
         $in = $in === TRUE ? 'IN' : '=';
         if (is_array($value)) {
             foreach ($value as $i => $v) {
                 if ($this->is_db_function($value)) {
                     $value[$i] = DB::expr($v);
                 }
             }
         } else {
             if ($this->is_db_function($value)) {
                 $value = DB::expr($value);
             }
         }
         if (isset($params['db_function']) and !$this->is_db_function($params['db_function'])) {
             unset($params['db_function']);
         }
         $conditions = array($in, 'BETWEEN', '>', '<', '>=', '<=', 'IN', 'LIKE');
         $condition = Arr::get($conditions, $condition, '=');
         if ($invert === TRUE) {
             switch ($condition) {
                 case '>':
                     $condition = '<=';
                     break;
                 case '<':
                     $condition = '>=';
                     break;
                 case '=':
                     $condition = '!=';
                     break;
                 case 'IN':
                 case 'LIKE':
                 case 'BETWEEN':
                     $condition = 'NOT ' . $condition;
                     break;
                 case '>=':
                     $condition = '<';
                     break;
                 case '<=':
                     $condition = '>';
                     break;
             }
         }
         $type = NULL;
         $fid = NULL;
         foreach ($ds_fields as $id => $f) {
             if ($f->key == $field->key) {
                 $type = $f->type;
                 $fid = $id;
             }
         }
         $field->filter_condition($query, $condition, $value, $params);
     }
     unset($field_names, $ds_fields, $sys_fields, $filters);
     return $query;
 }
Exemple #2
0
 public static function builder_call_slugs_children(Database_Query_Builder $builder, Jam_Event_Data $data, $slugs)
 {
     $builder->join(array('parent', 'parent'), 'LEFT')->where_open()->or_where('term.slug', 'IN', (array) $slugs)->or_where('parent.slug', 'IN', (array) $slugs)->where_close();
 }