Commit 49fa25fb authored by Dries's avatar Dries

- Rollback of patch #68418: clean up node submit hooks. Needs more thinkering.

  Restores the old node API.
parent c64db1da
......@@ -187,15 +187,18 @@ function book_load($node) {
return db_fetch_object(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid));
}
function book_form_alter($form_id, &$form) {
if ($form_id == 'node_delete_confirm') {
$form['#submit']['book_node_delete_confirm_submit'] = array();
}
/**
* 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_node_submit($form_id, $node) {
$node = (object)$node;
if ($node->is_new || $node->revision) {
/**
* Implementation of hook_update().
*/
function book_update($node) {
if ($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 {
......@@ -203,16 +206,30 @@ function book_node_submit($form_id, $node) {
}
}
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_delete().
*/
function book_delete(&$node) {
db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
}
/**
* Implementation of hook_form().
* Implementation of hook_submit().
*/
function book_form(&$node) {
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 : '';
}
}
/**
* Implementation of hook_form().
*/
function book_form(&$node) {
$type = node_get_types('type', $node);
if ($node->nid && !$node->parent && !user_access('create new books')) {
$form['parent'] = array('#type' => 'value', '#value' => $node->parent);
......@@ -248,23 +265,22 @@ function book_form(&$node) {
'#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
);
$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 (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 {
// 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;
......@@ -465,6 +481,9 @@ 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;
}
}
......
......@@ -278,7 +278,6 @@ 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',
......@@ -296,10 +295,6 @@ function comment_form_alter($form_id, &$form) {
);
}
}
if ($form_id == 'node_delete_confirm') {
$form['#submit']['comment_node_delete_confirm_submit'] = array();
}
}
/**
......@@ -318,6 +313,15 @@ 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);
......@@ -340,17 +344,6 @@ 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().
*
......
......@@ -231,31 +231,6 @@ 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)));
}
}
}
}
}
/**
......@@ -350,15 +325,16 @@ function forum_view(&$node, $teaser = FALSE, $page = FALSE) {
}
/**
* An early submit handler of the forum_node_form. Runs before the node is saved.
* Implementation of hook_submit().
*
* Check in particular that only a "leaf" term in the associated taxonomy
* vocabulary is selected, not a "container" term.
*/
function forum_node_submit_early($form_id, &$node) {
$node = (object)$node;
function forum_submit(&$node) {
// Make sure all fields are set properly:
$node->icon = $node->icon ? $node->icon : '';
if (!empty($node->taxonomy)) {
if ($node->taxonomy) {
// Get the forum terms from the (cached) tree
$tree = taxonomy_get_tree(_forum_get_vid());
if ($tree) {
......@@ -379,13 +355,35 @@ function forum_node_submit_early($form_id, &$node) {
}
}
}
}
$node = (array)$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)));
}
}
}
}
}
function forum_node_submit($form_id, $node) {
$node = (object)$node;
if ($node->is_new) {
/**
* Implementation of hook_update().
*/
function forum_update($node) {
if ($node->revision) {
db_query("INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $node->tid);
}
else {
......@@ -424,8 +422,18 @@ function forum_prepare(&$node) {
}
}
function forum_node_delete_confirm_submit($form_id, $form_values) {
db_query('DELETE FROM {forum} WHERE nid = %d', $form_values['node']->nid);
/**
* 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);
}
/**
......
......@@ -146,20 +146,31 @@ function menu_block($op = 'list', $delta = 0) {
}
}
function menu_node_delete_confirm_submit($form_id, $form_values) {
menu_node_form_delete($form_values['node']);
menu_rebuild();
}
/**
* 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_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();
case 'delete':
menu_node_form_delete($node);
menu_rebuild();
break;
}
}
}
......@@ -187,8 +198,6 @@ 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'),
......@@ -249,10 +258,6 @@ 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.
......@@ -207,6 +207,16 @@ 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.
......@@ -216,36 +226,26 @@ function path_nodeapi(&$node, $op, $arg) {
$node->path = db_result($result);
}
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 '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;
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);
}
}
case 'update':
path_set_alias("node/$node->nid", $node->path, $node->pid);
break;
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.'));
case 'delete':
$path = "node/$node->nid";
if (drupal_get_path_alias($path) != $path) {
path_set_alias($path);
}
break;
}
}
}
......@@ -254,12 +254,6 @@ function path_node_validate($form_id, $node) {
*/
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',
......@@ -284,10 +278,6 @@ function path_form_alter($form_id, &$form) {
);
}
}
if ($form_id == 'node_delete_confirm') {
$form['#submit']['path_node_delete_confirm_submit'] = array();
}
}
......
......@@ -76,45 +76,45 @@ function poll_cron() {
}
}
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_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);
}
/**
* An early submit handler of the poll_node_form. Runs before the node is saved.
* Implementation of hook_submit().
*/
function poll_node_submit_early($form_id, &$node) {
$node = (object)$node;
function poll_submit(&$node) {
// Renumber fields
$node->choice = array_values($node->choice);
$node->teaser = poll_teaser($node);
}
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 ($choice['chvotes'] < 0) {
form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
/**
* 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.'));
}
}
}
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();
if ($realchoices < 2) {
form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
}
}
}
......@@ -634,16 +634,9 @@ function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
return $node;
}
function poll_node_submit($form_id, $node) {
$node = (object)$node;
if ($node->is_new) {
poll_insert($node);
}
else {
poll_update($node);
}
}
/**
* Implementation of hook_update().
*/
function poll_update($node) {
db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);
......
......@@ -531,13 +531,14 @@ function _statistics_format_item($title, $path) {
return $output;
}
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();
/**
* 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);
}
}
......
......@@ -606,10 +606,6 @@ function taxonomy_get_vocabularies($type = NULL) {
*/
function taxonomy_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
// inject custom validate and submit handlers
$form['#validate']['taxonomy_node_validate'] = array();
$form['#submit']['taxonomy_node_submit'] = array();
$node = $form['#node'];
if (!isset($node->taxonomy)) {
......@@ -676,10 +672,6 @@ function taxonomy_form_alter($form_id, &$form) {
$form['taxonomy'] += array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3);
}
}
if ($form_id == 'node_delete_confirm') {
$form['#submit']['taxonomy_node_delete_confirm_submit'] = array();
}
}
/**
......@@ -713,9 +705,9 @@ function taxonomy_node_get_terms($nid, $key = 'tid') {
/**
* Make sure incoming vids are free tagging enabled.
*/
function taxonomy_node_validate($form_id, $node) {
if ($node['taxonomy']) {
$terms = $node['taxonomy'];
function taxonomy_node_validate(&$node) {
if ($node->taxonomy) {
$terms = $node->taxonomy;
if ($terms['tags']) {
foreach ($terms['tags'] as $vid => $vid_value) {
$vocabulary = taxonomy_get_vocabulary($vid);
......@@ -732,14 +724,14 @@ function taxonomy_node_validate($form_id, $node) {
/**
* Save term associations for a given node.
*/
function taxonomy_node_submit($form_id, $node) {
taxonomy_node_delete($node->nid);
function taxonomy_node_save($nid, $terms) {
taxonomy_node_delete($nid);
// Free tagging vocabularies do not send their tids in the form,
// so we'll detect them here and process them independently.
if (isset($node->taxonomy['tags'])) {
$typed_input = $node->taxonomy['tags'];
unset($node->taxonomy['tags']);
if (isset($terms['tags'])) {
$typed_input = $terms['tags'];
unset($terms['tags']);
foreach ($typed_input as $vid => $vid_value) {
// This regexp allows the following types of user input:
......@@ -775,27 +767,27 @@ function taxonomy_node_submit($form_id, $node) {
// Defend against duplicate, different cased tags
if (!isset($inserted[$typed_term_tid])) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $node->nid, $typed_term_tid);
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $typed_term_tid);
$inserted[$typed_term_tid] = TRUE;
}
}
}
}
if (is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
if (is_array($terms)) {
foreach ($terms as $term) {
if (is_array($term)) {
foreach ($term as $tid) {
if ($tid) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $node->nid, $tid);
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
}
}
}
else if (is_object($term)) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $node->nid, $term->tid);
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term->tid);
}
else if ($term) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $node->nid, $term);
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
}
}
}
......@@ -1165,6 +1157,18 @@ function taxonomy_nodeapi($node, $op, $arg = 0) {
case 'load':
$output['taxonomy'] = taxonomy_node_get_terms($node->nid);