$type) { if ($section == 'node/add#content-' . $type_name) { return t($types[$type_name]->description); } if ($section == 'node/add/content-' . $type) { return t($types[$type_name]->help); } } } } /** * Implementation of hook_perm(). */ function simplenode_perm() { $perms = array('administer content types'); foreach (_simplenode_types() as $name => $type) { $perms[] = 'create '. $name .' content'; $perms[] = 'edit own '. $name .' content'; } return $perms; } /** * Implementation of hook_menu(). */ function simplenode_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' => '_simplenode_admin_types_overview', 'access' => $access); $types = _simplenode_types(); foreach ($types as $type_name => $type) { $items[] = array('path' => 'node/add/content-'. $type_name, 'title' => t($type->label), 'access' => user_access('create '. $type_name .' content')); } } else { if (arg(0) == 'admin' && arg(1) == 'node' && arg(2) == 'types' && arg(3)) { $types = _simplenode_types(); if (isset($types[arg(3)])) { $items[] = array('path' => 'admin/node/types/'. arg(3), 'title' => t($types[arg(3)]->label), 'callback' => '_simplenode_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' => '_simplenode_admin_type_deleteform', 'access' => $access, 'callback arguments' => arg(3), 'type' => MENU_CALLBACK, 'weight' => -10); } } } return $items; } /** * Implementation of hook_node_types(). */ function simplenode_node_types() { $types = array(); foreach (_simplenode_types() as $name => $type) { $types[] = 'content-' . $name; } return $types; } /** * Implementation of hook_node_name(). */ function simplenode_node_name($node) { $types = _simplenode_types(); $type = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type); return t($types[substr($type, 8)]->label); } /** * Implementation of hook_access(). */ function simplenode_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; } } } /** * Menu callback; presents a listing of content types. */ function _simplenode_admin_types_overview() { $op = $_POST['op']; $edit = $_POST['edit']; switch ($op) { case t('Add content type'): _simplenode_admin_type_add($edit['type_name'], $edit['label'], $edit['description'], $edit['help']); break; } $types = _simplenode_types(); $header = array(t('Label'), t('Name'), t('Description'), array('data' => t('Operations'), 'colspan' => 2)); $rows = array(); foreach ($types as $type) { $row = array(); $row[] = $type->label; $row[] = $type->type_name; $row[] = $type->description; $row[] = l(t('edit'), 'admin/node/types/'. $type->type_name); $row[] = l(t('delete'), 'admin/node/types/'. $type->type_name .'/delete'); $rows[] = $row; } $group = theme('table', $header, $rows); $output = '

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

' . $group; $group = t('

To add a new content type, type its name here. After it has been added, you can edit it.

'); // 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' $form = form_textfield(t('Label'), 'label', '', 40, 255, t('Friendly name of your content type.'), NULL, TRUE); $form .= form_textfield(t('Name'), 'type_name', '', 40, 23, t('Short name, used in urls. No spaces or weird characters.'), NULL, TRUE); $form .= form_textfield(t('Description'), 'description', '', 80, 255); $form .= form_textarea(t('Help text'), 'help', $type->help, 60, 5, t('Instructions to present to the user when adding new content of this type.')); $form .= form_submit(t('Add content type')); $group .= form($form); $output .= '

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

'. $group; print theme('page', $output); } /** * Add a new content type. */ function _simplenode_admin_type_add($type_name, $label, $description, $help) { // TODO: check for existence of type with this name, and for illegal characters. $type_name = trim($type_name); $type_name = urlencode($type_name); if ($type_name == '' || $label == '') { drupal_set_message(t('Please fill in all required fields.'), 'error'); drupal_goto('admin/node/types'); } db_query("INSERT INTO {node_type} (type_name, label, description, help) VALUES ('%s', '%s', '%s', '%s')", $type_name, $label, $description, $help); drupal_set_message(t('Added content type %type.', array('%type' => ''. $type_name .''))); drupal_goto('admin/node/types'); } /** * Form to delete a content type. */ function _simplenode_admin_type_deleteform($type_name) { $edit = $_POST['edit']; if ($edit['confirm']) { _simplenode_admin_type_delete($type_name, $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', t('This action cannot be undone.'), t('Delete'), t('Cancel'), $extra); } } /** * Delete content type and/or all data of that type. */ function _simplenode_admin_type_delete($type_name, $delete_content = FALSE) { db_query("DELETE FROM {node_type} WHERE type_name = '%s'", $type_name); if ($delete_content) { $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", 'content-' . $type_name); while ($data = db_fetch_object($result)) { node_delete(array('confirm' => 1, 'nid' => $data->nid)); } } } /** * Menu callback; handles the editing of a content type. */ function _simplenode_admin_type($type_name) { $op = $_POST['op']; $edit = $_POST['edit']; $types = _simplenode_types(); $type = $types[$type_name]; switch ($op) { case t('Save content type'): $edit['type_name'] = $type->type_name; _simplenode_admin_type_save($type_name, $edit); break; } $form = ''; $form .= form_textfield(t('Label'), 'label', $type->label, 60, 255, '', NULL, TRUE); $form .= form_textarea(t('Description'), 'description', $type->description, 60, 5, t('A brief description of the content type.')); $form .= form_textarea(t('Help text'), 'help', $type->help, 60, 5, t('Instructions to present to the user when adding new content of this type.')); $form .= form_submit(t('Save content type')); $output = form($form); print theme('page', $output); } /** * Save a content type after editing. */ function _simplenode_admin_type_save($type_name, $edit) { db_query("UPDATE {node_type} SET label = '%s', description = '%s', help = '%s' WHERE type_name = '%s'", $edit['label'], $edit['description'], $edit['help'], $type_name); drupal_set_message(t('Updated content type %type.', array('%type' => ''. $edit['type_name'] .''))); drupal_goto('admin/node/types/'. $type_name); } /** * Return a list of all content types. */ function _simplenode_types() { static $types; if (!isset($types)) { $types = array(); $type_result = db_query('SELECT * FROM {node_type} nt ORDER BY nt.type_name ASC'); while ($type = db_fetch_object($type_result)) { $types[$type->type_name] = $type; } } return $types; } function simplenode_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; }