Commit 5524604a authored by Dries's avatar Dries

- Patch #68418 by Moshe et al: clean up node submit hooks.

parent 78ee75f1
......@@ -187,18 +187,15 @@ function book_load($node) {
return db_fetch_object(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid));
}
/**
* Implementation of hook_insert().
*/
function book_insert($node) {
db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight);
function book_form_alter($form_id, &$form) {
if ($form_id == 'node_delete_confirm') {
$form['#submit']['book_node_delete_confirm_submit'] = array();
}
}
/**
* Implementation of hook_update().
*/
function book_update($node) {
if ($node->revision) {
function book_node_submit($form_id, $node) {
$node = (object)$node;
if ($node->is_new || $node->revision) {
db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight);
}
else {
......@@ -206,30 +203,16 @@ function book_update($node) {
}
}
/**
* Implementation of hook_delete().
*/
function book_delete(&$node) {
db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
}
/**
* Implementation of hook_submit().
*/
function book_submit(&$node) {
global $user;
// Set default values for non-administrators.
if (!user_access('administer nodes')) {
$node->revision = 1;
$book->uid = $user->uid;
$book->name = $user->uid ? $user->name : '';
}
function book_node_delete_confirm_submit($form_id, $form_values) {
db_query('DELETE FROM {book} WHERE nid = %d OR parent = %d', $form_values['node']->nid, $form_values['node']->nid);
}
/**
* Implementation of hook_form().
*/
function book_form(&$node) {
global $user;
$type = node_get_types('type', $node);
if ($node->nid && !$node->parent && !user_access('create new books')) {
$form['parent'] = array('#type' => 'value', '#value' => $node->parent);
......@@ -265,22 +248,23 @@ function book_form(&$node) {
'#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
);
if (user_access('administer nodes')) {
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $node->weight,
'#delta' => 15,
'#weight' => 5,
'#description' => t('Pages at a given level are ordered first by weight and then by title.'),
);
}
else {
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#access' => user_access('administer nodes'),
'#default_value' => $node->weight,
'#delta' => 15,
'#weight' => 5,
'#description' => t('Pages at a given level are ordered first by weight and then by title.'),
);
if (!user_access('administer nodes')) {
// If a regular user updates a book page, we preserve the node weight; otherwise
// we use 0 as the default for new pages
$form['weight'] = array(
'#type' => 'value',
'#value' => isset($node->weight) ? $node->weight : 0,
);
$form['name'] = array('#type' => 'value', '#value' => $user->uid ? $user->name : '');
}
return $form;
......@@ -481,9 +465,6 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
case 'delete revision':
db_query('DELETE FROM {book} WHERE vid = %d', $node->vid);
break;
case 'delete':
db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
break;
}
}
......
......@@ -280,6 +280,7 @@ function comment_form_alter($form_id, &$form) {
}
elseif (isset($form['type'])) {
if ($form['type']['#value'] .'_node_form' == $form_id) {
$form['#submit']['comment_node_submit'] = array();
$node = $form['#node'];
$form['comment_settings'] = array(
'#type' => 'fieldset',
......@@ -297,6 +298,10 @@ function comment_form_alter($form_id, &$form) {
);
}
}
if ($form_id == 'node_delete_confirm') {
$form['#submit']['comment_node_delete_confirm_submit'] = array();
}
}
/**
......@@ -315,15 +320,6 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
}
break;
case 'insert':
db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
break;
case 'delete':
db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
break;
case 'update index':
$text = '';
$comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED);
......@@ -346,6 +342,17 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
}
}
function comment_node_delete_confirm_submit($form_id, $form_values) {
db_query('DELETE FROM {comments} WHERE nid = %d', $form_values['node']->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $form_values['node']->nid);
}
function comment_node_submit($form_id, $node) {
if ($node->is_new) {
db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
}
}
/**
* Implementation of hook_user().
*
......
......@@ -233,6 +233,31 @@ function forum_form_alter($form_id, &$form) {
unset($form['nodes']['forum']);
}
}
if ($form_id == 'node_delete_confirm' && $form['node']['#value']->type == 'forum') {
$form['#submit']['forum_node_delete_confirm_submit'] = array();
}
}
/**
* Check in particular that only a "leaf" term in the associated taxonomy
* vocabulary is selected, not a "container" term.
*/
function forum_node_validate($form_id, $node) {
$node = (object)$node;
if ($node->taxonomy) {
// Extract the node's proper topic ID.
$vocabulary = variable_get('forum_nav_vocabulary', '');
$containers = variable_get('forum_containers', array());
foreach ($node->taxonomy as $term) {
if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) {
if (in_array($term, $containers)) {
$term = taxonomy_get_term($term);
form_set_error('taxonomy', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name)));
}
}
}
}
}
/**
......@@ -327,16 +352,15 @@ function forum_view(&$node, $teaser = FALSE, $page = FALSE) {
}
/**
* Implementation of hook_submit().
*
* Check in particular that only a "leaf" term in the associated taxonomy
* vocabulary is selected, not a "container" term.
* An early submit handler of the forum_node_form. Runs before the node is saved.
*/
function forum_submit(&$node) {
function forum_node_submit_early($form_id, &$node) {
$node = (object)$node;
// Make sure all fields are set properly:
$node->icon = $node->icon ? $node->icon : '';
if ($node->taxonomy) {
if (!empty($node->taxonomy)) {
// Get the forum terms from the (cached) tree
$tree = taxonomy_get_tree(_forum_get_vid());
if ($tree) {
......@@ -357,35 +381,13 @@ function forum_submit(&$node) {
}
}
}
}
/**
* Implementation of hook_validate().
*
* Check in particular that only a "leaf" term in the associated taxonomy
* vocabulary is selected, not a "container" term.
*/
function forum_validate($node) {
if ($node->taxonomy) {
// Extract the node's proper topic ID.
$vocabulary = variable_get('forum_nav_vocabulary', '');
$containers = variable_get('forum_containers', array());
foreach ($node->taxonomy as $term) {
if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) {
if (in_array($term, $containers)) {
$term = taxonomy_get_term($term);
form_set_error('taxonomy', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name)));
}
}
}
}
$node = (array)$node;
}
/**
* Implementation of hook_update().
*/
function forum_update($node) {
if ($node->revision) {
function forum_node_submit($form_id, $node) {
$node = (object)$node;
if ($node->is_new) {
db_query("INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $node->tid);
}
else {
......@@ -424,18 +426,8 @@ function forum_prepare(&$node) {
}
}
/**
* Implementation of hook_insert().
*/
function forum_insert($node) {
db_query('INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $node->tid);
}
/**
* Implementation of hook_delete().
*/
function forum_delete(&$node) {
db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
function forum_node_delete_confirm_submit($form_id, $form_values) {
db_query('DELETE FROM {forum} WHERE nid = %d', $form_values['node']->nid);
}
/**
......
......@@ -147,31 +147,20 @@ function menu_block($op = 'list', $delta = 0) {
}
}
/**
* Implementation of hook_nodeapi().
*/
function menu_nodeapi(&$node, $op) {
if (user_access('administer menu')) {
switch ($op) {
case 'insert':
case 'update':
if ($node->menu['delete']) {
menu_node_form_delete($node);
menu_rebuild();
}
elseif ($node->menu['title']) {
$node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid";
menu_edit_item_save($node->menu);
menu_rebuild();
}
break;
function menu_node_delete_confirm_submit($form_id, $form_values) {
menu_node_form_delete($form_values['node']);
menu_rebuild();
}
case 'delete':
menu_node_form_delete($node);
menu_rebuild();
break;
}
function menu_node_submit($form_id, $node) {
if ($node->menu['delete']) {
menu_node_form_delete($node);
menu_rebuild();
}
elseif ($node->menu['title']) {
$node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid";
menu_edit_item_save($node->menu);
menu_rebuild();
}
}
......@@ -199,6 +188,8 @@ function menu_form_alter($form_id, &$form) {
}
}
$form['#submit']['menu_node_submit'] = array();
$form['menu'] = array('#type' => 'fieldset',
'#title' => t('Menu settings'),
'#access' => user_access('administer menu'),
......@@ -259,6 +250,10 @@ function menu_form_alter($form_id, &$form) {
);
}
}
if ($form_id == 'node_delete_confirm') {
$form['#submit']['menu_node_delete_confirm_submit'] = array();
}
}
/**
......
This diff is collapsed.
......@@ -209,16 +209,6 @@ function path_form($edit = '') {
function path_nodeapi(&$node, $op, $arg) {
if (user_access('create url aliases') || user_access('administer url aliases')) {
switch ($op) {
case 'validate':
$node->path = trim($node->path);
if ($node->path && !valid_url($node->path)) {
form_set_error('path', t('The path is invalid.'));
}
else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) {
form_set_error('path', t('The path is already in use.'));
}
break;
case 'load':
$path = "node/$node->nid";
// We don't use drupal_get_path_alias() to avoid custom rewrite functions.
......@@ -228,27 +218,37 @@ function path_nodeapi(&$node, $op, $arg) {
$node->path = db_result($result);
}
break;
}
}
}
case 'insert':
// Don't try to insert if path is NULL. We may have already set
// the alias ahead of time.
if ($node->path) {
path_set_alias("node/$node->nid", $node->path);
}
break;
case 'update':
path_set_alias("node/$node->nid", $node->path, $node->pid);
break;
function path_node_delete_confirm_submit($form_id, $form_values) {
$path = 'node/'. $form_values['node']->nid;
if (drupal_get_path_alias($path) != $path) {
path_set_alias($path);
}
}
case 'delete':
$path = "node/$node->nid";
if (drupal_get_path_alias($path) != $path) {
path_set_alias($path);
}
break;
function path_node_submit($form_id, $node) {
if ($node->is_new) {
// Don't try to insert if path is NULL. We may have already set the alias ahead of time.
if ($node->path) {
path_set_alias("node/$node->nid", $node->path);
}
}
else {
path_set_alias("node/$node->nid", $node->path, $node->pid);
}
}
function path_node_validate($form_id, $node) {
$node['path'] = trim($node['path']);
if ($node['path'] && !valid_url($node['path'])) {
form_set_error('path', t('The path is invalid.'));
}
else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node['path'], 'node/'. $node['nid']))) {
form_set_error('path', t('The path is already in use.'));
}
}
/**
......@@ -256,6 +256,12 @@ function path_nodeapi(&$node, $op, $arg) {
*/
function path_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
if (user_access('create url aliases')) {
$form['#validate']['path_node_validate'] = array();
$form['#submit']['path_node_submit'] = array();
}
$path = $form['#node']->path;
$form['path'] = array(
'#type' => 'fieldset',
......@@ -280,6 +286,10 @@ function path_form_alter($form_id, &$form) {
);
}
}
if ($form_id == 'node_delete_confirm') {
$form['#submit']['path_node_delete_confirm_submit'] = array();
}
}
......
......@@ -78,46 +78,46 @@ function poll_cron() {
}
}
/**
* Implementation of hook_delete().
*/
function poll_delete($node) {
db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid);
db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid);
db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid);
function poll_node_delete_confirm_submit($form_id, $form_values) {
db_query("DELETE FROM {poll} WHERE nid = %d", $form_values['node']->nid);
db_query("DELETE FROM {poll_choices} WHERE nid = %d", $form_values['node']->nid);
db_query("DELETE FROM {poll_votes} WHERE nid = %d", $form_values['node']->nid);
}
/**
* Implementation of hook_submit().
* An early submit handler of the poll_node_form. Runs before the node is saved.
*/
function poll_submit(&$node) {
function poll_node_submit_early($form_id, &$node) {
$node = (object)$node;
// Renumber fields
$node->choice = array_values($node->choice);
$node->teaser = poll_teaser($node);
}
/**
* Implementation of hook_validate().
*/
function poll_validate($node) {
if (isset($node->title)) {
// Check for at least two options and validate amount of votes:
$realchoices = 0;
// Renumber fields
$node->choice = array_values($node->choice);
foreach ($node->choice as $i => $choice) {
if ($choice['chtext'] != '') {
$realchoices++;
}
if ($choice['chvotes'] < 0) {
form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
}
function poll_node_validate($form_id, $node) {
$node = (object)$node;
// Check for at least two options and validate amount of votes:
$realchoices = 0;
// Renumber fields
$node->choice = array_values($node->choice);
foreach ($node->choice as $i => $choice) {
if ($choice['chtext'] != '') {
$realchoices++;
}
if ($realchoices < 2) {
form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
if ($choice['chvotes'] < 0) {
form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
}
}
if ($realchoices < 2) {
form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
}
}
function poll_form_alter($form_id, &$form) {
if ($form_id == 'node_delete_confirm' && $form['node']['#value']->type == 'poll') {
$form['#submit']['poll_node_delete_confirm_submit'] = array();
}
}
/**
......@@ -582,9 +582,16 @@ function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
return $node;
}
/**
* Implementation of hook_update().
*/
function poll_node_submit($form_id, $node) {
$node = (object)$node;
if ($node->is_new) {
poll_insert($node);
}
else {
poll_update($node);
}
}
function poll_update($node) {
db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);
......
......@@ -533,14 +533,13 @@ function _statistics_format_item($title, $path) {
return $output;
}
/**
* Implementation of hook_nodeapi().
*/
function statistics_nodeapi(&$node, $op, $arg = 0) {
switch ($op) {
case 'delete':
// clean up statistics table when node is deleted
db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
function statistics_node_delete_confirm_submit($form_id, $form_values) {
db_query('DELETE FROM {node_counter} WHERE nid = %d', $form_values['node']->nid);
}
function statistics_form_alter($form_id, &$form) {
if ($form_id == 'node_delete_confirm') {
$form['#submit']['statistics_node_delete_confirm_submit'] = array();
}
}
......
......@@ -550,35 +550,69 @@ function system_clean_url_settings() {
}
function system_error_reporting_settings() {
$form['site_403'] = array(
$form['403'] = array(
'#type' => 'fieldset',
'#title' => t('Access denied page (403)'),
'#description' => t('These settings apply when the requested document is denied to the current user, i.e. a <em>403 error</em>.'),
);
$form['403']['site_403'] = array(
'#type' => 'textfield',
'#title' => t('Default 403 (access denied) page'),
'#title' => t('Page to display'),
'#default_value' => variable_get('site_403', ''),
'#description' => t('This page is displayed when the requested document is denied to the current user. If you are not using clean URLs, specify the part after "<code>?q=</code>". If unsure, specify nothing.')
'#description' => t('This page is displayed when a 403 error occurs. If you are not using clean URLs, specify the part after "<code>?q=</code>". If unsure, leave blank.'),
);
$form['site_404'] = array(
$form['403']['site_403_message_show'] = array(
'#type' => 'checkbox',
'#title' => t('Enable error message'),
'#description' => t('Displays the message below.'),
'#default_value' => variable_get('site_403_message_show', TRUE),
);
$form['403']['site_403_message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#description' => t('This message is shown when a 403 error occurs, if enabled. "%path" will be replaced with the path of the requested document.'),
'#default_value' => variable_get('site_403_message', t('We are sorry, the requested page (%path) was not found on this webserver. Either the URL does not exist or the page you were looking has been deleted.')),
);
$form['404'] = array(
'#type' => 'fieldset',
'#title' => t('File not found page (404)'),
'#description' => t('These settings apply when the requested document could not be found, i.e. a <em>404 error</em>.'),
);
$form['404']['site_404'] = array(
'#type' => 'textfield',
'#title' => t('Default 404 (not found) page'),
'#title' => t('Page to display'),
'#default_value' => variable_get('site_404', ''),
'#description' => t('This page is displayed when no other content matches the requested document. If you are not using clean URLs, specify the part after "<code>?q=</code>". If unsure, specify nothing.')
'#description' => t('This page is displayed when a 404 error occurs. If you are not using clean URLs, specify the part after "<code>?q=</code>". If unsure, leave blank.'),
);
$form['error_level'] = array(
$form['404']['site_404_message_show'] = array(
'#type' => 'checkbox',
'#title' => t('Enable error message'),
'#description' => t('Displays the message below.'),
'#default_value' => variable_get('site_404_message_show', TRUE),
);
$form['404']['site_404_message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#description' => t('This message is shown when a 404 error occurs, if enabled. "%path" will be replaced with the path of the requested document.'),
'#default_value' => variable_get('site_404_message', t('We are sorry, %path could not be found. Please check the spelling and punctuation. This website is case sensitive, so also make sure that the address has the correct case.')),
);
$form['error'] = array(
'#type' => 'fieldset',
'#title' => t('Other error handling'),
);
$form['error']['error_level'] = array(
'#type' => 'select', '#title' => t('Error reporting'), '#default_value' => variable_get('error_level', 1),
'#options' => array(t('Write errors to the log'), t('Write errors to the log and to the screen')),
'#description' => t('Where Drupal, PHP and SQL errors are logged. On a production server it is recommended that errors are only written to the error log. On a test server it can be helpful to write logs to the screen.')
'#description' => t('Where Drupal, PHP and SQL errors are logged. On a production server it is recommended that errors are only written to the error log. On a test server it can be helpful to write logs to the screen.'),
);
$period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
$period['1000000000'] = t('Never');
$form['watchdog_clear'] = array(
$form['error']['watchdog_clear'] = array(
'#type' => 'select',
'#title' => t('Discard log entries older than'),
'#default_value' => variable_get('watchdog_clear', 604800),
'#options' => $period,
'#description' => t('The time log entries should be kept. Older entries will be automatically discarded. Requires crontab.')
'#description' => t('The time log entries should be kept. Older entries will be automatically discarded. Requires crontab.'),
);
return system_settings_form($form);
......
......@@ -606,6 +606,10 @@ function taxonomy_get_vocabularies($type = NULL) {
*/
function taxonomy_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' ==