Pods::find PHP Method

find() public method

Find items of a pod, much like WP_Query, but with advanced table handling.
Since: 2.0
public find ( array $params = null, integer $limit = 15, string $where = null, string $sql = null ) : Pods
$params array An associative array of parameters
$limit integer (optional) (deprecated) Limit the number of items to find, use -1 to return all items with no limit
$where string (optional) (deprecated) SQL WHERE declaration to use
$sql string (optional) (deprecated) For advanced use, a custom SQL query to run
return Pods The pod object
    public function find($params = null, $limit = 15, $where = null, $sql = null)
    {
        $tableless_field_types = PodsForm::tableless_field_types();
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
        $this->params = $params;
        $select = '`t`.*';
        if (!in_array($this->pod_data['type'], array('pod', 'table')) && 'table' == $this->pod_data['storage']) {
            $select .= ', `d`.*';
        }
        if (empty($this->data->table)) {
            return $this;
        }
        $defaults = array('table' => $this->data->table, 'select' => $select, 'join' => null, 'where' => $where, 'groupby' => null, 'having' => null, 'orderby' => null, 'limit' => (int) $limit, 'offset' => null, 'page' => (int) $this->page, 'page_var' => $this->page_var, 'pagination' => (bool) $this->pagination, 'search' => (bool) $this->search, 'search_var' => $this->search_var, 'search_query' => null, 'search_mode' => $this->search_mode, 'search_across' => false, 'search_across_picks' => false, 'search_across_files' => false, 'filters' => $this->filters, 'sql' => $sql, 'expires' => null, 'cache_mode' => 'cache');
        if (is_array($params)) {
            $params = (object) array_merge($defaults, $params);
        }
        if (is_object($params)) {
            $params = (object) array_merge($defaults, get_object_vars($params));
        } else {
            $defaults['orderby'] = $params;
            $params = (object) $defaults;
        }
        $params = apply_filters('pods_pods_find', $params);
        $params->limit = (int) $params->limit;
        if (0 == $params->limit) {
            $params->limit = -1;
        }
        $this->limit = (int) $params->limit;
        $this->offset = (int) $params->offset;
        $this->page = (int) $params->page;
        $this->page_var = $params->page_var;
        $this->pagination = (bool) $params->pagination;
        $this->search = (bool) $params->search;
        $this->search_var = $params->search_var;
        $params->join = (array) $params->join;
        if (empty($params->search_query)) {
            $params->search_query = pods_var($this->search_var, 'get', '');
        }
        // Allow orderby array ( 'field' => 'asc|desc' )
        if (!empty($params->orderby) && is_array($params->orderby)) {
            foreach ($params->orderby as $k => &$orderby) {
                if (!is_numeric($k)) {
                    $key = '';
                    $order = 'ASC';
                    if ('DESC' == strtoupper($orderby)) {
                        $order = 'DESC';
                    }
                    if (isset($this->fields[$k]) && in_array($this->fields[$k]['type'], $tableless_field_types)) {
                        if (in_array($this->fields[$k]['pick_object'], $simple_tableless_objects)) {
                            if ('table' == $this->pod_data['storage']) {
                                if (!in_array($this->pod_data['type'], array('pod', 'table'))) {
                                    $key = "`d`.`{$k}`";
                                } else {
                                    $key = "`t`.`{$k}`";
                                }
                            } else {
                                $key = "`{$k}`.`meta_value`";
                            }
                        } else {
                            $pick_val = $this->fields[$k]['pick_val'];
                            if ('__current__' == $pick_val) {
                                $pick_val = $this->pod;
                            }
                            $table = $this->api->get_table_info($this->fields[$k]['pick_object'], $pick_val);
                            if (!empty($table)) {
                                $key = "`{$k}`.`" . $table['field_index'] . '`';
                            }
                        }
                    }
                    if (empty($key)) {
                        if (!in_array($this->pod_data['type'], array('pod', 'table'))) {
                            if (isset($this->pod_data['object_fields'][$k])) {
                                $key = "`t`.`{$k}`";
                            } elseif (isset($this->fields[$k])) {
                                if ('table' == $this->pod_data['storage']) {
                                    $key = "`d`.`{$k}`";
                                } else {
                                    $key = "`{$k}`.`meta_value`";
                                }
                            } else {
                                foreach ($this->pod_data['object_fields'] as $object_field => $object_field_opt) {
                                    if ($object_field == $k || in_array($k, $object_field_opt['alias'])) {
                                        $key = "`t`.`{$object_field}`";
                                    }
                                }
                            }
                        } elseif (isset($this->fields[$k])) {
                            if ('table' == $this->pod_data['storage']) {
                                $key = "`t`.`{$k}`";
                            } else {
                                $key = "`{$k}`.`meta_value`";
                            }
                        }
                        if (empty($key)) {
                            $key = $k;
                            if (false === strpos($key, ' ') && false === strpos($key, '`')) {
                                $key = '`' . str_replace('.', '`.`', $key) . '`';
                            }
                        }
                    }
                    $orderby = $key;
                    if (false === strpos($orderby, ' ')) {
                        $orderby .= ' ' . $order;
                    }
                }
            }
        }
        // Add prefix to $params->orderby if needed
        if (!empty($params->orderby)) {
            if (!is_array($params->orderby)) {
                $params->orderby = array($params->orderby);
            }
            foreach ($params->orderby as &$prefix_orderby) {
                if (false === strpos($prefix_orderby, ',') && false === strpos($prefix_orderby, '(') && false === stripos($prefix_orderby, ' AS ') && false === strpos($prefix_orderby, '`') && false === strpos($prefix_orderby, '.')) {
                    if (false !== stripos($prefix_orderby, ' DESC')) {
                        $k = trim(str_ireplace(array('`', ' DESC'), '', $prefix_orderby));
                        $dir = 'DESC';
                    } else {
                        $k = trim(str_ireplace(array('`', ' ASC'), '', $prefix_orderby));
                        $dir = 'ASC';
                    }
                    $key = $k;
                    if (!in_array($this->pod_data['type'], array('pod', 'table'))) {
                        if (isset($this->pod_data['object_fields'][$k])) {
                            $key = "`t`.`{$k}`";
                        } elseif (isset($this->fields[$k])) {
                            if ('table' == $this->pod_data['storage']) {
                                $key = "`d`.`{$k}`";
                            } else {
                                $key = "`{$k}`.`meta_value`";
                            }
                        } else {
                            foreach ($this->pod_data['object_fields'] as $object_field => $object_field_opt) {
                                if ($object_field == $k || in_array($k, $object_field_opt['alias'])) {
                                    $key = "`t`.`{$object_field}`";
                                }
                            }
                        }
                    } elseif (isset($this->fields[$k])) {
                        if ('table' == $this->pod_data['storage']) {
                            $key = "`t`.`{$k}`";
                        } else {
                            $key = "`{$k}`.`meta_value`";
                        }
                    }
                    $prefix_orderby = "{$key} {$dir}";
                }
            }
        }
        $this->data->select($params);
        return $this;
    }

Usage Example

Exemplo 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 Pods::find