/**
  * Get the metabox-holder
  * @param string $metabox_name
  * @param array $items fields 
  * @return string
  */
 public static function get_metabox_holder($metabox_name, $items)
 {
     $d = array();
     $d['items'] = implode('', $items);
     $d['title'] = __('Custom Fields', CCTM_TXTDOMAIN);
     if (isset(CCTM::$data['metabox_defs'][$metabox_name]['title'])) {
         $d['title'] = __(CCTM::$data['metabox_defs'][$metabox_name]['title']);
     }
     $d['metabox'] = $metabox_name;
     $d['edit_metabox_link'] = get_site_url() . '/wp-admin/admin.php?page=cctm&a=edit_metabox&id=' . $metabox_name;
     return CCTM::load_view('metabox-holder.php', $d);
 }
    // Remove the primary definition
    unset(CCTM::$data['metabox_defs'][$id]);
    // Remove the map_field_metabox for each post-type so any fields point to the default.
    // See: https://code.google.com/p/wordpress-custom-content-type-manager/wiki/DataStructures
    $defs = self::get_post_type_defs();
    foreach ($defs as $pt => $d) {
        if (isset($d['map_field_metabox']) && in_array($id, array_values($d['map_field_metabox']))) {
            foreach ($d['map_field_metabox'] as $cf => $mb) {
                if ($mb == $id) {
                    $defs[$pt]['map_field_metabox'][$cf] = 'default';
                }
            }
        }
    }
    self::$data['post_type_defs'] = $defs;
    update_option(self::db_key, self::$data);
    $msg = '<div class="updated"><p>' . sprintf(__('The Metabox %s has been deleted', CCTM_TXTDOMAIN), "<em>{$id}</em>") . '</p></div>';
    self::set_flash($msg);
    include CCTM_PATH . '/controllers/list_post_types.php';
    return;
}
$data['content'] = '<div class="error">
	<img src="' . CCTM_URL . '/images/warning-icon.png" width="50" height="44" style="float:left; padding:10px;"/>
	<p>' . sprintf(__('You are about to delete the %s Metabox. Any fields that have been assigned to this Metabox will be moved into the default Metabox.  This will not delete any of your custom fields, but it may make your admin pages to become disorganized.', CCTM_TXTDOMAIN), "<em>{$id}</em>") . '</p>' . '<p>' . __('Are you sure you want to do this?', CCTM_TXTDOMAIN) . '
	<a href="http://code.google.com/p/wordpress-custom-content-type-manager/wiki/Metaboxes" title="Deleting a Metabox" target="_blank">
	<img src="' . CCTM_URL . '/images/question-mark.gif" width="16" height="16" />
	</a>
	</p></div>';
$data['content'] = CCTM::load_view('basic_form.php', $data);
print CCTM::load_view('templates/default.php', $data);
/*EOF*/
 /**
  * Get all available columns (i.e. fields) for this post_type, used for 
  * showing which columns to include.
  *
  * @param	string	$post_type
  * @param	string	HTML output (table rows)
  */
 public static function get_columns($post_type)
 {
     $output = '';
     //$built_in_columns = CCTM::$reserved_field_names;
     $built_in_columns = array('title' => __('Title'), 'author' => __('Author'), 'comments' => __('Comments'), 'date' => __('Date'));
     $custom_fields = array();
     if (isset(CCTM::$data['post_type_defs'][$post_type]['custom_fields'])) {
         $custom_fields = CCTM::$data['post_type_defs'][$post_type]['custom_fields'];
     }
     $taxonomies = array();
     if (isset(CCTM::$data['post_type_defs'][$post_type]['taxonomies'])) {
         $taxonomies = CCTM::$data['post_type_defs'][$post_type]['taxonomies'];
     }
     // Get selected columns (float to top)
     $custom_columns = array();
     if (isset(CCTM::$data['post_type_defs'][$post_type]['cctm_custom_columns'])) {
         $custom_columns = CCTM::$data['post_type_defs'][$post_type]['cctm_custom_columns'];
     }
     foreach ($custom_columns as $c) {
         $d = array();
         if (in_array($c, array_keys($built_in_columns))) {
             $d['name'] = $c;
             $d['label'] = $built_in_columns[$c];
             $d['class'] = 'cctm_builtin_column';
             $d['description'] = __('Built-in WordPress column.', CCTM_TXTDOMAIN);
         } elseif (in_array($c, $taxonomies)) {
             $t = get_taxonomy($c);
             if (isset($t)) {
                 $d['name'] = $c;
                 $d['label'] = __($t->labels->singular_name);
                 $d['class'] = 'cctm_taxonomy_column';
                 $d['description'] = __('WordPress Taxonomy', CCTM_TXTDOMAIN);
             }
         } elseif (in_array($c, $custom_fields)) {
             if (isset(CCTM::$data['custom_field_defs'][$c])) {
                 $d['name'] = CCTM::$data['custom_field_defs'][$c]['name'];
                 $d['label'] = CCTM::$data['custom_field_defs'][$c]['label'];
                 $d['class'] = 'cctm_custom_column';
                 $d['description'] = CCTM::$data['custom_field_defs'][$c]['description'];
             }
         } else {
             continue;
         }
         $d['is_checked'] = 'checked="checked"';
         $output .= CCTM::load_view('tr_column.php', $d);
     }
     // Separator
     $output .= '<tr class="no-sort"><td colspan="4" style="background-color:#ededed;"><hr /></td></tr>';
     // Get built-in columns
     foreach ($built_in_columns as $c => $label) {
         if (in_array($c, $custom_columns)) {
             continue;
         }
         $d = array();
         $d['name'] = $c;
         $d['label'] = $label;
         $d['class'] = 'cctm_builtin_column';
         $d['description'] = __('Built-in WordPress column.', CCTM_TXTDOMAIN);
         $d['is_checked'] = '';
         $output .= CCTM::load_view('tr_column.php', $d);
     }
     // Get custom fields
     foreach ($custom_fields as $c) {
         if (in_array($c, $custom_columns)) {
             continue;
         }
         if (isset(CCTM::$data['custom_field_defs'][$c])) {
             $d = array();
             $d['name'] = CCTM::$data['custom_field_defs'][$c]['name'];
             $d['label'] = CCTM::$data['custom_field_defs'][$c]['label'];
             $d['class'] = 'cctm_custom_column';
             $d['description'] = CCTM::$data['custom_field_defs'][$c]['description'];
             $d['is_checked'] = '';
             $output .= CCTM::load_view('tr_column.php', $d);
         }
     }
     // Get taxonomies
     foreach ($taxonomies as $taxonomy) {
         if (in_array($taxonomy, $custom_columns)) {
             continue;
         }
         $t = get_taxonomy($taxonomy);
         if (isset($t)) {
             //				die(print_r($t,true));
             $d['name'] = $taxonomy;
             //$t->labels->singular_name;
             $d['label'] = $t->labels->singular_name;
             $d['class'] = 'cctm_taxonomy_column';
             $d['description'] = __('WordPress Taxonomy', CCTM_TXTDOMAIN);
             $d['is_checked'] = '';
             $output .= CCTM::load_view('tr_column.php', $d);
         }
     }
     return $output;
 }
$data = array();
$data['i'] = md5(uniqid());
// we just need a unique identifier for each row element
$data['name'] = '';
$data['label'] = '';
$data['type'] = '';
$data['field_types'] = '';
$data['name_class'] = '';
// 'cctm_validation_error';
// 1. User has specified a desired field type
if ($desired_field_type) {
    $elements = CCTM::get_available_helper_classes('fields');
    foreach ($elements as $field_type => $file) {
        if ($FieldObj = CCTM::load_object($field_type, 'fields')) {
            $is_selected = '';
            if ($desired_field_type == $field_type) {
                $is_selected = ' selected="selected"';
            }
            $data['field_types'] .= '<option value="' . $field_type . '"' . $is_selected . '>' . $FieldObj->get_name() . '</option>';
        } else {
            // Form element not found.  Did someone move a custom class file?
        }
    }
    print CCTM::load_view('tr_bulk.php', $data);
} elseif ($post_type || $post_ids) {
    // Search the database
} else {
    printf('<div class="error"><table><tr><td><img src="%s/images/warning-icon.png" height="44" width="50"/></td><td><p>%s</p></td></tr></table></div>', CCTM_URL, __('Invalid selection.', CCTM_TXTDOMAIN));
    return;
}
/*EOF*/
$hash['post_parent'] = __('Parent', CCTM_TXTDOMAIN);
$hash['post_type'] = __('Post Type', CCTM_TXTDOMAIN);
$hash['add_to_post'] = __('Add to Post', CCTM_TXTDOMAIN);
$hash['add_to_post_and_close'] = __('Add to Post and Close', CCTM_TXTDOMAIN);
//$hash['filter'] 		= __('Filter', CCTM_TXTDOMAIN);
//$hash['show_all']		= __('Show All', CCTM_TXTDOMAIN);
$hash['content'] = '';
// And the items
//$results = array();
foreach ($results as $r) {
    $r['name'] = $raw_fieldname;
    $r['preview'] = __('Preview', CCTM_TXTDOMAIN);
    $r['select'] = __('Select', CCTM_TXTDOMAIN);
    $r['field_id'] = $raw_fieldname;
    $r['thumbnail_url'] = CCTM::get_thumbnail($r['ID']);
    // Translate stuff (issue 279)
    $r['post_title'] = __($r['post_title']);
    $r['post_content'] = __($r['post_content']);
    $r['post_excerpt'] = __($r['post_excerpt']);
    $hash['content'] .= CCTM::parse($item_tpl, $r);
}
$d['content'] .= CCTM::parse($wrapper_tpl, $hash);
$d['content'] .= '<div class="cctm_pagination_links">' . $Q->get_pagination_links() . '</div>';
if (isset($_POST['wrap_thickbox'])) {
    print CCTM::load_view('templates/thickbox.php', $d);
} else {
    //print CCTM::load_view('templates/thickbox_inner.php', $d);
    print $d['content'];
}
exit;
/*EOF*/
// Make sure a file was specified
$filename = CCTM::get_value($_REQUEST, 'file');
if (empty($filename)) {
    print CCTM::format_error_msg(__('Definition file not specified.', CCTM_TXTDOMAIN));
    exit;
}
// Make sure the filename is legit
if (!CCTM_ImportExport::is_valid_basename($filename)) {
    print CCTM::format_error_msg(__('Invalid filename: the definition filename should not contain spaces and should use an extension of <code>.cctm.json</code>.', CCTM_TXTDOMAIN));
    exit;
}
// Load up this thing... errors will be thrown
$upload_dir = wp_upload_dir();
$dir = $upload_dir['basedir'] . '/' . CCTM::base_storage_dir . '/' . CCTM::def_dir . '/';
$data = CCTM_ImportExport::load_def_file($dir . $filename);
$data['filename'] = $filename;
// Bail if there were errors
if (!empty(CCTM::$errors)) {
    print CCTM::format_errors();
    exit;
}
// Check encoding, warn if it differs (warn only: it may not be a problem for the importer)
// See http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=322
$this_charset = get_bloginfo('charset');
if (isset($data['export_info']['_charset']) && $data['export_info']['_charset'] != $this_charset) {
    CCTM::$errors['encoding'] = sprintf(__("Your site's encoding differs from the encoding used to create this definition file.  This may create problems if the post-type and field definitions use foreign characters.  Adding the following to your wp-config.php file may alleviate problems with character encoding: <code>define('DB_CHARSET', '%s');</code>", CCTM_TXTDOMAIN), $data['export_info']['_charset']);
}
print CCTM::format_errors();
// but do NOT exit.  It's only a warning, so we continue.
print CCTM::load_view('preview_def.php', $data);
/*EOF*/
}
if (!isset(CCTM::$data['post_type_defs'][$post_type]['is_active']) || !CCTM::$data['post_type_defs'][$post_type]['is_active']) {
    printf('<div class="error"><table><tr><td><img src="%s/images/warning-icon.png" height="44" width="50"/></td><td><p>%s <a href="http://code.google.com/p/wordpress-custom-content-type-manager/wiki/custom_field_shortcode"><img src="' . CCTM_URL . '/images/question-mark.gif" width="16" height="16" /></a></p></td></tr></table></div>', CCTM_URL, __('The custom fields for this post-type are not standardized. You can manually type in the shortcode to print the custom field value from another post using the following format:  <code>[custom_field name="name_of_field" filter="optional_output_filter" post_id="123"]</code>', CCTM_TXTDOMAIN));
    return;
}
// Template variables
$d = array();
$d['msg'] = '';
$d['page_title'] = __('Custom Fields', CCTM_TXTDOMAIN);
$d['content'] = '';
$custom_fields = array();
if (isset(CCTM::$data['post_type_defs'][$post_type]['custom_fields'])) {
    $custom_fields = CCTM::$data['post_type_defs'][$post_type]['custom_fields'];
}
if (empty($custom_fields)) {
    $d['content'] = '<div class="error"><p>' . __('This post-type does not have any custom fields associated with it.', CCTM_TXTDOMAIN) . '</p></div>';
}
$d['content'] .= '<ul>';
foreach ($custom_fields as $cf) {
    if (!isset(CCTM::$data['custom_field_defs'][$cf])) {
        continue;
    }
    $def = CCTM::$data['custom_field_defs'][$cf];
    $shortcode = sprintf('[custom_field name="%s"]', $def['name']);
    $d['content'] .= sprintf('<li>
		<strong class="linklike" onclick="javascript:insert_shortcode(\'%s\');">%s</strong> 
		: <span>%s</span></li>', htmlspecialchars(addslashes($shortcode)), $def['label'], $def['description']);
}
$d['content'] .= '</ul>';
print CCTM::load_view('templates/tinymce.php', $d);
/*EOF*/