2007-08-20 07:03:08 +00:00
< ? php
// $Id$
/**
* @ file
* Page callbacks for adding , editing , deleting , and revisions management for content .
*/
/**
* Menu callback ; presents the node editing form , or redirects to delete confirmation .
*/
function node_page_edit ( $node ) {
2009-06-04 03:33:29 +00:00
$type_name = node_type_get_name ( $node );
2009-02-13 02:22:09 +00:00
drupal_set_title ( t ( '<em>Edit @type</em> @title' , array ( '@type' => $type_name , '@title' => $node -> title )), PASS_THROUGH );
2008-04-14 17:48:46 +00:00
return drupal_get_form ( $node -> type . '_node_form' , $node );
2007-08-20 07:03:08 +00:00
}
function node_add_page () {
$item = menu_get_item ();
2007-12-22 23:24:26 +00:00
$content = system_admin_menu_block ( $item );
2008-08-14 13:47:35 +00:00
// Bypass the node/add listing if only one content type is available.
if ( count ( $content ) == 1 ) {
$item = array_shift ( $content );
drupal_goto ( $item [ 'href' ]);
}
2007-08-20 07:03:08 +00:00
return theme ( 'node_add_list' , $content );
}
2007-12-06 09:58:34 +00:00
/**
* Display the list of available node types for node creation .
*
* @ ingroup themeable
*/
2007-08-20 07:03:08 +00:00
function theme_node_add_list ( $content ) {
$output = '' ;
if ( $content ) {
$output = '<dl class="node-type-list">' ;
foreach ( $content as $item ) {
2008-07-10 11:12:02 +00:00
$output .= '<dt>' . l ( $item [ 'title' ], $item [ 'href' ], $item [ 'localized_options' ]) . '</dt>' ;
2008-04-14 17:48:46 +00:00
$output .= '<dd>' . filter_xss_admin ( $item [ 'description' ]) . '</dd>' ;
2007-08-20 07:03:08 +00:00
}
$output .= '</dl>' ;
}
return $output ;
}
/**
* Present a node submission form or a set of links to such forms .
*/
function node_add ( $type ) {
global $user ;
2009-06-04 03:33:29 +00:00
$types = node_type_get_types ();
2007-08-20 07:03:08 +00:00
$type = isset ( $type ) ? str_replace ( '-' , '_' , $type ) : NULL ;
// If a node type has been specified, validate its existence.
if ( isset ( $types [ $type ]) && node_access ( 'create' , $type )) {
// Initialize settings:
2008-01-10 14:31:23 +00:00
$node = array ( 'uid' => $user -> uid , 'name' => ( isset ( $user -> name ) ? $user -> name : '' ), 'type' => $type , 'language' => '' );
2007-08-20 07:03:08 +00:00
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Create @name' , array ( '@name' => $types [ $type ] -> name )), PASS_THROUGH );
2008-04-14 17:48:46 +00:00
$output = drupal_get_form ( $type . '_node_form' , $node );
2007-08-20 07:03:08 +00:00
}
return $output ;
}
function node_form_validate ( $form , & $form_state ) {
2009-03-26 13:31:28 +00:00
$node = $form_state [ 'values' ];
node_validate ( $node , $form );
// Field validation. Requires access to $form_state, so this cannot be
// done in node_validate() as it currently exists.
$node = ( object ) $node ;
field_attach_form_validate ( 'node' , $node , $form , $form_state );
2007-08-20 07:03:08 +00:00
}
2009-05-09 18:28:13 +00:00
function node_object_prepare ( $node ) {
2007-09-19 18:11:08 +00:00
// Set up default values, if required.
2008-04-14 17:48:46 +00:00
$node_options = variable_get ( 'node_options_' . $node -> type , array ( 'status' , 'promote' ));
2007-08-20 07:03:08 +00:00
// If this is a new node, fill in the default values.
if ( ! isset ( $node -> nid )) {
foreach ( array ( 'status' , 'promote' , 'sticky' ) as $key ) {
$node -> $key = in_array ( $key , $node_options );
}
global $user ;
$node -> uid = $user -> uid ;
2008-09-17 07:11:59 +00:00
$node -> created = REQUEST_TIME ;
2007-09-19 18:11:08 +00:00
}
else {
$node -> date = format_date ( $node -> created , 'custom' , 'Y-m-d H:i:s O' );
2009-04-15 13:28:08 +00:00
// Remove the log message from the original node object.
$node -> log = NULL ;
2007-08-20 07:03:08 +00:00
}
// Always use the default revision setting.
$node -> revision = in_array ( 'revision' , $node_options );
node_invoke ( $node , 'prepare' );
2009-04-26 09:14:32 +00:00
module_invoke_all ( 'node_prepare' , $node );
2007-08-20 07:03:08 +00:00
}
/**
* Generate the node add / edit form array .
*/
function node_form ( & $form_state , $node ) {
global $user ;
if ( isset ( $form_state [ 'node' ])) {
$node = $form_state [ 'node' ] + ( array ) $node ;
}
if ( isset ( $form_state [ 'node_preview' ])) {
$form [ '#prefix' ] = $form_state [ 'node_preview' ];
}
$node = ( object ) $node ;
2009-06-12 08:39:40 +00:00
foreach ( array ( 'title' ) as $key ) {
2007-08-20 07:03:08 +00:00
if ( ! isset ( $node -> $key )) {
$node -> $key = NULL ;
}
}
2007-09-09 19:52:29 +00:00
if ( ! isset ( $form_state [ 'node_preview' ])) {
node_object_prepare ( $node );
}
2007-10-05 13:14:04 +00:00
else {
2009-06-22 09:10:07 +00:00
$node -> in_preview = TRUE ;
2007-10-05 13:14:04 +00:00
}
2007-08-20 07:03:08 +00:00
2008-09-27 20:37:01 +00:00
// Set the id and identify this as a node edit form.
2007-08-20 07:03:08 +00:00
$form [ '#id' ] = 'node-form' ;
2008-09-27 20:37:01 +00:00
$form [ '#node_edit_form' ] = TRUE ;
2007-08-20 07:03:08 +00:00
2007-12-31 08:54:37 +00:00
// Basic node information.
// These elements are just values so they are not even sent to the client.
2007-08-20 07:03:08 +00:00
foreach ( array ( 'nid' , 'vid' , 'uid' , 'created' , 'type' , 'language' ) as $key ) {
2007-11-27 13:31:04 +00:00
$form [ $key ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'value' ,
2007-11-27 13:31:04 +00:00
'#value' => isset ( $node -> $key ) ? $node -> $key : NULL ,
);
2007-08-20 07:03:08 +00:00
}
// Changed must be sent to the client, for later overwrite error checking.
2007-11-27 13:31:04 +00:00
$form [ 'changed' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'hidden' ,
2007-11-27 13:31:04 +00:00
'#default_value' => isset ( $node -> changed ) ? $node -> changed : NULL ,
);
2007-08-20 07:03:08 +00:00
// Get the node-specific bits.
if ( $extra = node_invoke ( $node , 'form' , $form_state )) {
$form = array_merge_recursive ( $form , $extra );
}
if ( ! isset ( $form [ 'title' ][ '#weight' ])) {
$form [ 'title' ][ '#weight' ] = - 5 ;
}
$form [ '#node' ] = $node ;
2009-04-11 22:19:46 +00:00
$form [ 'additional_settings' ] = array (
'#type' => 'vertical_tabs' ,
);
2007-08-20 07:03:08 +00:00
// Add a log field if the "Create new revision" option is checked, or if the
// current user has the ability to check that option.
2007-09-27 12:56:04 +00:00
if ( ! empty ( $node -> revision ) || user_access ( 'administer nodes' )) {
2007-08-20 07:03:08 +00:00
$form [ 'revision_information' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Revision information' ),
'#collapsible' => TRUE ,
// Collapsed by default when "Create new revision" is unchecked
'#collapsed' => ! $node -> revision ,
2009-04-11 22:19:46 +00:00
'#group' => 'additional_settings' ,
'#attached_js' => array ( drupal_get_path ( 'module' , 'node' ) . '/node.js' ),
2007-08-20 07:03:08 +00:00
'#weight' => 20 ,
);
$form [ 'revision_information' ][ 'revision' ] = array (
2007-12-22 23:24:26 +00:00
'#access' => user_access ( 'administer nodes' ),
2007-08-20 07:03:08 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Create new revision' ),
'#default_value' => $node -> revision ,
);
$form [ 'revision_information' ][ 'log' ] = array (
'#type' => 'textarea' ,
2009-01-22 12:04:27 +00:00
'#title' => t ( 'Revision log message' ),
2009-04-11 22:19:46 +00:00
'#rows' => 4 ,
2009-04-15 13:28:08 +00:00
'#default_value' => ! empty ( $node -> log ) ? $node -> log : '' ,
2009-01-22 12:04:27 +00:00
'#description' => t ( 'Provide an explanation of the changes you are making. This will help other authors understand your motivations.' ),
2007-08-20 07:03:08 +00:00
);
}
// Node author information for administrators
$form [ 'author' ] = array (
'#type' => 'fieldset' ,
'#access' => user_access ( 'administer nodes' ),
'#title' => t ( 'Authoring information' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
2009-04-11 22:19:46 +00:00
'#group' => 'additional_settings' ,
'#attached_js' => array ( drupal_get_path ( 'module' , 'node' ) . '/node.js' ),
2008-08-22 12:38:02 +00:00
'#weight' => 90 ,
2007-08-20 07:03:08 +00:00
);
2007-11-27 13:31:04 +00:00
$form [ 'author' ][ 'name' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Authored by' ),
'#maxlength' => 60 ,
'#autocomplete_path' => 'user/autocomplete' ,
2008-10-11 20:45:26 +00:00
'#default_value' => ! empty ( $node -> name ) ? $node -> name : '' ,
2007-11-28 10:29:21 +00:00
'#weight' => - 1 ,
2007-11-27 13:31:04 +00:00
'#description' => t ( 'Leave blank for %anonymous.' , array ( '%anonymous' => variable_get ( 'anonymous' , t ( 'Anonymous' )))),
);
$form [ 'author' ][ 'date' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Authored on' ),
'#maxlength' => 25 ,
2009-05-06 11:27:47 +00:00
'#description' => t ( 'Format: %time. The date format is YYYY-MM-DD and %timezone is the timezone offset from UTC. Leave blank to use the time of form submission.' , array ( '%time' => ! empty ( $node -> date ) ? $node -> date : format_date ( $node -> created , 'custom' , 'Y-m-d H:i:s O' ), '%timezone' => ! empty ( $node -> date ) ? $node -> date : format_date ( $node -> created , 'custom' , 'O' ))),
2007-11-27 13:31:04 +00:00
);
2007-08-20 07:03:08 +00:00
2007-09-19 18:11:08 +00:00
if ( isset ( $node -> date )) {
2007-08-20 07:03:08 +00:00
$form [ 'author' ][ 'date' ][ '#default_value' ] = $node -> date ;
}
// Node options for administrators
$form [ 'options' ] = array (
'#type' => 'fieldset' ,
'#access' => user_access ( 'administer nodes' ),
'#title' => t ( 'Publishing options' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
2009-04-11 22:19:46 +00:00
'#group' => 'additional_settings' ,
'#attached_js' => array ( drupal_get_path ( 'module' , 'node' ) . '/node.js' ),
2008-08-22 12:38:02 +00:00
'#weight' => 95 ,
2007-08-20 07:03:08 +00:00
);
2007-11-27 13:31:04 +00:00
$form [ 'options' ][ 'status' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Published' ),
2007-11-27 13:31:04 +00:00
'#default_value' => $node -> status ,
);
$form [ 'options' ][ 'promote' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Promoted to front page' ),
2007-11-27 13:31:04 +00:00
'#default_value' => $node -> promote ,
);
$form [ 'options' ][ 'sticky' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Sticky at top of lists' ),
2007-11-27 13:31:04 +00:00
'#default_value' => $node -> sticky ,
);
2007-08-20 07:03:08 +00:00
// These values are used when the user has no administrator access.
foreach ( array ( 'uid' , 'created' ) as $key ) {
2007-11-27 13:31:04 +00:00
$form [ $key ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'value' ,
2007-11-27 13:31:04 +00:00
'#value' => $node -> $key ,
);
2007-08-20 07:03:08 +00:00
}
// Add the buttons.
$form [ 'buttons' ] = array ();
2008-08-22 12:38:02 +00:00
$form [ 'buttons' ][ '#weight' ] = 100 ;
2007-11-29 11:54:37 +00:00
$form [ 'buttons' ][ 'submit' ] = array (
2007-08-20 07:03:08 +00:00
'#type' => 'submit' ,
2009-06-05 05:28:28 +00:00
'#access' => variable_get ( 'node_preview_' . $node -> type , 1 ) != DRUPAL_REQUIRED || ( ! form_get_errors () && isset ( $form_state [ 'node_preview' ])),
2007-11-29 11:54:37 +00:00
'#value' => t ( 'Save' ),
2007-08-20 07:03:08 +00:00
'#weight' => 5 ,
2007-11-29 11:54:37 +00:00
'#submit' => array ( 'node_form_submit' ),
2007-08-20 07:03:08 +00:00
);
2007-11-29 11:54:37 +00:00
$form [ 'buttons' ][ 'preview' ] = array (
2009-06-05 05:28:28 +00:00
'#access' => variable_get ( 'node_preview_' . $node -> type , 1 ) != DRUPAL_DISABLED ,
2007-08-20 07:03:08 +00:00
'#type' => 'submit' ,
2007-11-29 11:54:37 +00:00
'#value' => t ( 'Preview' ),
2007-08-20 07:03:08 +00:00
'#weight' => 10 ,
2007-11-29 11:54:37 +00:00
'#submit' => array ( 'node_form_build_preview' ),
2007-08-20 07:03:08 +00:00
);
if ( ! empty ( $node -> nid ) && node_access ( 'delete' , $node )) {
$form [ 'buttons' ][ 'delete' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Delete' ),
'#weight' => 15 ,
'#submit' => array ( 'node_form_delete_submit' ),
);
}
$form [ '#validate' ][] = 'node_form_validate' ;
2009-02-05 03:42:58 +00:00
$form [ '#theme' ] = array ( $node -> type . '_node_form' , 'node_form' );
$form [ '#builder_function' ] = 'node_form_submit_build_node' ;
field_attach_form ( 'node' , $node , $form , $form_state );
2009-02-03 17:30:13 +00:00
2007-08-20 07:03:08 +00:00
return $form ;
}
/**
2008-12-30 16:43:20 +00:00
* Button submit function : handle the 'Delete' button on the node form .
2007-08-20 07:03:08 +00:00
*/
function node_form_delete_submit ( $form , & $form_state ) {
$destination = '' ;
if ( isset ( $_REQUEST [ 'destination' ])) {
$destination = drupal_get_destination ();
unset ( $_REQUEST [ 'destination' ]);
}
$node = $form [ '#node' ];
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = array ( 'node/' . $node -> nid . '/delete' , $destination );
2007-08-20 07:03:08 +00:00
}
function node_form_build_preview ( $form , & $form_state ) {
$node = node_form_submit_build_node ( $form , $form_state );
$form_state [ 'node_preview' ] = node_preview ( $node );
}
2007-12-06 09:58:34 +00:00
/**
* Present a node submission form .
*
* @ ingroup themeable
*/
2007-08-20 07:03:08 +00:00
function theme_node_form ( $form ) {
$output = " \n <div class= \" node-form \" > \n " ;
$output .= " <div class= \" standard \" > \n " ;
2009-02-03 18:55:32 +00:00
$output .= drupal_render_children ( $form );
2007-08-20 07:03:08 +00:00
$output .= " </div> \n " ;
$output .= " </div> \n " ;
return $output ;
}
/**
* Generate a node preview .
*/
function node_preview ( $node ) {
if ( node_access ( 'create' , $node ) || node_access ( 'update' , $node )) {
2008-01-29 11:08:17 +00:00
// Load the user's name when needed.
2007-08-20 07:03:08 +00:00
if ( isset ( $node -> name )) {
// The use of isset() is mandatory in the context of user IDs, because
// user ID 0 denotes the anonymous user.
2009-03-14 23:01:38 +00:00
if ( $user = user_load_by_name ( $node -> name )) {
2007-08-20 07:03:08 +00:00
$node -> uid = $user -> uid ;
$node -> picture = $user -> picture ;
}
else {
$node -> uid = 0 ; // anonymous user
}
}
2008-10-12 04:30:09 +00:00
elseif ( $node -> uid ) {
2009-03-14 23:01:38 +00:00
$user = user_load ( $node -> uid );
2007-08-20 07:03:08 +00:00
$node -> name = $user -> name ;
$node -> picture = $user -> picture ;
}
2008-09-17 07:11:59 +00:00
$node -> changed = REQUEST_TIME ;
2007-08-20 07:03:08 +00:00
2008-01-29 11:08:17 +00:00
// Display a preview of the node.
2007-08-20 07:03:08 +00:00
// Previewing alters $node so it needs to be cloned.
if ( ! form_get_errors ()) {
2008-02-17 19:29:07 +00:00
$cloned_node = clone $node ;
2009-06-22 09:10:07 +00:00
$cloned_node -> in_preview = TRUE ;
2007-08-20 07:03:08 +00:00
$output = theme ( 'node_preview' , $cloned_node );
}
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Preview' ), PASS_THROUGH );
2007-08-20 07:03:08 +00:00
return $output ;
}
}
/**
* Display a node preview for display during node creation and editing .
*
* @ param $node
* The node object which is being previewed .
2007-12-06 09:58:34 +00:00
*
* @ ingroup themeable
2007-08-20 07:03:08 +00:00
*/
function theme_node_preview ( $node ) {
$output = '<div class="preview">' ;
2008-01-10 15:57:10 +00:00
2008-01-09 12:10:04 +00:00
$preview_trimmed_version = FALSE ;
2008-01-10 15:57:10 +00:00
2009-06-22 09:10:07 +00:00
$trimmed = drupal_render ( node_build ( clone $node , 'teaser' ));
$full = drupal_render ( node_build ( $node , 'full' ));
2009-06-12 08:39:40 +00:00
// Do we need to preview trimmed version of post as well as full version?
if ( $trimmed != $full ) {
2007-08-20 07:03:08 +00:00
drupal_set_message ( t ( 'The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication.<span class="no-js"> You can insert the delimiter "<!--break-->" (without the quotes) to fine-tune where your post gets split.</span>' ));
2008-04-14 17:48:46 +00:00
$output .= '<h3>' . t ( 'Preview trimmed version' ) . '</h3>' ;
2009-06-12 08:39:40 +00:00
$output .= $trimmed ;
2008-04-14 17:48:46 +00:00
$output .= '<h3>' . t ( 'Preview full version' ) . '</h3>' ;
2009-06-12 08:39:40 +00:00
$output .= $full ;
2007-08-20 07:03:08 +00:00
}
else {
2009-06-12 08:39:40 +00:00
$output .= $full ;
2007-08-20 07:03:08 +00:00
}
$output .= " </div> \n " ;
return $output ;
}
function node_form_submit ( $form , & $form_state ) {
global $user ;
$node = node_form_submit_build_node ( $form , $form_state );
$insert = empty ( $node -> nid );
node_save ( $node );
2008-04-14 17:48:46 +00:00
$node_link = l ( t ( 'view' ), 'node/' . $node -> nid );
2007-08-20 07:03:08 +00:00
$watchdog_args = array ( '@type' => $node -> type , '%title' => $node -> title );
2009-06-04 03:33:29 +00:00
$t_args = array ( '@type' => node_type_get_name ( $node ), '%title' => $node -> title );
2007-08-20 07:03:08 +00:00
if ( $insert ) {
watchdog ( 'content' , '@type: added %title.' , $watchdog_args , WATCHDOG_NOTICE , $node_link );
2007-12-11 12:13:39 +00:00
drupal_set_message ( t ( '@type %title has been created.' , $t_args ));
2007-08-20 07:03:08 +00:00
}
else {
watchdog ( 'content' , '@type: updated %title.' , $watchdog_args , WATCHDOG_NOTICE , $node_link );
2007-12-11 12:13:39 +00:00
drupal_set_message ( t ( '@type %title has been updated.' , $t_args ));
2007-08-20 07:03:08 +00:00
}
if ( $node -> nid ) {
unset ( $form_state [ 'rebuild' ]);
2008-02-03 19:26:10 +00:00
$form_state [ 'nid' ] = $node -> nid ;
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = 'node/' . $node -> nid ;
2007-08-20 07:03:08 +00:00
}
else {
// In the unlikely case something went wrong on save, the node will be
// rebuilt and node form redisplayed the same way as in preview.
2007-11-12 19:06:33 +00:00
drupal_set_message ( t ( 'The post could not be saved.' ), 'error' );
2007-08-20 07:03:08 +00:00
}
}
/**
* Build a node by processing submitted form values and prepare for a form rebuild .
*/
function node_form_submit_build_node ( $form , & $form_state ) {
// Unset any button-level handlers, execute all the form-level submit
// functions to process the form values into an updated node.
unset ( $form_state [ 'submit_handlers' ]);
2009-02-05 03:42:58 +00:00
form_execute_handlers ( 'submit' , $form , $form_state );
$node = node_submit ( $form_state [ 'values' ]);
field_attach_submit ( 'node' , $node , $form , $form_state );
2009-02-03 17:30:13 +00:00
2007-08-20 07:03:08 +00:00
$form_state [ 'node' ] = ( array ) $node ;
$form_state [ 'rebuild' ] = TRUE ;
return $node ;
}
/**
* Menu callback -- ask for confirmation of node deletion
*/
function node_delete_confirm ( & $form_state , $node ) {
2007-11-27 13:31:04 +00:00
$form [ 'nid' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'value' ,
2007-11-27 13:31:04 +00:00
'#value' => $node -> nid ,
);
2007-08-20 07:03:08 +00:00
return confirm_form ( $form ,
t ( 'Are you sure you want to delete %title?' , array ( '%title' => $node -> title )),
2008-04-14 17:48:46 +00:00
isset ( $_GET [ 'destination' ]) ? $_GET [ 'destination' ] : 'node/' . $node -> nid ,
2007-08-20 07:03:08 +00:00
t ( 'This action cannot be undone.' ),
2007-11-28 10:29:21 +00:00
t ( 'Delete' ),
2007-11-27 13:31:04 +00:00
t ( 'Cancel' )
);
2007-08-20 07:03:08 +00:00
}
/**
* Execute node deletion
*/
function node_delete_confirm_submit ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'confirm' ]) {
2009-05-20 05:39:45 +00:00
$node = node_load ( $form_state [ 'values' ][ 'nid' ]);
2007-08-20 07:03:08 +00:00
node_delete ( $form_state [ 'values' ][ 'nid' ]);
2009-05-20 05:39:45 +00:00
watchdog ( 'content' , '@type: deleted %title.' , array ( '@type' => $node -> type , '%title' => $node -> title ));
2009-06-04 03:33:29 +00:00
drupal_set_message ( t ( '@type %title has been deleted.' , array ( '@type' => node_type_get_name ( $node ), '%title' => $node -> title )));
2007-08-20 07:03:08 +00:00
}
$form_state [ 'redirect' ] = '<front>' ;
}
/**
* Generate an overview table of older revisions of a node .
*/
function node_revision_overview ( $node ) {
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Revisions for %title' , array ( '%title' => $node -> title )), PASS_THROUGH );
2007-08-20 07:03:08 +00:00
$header = array ( t ( 'Revision' ), array ( 'data' => t ( 'Operations' ), 'colspan' => 2 ));
$revisions = node_revision_list ( $node );
$rows = array ();
$revert_permission = FALSE ;
if (( user_access ( 'revert revisions' ) || user_access ( 'administer nodes' )) && node_access ( 'update' , $node )) {
$revert_permission = TRUE ;
}
$delete_permission = FALSE ;
2007-12-20 09:20:41 +00:00
if (( user_access ( 'delete revisions' ) || user_access ( 'administer nodes' )) && node_access ( 'delete' , $node )) {
2007-08-20 07:03:08 +00:00
$delete_permission = TRUE ;
}
foreach ( $revisions as $revision ) {
$row = array ();
$operations = array ();
if ( $revision -> current_vid > 0 ) {
$row [] = array ( 'data' => t ( '!date by !username' , array ( '!date' => l ( format_date ( $revision -> timestamp , 'small' ), " node/ $node->nid " ), '!username' => theme ( 'username' , $revision )))
2008-04-14 17:48:46 +00:00
. (( $revision -> log != '' ) ? '<p class="revision-log">' . filter_xss ( $revision -> log ) . '</p>' : '' ),
2007-08-20 07:03:08 +00:00
'class' => 'revision-current' );
$operations [] = array ( 'data' => theme ( 'placeholder' , t ( 'current revision' )), 'class' => 'revision-current' , 'colspan' => 2 );
}
else {
$row [] = t ( '!date by !username' , array ( '!date' => l ( format_date ( $revision -> timestamp , 'small' ), " node/ $node->nid /revisions/ $revision->vid /view " ), '!username' => theme ( 'username' , $revision )))
2008-04-14 17:48:46 +00:00
. (( $revision -> log != '' ) ? '<p class="revision-log">' . filter_xss ( $revision -> log ) . '</p>' : '' );
2007-08-20 07:03:08 +00:00
if ( $revert_permission ) {
$operations [] = l ( t ( 'revert' ), " node/ $node->nid /revisions/ $revision->vid /revert " );
}
if ( $delete_permission ) {
$operations [] = l ( t ( 'delete' ), " node/ $node->nid /revisions/ $revision->vid /delete " );
}
}
$rows [] = array_merge ( $row , $operations );
}
return theme ( 'table' , $header , $rows );
}
2007-12-03 07:45:15 +00:00
/**
* Ask for confirmation of the reversion to prevent against CSRF attacks .
*/
function node_revision_revert_confirm ( $form_state , $node_revision ) {
$form [ '#node_revision' ] = $node_revision ;
2008-04-14 17:48:46 +00:00
return confirm_form ( $form , t ( 'Are you sure you want to revert to the revision from %revision-date?' , array ( '%revision-date' => format_date ( $node_revision -> revision_timestamp ))), 'node/' . $node_revision -> nid . '/revisions' , '' , t ( 'Revert' ), t ( 'Cancel' ));
2007-12-03 07:45:15 +00:00
}
function node_revision_revert_confirm_submit ( $form , & $form_state ) {
$node_revision = $form [ '#node_revision' ];
$node_revision -> revision = 1 ;
$node_revision -> log = t ( 'Copy of the revision from %date.' , array ( '%date' => format_date ( $node_revision -> revision_timestamp )));
if ( module_exists ( 'taxonomy' )) {
$node_revision -> taxonomy = array_keys ( $node_revision -> taxonomy );
}
node_save ( $node_revision );
watchdog ( 'content' , '@type: reverted %title revision %revision.' , array ( '@type' => $node_revision -> type , '%title' => $node_revision -> title , '%revision' => $node_revision -> vid ));
2009-06-04 03:33:29 +00:00
drupal_set_message ( t ( '@type %title has been reverted back to the revision from %revision-date.' , array ( '@type' => node_type_get_name ( $node_revision ), '%title' => $node_revision -> title , '%revision-date' => format_date ( $node_revision -> revision_timestamp ))));
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = 'node/' . $node_revision -> nid . '/revisions' ;
2007-12-03 07:45:15 +00:00
}
2007-08-20 07:03:08 +00:00
2007-12-03 07:45:15 +00:00
function node_revision_delete_confirm ( $form_state , $node_revision ) {
$form [ '#node_revision' ] = $node_revision ;
2008-04-14 17:48:46 +00:00
return confirm_form ( $form , t ( 'Are you sure you want to delete the revision from %revision-date?' , array ( '%revision-date' => format_date ( $node_revision -> revision_timestamp ))), 'node/' . $node_revision -> nid . '/revisions' , t ( 'This action cannot be undone.' ), t ( 'Delete' ), t ( 'Cancel' ));
2007-12-03 07:45:15 +00:00
}
function node_revision_delete_confirm_submit ( $form , & $form_state ) {
$node_revision = $form [ '#node_revision' ];
2009-04-25 16:33:48 +00:00
db_delete ( 'node_revision' )
-> condition ( 'nid' , $node_revision -> nid )
-> condition ( 'vid' , $node_revision -> vid )
-> execute ();
2009-04-26 09:14:32 +00:00
module_invoke_all ( 'node_delete_revision' , $node_revision );
2007-12-03 07:45:15 +00:00
watchdog ( 'content' , '@type: deleted %title revision %revision.' , array ( '@type' => $node_revision -> type , '%title' => $node_revision -> title , '%revision' => $node_revision -> vid ));
2009-06-04 03:33:29 +00:00
drupal_set_message ( t ( 'Revision from %revision-date of @type %title has been deleted.' , array ( '%revision-date' => format_date ( $node_revision -> revision_timestamp ), '@type' => node_type_get_name ( $node_revision ), '%title' => $node_revision -> title )));
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = 'node/' . $node_revision -> nid ;
2009-04-25 16:33:48 +00:00
if ( db_query ( 'SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid' , array ( ':nid' => $node_revision -> nid )) -> fetchField () > 1 ) {
2007-12-03 07:45:15 +00:00
$form_state [ 'redirect' ] .= '/revisions' ;
}
}