PodsData::query_fields PHP Method

query_fields() public static method

Get the string to use in a query for WHERE/HAVING, uses WP_Query meta_query arguments
Since: 2.3
public static query_fields ( array $fields, array $pod = null, object &$params = null ) : string | null
$fields array Array of field matches for querying
$pod array Related Pod
$params object Parameters passed from select()
return string | null Query string for WHERE/HAVING
    public static function query_fields($fields, $pod = null, &$params = null)
    {
        $query_fields = array();
        if (!is_object($params)) {
            $params = new stdClass();
        }
        if (!isset($params->query_field_level) || 0 == $params->query_field_level) {
            $params->query_fields = array();
            $params->query_field_syntax = false;
            $params->query_field_level = 1;
            if (!isset($params->where_default)) {
                $params->where_default = array();
            }
            if (!isset($params->where_defaulted)) {
                $params->where_defaulted = false;
            }
        }
        $current_level = $params->query_field_level;
        $relation = 'AND';
        if (isset($fields['relation'])) {
            $relation = strtoupper(trim(pods_var('relation', $fields, 'AND', null, true)));
            if ('AND' != $relation) {
                $relation = 'OR';
            }
            unset($fields['relation']);
        }
        foreach ($fields as $field => $match) {
            if (is_array($match) && isset($match['relation'])) {
                $params->query_field_level = $current_level + 1;
                $query_field = self::query_fields($match, $pod, $params);
                $params->query_field_level = $current_level;
                if (!empty($query_field)) {
                    $query_fields[] = $query_field;
                }
            } else {
                $query_field = self::query_field($field, $match, $pod, $params);
                if (!empty($query_field)) {
                    $query_fields[] = $query_field;
                }
            }
        }
        if (!empty($query_fields)) {
            // If post_status not sent, detect it
            if (!empty($pod) && 'post_type' == $pod['type'] && 1 == $current_level && !$params->where_defaulted && !empty($params->where_default)) {
                $post_status_found = false;
                if (!$params->query_field_syntax) {
                    $haystack = implode(' ', (array) $query_fields);
                    $haystack = preg_replace('/\\s/', ' ', $haystack);
                    $haystack = preg_replace('/\\w\\(/', ' ', $haystack);
                    $haystack = str_replace(array('(', ')', '  ', '\\\'', "\\\""), ' ', $haystack);
                    preg_match_all('/`?[\\w\\-]+`?(?:\\.`?[\\w\\-]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER);
                    $found = (array) @current($found);
                    foreach ($found as $value) {
                        $value = str_replace('`', '', $value);
                        $value = explode('.', $value);
                        if ('post_status' == $value[0] && 1 == count($value) || 2 == count($value) && 't' == $value[0] && 'post_status' == $value[1]) {
                            $post_status_found = true;
                            break;
                        }
                    }
                } elseif (!empty($params->query_fields) && in_array('post_status', $params->query_fields)) {
                    $post_status_found = true;
                }
                if (!$post_status_found) {
                    $query_fields[] = $params->where_default;
                }
            }
            if (1 < count($query_fields)) {
                $query_fields = '( ( ' . implode(' ) ' . $relation . ' ( ', $query_fields) . ' ) )';
            } else {
                $query_fields = '( ' . implode(' ' . $relation . ' ', $query_fields) . ' )';
            }
        } else {
            $query_fields = null;
        }
        // query_fields level complete
        if (1 == $params->query_field_level) {
            $params->query_field_level = 0;
        }
        return $query_fields;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param bool $full Whether to get ALL data or use pagination
  *
  * @return bool
  */
 public function get_data($params = null)
 {
     $action = $this->action;
     $defaults = array('full' => false, 'flatten' => true, 'fields' => null, 'type' => '');
     if (!empty($params) && is_array($params)) {
         $params = (object) array_merge($defaults, $params);
     } else {
         $params = (object) $defaults;
     }
     if (!in_array($action, array('manage', 'reorder'))) {
         $action = 'manage';
     }
     if (false !== $this->pod && is_object($this->pod) && ('Pods' == get_class($this->pod) || 'Pod' == get_class($this->pod))) {
         $orderby = array();
         $limit = $this->limit;
         $sql = null;
         if ('reorder' == $this->action) {
             if (!empty($this->reorder['orderby'])) {
                 $orderby[$this->reorder['orderby']] = $this->reorder['orderby_dir'];
             } else {
                 $orderby[$this->reorder['on']] = $this->reorder['orderby_dir'];
             }
             if (!empty($this->reorder['limit'])) {
                 $limit = $this->reorder['limit'];
             }
             if (!empty($this->reorder['sql'])) {
                 $sql = $this->reorder['sql'];
             }
         }
         if (!empty($this->orderby)) {
             $this->orderby = (array) $this->orderby;
             foreach ($this->orderby as $order) {
                 if (false === strpos(' ', $order) && !isset($orderby[$order])) {
                     $orderby[$order] = $this->orderby_dir;
                 }
             }
         }
         $find_params = array('where' => pods_var_raw($action, $this->where, null, null, true), 'orderby' => $orderby, 'page' => (int) $this->page, 'pagination' => true, 'limit' => (int) $limit, 'search' => $this->searchable, 'search_query' => $this->search, 'search_across' => $this->search_across, 'search_across_picks' => $this->search_across_picks, 'filters' => $this->filters, 'sql' => $sql);
         if (empty($find_params['where']) && $this->restricted($this->action)) {
             $find_params['where'] = $this->pods_data->query_fields($this->restrict[$this->action], is_object($this->pod) ? $this->pod->pod_data : null);
         }
         if ($params->full) {
             $find_params['limit'] = -1;
         }
         $find_params = array_merge($find_params, (array) $this->params);
         // Debug purposes
         if (1 == pods_var('pods_debug_params', 'get', 0) && pods_is_admin(array('pods'))) {
             pods_debug($find_params);
         }
         $this->pod->find($find_params);
         if (!$params->full) {
             $data = $this->pod->data();
             $this->data = $data;
             if (!empty($this->data)) {
                 $this->data_keys = array_keys($this->data);
             }
             $this->total = $this->pod->total();
             $this->total_found = $this->pod->total_found();
         } else {
             $this->data_full = array();
             $export_params = array('fields' => $params->fields, 'flatten' => true);
             if (in_array($params->type, array('json', 'xml'))) {
                 $export_params['flatten'] = false;
             }
             $export_params = $this->do_hook('export_options', $export_params, $params);
             while ($this->pod->fetch()) {
                 $this->data_full[$this->pod->id()] = $this->pod->export($export_params);
             }
             $this->pod->reset();
             return $this->data_full;
         }
     } else {
         if (!empty($this->data)) {
             return $this->data;
         }
         if (empty($this->sql['table'])) {
             return $this->data;
         }
         $orderby = '';
         if (!empty($this->orderby)) {
             $orderby = '`' . $this->orderby . '` ' . (false === strpos($this->orderby, ' ') ? strtoupper($this->orderby_dir) : '');
         }
         $find_params = array('table' => $this->sql['table'], 'where' => pods_var_raw($action, $this->where, null, null, true), 'orderby' => $orderby, 'page' => (int) $this->page, 'pagination' => true, 'limit' => (int) $this->limit, 'search' => $this->searchable, 'search_query' => $this->search, 'fields' => $this->fields['search']);
         if (empty($find_params['where']) && $this->restricted($this->action)) {
             $find_params['where'] = $this->pods_data->query_fields($this->restrict[$this->action], is_object($this->pod) ? $this->pod->pod_data : null);
         }
         if ($params->full) {
             $find_params['limit'] = -1;
         }
         // Debug purposes
         if (1 == pods_var('pods_debug_params', 'get', 0) && pods_is_admin(array('pods'))) {
             pods_debug($find_params);
         }
         $this->pods_data->select($find_params);
         if (!$params->full) {
             $this->data = $this->pods_data->data;
             if (!empty($this->data)) {
                 $this->data_keys = array_keys($this->data);
             }
             $this->total = $this->pods_data->total();
             $this->total_found = $this->pods_data->total_found();
         } else {
             $this->data_full = $this->pods_data->data;
             return $this->data_full;
         }
     }
     return $this->data;
 }
All Usage Examples Of PodsData::query_fields