PodsUI::setup_fields PHP Method

setup_fields() public method

public setup_fields ( null $fields = null, string $which = 'fields' ) : array | boolean | mixed | null
$fields null
$which string
return array | boolean | mixed | null
    public function setup_fields($fields = null, $which = 'fields')
    {
        $init = false;
        if (null === $fields) {
            if (isset($this->fields[$which])) {
                $fields = (array) $this->fields[$which];
            } elseif (isset($this->fields['manage'])) {
                $fields = (array) $this->fields['manage'];
            } else {
                $fields = array();
            }
            if ('fields' == $which) {
                $init = true;
            }
        }
        if (!empty($fields)) {
            // Available Attributes
            // type = field type
            // type = date (data validation as date)
            // type = time (data validation as time)
            // type = datetime (data validation as datetime)
            // date_touch = use current timestamp when saving (even if readonly, if type is date-related)
            // date_touch_on_create = use current timestamp when saving ONLY on create (even if readonly, if type is date-related)
            // date_ongoing = use this additional field to search between as if the first is the "start" and the date_ongoing is the "end" for filter
            // type = text / other (single line text box)
            // type = desc (textarea)
            // type = number (data validation as int float)
            // type = decimal (data validation as decimal)
            // type = password (single line password box)
            // type = bool (checkbox)
            // type = related (select box)
            // related = table to relate to (if type=related) OR custom array of (key => label or comma separated values) items
            // related_field = field name on table to show (if type=related) - default "name"
            // related_multiple = true (ability to select multiple values if type=related)
            // related_sql = custom where / order by SQL (if type=related)
            // readonly = true (shows as text)
            // display = false (doesn't show on form, but can be saved)
            // search = this field is searchable
            // filter = this field will be independently searchable (by default, searchable fields are searched by the primary search box)
            // comments = comments to show for field
            // comments_top = true (shows comments above field instead of below)
            // real_name = the real name of the field (if using an alias for 'name')
            // group_related = true (uses HAVING instead of WHERE for filtering field)
            $new_fields = array();
            $filterable = false;
            if (empty($this->filters) && (empty($this->fields['search']) || 'search' == $which) && false !== $this->searchable) {
                $filterable = true;
                $this->filters = array();
            }
            foreach ($fields as $field => $attributes) {
                if (!is_array($attributes)) {
                    if (is_int($field)) {
                        $field = $attributes;
                        $attributes = array();
                    } else {
                        $attributes = array('label' => $attributes);
                    }
                }
                if (!isset($attributes['real_name'])) {
                    $attributes['real_name'] = pods_var('name', $attributes, $field);
                }
                if (is_object($this->pod) && isset($this->pod->fields) && isset($this->pod->fields[$attributes['real_name']])) {
                    $attributes = array_merge($this->pod->fields[$attributes['real_name']], $attributes);
                }
                if (!isset($attributes['options'])) {
                    $attributes['options'] = array();
                }
                if (!isset($attributes['id'])) {
                    $attributes['id'] = '';
                }
                if (!isset($attributes['label'])) {
                    $attributes['label'] = ucwords(str_replace('_', ' ', $field));
                }
                if (!isset($attributes['type'])) {
                    $attributes['type'] = 'text';
                }
                if (!isset($attributes['options']['date_format_type'])) {
                    $attributes['options']['date_format_type'] = 'date';
                }
                if ('related' != $attributes['type'] || !isset($attributes['related'])) {
                    $attributes['related'] = false;
                }
                if ('related' != $attributes['type'] || !isset($attributes['related_id'])) {
                    $attributes['related_id'] = 'id';
                }
                if ('related' != $attributes['type'] || !isset($attributes['related_field'])) {
                    $attributes['related_field'] = 'name';
                }
                if ('related' != $attributes['type'] || !isset($attributes['related_multiple'])) {
                    $attributes['related_multiple'] = false;
                }
                if ('related' != $attributes['type'] || !isset($attributes['related_sql'])) {
                    $attributes['related_sql'] = false;
                }
                if ('related' == $attributes['type'] && (is_array($attributes['related']) || strpos($attributes['related'], ','))) {
                    if (!is_array($attributes['related'])) {
                        $attributes['related'] = @explode(',', $attributes['related']);
                        $related_items = array();
                        foreach ($attributes['related'] as $key => $label) {
                            if (is_numeric($key)) {
                                $key = $label;
                                $label = ucwords(str_replace('_', ' ', $label));
                            }
                            $related_items[$key] = $label;
                        }
                        $attributes['related'] = $related_items;
                    }
                    if (empty($attributes['related'])) {
                        $attributes['related'] = false;
                    }
                }
                if (!isset($attributes['readonly'])) {
                    $attributes['readonly'] = false;
                }
                if (!isset($attributes['date_touch']) || 'date' != $attributes['type']) {
                    $attributes['date_touch'] = false;
                }
                if (!isset($attributes['date_touch_on_create']) || 'date' != $attributes['type']) {
                    $attributes['date_touch_on_create'] = false;
                }
                if (!isset($attributes['display'])) {
                    $attributes['display'] = true;
                }
                if (!isset($attributes['hidden'])) {
                    $attributes['hidden'] = false;
                }
                if (!isset($attributes['sortable']) || false === $this->sortable) {
                    $attributes['sortable'] = $this->sortable;
                }
                if (!isset($attributes['options']['search']) || false === $this->searchable) {
                    $attributes['options']['search'] = $this->searchable;
                }
                if (!isset($attributes['options']['filter']) || false === $this->searchable) {
                    $attributes['options']['filter'] = $this->searchable;
                }
                /*if ( false !== $attributes[ 'options' ][ 'filter' ] && false !== $filterable )
                  $this->filters[] = $field;*/
                if (false === $attributes['options']['filter'] || !isset($attributes['filter_label']) || !in_array($field, $this->filters)) {
                    $attributes['filter_label'] = $attributes['label'];
                }
                if (false === $attributes['options']['filter'] || !isset($attributes['filter_default']) || !in_array($field, $this->filters)) {
                    $attributes['filter_default'] = false;
                }
                if (false === $attributes['options']['filter'] || !isset($attributes['date_ongoing']) || 'date' != $attributes['type'] || !in_array($field, $this->filters)) {
                    $attributes['date_ongoing'] = false;
                }
                if (false === $attributes['options']['filter'] || !isset($attributes['date_ongoing']) || 'date' != $attributes['type'] || !isset($attributes['date_ongoing_default']) || !in_array($field, $this->filters)) {
                    $attributes['date_ongoing_default'] = false;
                }
                if (!isset($attributes['export'])) {
                    $attributes['export'] = true;
                }
                if (!isset($attributes['group_related'])) {
                    $attributes['group_related'] = false;
                }
                if (!isset($attributes['comments'])) {
                    $attributes['comments'] = '';
                }
                if (!isset($attributes['comments_top'])) {
                    $attributes['comments_top'] = false;
                }
                if (!isset($attributes['custom_view'])) {
                    $attributes['custom_view'] = false;
                }
                if (!isset($attributes['custom_input'])) {
                    $attributes['custom_input'] = false;
                }
                if (isset($attributes['display_helper'])) {
                    // pods ui backward compatibility
                    $attributes['custom_display'] = $attributes['display_helper'];
                }
                if (!isset($attributes['custom_display'])) {
                    $attributes['custom_display'] = false;
                }
                if (!isset($attributes['custom_relate'])) {
                    $attributes['custom_relate'] = false;
                }
                if (!isset($attributes['custom_form_display'])) {
                    $attributes['custom_form_display'] = false;
                }
                if (!isset($attributes['css_values'])) {
                    $attributes['css_values'] = true;
                }
                if ('search_columns' == $which && !$attributes['options']['search']) {
                    continue;
                }
                $attributes = PodsForm::field_setup($attributes, null, $attributes['type']);
                $new_fields[$field] = $attributes;
            }
            $fields = $new_fields;
        }
        if (false !== $init) {
            if ('fields' != $which && !empty($this->fields)) {
                $this->fields = $this->setup_fields($this->fields, 'fields');
            } else {
                $this->fields['manage'] = $fields;
            }
            if (!in_array('add', $this->actions_disabled) || !in_array('edit', $this->actions_disabled) || !in_array('duplicate', $this->actions_disabled)) {
                if ('form' != $which && isset($this->fields['form']) && is_array($this->fields['form'])) {
                    $this->fields['form'] = $this->setup_fields($this->fields['form'], 'form');
                } else {
                    $this->fields['form'] = $fields;
                }
                if (!in_array('add', $this->actions_disabled)) {
                    if ('add' != $which && isset($this->fields['add']) && is_array($this->fields['add'])) {
                        $this->fields['add'] = $this->setup_fields($this->fields['add'], 'add');
                    }
                }
                if (!in_array('edit', $this->actions_disabled)) {
                    if ('edit' != $which && isset($this->fields['edit']) && is_array($this->fields['edit'])) {
                        $this->fields['edit'] = $this->setup_fields($this->fields['edit'], 'edit');
                    }
                }
                if (!in_array('duplicate', $this->actions_disabled)) {
                    if ('duplicate' != $which && isset($this->fields['duplicate']) && is_array($this->fields['duplicate'])) {
                        $this->fields['duplicate'] = $this->setup_fields($this->fields['duplicate'], 'duplicate');
                    }
                }
            }
            if (false !== $this->searchable) {
                if ('search' != $which && isset($this->fields['search']) && !empty($this->fields['search'])) {
                    $this->fields['search'] = $this->setup_fields($this->fields['search'], 'search');
                } else {
                    $this->fields['search'] = $fields;
                }
            } else {
                $this->fields['search'] = false;
            }
            if (!in_array('export', $this->actions_disabled)) {
                if ('export' != $which && isset($this->fields['export']) && !empty($this->fields['export'])) {
                    $this->fields['export'] = $this->setup_fields($this->fields['export'], 'export');
                }
            }
            if (!in_array('reorder', $this->actions_disabled) && false !== $this->reorder['on']) {
                if ('reorder' != $which && isset($this->fields['reorder']) && !empty($this->fields['reorder'])) {
                    $this->fields['reorder'] = $this->setup_fields($this->fields['reorder'], 'reorder');
                } else {
                    $this->fields['reorder'] = $fields;
                }
            }
        }
        return $this->do_hook('setup_fields', $fields, $which, $init);
    }