$type_object) { if ($section == 'node/add#content-' . $type) { return t($type_object->description); } if ($section == 'node/add/content-' . $type) { return t($type_object->help); } } } } /** * Implementation of hook_perm(). */ function content_perm() { $perms = array('administer content types'); foreach (_content_types() as $type => $type_object) { $perms[] = "create $type content"; $perms[] = "edit own $type content"; } return $perms; } /** * Implementation of hook_menu(). */ function content_menu($may_cache) { $items = array(); $access = user_access('administer content types'); if ($may_cache) { $items[] = array('path' => 'admin/node/types', 'title' => t('content types'), 'callback' => '_content_admin_types_overview', 'access' => $access); foreach (_content_types() as $type => $type_object) { $items[] = array('path' => "node/add/content-$type", 'title' => t($type_object->name), 'access' => user_access("create $type content")); } } else { if (arg(0) == 'admin' && arg(1) == 'node' && arg(2) == 'types' && arg(3)) { $types = _content_types(); if (isset($types[arg(3)])) { $items[] = array('path' => 'admin/node/types/'. arg(3), 'title' => t($types[arg(3)]->name), 'callback' => '_content_admin_type', 'access' => $access, 'callback arguments' => array(arg(3)), 'type' => MENU_CALLBACK); $items[] = array('path' => 'admin/node/types/'. arg(3) .'/delete', 'title' => t('delete'), 'callback' => '_content_admin_type_delete_form', 'access' => $access, 'callback arguments' => arg(3), 'type' => MENU_CALLBACK, 'weight' => -10); } } } return $items; } /** * Implementation of hook_node(). */ function content_node() { $types = array(); foreach (_content_types() as $type => $type_object) { $types[$type] = array('name' => $type_object->name, 'base' => 'content'); } return $types; } /** * Implementation of hook_access(). */ function content_access($op, $node) { global $user; $type = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type); $type = substr($type, 8); if ($op == 'create') { return user_access("create $type content"); } if ($op == 'update' || $op == 'delete') { if (user_access("edit own $type content") && ($user->uid == $node->uid)) { return TRUE; } } } function _content_validate_form($edit, $update = FALSE) { $types = _content_types(); if (empty($edit['type'])) { form_set_error('type', t('You must have a short name for your content type')); } if (!$update && isset($types[$edit['type']])) { form_set_error('type', t('This node type already exists')); } if (empty($edit['name'])) { form_set_error('name', t('You must give a friendly name to your content type')); } } /** * Menu callback; presents a listing of content types. */ function _content_admin_types_overview() { $op = isset($_POST['op']) ? $_POST['op'] : ''; $edit = isset($_POST['edit']) ? $_POST['edit'] : array(); $submit_message = t('Add content type'); switch ($op) { case $submit_message: _content_validate_form($edit); if (!form_get_errors()) { _content_admin_type_add($edit); } break; } $types = _content_types(); $header = array(t('Name'), t('Type'), t('Description'), array('data' => t('Operations'), 'colspan' => 2)); $rows = array(); foreach ($types as $type => $type_object) { $edit_path = "admin/node/types/$type"; $rows[] = array($type_object->name, $type, $type_object->description, l(t('edit'), $edit_path), l(t('delete'), "$edit_path/delete")); } $output = '

'. t('Available content types') . '

'; $output .= theme('table', $header, $rows); // TODO: we are currently limited to 24 chars for the content type name // because the node table's type field is limited to 32 chars // and we use 8 of those for 'content-', e.g. 'content-foo' $output .= '

'. t('Add new content type') .'

'; $output .= _content_admin_form($submit_message, '', $edit); print theme('page', $output); } function _content_admin_form($submit_message, $type = '', $edit = array()) { if ($type) { $types = _content_types(); if (empty($edit)) { $edit = get_object_vars($types[$type]); } } $form = form_textfield(t('Name'), 'name', empty($edit) ? '': $edit['name'], 40, 255, t('Friendly name of your content type.'), NULL, TRUE); if (!$type) { $form .= form_textfield(t('Type'), 'type', $type, 40, 23, t("Short name, used in urls. No spaces or weird characters. You can't change this later."), NULL, TRUE); } $form .= form_textfield(t('Description'), 'description', empty($edit) ? '': $edit['description'], 80, 255); $form .= form_textarea(t('Help text'), 'help', empty($edit) ? '': $edit['help'], 60, 5, t('Instructions to present to the user when adding new content of this type.')); $form .= form_submit($submit_message); return form($form); } /** * Add a new content type. */ function _content_admin_type_add($edit) { db_query("INSERT INTO {node_type} (type, name, description, help) VALUES ('%s', '%s', '%s', '%s')", urlencode(trim($edit['type'])), $edit['name'], $edit['description'], $edit['help']); drupal_set_message(t('Added content type %type.', array('%type' => theme('placeholder', $edit['name'])))); drupal_goto('admin/node/types'); } /** * Form to delete a content type. */ function _content_admin_type_delete_form($type) { $edit = isset($_POST['edit']) ? $_POST['edit'] : array(); if ($edit['confirm']) { _content_admin_type_delete($type, $edit['delete-content']); drupal_set_message(t('Deletion complete.')); drupal_goto('admin/node/types'); } else { $options = array(t('Delete this content type but preserve data of this content type in the database'), t('Delete this content type and all data of this content type')); $extra = form_radios(t('Delete all content of this type'), 'delete-content', 0, $options); return theme('confirm', t('Are you sure you want to delete this content type?'), 'admin/node/types', t('This action cannot be undone.'), t('Delete'), t('Cancel'), $extra); } } /** * Delete content type and/or all data of that type. */ function _content_admin_type_delete($type, $delete_content = FALSE) { db_query("DELETE FROM {node_type} WHERE type = '%s'", $type); if ($delete_content) { $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", 'content-' . $type); while ($data = db_fetch_object($result)) { node_delete(array('confirm' => 1, 'nid' => $data->nid)); } } } /** * Menu callback; handles the editing of a content type. */ function _content_admin_type($type) { $op = isset($_POST['op']) ? $_POST['op'] : ''; $edit = isset($_POST['edit']) ? $_POST['edit'] : array(); $submit_message = t('Save content type'); switch ($op) { case $submit_message: _content_validate_form($edit, TRUE); if (!form_get_errors()) { _content_admin_type_save($type, $edit); } break; } print theme('page', _content_admin_form($submit_message, $type, $edit)); } /** * Save a content type after editing. */ function _content_admin_type_save($type, $edit) { db_query("UPDATE {node_type} SET name = '%s', description = '%s', help = '%s' WHERE type = '%s'", $edit['name'], $edit['description'], $edit['help'], $type); drupal_set_message(t('Updated content type %type.', array('%type' => theme('placeholder', $edit['name'])))); drupal_goto('admin/node/types'); } /** * Return a list of all content types. */ function _content_types() { static $types; if (!isset($types)) { $types = array(); $result = db_query('SELECT * FROM {node_type} nt ORDER BY nt.type ASC'); while ($type_object = db_fetch_object($result)) { $types[$type_object->type] = $type_object; } } return $types; } function content_form(&$node) { $output = ''; if (function_exists('taxonomy_node_form')) { $output .= implode('', taxonomy_node_form($node->type, $node)); } $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, '', NULL, TRUE); $output .= filter_form('format', $node->format); return $output; }