<?php
global $wpdb,$table_prefix;
$custom_post_meta_db_table_name = $table_prefix . "templatic_custom_post_fields";
$wpdb->query('CREATE TABLE IF NOT EXISTS `'.$custom_fields_db_table_name.'` (
	  `cid` int(11) NOT NULL AUTO_INCREMENT,
	  `admin_title` varchar(255) NOT NULL,
	  `htmlvar_name` varchar(255) NOT NULL,
	  `admin_desc` text NOT NULL,
	  `site_title` varchar(255) NOT NULL,
	  `ctype` varchar(255) NOT NULL COMMENT "text,checkbox,radio,select,textarea",
	  `default_value` text NOT NULL,
	  `option_values` text NOT NULL,
	  `clabels` text NOT NULL,
	  `sort_order` int(11) NOT NULL,
	  `is_active` tinyint(4) NOT NULL DEFAULT "1",
	  `show_on_listing` tinyint(4) NOT NULL DEFAULT "1",
	  `show_on_detail` tinyint(4) NOT NULL DEFAULT "1",
	  `extrafield1` text NOT NULL,
	  `extrafield2` text NOT NULL,
	  PRIMARY KEY (`cid`)
	)');

function get_post_custom_fields_templ()
{
	global $wpdb,$custom_post_meta_db_table_name;
	$post_meta_info = $wpdb->get_results("select * from $custom_post_meta_db_table_name");
	$return_arr = array();
	if($post_meta_info){
		foreach($post_meta_info as $post_meta_info_obj){	
			if($post_meta_info_obj->ctype){
				$options = explode(',',$post_meta_info_obj->option_values);
			}
			$custom_fields = array(
					"name"		=> $post_meta_info_obj->htmlvar_name,
					"label" 	=> $post_meta_info_obj->clabels,
					"type" 		=> $post_meta_info_obj->ctype,
					"options"	=> $options,
					"desc"      => $post_meta_info_obj->admin_desc,
					);
			$return_arr[] = $custom_fields;
		}
	}
	return $return_arr;
}
$custom_metaboxes = get_post_custom_fields_templ();
?>