/**
 * Has form.
 * 
 * @param type $post
 * @param type $post_type
 * @param type $data
 * @param type $parent_post_type
 * @return string 
 */
function wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $data, $parent_post_type)
{
    if (empty($post)) {
        return '';
    }
    $output = array();
    // Sorting
    $dir = isset($_GET['sort']) && $_GET['sort'] == 'ASC' ? 'DESC' : 'ASC';
    $dir_default = 'ASC';
    $sort_field = isset($_GET['field']) ? $_GET['field'] : '';
    // Cleanup data
    if (empty($data['fields_setting'])) {
        $data['fields_setting'] = 'all_cf';
    }
    // List items
    if (isset($_GET['sort']) && isset($_GET['field'])) {
        if ($_GET['field'] == '_wp_title') {
            $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&orderby=title&order=' . $_GET['sort']);
        } else {
            if ($_GET['field'] == '_wpcf_pr_parent') {
                global $wpdb;
                $query = "\n        SELECT p.ID, p.post_title, p.post_content, p.post_type, mr.meta_key, mr.meta_value\n        FROM {$wpdb->posts} p\n        INNER JOIN {$wpdb->postmeta} ml ON (p.ID = ml.post_id )\n        INNER JOIN {$wpdb->postmeta} mr ON (p.ID = mr.post_id )\n        INNER JOIN {$wpdb->posts} pp ON (pp.ID = mr.meta_value )\n        WHERE p.post_type = %s\n        AND p.post_status <> 'auto-draft'\n        AND ml.meta_key = %s\n        AND ml.meta_value = %s\n        AND mr.meta_key = %s\n        GROUP BY p.ID\n        ORDER BY pp.post_title " . esc_attr(strtoupper($_GET['sort'])) . "\n";
                $items = $wpdb->get_results($wpdb->prepare($query, $post_type, '_wpcf_belongs_' . $parent_post_type . '_id', $post->ID, '_wpcf_belongs_' . $_GET['post_type_sort_parent'] . '_id'));
                $exclude = array();
                foreach ($items as $key => $item) {
                    $exclude[] = $item->ID;
                }
                $additional = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&exclude=' . implode(',', $exclude));
                $items = array_merge(array_values($items), array_values($additional));
            } else {
                if ($_GET['field'] == '_wp_title' || $_GET['field'] == '_wp_body') {
                    if ($_GET['field'] == '_wp_body') {
                        $query['orderby'] = 'post_content';
                    } else {
                        $query['orderby'] = 'post_title';
                    }
                    $query['numberposts'] = -1;
                    $query['post_type'] = $post_type;
                    $query['order'] = strtoupper($_GET['sort']);
                    $items = query_posts($query);
                } else {
                    global $wpdb;
                    $field = wpcf_admin_fields_get_field($_GET['field']);
                    if (isset($field['type'])) {
                        $field_db_type = types_get_field_type($field['type']);
                    } else {
                        $field_db_type = 'CHAR';
                    }
                    $query = "\n        SELECT p.ID, p.post_title, p.post_content, p.post_type, CAST(mr.meta_key AS " . $field_db_type . "), mr.meta_value\n        FROM {$wpdb->posts} p\n        INNER JOIN {$wpdb->postmeta} ml ON (p.ID = ml.post_id )\n        INNER JOIN {$wpdb->postmeta} mr ON (p.ID = mr.post_id )\n        WHERE p.post_type = %s\n        AND p.post_status <> 'auto-draft'\n        AND ml.meta_key = %s\n        AND ml.meta_value = %s\n        AND mr.meta_key = %s\n        GROUP BY p.ID\n        ORDER BY mr.meta_value " . esc_attr(strtoupper($_GET['sort'])) . "\n";
                    $items = $wpdb->get_results($wpdb->prepare($query, $post_type, '_wpcf_belongs_' . $parent_post_type . '_id', $post->ID, $_GET['field']));
                    $exclude = array();
                    foreach ($items as $key => $item) {
                        $exclude[] = $item->ID;
                    }
                    $additional = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&exclude=' . implode(',', $exclude));
                    $items = array_merge(array_values($items), array_values($additional));
                }
            }
        }
    } else {
        $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID);
    }
    // Pagination
    $total_items = count($items);
    $per_page = 5;
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $numberposts = $page == 1 ? 1 : ($page - 1) * $per_page;
    $slice = $page == 1 ? 0 : ($page - 1) * $per_page;
    $next = count($items) >= $numberposts + $per_page;
    $prev = $page == 1 ? false : true;
    if ($total_items > $per_page) {
        $items = array_splice($items, $slice, $per_page);
    }
    $headers = array();
    $wpcf_fields = wpcf_admin_fields_get_fields(true);
    if ($data['fields_setting'] == 'specific') {
        $title_dir = $sort_field == '_wp_title' ? $dir : 'ASC';
        $_wp_title = '';
        $_wp_title .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
        $_wp_title .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
        $keys = array_keys($data['fields']);
        foreach ($keys as $k => $header) {
            if ($header == '_wp_title' || $header == '_wpcf_pr_parents') {
                continue;
            }
            if ($header == '_wp_body') {
                $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
                $headers[$k] = '';
                $headers[$k] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers[$k] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
            } else {
                if (strpos($header, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) {
                    wpcf_admin_post_field_load_js_css(wpcf_fields_type_action($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['type']));
                    $field_dir = $sort_field == $header ? $dir : $dir_default;
                    $headers[$k] = '';
                    $headers[$k] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                    $headers[$k] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['name'] . '</a>';
                } else {
                    $field_dir = $sort_field == $header ? $dir : $dir_default;
                    $headers[$k] = '';
                    $headers[$k] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                    $headers[$k] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $header . '</a>';
                }
            }
        }
        if (!empty($data['fields']['_wpcf_pr_parents'])) {
            foreach ($data['fields']['_wpcf_pr_parents'] as $temp_parent => $temp_data) {
                if ($temp_parent == $parent_post_type) {
                    continue;
                }
                $temp_parent_type = get_post_type_object($temp_parent);
                if (empty($temp_parent_type)) {
                    continue;
                }
                $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
                $headers['_wpcf_pr_parent_' . $temp_parent] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $temp_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
            }
        }
        array_unshift($headers, $_wp_title);
    } else {
        $item = new stdClass();
        $item->ID = 'new_' . mt_rand();
        $item->post_title = '';
        $item->post_content = '';
        $item->post_type = $post_type;
        $groups = wpcf_admin_post_get_post_groups_fields($item, 'post_relationships');
        $title_dir = $sort_field == '_wp_title' ? $dir : $dir_default;
        $headers['_wp_title'] = '';
        $headers['_wp_title'] .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
        $headers['_wp_title'] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
        if ($data['fields_setting'] == 'all_cf_standard') {
            $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
            $headers['_wp_body'] = '';
            $headers['_wp_body'] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
            $headers['_wp_body'] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
        }
        foreach ($groups as $group) {
            foreach ($group['fields'] as $field) {
                wpcf_admin_post_field_load_js_css(wpcf_fields_type_action($field['type']));
                $field_dir = $sort_field == wpcf_types_get_meta_prefix($field) . $field['slug'] ? $dir : $dir_default;
                $headers[$field['id']] = '';
                $headers[$field['id']] .= $sort_field == wpcf_types_get_meta_prefix($field) . $field['slug'] ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers[$field['id']] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . wpcf_types_get_meta_prefix($field) . $field['slug'] . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $field['name'] . '</a>';
            }
        }
        // Get all parents
        $item_parents = wpcf_pr_admin_get_belongs($post_type);
        if ($item_parents) {
            foreach ($item_parents as $temp_parent => $temp_data) {
                if ($temp_parent == $parent_post_type) {
                    continue;
                }
                $temp_parent_type = get_post_type_object($temp_parent);
                $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
                $headers['_wpcf_pr_parent_' . $temp_parent] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $temp_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
            }
        }
    }
    // If headers are empty, that means there is nothing to render
    if (empty($headers)) {
        return '';
    }
    $header = '<thead><tr><th class="wpcf-sortable">' . implode('&nbsp;&nbsp;&nbsp;</th><th class="wpcf-sortable">', $headers) . '&nbsp;&nbsp;&nbsp;</th><th>' . __('Action', 'wpcf') . '</th></tr></thead>';
    foreach ($items as $key => $item) {
        $output[] = wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, $item);
    }
    $return = '';
    $return .= wpcf_pr_admin_has_pagination($post, $post_type, $page, $prev, $next, $per_page, $total_items);
    $return .= '<table id="wpcf_pr_table_sortable_' . md5($post_type) . '" class="tablesorter wpcf_pr_table_sortable" cellpadding="0" cellspacing="0" style="width:100%;">' . $header . '<tbody>' . implode($output) . '</tbody></table>';
    $return .= wpcf_form_render_js_validation('#post', false);
    return $return;
}
 /**
  * getCustomFieldProperties function.
  *
  * @access private
  * @param mixed $field
  * @return array
  */
 private function getCustomFieldProperties($field_slug = '')
 {
     $type = 'CHAR';
     $input = 'textfield';
     $name = '';
     $relation = 'AND';
     $custom = '';
     $id = '';
     $is_types = false;
     $group = 'Custom fields';
     //	if( !function_exists('types_get_field_type') || !$field_slug ) return $type;
     // Note: this was making the select not being populated when Types was not active. Of course. Do not know for how long...
     $nice_name = explode('wpcf-', $field_slug);
     $search = isset($nice_name[1]) ? $nice_name[1] : $field_slug;
     $can_force_zero = false;
     $options = get_option('wpcf-fields', array());
     foreach ($options as $key => $value) {
         if ($key == $search) {
             if (function_exists('types_get_field_type')) {
                 $type = types_get_field_type($value['type']);
             }
             $input = $value['type'];
             if ($input == 'checkbox' && $value['data']['save_empty'] == 'yes') {
                 $can_force_zero = true;
             }
             $name = $value['name'];
             $relation = isset($value['data']['conditional_display']['relation']) ? $value['data']['conditional_display']['relation'] : $relation;
             $custom = isset($value['data']['conditional_display']['custom']) ? $value['data']['conditional_display']['custom'] : $custom;
             $id = $value['id'];
             $g = '';
             if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                 foreach (wpcf_admin_fields_get_groups_by_field($key) as $gs) {
                     $g = $gs['name'];
                 }
             }
             $group = $g ? $g : "Custom fields";
             $name = $g ? $name : $field_slug;
             $is_types = $g ? true : false;
             $name = $g ? $name : $field_slug;
             $id = $g ? $id : $field_slug;
         }
     }
     return array('data_type' => $type, 'type' => $input, 'name' => $name ? $name : $search, 'relation' => $relation, 'custom' => $custom, 'id' => $id ? $id : $search, 'group' => $group, 'is_types' => $is_types, 'can_force_zero' => $can_force_zero);
 }
 /**
  * getCustomFieldProperties function.
  *
  * @access private
  * @param mixed $field
  * @return array
  */
 private function getCustomFieldProperties($field_slug = '')
 {
     $type = 'CHAR';
     $input = 'textfield';
     $name = '';
     $relation = 'AND';
     $custom = '';
     $id = '';
     $is_types = false;
     $group = 'Custom fields';
     if (!function_exists('types_get_field_type') || !$field_slug) {
         return $type;
     }
     $nice_name = explode('wpcf-', $field_slug);
     $search = isset($nice_name[1]) ? $nice_name[1] : $field_slug;
     $options = get_option('wpcf-fields', array());
     foreach ($options as $key => $value) {
         if ($key == $search) {
             if (function_exists('types_get_field_type')) {
                 $type = types_get_field_type($value['type']);
             }
             $input = $value['type'];
             $name = $value['name'];
             $relation = isset($value['data']['conditional_display']['relation']) ? $value['data']['conditional_display']['relation'] : $relation;
             $custom = isset($value['data']['conditional_display']['custom']) ? $value['data']['conditional_display']['custom'] : $custom;
             $id = $value['id'];
             $g = '';
             if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                 foreach (wpcf_admin_fields_get_groups_by_field($key) as $gs) {
                     $g = $gs['name'];
                 }
             }
             $group = $g ? $g : "Custom fields";
             $name = $g ? $name : $field_slug;
             $is_types = $g ? true : false;
             $name = $g ? $name : $field_slug;
             $id = $g ? $id : $field_slug;
         }
     }
     return array('data_type' => $type, 'type' => $input, 'name' => $name ? $name : $search, 'relation' => $relation, 'custom' => $custom, 'id' => $id ? $id : $search, 'group' => $group, 'is_types' => $is_types);
 }