2007-08-20 07:03:08 +00:00
< ? php
// $Id$
/**
* @ file
* Content administration and module settings UI .
*/
/**
* Menu callback : confirm rebuilding of permissions .
*/
function node_configure_rebuild_confirm () {
2007-11-12 19:06:33 +00:00
return confirm_form ( array (), t ( 'Are you sure you want to rebuild the permissions on site content?' ),
2009-07-06 13:52:57 +00:00
'admin/reports/status' , t ( 'This action rebuilds all permissions on site content, and may be a lengthy process. This action cannot be undone.' ), t ( 'Rebuild permissions' ), t ( 'Cancel' ));
2007-08-20 07:03:08 +00:00
}
/**
* Handler for wipe confirmation
*/
2007-08-29 19:56:09 +00:00
function node_configure_rebuild_confirm_submit ( $form , & $form_state ) {
2007-09-02 14:42:30 +00:00
node_access_rebuild ( TRUE );
2009-06-05 05:28:28 +00:00
$form_state [ 'redirect' ] = 'admin/reports/status' ;
2007-08-20 07:03:08 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_operations () .
2007-08-20 07:03:08 +00:00
*/
function node_node_operations () {
$operations = array (
'publish' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Publish selected content' ),
2007-10-03 17:35:22 +00:00
'callback' => 'node_mass_update' ,
2009-09-05 06:53:01 +00:00
'callback arguments' => array ( 'updates' => array ( 'status' => NODE_PUBLISHED )),
2007-08-20 07:03:08 +00:00
),
'unpublish' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Unpublish selected content' ),
2007-10-03 17:35:22 +00:00
'callback' => 'node_mass_update' ,
2009-09-05 06:53:01 +00:00
'callback arguments' => array ( 'updates' => array ( 'status' => NODE_NOT_PUBLISHED )),
2007-08-20 07:03:08 +00:00
),
'promote' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Promote selected content to front page' ),
2007-10-03 17:35:22 +00:00
'callback' => 'node_mass_update' ,
2009-09-05 06:53:01 +00:00
'callback arguments' => array ( 'updates' => array ( 'status' => NODE_PUBLISHED , 'promote' => NODE_PROMOTED )),
2007-08-20 07:03:08 +00:00
),
'demote' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Demote selected content from front page' ),
2007-10-03 17:35:22 +00:00
'callback' => 'node_mass_update' ,
2009-09-05 06:53:01 +00:00
'callback arguments' => array ( 'updates' => array ( 'promote' => NODE_NOT_PROMOTED )),
2007-08-20 07:03:08 +00:00
),
'sticky' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Make selected content sticky' ),
2007-10-03 17:35:22 +00:00
'callback' => 'node_mass_update' ,
2009-09-05 06:53:01 +00:00
'callback arguments' => array ( 'updates' => array ( 'status' => NODE_PUBLISHED , 'sticky' => NODE_STICKY )),
2007-08-20 07:03:08 +00:00
),
'unsticky' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Make selected content not sticky' ),
2007-10-03 17:35:22 +00:00
'callback' => 'node_mass_update' ,
2009-09-05 06:53:01 +00:00
'callback arguments' => array ( 'updates' => array ( 'sticky' => NODE_NOT_STICKY )),
2007-08-20 07:03:08 +00:00
),
'delete' => array (
2010-01-13 23:30:43 +00:00
'label' => t ( 'Delete selected content' ),
2007-08-20 07:03:08 +00:00
'callback' => NULL ,
),
);
return $operations ;
}
/**
* List node administration filters that can be applied .
*/
function node_filters () {
// Regular filters
2008-01-29 11:08:17 +00:00
$filters [ 'status' ] = array (
'title' => t ( 'status' ),
'options' => array (
2009-11-03 04:35:01 +00:00
'[any]' => t ( 'any' ),
2008-01-29 11:08:17 +00:00
'status-1' => t ( 'published' ),
'status-0' => t ( 'not published' ),
'promote-1' => t ( 'promoted' ),
'promote-0' => t ( 'not promoted' ),
'sticky-1' => t ( 'sticky' ),
'sticky-0' => t ( 'not sticky' ),
),
2007-08-20 07:03:08 +00:00
);
// Include translation states if we have this module enabled
if ( module_exists ( 'translation' )) {
2008-01-29 11:08:17 +00:00
$filters [ 'status' ][ 'options' ] += array (
'translate-0' => t ( 'Up to date translation' ),
'translate-1' => t ( 'Outdated translation' ),
);
2007-08-20 07:03:08 +00:00
}
2009-11-03 04:35:01 +00:00
$filters [ 'type' ] = array (
'title' => t ( 'type' ),
'options' => array (
'[any]' => t ( 'any' ),
) + node_type_get_names (),
);
2007-08-20 07:03:08 +00:00
// Language filter if there is a list of languages
if ( $languages = module_invoke ( 'locale' , 'language_list' )) {
2009-12-02 19:26:23 +00:00
$languages = array ( LANGUAGE_NONE => t ( 'Language neutral' )) + $languages ;
2009-11-03 04:35:01 +00:00
$filters [ 'language' ] = array (
'title' => t ( 'language' ),
'options' => array (
'[any]' => t ( 'any' ),
) + $languages ,
);
2007-08-20 07:03:08 +00:00
}
return $filters ;
}
/**
2009-07-21 01:36:51 +00:00
* Apply filters for node administration filters based on session .
*
* @ param $query
* A SelectQuery to which the filters should be applied .
2007-08-20 07:03:08 +00:00
*/
2009-07-21 01:36:51 +00:00
function node_build_filter_query ( SelectQueryInterface $query ) {
2007-08-20 07:03:08 +00:00
// Build query
2009-06-02 06:58:17 +00:00
$filter_data = isset ( $_SESSION [ 'node_overview_filter' ]) ? $_SESSION [ 'node_overview_filter' ] : array ();
2009-07-21 01:36:51 +00:00
$counter = 0 ;
2009-06-02 06:58:17 +00:00
foreach ( $filter_data as $index => $filter ) {
2007-08-20 07:03:08 +00:00
list ( $key , $value ) = $filter ;
switch ( $key ) {
2009-07-21 01:36:51 +00:00
case 'term' :
2010-01-08 07:05:52 +00:00
$index = 'ti' . $counter ++ ;
$query -> join ( 'taxonomy_index' , $index , " n.nid = $index .nid " );
2009-07-21 01:36:51 +00:00
$query -> condition ( $index . '.tid' , $value );
break ;
2007-08-20 07:03:08 +00:00
case 'status' :
// Note: no exploitable hole as $key/$value have already been checked when submitted
list ( $key , $value ) = explode ( '-' , $value , 2 );
case 'type' :
case 'language' :
2009-07-21 01:36:51 +00:00
$query -> condition ( 'n.' . $key , $value );
2007-11-12 17:26:16 +00:00
break ;
2007-08-20 07:03:08 +00:00
}
}
}
/**
* Return form for node administration filters .
*/
function node_filter_form () {
2009-06-02 06:58:17 +00:00
$session = isset ( $_SESSION [ 'node_overview_filter' ]) ? $_SESSION [ 'node_overview_filter' ] : array ();
2007-08-20 07:03:08 +00:00
$filters = node_filters ();
$i = 0 ;
2008-01-29 11:08:17 +00:00
$form [ 'filters' ] = array (
'#type' => 'fieldset' ,
2007-08-20 07:03:08 +00:00
'#title' => t ( 'Show only items where' ),
'#theme' => 'node_filters' ,
);
foreach ( $session as $filter ) {
list ( $type , $value ) = $filter ;
2008-11-22 14:09:41 +00:00
if ( $type == 'term' ) {
2007-08-20 07:03:08 +00:00
// Load term name from DB rather than search and parse options array.
2009-01-08 01:16:57 +00:00
$value = module_invoke ( 'taxonomy' , 'term_load' , $value );
2007-08-20 07:03:08 +00:00
$value = $value -> name ;
}
2008-10-12 04:30:09 +00:00
elseif ( $type == 'language' ) {
2009-12-02 19:26:23 +00:00
$value = $value == LANGUAGE_NONE ? t ( 'Language neutral' ) : module_invoke ( 'locale' , 'language_name' , $value );
2007-08-20 07:03:08 +00:00
}
else {
$value = $filters [ $type ][ 'options' ][ $value ];
}
if ( $i ++ ) {
2009-08-23 04:37:52 +00:00
$form [ 'filters' ][ 'current' ][] = array ( '#markup' => t ( '<em>and</em> where <strong>%type</strong> is <strong>%value</strong>' , array ( '%type' => $filters [ $type ][ 'title' ], '%value' => $value )));
2007-08-20 07:03:08 +00:00
}
else {
2009-08-23 04:37:52 +00:00
$form [ 'filters' ][ 'current' ][] = array ( '#markup' => t ( '<strong>%type</strong> is <strong>%value</strong>' , array ( '%type' => $filters [ $type ][ 'title' ], '%value' => $value )));
2007-08-20 07:03:08 +00:00
}
if ( in_array ( $type , array ( 'type' , 'language' ))) {
// Remove the option if it is already being filtered on.
unset ( $filters [ $type ]);
}
}
foreach ( $filters as $key => $filter ) {
$names [ $key ] = $filter [ 'title' ];
2009-11-03 04:35:01 +00:00
$form [ 'filters' ][ 'status' ][ $key ] = array (
'#type' => 'select' ,
'#options' => $filter [ 'options' ],
'#title' => $filter [ 'title' ],
'#default_value' => '[any]' ,
);
2007-08-20 07:03:08 +00:00
}
2010-01-03 21:01:04 +00:00
$form [ 'filters' ][ 'actions' ] = array (
'#type' => 'container' ,
'#id' => 'node-admin-buttons' ,
'#attributes' => array ( 'class' => array ( 'form-actions' , 'container-inline' )),
);
$form [ 'filters' ][ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => ( count ( $session ) ? t ( 'Refine' ) : t ( 'Filter' )));
2007-08-20 07:03:08 +00:00
if ( count ( $session )) {
2010-01-03 21:01:04 +00:00
$form [ 'filters' ][ 'actions' ][ 'undo' ] = array ( '#type' => 'submit' , '#value' => t ( 'Undo' ));
$form [ 'filters' ][ 'actions' ][ 'reset' ] = array ( '#type' => 'submit' , '#value' => t ( 'Reset' ));
2007-08-20 07:03:08 +00:00
}
2008-11-10 05:23:01 +00:00
drupal_add_js ( 'misc/form.js' );
2007-08-20 07:03:08 +00:00
return $form ;
}
/**
* Theme node administration filter selector .
2007-12-06 09:58:34 +00:00
*
* @ ingroup themeable
2007-08-20 07:03:08 +00:00
*/
2009-10-09 01:00:08 +00:00
function theme_node_filters ( $variables ) {
$form = $variables [ 'form' ];
2007-08-20 07:03:08 +00:00
$output = '' ;
2009-10-09 01:00:08 +00:00
2009-02-18 14:28:25 +00:00
$output .= '<ul class="clearfix">' ;
2007-08-20 07:03:08 +00:00
if ( ! empty ( $form [ 'current' ])) {
foreach ( element_children ( $form [ 'current' ]) as $key ) {
2008-04-14 17:48:46 +00:00
$output .= '<li>' . drupal_render ( $form [ 'current' ][ $key ]) . '</li>' ;
2007-08-20 07:03:08 +00:00
}
}
2009-11-03 04:35:01 +00:00
$output .= '</ul>' ;
2007-08-20 07:03:08 +00:00
2010-03-07 06:49:10 +00:00
$output .= '<div class="clearfix">' ;
2009-11-03 04:35:01 +00:00
$output .= '<dl class="multiselect">' . ( ! empty ( $form [ 'current' ]) ? '<dt><em>' . t ( 'and' ) . '</em> ' . t ( 'where' ) . '</dt>' : '' );
2007-08-20 07:03:08 +00:00
2009-11-17 02:50:41 +00:00
$output .= '<dd>' ;
2007-08-20 07:03:08 +00:00
foreach ( element_children ( $form [ 'status' ]) as $key ) {
$output .= drupal_render ( $form [ 'status' ][ $key ]);
}
$output .= '</dd>' ;
$output .= '</dl>' ;
2010-01-03 21:01:04 +00:00
$output .= drupal_render ( $form [ 'actions' ]);
2007-08-20 07:03:08 +00:00
2010-03-07 06:49:10 +00:00
$output .= '</div>' ;
2007-08-20 07:03:08 +00:00
return $output ;
}
/**
* Process result from node administration filter form .
*/
function node_filter_form_submit ( $form , & $form_state ) {
$filters = node_filters ();
switch ( $form_state [ 'values' ][ 'op' ]) {
case t ( 'Filter' ) :
case t ( 'Refine' ) :
2009-11-03 04:35:01 +00:00
// Apply every filter that has a choice selected other than 'any'.
foreach ( $filters as $filter => $options ) {
if ( isset ( $form_state [ 'values' ][ $filter ]) && $form_state [ 'values' ][ $filter ] != '[any]' ) {
// Flatten the options array to accommodate hierarchical/nested options.
$flat_options = form_options_flatten ( $filters [ $filter ][ 'options' ]);
// Only accept valid selections offered on the dropdown, block bad input.
if ( isset ( $flat_options [ $form_state [ 'values' ][ $filter ]])) {
$_SESSION [ 'node_overview_filter' ][] = array ( $filter , $form_state [ 'values' ][ $filter ]);
}
2007-08-20 07:03:08 +00:00
}
}
break ;
case t ( 'Undo' ) :
array_pop ( $_SESSION [ 'node_overview_filter' ]);
break ;
case t ( 'Reset' ) :
2009-06-02 06:58:17 +00:00
$_SESSION [ 'node_overview_filter' ] = array ();
2007-08-20 07:03:08 +00:00
break ;
}
}
2007-12-20 09:08:35 +00:00
/**
* Make mass update of nodes , changing all nodes in the $nodes array
* to update them with the field values in $updates .
*
* IMPORTANT NOTE : This function is intended to work when called
* from a form submit handler . Calling it outside of the form submission
* process may not work correctly .
*
* @ param array $nodes
* Array of node nids to update .
* @ param array $updates
* Array of key / value pairs with node field names and the
* value to update that field to .
*/
function node_mass_update ( $nodes , $updates ) {
// We use batch processing to prevent timeout when updating a large number
// of nodes.
if ( count ( $nodes ) > 10 ) {
$batch = array (
'operations' => array (
array ( '_node_mass_update_batch_process' , array ( $nodes , $updates ))
),
'finished' => '_node_mass_update_batch_finished' ,
'title' => t ( 'Processing' ),
// We use a single multi-pass operation, so the default
// 'Remaining x of y operations' message will be confusing here.
'progress_message' => '' ,
'error_message' => t ( 'The update has encountered an error.' ),
// The operations do not live in the .module file, so we need to
// tell the batch engine which file to load before calling them.
2008-04-14 17:48:46 +00:00
'file' => drupal_get_path ( 'module' , 'node' ) . '/node.admin.inc' ,
2007-12-20 09:08:35 +00:00
);
batch_set ( $batch );
}
else {
2007-12-28 12:02:52 +00:00
foreach ( $nodes as $nid ) {
2007-12-20 09:08:35 +00:00
_node_mass_update_helper ( $nid , $updates );
}
drupal_set_message ( t ( 'The update has been performed.' ));
}
}
/**
* Node Mass Update - helper function .
*/
function _node_mass_update_helper ( $nid , $updates ) {
$node = node_load ( $nid , NULL , TRUE );
foreach ( $updates as $name => $value ) {
$node -> $name = $value ;
}
node_save ( $node );
return $node ;
}
/**
* Node Mass Update Batch operation
*/
function _node_mass_update_batch_process ( $nodes , $updates , & $context ) {
if ( ! isset ( $context [ 'sandbox' ][ 'progress' ])) {
$context [ 'sandbox' ][ 'progress' ] = 0 ;
$context [ 'sandbox' ][ 'max' ] = count ( $nodes );
$context [ 'sandbox' ][ 'nodes' ] = $nodes ;
}
// Process nodes by groups of 5.
$count = min ( 5 , count ( $context [ 'sandbox' ][ 'nodes' ]));
for ( $i = 1 ; $i <= $count ; $i ++ ) {
// For each nid, load the node, reset the values, and save it.
$nid = array_shift ( $context [ 'sandbox' ][ 'nodes' ]);
$node = _node_mass_update_helper ( $nid , $updates );
// Store result for post-processing in the finished callback.
2010-01-09 21:54:01 +00:00
$context [ 'results' ][] = l ( $node -> title , 'node/' . $node -> nid );
2007-12-20 09:08:35 +00:00
// Update our progress information.
$context [ 'sandbox' ][ 'progress' ] ++ ;
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ( $context [ 'sandbox' ][ 'progress' ] != $context [ 'sandbox' ][ 'max' ]) {
$context [ 'finished' ] = $context [ 'sandbox' ][ 'progress' ] / $context [ 'sandbox' ][ 'max' ];
}
}
/**
* Node Mass Update Batch 'finished' callback .
*/
function _node_mass_update_batch_finished ( $success , $results , $operations ) {
if ( $success ) {
drupal_set_message ( t ( 'The update has been performed.' ));
}
else {
drupal_set_message ( t ( 'An error occurred and processing did not complete.' ), 'error' );
$message = format_plural ( count ( $results ), '1 item successfully processed:' , '@count items successfully processed:' );
2009-10-09 01:00:08 +00:00
$message .= theme ( 'item_list' , array ( 'items' => $results ));
2007-12-20 09:08:35 +00:00
drupal_set_message ( $message );
}
}
2007-08-20 07:03:08 +00:00
/**
* Menu callback : content administration .
*/
2009-09-18 00:12:48 +00:00
function node_admin_content ( $form , $form_state ) {
2007-08-20 07:03:08 +00:00
if ( isset ( $form_state [ 'values' ][ 'operation' ]) && $form_state [ 'values' ][ 'operation' ] == 'delete' ) {
2009-10-22 01:19:45 +00:00
return node_multiple_delete_confirm ( $form , $form_state , array_filter ( $form_state [ 'values' ][ 'nodes' ]));
2007-08-20 07:03:08 +00:00
}
2009-11-14 07:58:50 +00:00
$form [ 'filter' ] = node_filter_form ();
2009-08-23 04:37:52 +00:00
$form [ '#submit' ][] = 'node_filter_form_submit' ;
2008-09-15 00:02:10 +00:00
$form [ 'admin' ] = node_admin_nodes ();
2007-08-20 07:03:08 +00:00
return $form ;
}
2008-01-29 11:08:17 +00:00
/**
* Form builder : Builds the node administration overview .
*/
2007-08-20 07:03:08 +00:00
function node_admin_nodes () {
2009-11-14 07:58:50 +00:00
$admin_access = user_access ( 'administer nodes' );
2008-09-15 20:48:10 +00:00
2008-09-15 00:02:10 +00:00
// Build the 'Update options' form.
2008-01-29 11:08:17 +00:00
$form [ 'options' ] = array (
'#type' => 'fieldset' ,
2007-08-20 07:03:08 +00:00
'#title' => t ( 'Update options' ),
2010-03-03 19:46:26 +00:00
'#attributes' => array ( 'class' => array ( 'container-inline' )),
2009-11-14 07:58:50 +00:00
'#access' => $admin_access ,
2007-08-20 07:03:08 +00:00
);
$options = array ();
foreach ( module_invoke_all ( 'node_operations' ) as $operation => $array ) {
$options [ $operation ] = $array [ 'label' ];
}
2008-01-29 11:08:17 +00:00
$form [ 'options' ][ 'operation' ] = array (
'#type' => 'select' ,
'#options' => $options ,
'#default_value' => 'approve' ,
);
2007-08-20 07:03:08 +00:00
$form [ 'options' ][ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Update' ),
2009-10-24 21:39:40 +00:00
'#validate' => array ( 'node_admin_nodes_validate' ),
2009-11-14 07:58:50 +00:00
'#submit' => array ( 'node_admin_nodes_submit' ),
2007-08-20 07:03:08 +00:00
);
2009-11-14 07:58:50 +00:00
// Enable language column if translation module is enabled
// or if we have any node with language.
2009-12-02 19:26:23 +00:00
$multilanguage = ( module_exists ( 'translation' ) || db_query ( " SELECT COUNT(*) FROM { node} WHERE language <> :language " , array ( ':language' => LANGUAGE_NONE )) -> fetchField ());
2009-11-14 07:58:50 +00:00
// Build the sortable table header.
$header = array (
'title' => array ( 'data' => t ( 'Title' ), 'field' => 'n.title' ),
'type' => array ( 'data' => t ( 'Type' ), 'field' => 'n.type' ),
2010-01-08 05:57:12 +00:00
'author' => t ( 'Author' ),
2009-11-14 07:58:50 +00:00
'status' => array ( 'data' => t ( 'Status' ), 'field' => 'n.status' ),
'changed' => array ( 'data' => t ( 'Updated' ), 'field' => 'n.changed' , 'sort' => 'desc' )
);
if ( $multilanguage ) {
$header [ 'language' ] = array ( 'data' => t ( 'Language' ), 'field' => 'n.language' );
}
$header [ 'operations' ] = array ( 'data' => t ( 'Operations' ));
$query = db_select ( 'node' , 'n' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
node_build_filter_query ( $query );
if ( ! user_access ( 'bypass node access' )) {
// If the user is able to view their own unpublished nodes, allow them
// to see these in addition to published nodes. Check that they actually
// have some unpublished nodes to view before adding the condition.
if ( user_access ( 'view own unpublished content' ) && $own_unpublished = db_query ( 'SELECT nid FROM {node} WHERE uid = :uid AND status = :status' , array ( ':uid' => $GLOBALS [ 'user' ] -> uid , ':status' => 0 )) -> fetchCol ()) {
$query -> condition ( db_or ()
-> condition ( 'n.status' , 1 )
-> condition ( 'n.nid' , $own_unpublished , 'IN' )
);
}
else {
// If not, restrict the query to published nodes.
$query -> condition ( 'n.status' , 1 );
}
}
$nids = $query
-> fields ( 'n' , array ( 'nid' ))
-> limit ( 50 )
-> orderByHeader ( $header )
-> execute ()
-> fetchCol ();
$nodes = node_load_multiple ( $nids );
// Prepare the list of nodes.
2007-12-08 14:06:23 +00:00
$languages = language_list ();
2007-08-20 07:03:08 +00:00
$destination = drupal_get_destination ();
2009-11-14 07:58:50 +00:00
$options = array ();
foreach ( $nodes as $node ) {
2009-12-02 19:26:23 +00:00
$l_options = $node -> language != LANGUAGE_NONE ? array ( 'language' => $languages [ $node -> language ]) : array ();
2009-10-08 18:26:33 +00:00
$options [ $node -> nid ] = array (
2009-11-03 05:27:18 +00:00
'title' => array (
'data' => array (
'#type' => 'link' ,
2010-01-09 21:54:01 +00:00
'#title' => $node -> title ,
2009-11-03 05:27:18 +00:00
'#href' => 'node/' . $node -> nid ,
'#options' => $l_options ,
'#suffix' => ' ' . theme ( 'mark' , array ( 'type' => node_mark ( $node -> nid , $node -> changed ))),
),
),
2009-11-14 07:58:50 +00:00
'type' => check_plain ( node_type_get_name ( $node )),
2009-10-09 01:00:08 +00:00
'author' => theme ( 'username' , array ( 'account' => $node )),
2009-11-14 07:58:50 +00:00
'status' => $node -> status ? t ( 'published' ) : t ( 'not published' ),
2009-10-08 18:26:33 +00:00
'changed' => format_date ( $node -> changed , 'short' ),
);
2007-08-20 07:03:08 +00:00
if ( $multilanguage ) {
2009-12-02 19:26:23 +00:00
$options [ $node -> nid ][ 'language' ] = $node -> language == LANGUAGE_NONE ? t ( 'Language neutral' ) : t ( $languages [ $node -> language ] -> name );
2007-08-20 07:03:08 +00:00
}
2009-11-14 07:58:50 +00:00
// Build a list of all the accessible operations for the current node.
$operations = array ();
if ( node_access ( 'update' , $node )) {
$operations [ 'edit' ] = array (
'title' => t ( 'edit' ),
'href' => 'node/' . $node -> nid . '/edit' ,
'query' => $destination ,
);
}
if ( node_access ( 'delete' , $node )) {
$operations [ 'delete' ] = array (
'title' => t ( 'delete' ),
'href' => 'node/' . $node -> nid . '/delete' ,
'query' => $destination ,
);
}
$options [ $node -> nid ][ 'operations' ] = array ();
if ( count ( $operations ) > 1 ) {
// Render an unordered list of operations links.
$options [ $node -> nid ][ 'operations' ] = array (
'data' => array (
2010-01-08 07:36:53 +00:00
'#theme' => 'links__node_operations' ,
2009-11-14 07:58:50 +00:00
'#links' => $operations ,
'#attributes' => array ( 'class' => array ( 'links' , 'inline' )),
),
);
}
elseif ( ! empty ( $operations )) {
// Render the first and only operation as a link.
$link = reset ( $operations );
$options [ $node -> nid ][ 'operations' ] = array (
'data' => array (
'#type' => 'link' ,
'#title' => $link [ 'title' ],
'#href' => $link [ 'href' ],
'#options' => array ( 'query' => $link [ 'query' ]),
),
);
}
}
// Only use a tableselect when the current user is able to perform any
// operations.
if ( $admin_access ) {
$form [ 'nodes' ] = array (
'#type' => 'tableselect' ,
'#header' => $header ,
'#options' => $options ,
'#empty' => t ( 'No content available.' ),
2009-11-03 05:27:18 +00:00
);
2007-08-20 07:03:08 +00:00
}
2009-11-14 07:58:50 +00:00
// Otherwise, use a simple table.
else {
$form [ 'nodes' ] = array (
'#theme' => 'table' ,
'#header' => $header ,
'#rows' => $options ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No content available.' ),
2009-11-14 07:58:50 +00:00
);
}
2009-10-09 01:00:08 +00:00
$form [ 'pager' ] = array ( '#markup' => theme ( 'pager' , array ( 'tags' => NULL )));
2007-08-20 07:03:08 +00:00
return $form ;
}
2008-01-29 11:08:17 +00:00
/**
* Validate node_admin_nodes form submissions .
2008-02-06 19:38:28 +00:00
*
2008-01-29 11:08:17 +00:00
* Check if any nodes have been selected to perform the chosen
* 'Update option' on .
*/
function node_admin_nodes_validate ( $form , & $form_state ) {
2009-10-24 21:39:40 +00:00
// Error if there are no items to select.
if ( ! is_array ( $form_state [ 'values' ][ 'nodes' ]) || ! count ( array_filter ( $form_state [ 'values' ][ 'nodes' ]))) {
2008-01-29 11:08:17 +00:00
form_set_error ( '' , t ( 'No items selected.' ));
}
}
/**
* Process node_admin_nodes form submissions .
2008-02-06 19:38:28 +00:00
*
2008-01-29 11:08:17 +00:00
* Execute the chosen 'Update option' on the selected nodes .
*/
function node_admin_nodes_submit ( $form , & $form_state ) {
$operations = module_invoke_all ( 'node_operations' );
$operation = $operations [ $form_state [ 'values' ][ 'operation' ]];
// Filter out unchecked nodes
$nodes = array_filter ( $form_state [ 'values' ][ 'nodes' ]);
if ( $function = $operation [ 'callback' ]) {
// Add in callback arguments if present.
if ( isset ( $operation [ 'callback arguments' ])) {
$args = array_merge ( array ( $nodes ), $operation [ 'callback arguments' ]);
}
else {
$args = array ( $nodes );
}
call_user_func_array ( $function , $args );
cache_clear_all ();
}
else {
2008-12-20 18:24:41 +00:00
// We need to rebuild the form to go to a second step. For example, to
2008-01-29 11:08:17 +00:00
// show the confirmation form for the deletion of nodes.
$form_state [ 'rebuild' ] = TRUE ;
}
}
2009-09-18 00:12:48 +00:00
function node_multiple_delete_confirm ( $form , & $form_state , $nodes ) {
2007-08-20 07:03:08 +00:00
$form [ 'nodes' ] = array ( '#prefix' => '<ul>' , '#suffix' => '</ul>' , '#tree' => TRUE );
// array_filter returns only elements with TRUE values
foreach ( $nodes as $nid => $value ) {
2009-04-25 16:33:48 +00:00
$title = db_query ( 'SELECT title FROM {node} WHERE nid = :nid' , array ( ':nid' => $nid )) -> fetchField ();
2008-01-29 11:08:17 +00:00
$form [ 'nodes' ][ $nid ] = array (
'#type' => 'hidden' ,
'#value' => $nid ,
'#prefix' => '<li>' ,
2008-04-14 17:48:46 +00:00
'#suffix' => check_plain ( $title ) . " </li> \n " ,
2008-01-29 11:08:17 +00:00
);
2007-08-20 07:03:08 +00:00
}
$form [ 'operation' ] = array ( '#type' => 'hidden' , '#value' => 'delete' );
$form [ '#submit' ][] = 'node_multiple_delete_confirm_submit' ;
2009-05-24 17:39:35 +00:00
$confirm_question = format_plural ( count ( $nodes ),
'Are you sure you want to delete this item?' ,
2009-05-15 03:58:35 +00:00
'Are you sure you want to delete these items?' );
2009-05-24 17:39:35 +00:00
return confirm_form ( $form ,
2009-05-15 03:58:35 +00:00
$confirm_question ,
2009-07-30 19:24:21 +00:00
'admin/content' , t ( 'This action cannot be undone.' ),
2009-05-15 03:58:35 +00:00
t ( 'Delete' ), t ( 'Cancel' ));
2007-08-20 07:03:08 +00:00
}
function node_multiple_delete_confirm_submit ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'confirm' ]) {
2009-05-20 05:39:45 +00:00
node_delete_multiple ( array_keys ( $form_state [ 'values' ][ 'nodes' ]));
$count = count ( $form_state [ 'values' ][ 'nodes' ]);
watchdog ( 'content' , 'Deleted @count posts.' , array ( '@count' => $count ));
drupal_set_message ( t ( 'Deleted @count posts.' , array ( '@count' => $count )));
2007-08-20 07:03:08 +00:00
}
2009-07-30 19:24:21 +00:00
$form_state [ 'redirect' ] = 'admin/content' ;
2007-08-20 07:03:08 +00:00
}