PodsField_Pick::get_object_data PHP Method

get_object_data() public method

Get data from relationship objects
public get_object_data ( array $object_params = null ) : array | boolean
$object_params array Object data parameters
return array | boolean Object data
    public function get_object_data($object_params = null)
    {
        /**
         * @var $wpdb wpdb
         */
        global $wpdb;
        $object_params = array_merge(array('name' => '', 'value' => '', 'options' => array(), 'pod' => '', 'id' => '', 'context' => '', 'data_params' => array('query' => ''), 'page' => 1, 'limit' => 0), $object_params);
        $name = $object_params['name'];
        $value = $object_params['value'];
        $options = $object_params['options'] = (array) $object_params['options'];
        $pod = $object_params['pod'];
        $id = $object_params['id'];
        $context = $object_params['context'];
        $data_params = $object_params['data_params'] = (array) $object_params['data_params'];
        $page = min(1, (int) $object_params['page']);
        $limit = (int) $object_params['limit'];
        if (isset($options['options'])) {
            $options = array_merge($options, $options['options']);
            unset($options['options']);
        }
        $data = apply_filters('pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params);
        $items = array();
        if (!isset($options[self::$type . '_object'])) {
            $data = pods_var_raw('data', $options, array(), null, true);
        }
        $simple = false;
        if (null === $data) {
            $data = array();
            if ('custom-simple' == $options[self::$type . '_object']) {
                $custom = pods_var_raw(self::$type . '_custom', $options, '');
                $custom = apply_filters('pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params);
                if (!empty($custom)) {
                    if (!is_array($custom)) {
                        $data = array();
                        $custom = explode("\n", trim($custom));
                        foreach ($custom as $custom_value) {
                            $custom_label = explode('|', $custom_value);
                            if (empty($custom_label)) {
                                continue;
                            }
                            if (1 == count($custom_label)) {
                                $custom_label = $custom_value;
                            } else {
                                $custom_value = $custom_label[0];
                                $custom_label = $custom_label[1];
                            }
                            $custom_value = trim((string) $custom_value);
                            $custom_label = trim((string) $custom_label);
                            $data[$custom_value] = $custom_label;
                        }
                    } else {
                        $data = $custom;
                    }
                    $simple = true;
                }
            } elseif (isset(self::$related_objects[$options[self::$type . '_object']]) && isset(self::$related_objects[$options[self::$type . '_object']]['data']) && !empty(self::$related_objects[$options[self::$type . '_object']]['data'])) {
                $data = self::$related_objects[$options[self::$type . '_object']]['data'];
                $simple = true;
            } elseif (isset(self::$related_objects[$options[self::$type . '_object']]) && isset(self::$related_objects[$options[self::$type . '_object']]['data_callback']) && is_callable(self::$related_objects[$options[self::$type . '_object']]['data_callback'])) {
                $data = call_user_func_array(self::$related_objects[$options[self::$type . '_object']]['data_callback'], array($name, $value, $options, $pod, $id));
                $simple = true;
                // Cache data from callback
                if (!empty($data)) {
                    self::$related_objects[$options[self::$type . '_object']]['data'] = $data;
                }
            } elseif ('simple_value' != $context) {
                $pick_val = pods_var(self::$type . '_val', $options);
                if ('table' == pods_var(self::$type . '_object', $options)) {
                    $pick_val = pods_var(self::$type . '_table', $options, $pick_val, null, true);
                }
                if ('__current__' == $pick_val) {
                    if (is_object($pod)) {
                        $pick_val = $pod->pod;
                    } elseif (is_array($pod)) {
                        $pick_val = $pod['name'];
                    } elseif (0 < strlen($pod)) {
                        $pick_val = $pod;
                    }
                }
                $options['table_info'] = pods_api()->get_table_info(pods_var(self::$type . '_object', $options), $pick_val, null, null, $options);
                $search_data = pods_data();
                $search_data->table($options['table_info']);
                if (isset($options['table_info']['pod']) && !empty($options['table_info']['pod']) && isset($options['table_info']['pod']['name'])) {
                    $search_data->pod = $options['table_info']['pod']['name'];
                    $search_data->fields = $options['table_info']['pod']['fields'];
                }
                $params = array('select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`", 'table' => $search_data->table, 'where' => pods_var_raw(self::$type . '_where', $options, (array) $options['table_info']['where_default'], null, true), 'orderby' => pods_var_raw(self::$type . '_orderby', $options, null, null, true), 'groupby' => pods_var_raw(self::$type . '_groupby', $options, null, null, true), 'pagination' => false, 'search' => false);
                if (in_array($options[self::$type . '_object'], array('site', 'network'))) {
                    $params['select'] .= ', `t`.`path`';
                }
                if (!empty($params['where']) && (array) $options['table_info']['where_default'] != $params['where']) {
                    $params['where'] = pods_evaluate_tags($params['where'], true);
                }
                if (empty($params['where']) || !is_array($params['where']) && strlen(trim($params['where'])) < 1) {
                    $params['where'] = array();
                } elseif (!is_array($params['where'])) {
                    $params['where'] = (array) $params['where'];
                }
                if ('value_to_label' == $context) {
                    $params['where'][] = "`t`.`{$search_data->field_id}` = " . number_format($value, 0, '', '');
                }
                /* not needed yet
                                if ( !empty( $params[ 'orderby' ] ) )
                                    $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
                
                                if ( !empty( $params[ 'groupby' ] ) )
                                    $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
                $display = trim(pods_var(self::$type . '_display', $options), ' {@}');
                if (0 < strlen($display)) {
                    if (isset($options['table_info']['pod']) && !empty($options['table_info']['pod'])) {
                        if (isset($options['table_info']['pod']['object_fields']) && isset($options['table_info']['pod']['object_fields'][$display])) {
                            $search_data->field_index = $display;
                            $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                        } elseif (isset($options['table_info']['pod']['fields'][$display])) {
                            $search_data->field_index = $display;
                            if ('table' == $options['table_info']['pod']['storage'] && !in_array($options['table_info']['pod']['type'], array('pod', 'table'))) {
                                $params['select'] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
                            } elseif ('meta' == $options['table_info']['pod']['storage']) {
                                $params['select'] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
                            } else {
                                $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                            }
                        }
                    } elseif (isset($options['table_info']['object_fields']) && isset($options['table_info']['object_fields'][$display])) {
                        $search_data->field_index = $display;
                        $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                    }
                }
                $autocomplete = false;
                if ('single' == pods_var(self::$type . '_format_type', $options, 'single') && 'autocomplete' == pods_var(self::$type . '_format_single', $options, 'dropdown')) {
                    $autocomplete = true;
                } elseif ('multi' == pods_var(self::$type . '_format_type', $options, 'single') && 'autocomplete' == pods_var(self::$type . '_format_multi', $options, 'checkbox')) {
                    $autocomplete = true;
                }
                $hierarchy = false;
                if ('data' == $context && !$autocomplete) {
                    if ('single' == pods_var(self::$type . '_format_type', $options, 'single') && in_array(pods_var(self::$type . '_format_single', $options, 'dropdown'), array('dropdown', 'radio'))) {
                        $hierarchy = true;
                    } elseif ('multi' == pods_var(self::$type . '_format_type', $options, 'single') && in_array(pods_var(self::$type . '_format_multi', $options, 'checkbox'), array('multiselect', 'checkbox'))) {
                        $hierarchy = true;
                    }
                }
                if ($hierarchy && $options['table_info']['object_hierarchical'] && !empty($options['table_info']['field_parent'])) {
                    $params['select'] .= ', ' . $options['table_info']['field_parent_select'];
                }
                if ($autocomplete) {
                    if (0 == $limit) {
                        $limit = 30;
                    }
                    $params['limit'] = apply_filters('pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params);
                    if (is_array($value) && $params['limit'] < count($value)) {
                        $params['limit'] = count($value);
                    }
                    $params['page'] = $page;
                    if ('admin_ajax_relationship' == $context) {
                        $lookup_where = array($search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'");
                        // @todo Hook into WPML for each table
                        if ($wpdb->users == $search_data->table) {
                            $lookup_where['display_name'] = "`t`.`display_name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['user_login'] = "`t`.`user_login` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['user_email'] = "`t`.`user_email` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                        } elseif ($wpdb->posts == $search_data->table) {
                            $lookup_where['post_title'] = "`t`.`post_title` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['post_name'] = "`t`.`post_name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['post_content'] = "`t`.`post_content` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['post_excerpt'] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                        } elseif ($wpdb->terms == $search_data->table) {
                            $lookup_where['name'] = "`t`.`name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['slug'] = "`t`.`slug` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                        } elseif ($wpdb->comments == $search_data->table) {
                            $lookup_where['comment_content'] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['comment_author'] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                            $lookup_where['comment_author_email'] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                        }
                        $lookup_where = apply_filters('pods_form_ui_field_pick_autocomplete_lookup', $lookup_where, $data_params['query'], $name, $value, $options, $pod, $id, $object_params, $search_data);
                        if (!empty($lookup_where)) {
                            $params['where'][] = implode(' OR ', $lookup_where);
                        }
                        $orderby = array();
                        $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like($data_params['query']) . "%' ) DESC";
                        $pick_orderby = pods_var_raw(self::$type . '_orderby', $options, null, null, true);
                        if (0 < strlen($pick_orderby)) {
                            $orderby[] = $pick_orderby;
                        }
                        $orderby[] = "`t`.`{$search_data->field_index}`";
                        $orderby[] = "`t`.`{$search_data->field_id}`";
                        $params['orderby'] = $orderby;
                    }
                } elseif (0 < $limit) {
                    $params['limit'] = $limit;
                    $params['page'] = $page;
                }
                $extra = '';
                if ($wpdb->posts == $search_data->table) {
                    $extra = ', `t`.`post_type`';
                } elseif ($wpdb->terms == $search_data->table) {
                    $extra = ', `tt`.`taxonomy`';
                } elseif ($wpdb->comments == $search_data->table) {
                    $extra = ', `t`.`comment_type`';
                }
                $params['select'] .= $extra;
                if ('user' == pods_var(self::$type . '_object', $options)) {
                    $roles = pods_var(self::$type . '_user_role', $options);
                    if (!empty($roles)) {
                        $where = array();
                        foreach ((array) $roles as $role) {
                            if (empty($role) || pods_clean_name($role) != $role && sanitize_title($role) != $role) {
                                continue;
                            }
                            $where[] = $wpdb->base_prefix . (is_multisite() && !is_main_site() ? get_current_blog_id() . '_' : '') . 'capabilities.meta_value LIKE "%\\"' . pods_sanitize_like($role) . '\\"%"';
                        }
                        if (!empty($where)) {
                            $params['where'][] = implode(' OR ', $where);
                        }
                    }
                }
                $results = $search_data->select($params);
                if ($autocomplete && $params['limit'] < $search_data->total_found()) {
                    if (!empty($value)) {
                        $ids = $value;
                        if (is_array($ids) && isset($ids[0]) && is_array($ids[0])) {
                            $ids = wp_list_pluck($ids, $search_data->field_id);
                        }
                        if (is_array($ids)) {
                            $ids = implode(', ', $ids);
                        }
                        if (is_array($params['where'])) {
                            $params['where'] = implode(' AND ', $params['where']);
                        }
                        if (!empty($params['where'])) {
                            $params['where'] .= ' AND ';
                        }
                        $params['where'] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
                        $results = $search_data->select($params);
                    }
                } else {
                    $autocomplete = false;
                }
                if ('data' == $context) {
                    self::$field_data = array('field' => $name, 'id' => $options['id'], 'autocomplete' => $autocomplete);
                }
                if ($hierarchy && !$autocomplete && !empty($results) && $options['table_info']['object_hierarchical'] && !empty($options['table_info']['field_parent'])) {
                    $args = array('id' => $options['table_info']['field_id'], 'index' => $options['table_info']['field_index'], 'parent' => $options['table_info']['field_parent']);
                    $results = pods_hierarchical_select($results, $args);
                }
                $ids = array();
                if (!empty($results)) {
                    $display_filter = pods_var('display_filter', pods_var_raw('options', pods_var_raw($search_data->field_index, $search_data->pod_data['object_fields'])));
                    foreach ($results as $result) {
                        $result = get_object_vars($result);
                        if (!isset($result[$search_data->field_id]) || !isset($result[$search_data->field_index])) {
                            continue;
                        }
                        $result[$search_data->field_index] = trim($result[$search_data->field_index]);
                        $object = $object_type = '';
                        if ($wpdb->posts == $search_data->table && isset($result['post_type'])) {
                            $object = $result['post_type'];
                            $object_type = 'post_type';
                        } elseif ($wpdb->terms == $search_data->table && isset($result['taxonomy'])) {
                            $object = $result['taxonomy'];
                            $object_type = 'taxonomy';
                        }
                        if (0 < strlen($display_filter)) {
                            $display_filter_args = pods_var('display_filter_args', pods_var_raw('options', pods_var_raw($search_data->field_index, $search_data->pod_data['object_fields'])));
                            $args = array($display_filter, $result[$search_data->field_index]);
                            if (!empty($display_filter_args)) {
                                foreach ((array) $display_filter_args as $display_filter_arg) {
                                    if (isset($result[$display_filter_arg])) {
                                        $args[] = $result[$display_filter_arg];
                                    }
                                }
                            }
                            $result[$search_data->field_index] = call_user_func_array('apply_filters', $args);
                        }
                        if (in_array($options[self::$type . '_object'], array('site', 'network'))) {
                            $result[$search_data->field_index] = $result[$search_data->field_index] . $result['path'];
                        } elseif (strlen($result[$search_data->field_index]) < 1) {
                            $result[$search_data->field_index] = '(No Title)';
                        }
                        if ('admin_ajax_relationship' == $context) {
                            $items[] = array('id' => $result[$search_data->field_id], 'text' => $result[$search_data->field_index], 'image' => '');
                        } else {
                            $data[$result[$search_data->field_id]] = $result[$search_data->field_index];
                        }
                        $ids[] = $result[$search_data->field_id];
                    }
                }
            }
            if ($simple && 'admin_ajax_relationship' == $context) {
                $found_data = array();
                foreach ($data as $k => $v) {
                    if (false !== stripos($v, $data_params['query']) || false !== stripos($k, $data_params['query'])) {
                        $found_data[$k] = $v;
                    }
                }
                $data = $found_data;
            }
        }
        if ('admin_ajax_relationship' == $context) {
            if (empty($items) && !empty($data)) {
                foreach ($data as $k => $v) {
                    $items[] = array('id' => $k, 'text' => $v, 'image' => '');
                }
            }
            return $items;
        }
        return $data;
    }