Beispiel #1
0
    /**
     * Display HTML for all datatype fields
     *
     * @deprecated deprecated since 2.0
     */
    public function showform($id = null, $public_fields = null, $label = 'Save changes')
    {
        pods_deprecated('Pods::showform', '2.0');
        $public_columns =& $public_fields;
        $pod = $this->obj->pod;
        $pod_id = $this->obj->pod_id;
        $this->obj->type_counter = array();
        if (!empty($public_fields)) {
            $attributes = array();
            foreach ($public_fields as $key => $value) {
                if (is_array($public_fields[$key])) {
                    $attributes[$key] = $value;
                } else {
                    $attributes[$value] = array();
                }
            }
        }
        $fields = $this->obj->fields;
        // Re-order the fields if a public form
        if (!empty($attributes)) {
            $fields = array();
            foreach ($attributes as $key => $value) {
                if (isset($this->obj->fields[$key])) {
                    $fields[$key] = $this->obj->fields[$key];
                }
            }
        }
        do_action('pods_showform_pre', $pod_id, $public_fields, $label, $this);
        foreach ($fields as $key => $field) {
            if (!is_array($field) || in_array($key, array('created', 'modified'))) {
                continue;
            }
            // Pass options so they can be manipulated via form
            $field = array_merge($field['options'], $field);
            // Replace field attributes with public form attributes
            if (!empty($attributes) && is_array($attributes[$key])) {
                $field = array_merge($field, $attributes[$key]);
            }
            // Replace the input helper name with the helper code
            if (!empty($field['input_helper'])) {
                $helper = $this->obj->api->load_helper(array('name' => $field['input_helper']));
                $field['input_helper'] = '';
                if (!empty($helper)) {
                    $field['input_helper'] = $helper['code'];
                }
            }
            if (empty($field['label'])) {
                $field['label'] = ucwords($key);
            }
            if (1 == $field['required']) {
                $field['label'] .= ' <span class="red">*</span>';
            }
            if (!empty($field['pick_val'])) {
                $selected_ids = array();
                $pick_object = $field['pick_object'];
                $pick_val = $field['pick_val'];
                if ('pod' == $pick_object) {
                    $pick_pod = $this->obj->api->load_pod(array('name' => $pick_val));
                    $pick_object = $pick_pod['type'];
                    $pick_val = $pick_pod['name'];
                }
                $pick_table = $pick_join = $pick_where = '';
                $pick_field_id = 'id';
                $pick_field_name = 'name';
                switch ($pick_object) {
                    case 'pod':
                        $pick_table = "@wp_pods_{$pick_val}";
                        $pick_field_id = 'id';
                        $pick_field_name = 'name';
                        break;
                    case 'post_type':
                        $pick_table = '@wp_posts';
                        $pick_field_id = 'ID';
                        $pick_field_name = 'post_title';
                        $pick_where = "t.`post_type` = '{$pick_val}'";
                        break;
                    case 'taxonomy':
                        $pick_table = '@wp_terms';
                        $pick_field_id = 'term_id';
                        $pick_field_name = 'name';
                        $pick_join = "`@wp_term_taxonomy` AS tx ON tx.`term_id` = t.`term_id";
                        $pick_where = "tx.`taxonomy` = '{$pick_val}' AND tx.`taxonomy` IS NOT NULL";
                        break;
                    case 'user':
                        $pick_table = '@wp_users';
                        $pick_field_id = 'ID';
                        $pick_field_name = 'user_login';
                        break;
                    case 'comment':
                        $pick_table = '@wp_comments';
                        $pick_field_id = 'comment_ID';
                        $pick_field_name = 'comment_date';
                        $pick_where = "t.`comment_type` = '{$pick_val}'";
                        break;
                    case 'table':
                        $pick_table = "{$pick_val}";
                        $pick_field_id = 'id';
                        $pick_field_name = 'name';
                        break;
                }
                $sql = "SELECT `related_item_id` FROM `@wp_podsrel` WHERE `item_id` = %d AND `field_id` = %d";
                $sql = array($sql, array($id, $field['id']));
                $result = pods_query($sql, $this);
                foreach ($result as $row) {
                    $selected_ids[] = $row->related_item_id;
                }
                // Use default values for public forms
                if (empty($selected_ids) && !empty($field['default'])) {
                    $default_ids = $field['default'];
                    if (!is_array($field['default'])) {
                        $default_ids = explode(',', $default_ids);
                    }
                    foreach ($default_ids as $default_id) {
                        $default_id = pods_absint($default_id);
                        if (0 < $default_id) {
                            $selected_ids[] = $default_id;
                        }
                    }
                }
                // If the PICK field is unique, get values already chosen
                $exclude = false;
                if (1 == $field['unique']) {
                    $unique_where = empty($id) ? '' : " AND `item_id` != %d";
                    $sql = "SELECT `related_item_id` FROM `@wp_podsrel` WHERE `field_id` = %d {$unique_where}";
                    $sql = array($sql, array($field['id']));
                    if (!empty($id)) {
                        $sql[1][] = $id;
                    }
                    $result = pods_query($sql, $this);
                    if (!empty($result)) {
                        $exclude = array();
                        foreach ($result as $row) {
                            $exclude[] = (int) $row->related_item_id;
                        }
                        $exclude = implode(',', $exclude);
                    }
                }
                if (!empty($field['options']['pick_filter'])) {
                    $pick_where .= ' AND ' . $field['options']['pick_filter'];
                }
                $params = array('exclude' => $exclude, 'selected_ids' => $selected_ids, 'table' => $pick_table, 'field_id' => $pick_field_id, 'field_name' => $pick_field_name, 'join' => $pick_join, 'orderby' => $field['options']['pick_orderby'], 'where' => $pick_where);
                $this->obj->row[$key] = $this->get_dropdown_values($params);
            } else {
                // Set a default value if no value is entered
                if (!isset($this->obj->row[$key]) || (null === $this->obj->row[$key] || false === $this->obj->row[$key])) {
                    if (!empty($field['default'])) {
                        $this->obj->row[$key] = $field['default'];
                    } else {
                        $this->obj->row[$key] = null;
                    }
                }
            }
            $this->obj->build_field_html($field);
        }
        $uri_hash = wp_hash($_SERVER['REQUEST_URI']);
        $save_button_atts = array('type' => 'button', 'class' => 'button btn_save', 'value' => $label, 'onclick' => "saveForm(1)");
        $save_button_atts = apply_filters('pods_showform_save_button_atts', $save_button_atts, $this);
        $atts = '';
        foreach ($save_button_atts as $att => $value) {
            $atts .= ' ' . esc_attr($att) . '="' . esc_attr($value) . '"';
        }
        $save_button = '<input ' . $atts . '/>';
        ?>
    <div>
    <input type="hidden" class="form num id" value="<?php 
        echo $id;
        ?>
" />
    <input type="hidden" class="form txt pod" value="<?php 
        echo $pod;
        ?>
" />
    <input type="hidden" class="form txt pod_id" value="<?php 
        echo $pod_id;
        ?>
" />
    <input type="hidden" class="form txt form_count" value="1" />
    <input type="hidden" class="form txt token" value="<?php 
        echo pods_generate_key($pod, $uri_hash, $public_fields, 1);
        ?>
" />
    <input type="hidden" class="form txt uri_hash" value="<?php 
        echo $uri_hash;
        ?>
" />
    <?php 
        echo apply_filters('pods_showform_save_button', $save_button, $save_button_atts, $this);
        ?>
    </div>
<?php 
        do_action('pods_showform_post', $pod_id, $public_fields, $label, $this);
    }
Beispiel #2
0
    /**
     * Display HTML for all datatype fields
     */
    function showform($pod_id = null, $public_columns = null, $label = 'Save changes')
    {
        if (empty($this->datatype_id)) {
            echo "<e>Error: Pod name invalid</e>";
            return null;
        }
        $pod_id = absint($pod_id);
        $cache = PodCache::instance();
        $datatype = $this->datatype;
        $datatype_id = (int) $this->datatype_id;
        $this->data['pod_id'] = $pod_id = (int) $pod_id;
        $where = '';
        if (!empty($public_columns)) {
            foreach ($public_columns as $key => $val) {
                if (is_array($public_columns[$key])) {
                    $where[] = $key;
                    $attributes[$key] = $val;
                } else {
                    $where[] = $val;
                    $attributes[$val] = array();
                }
            }
            $where = "AND name IN ('" . implode("','", $where) . "')";
        }
        $result = pod_query("SELECT * FROM @wp_pod_fields WHERE datatype = {$datatype_id} {$where} ORDER BY weight ASC");
        $public_columns = array();
        while ($row = mysql_fetch_assoc($result)) {
            $fields[$row['name']] = $row;
            $public_columns[] = $row['name'];
        }
        // Re-order the fields if a public form
        if (!empty($attributes)) {
            $tmp = $fields;
            $fields = array();
            foreach ($attributes as $key => $val) {
                if (isset($tmp[$key])) {
                    $fields[$key] = $tmp[$key];
                }
            }
            unset($tmp);
        }
        // Edit an existing item
        if (!empty($pod_id)) {
            $sql = "\n            SELECT\n                `t`.*\n            FROM\n                @wp_pod `p`\n            INNER JOIN\n                `@wp_pod_tbl_{$datatype}` AS `t` ON `t`.`id` = `p`.`tbl_row_id`\n            WHERE\n                `p`.`id` = {$pod_id}\n            LIMIT\n                1\n            ";
            $result = pod_query($sql);
            if (0 < mysql_num_rows($result)) {
                $tbl_cols = mysql_fetch_assoc($result);
            }
        }
        $uri_hash = wp_hash($_SERVER['REQUEST_URI']);
        do_action('pods_showform_pre', $pod_id, $public_columns, $label, $this);
        foreach ($fields as $key => $field) {
            // Replace field attributes with public form attributes
            if (!empty($attributes) && is_array($attributes[$key])) {
                $field = array_merge($field, $attributes[$key]);
            }
            if (empty($field['label'])) {
                $field['label'] = ucwords($key);
            }
            if (1 == $field['required']) {
                $field['label'] .= ' <span class="red">*</span>';
            }
            if (!empty($field['pickval'])) {
                $val = array();
                $tbl_row_ids = array();
                $table = $field['pickval'];
                $result = pod_query("SELECT `id` FROM `@wp_pod_fields` WHERE `datatype` = {$datatype_id} AND `name` = '{$key}' LIMIT 1");
                $field_id = (int) mysql_result($result, 0);
                $result = pod_query("SELECT `tbl_row_id` FROM `@wp_pod_rel` WHERE `field_id` = {$field_id} AND `pod_id` = {$pod_id}");
                while ($row = mysql_fetch_assoc($result)) {
                    $tbl_row_ids[] = (int) $row['tbl_row_id'];
                }
                // Use default values for public forms
                if (empty($tbl_row_ids) && !empty($field['default'])) {
                    $tbl_row_ids = $field['default'];
                    if (!is_array($field['default'])) {
                        $tbl_row_ids = explode(',', $tbl_row_ids);
                        foreach ($tbl_row_ids as $row_key => $row_val) {
                            $tbl_row_ids[$row_key] = (int) trim($row_val);
                        }
                    }
                }
                // If the PICK column is unique, get values already chosen
                $unique_vals = false;
                if (1 == $field['unique']) {
                    $exclude = empty($pod_id) ? '' : "AND `pod_id` != {$pod_id}";
                    $result = pod_query("SELECT `tbl_row_id` FROM `@wp_pod_rel` WHERE `field_id` = {$field_id} {$exclude}");
                    if (0 < mysql_num_rows($result)) {
                        $unique_vals = array();
                        while ($row = mysql_fetch_assoc($result)) {
                            $unique_vals[] = (int) $row['tbl_row_id'];
                        }
                        $unique_vals = implode(',', $unique_vals);
                    }
                }
                $params = array('table' => $table, 'field_name' => null, 'tbl_row_ids' => $tbl_row_ids, 'unique_vals' => $unique_vals, 'pick_filter' => $field['pick_filter'], 'pick_orderby' => $field['pick_orderby']);
                $this->data[$key] = $this->get_dropdown_values($params);
            } else {
                // Set a default value if no value is entered
                if (!isset($this->data[$key]) && !empty($field['default'])) {
                    $this->data[$key] = $field['default'];
                } else {
                    $this->data[$key] = isset($tbl_cols[$key]) && $this->is_val($tbl_cols[$key]) ? $tbl_cols[$key] : null;
                }
            }
            $this->build_field_html($field);
        }
        $uri_hash = wp_hash($_SERVER['REQUEST_URI']);
        $save_button_atts = array('type' => 'button', 'class' => 'button btn_save', 'value' => $label, 'onclick' => "saveForm({$cache->form_count})");
        $save_button_atts = apply_filters('pods_showform_save_button_atts', $save_button_atts, $this);
        $atts = '';
        foreach ($save_button_atts as $att => $value) {
            $atts .= $att . '="' . $value . '" ';
        }
        $save_button = '<input ' . $atts . '/>';
        ?>
    <div>
    <input type="hidden" class="form num pod_id" value="<?php 
        echo $pod_id;
        ?>
" />
    <input type="hidden" class="form num tbl_row_id" value="<?php 
        echo !empty($tbl_cols) ? $tbl_cols['id'] : 0;
        ?>
" />
    <input type="hidden" class="form txt datatype" value="<?php 
        echo $datatype;
        ?>
" />
    <input type="hidden" class="form txt form_count" value="<?php 
        echo $cache->form_count;
        ?>
" />
    <input type="hidden" class="form txt token" value="<?php 
        echo pods_generate_key($datatype, $uri_hash, $public_columns, $cache->form_count);
        ?>
" />
    <input type="hidden" class="form txt uri_hash" value="<?php 
        echo $uri_hash;
        ?>
" />
	<?php 
        echo apply_filters('pods_showform_save_button', $save_button, $save_button_atts, $this);
        ?>
    </div>
<?php 
        do_action('pods_showform_post', $pod_id, $public_columns, $label, $this);
    }