Index: delete_all.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/delete_all/delete_all.module,v retrieving revision 1.2 diff -u -r1.2 delete_all.module --- delete_all.module 4 Aug 2008 00:20:17 -0000 1.2 +++ delete_all.module 4 Aug 2008 20:19:28 -0000 @@ -10,7 +10,16 @@ 'page callback' => 'drupal_get_form', 'page arguments' => array('delete_all_content'), 'access arguments' => array('administer nodes'), - 'type' => MENU_NORMAL_ITEM); + 'type' => MENU_NORMAL_ITEM + ); + + $items['admin/content/delete_content/confirm'] = array( + 'title' => 'Confirm deletion of all content', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('delete_all_content_confirm'), + 'access arguments' => array('administer nodes'), + 'type' => MENU_CALLBACK, + ); $items['admin/content/delete_users'] = array( 'title' => 'Delete all users', @@ -18,30 +27,22 @@ 'page callback' => 'drupal_get_form', 'page arguments' => array('delete_all_users'), 'access arguments' => array('administer users'), - 'type' => MENU_NORMAL_ITEM); + 'type' => MENU_NORMAL_ITEM + ); return $items; } function delete_all_content() { - drupal_add_js(drupal_get_path('module', 'delete_all') .'/delete_all.js'); - $form = array(); - $form['all'] = array( - '#type' => 'checkbox', - '#default_value' => TRUE, - '#title' => t('Delete All Content'), - '#description' => t('Select to delete all content'), - '#attributes' => array('class' => 'delete-all'), - ); - // count how many nodes we have of each type $result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type"); $count = array(); while ($data = db_fetch_object($result)) { $count[$data->type] = $data->num; } - - // add the types to the form + + // Add the types to the form. If there are no eligible types to delete, + // we don't need to render the form. $types = array(); foreach (node_get_types() as $type => $info) { if ($count[$type] > 0) { @@ -49,6 +50,35 @@ } } asort($types); + + if (empty($types)) { + $form = array(); + $form['no_content_types'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#value' => t('There are no content types with content available to delete. You must create some content in order to delete it.', array('@node-add' => url('node/add'))), + ); + + if (module_exists('devel')) { + $form['generate_content_suggestion'] = array( + '#prefix' => '', + '#suffix' => '
', + '#value' => t('You can generate content quickly at the generate content page.', array('@generate-content-page' => url('admin/generate/content'))), + ); + } + return $form; + } + + drupal_add_js(drupal_get_path('module', 'delete_all') .'/delete_all.js'); + $form = array(); + $form['all'] = array( + '#type' => 'checkbox', + '#default_value' => TRUE, + '#title' => t('Delete All Content'), + '#description' => t('Select to delete all content'), + '#attributes' => array('class' => 'delete-all'), + ); + $form['type-fieldset'] = array( '#type' => 'fieldset', '#title' => t('Types'), @@ -74,17 +104,11 @@ '#description' => t('Normal node delete calls node_delete() on every node in the database. If you have only a few hundred nodes, this can take a very long time. Use the quick node delete method to get around this problem. This method deletes directly from the database, skipping the extra php processing. The downside is that it can miss related tables that are normally handled by module hook_delete\'s.'), ), ); - $form['confirm'] = array( - '#type' => 'checkbox', - '#title' => t('Delete Confirmation'), - '#description' => t('Please check this box to confirm that you understand you are deleting node content. This should only be done in a development environment.'), - '#required' => TRUE, - ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Delete'), ); - $form['#submit'] = array('delete_all_content_submit'); + $form['#action'] = url('admin/content/delete_content/confirm'); return $form; } @@ -123,13 +147,62 @@ return theme('table', array(), $rows); } -function delete_all_content_submit($form, &$form_state) { +function delete_all_content_confirm() { + $results = $_POST; + + $form = array(); + $form['method'] = array( + '#type' => 'hidden', + '#value' => $results['method'], + ); + + $form['all'] = array( + '#type' => 'hidden', + '#value' => $results['all'], + ); + if ($results['all']) { + $form['all_warning'] = array( + '#prefix' => '', + '#suffix' => '
', + '#value' => t('All content in all content types will be deleted. Be sure to have a backup of any important data!'), + ); + } + + if (is_array($results['types'])) { + foreach($results['types'] as $key_type => $type) { + $form['type_' . $key_type] = array( + '#type' => 'hidden', + '#value' => $type, + ); + $info = node_get_types('type', $type); + $form[$type . '_warning'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#value' => t('All content in the %type content type will be deleted. Be sure to have a backup of any important data!', array('%type' => $info->name)), + ); + } + } + + $keys = array_filter(array_keys($results), "_delete_all_type_keys"); + + foreach ($keys as $key) { + $form[$key] = array( + '#type' => 'hidden', + '#value' => $results[$key], + ); + } + + return confirm_form($form, t('Are you sure you wish to delete content?'), 'admin/content/delete_content', NULL, t('Delete all content now'), t('Cancel delete of all content')); +} + +function delete_all_content_confirm_submit($form, &$form_state) { $types = array(); + $form_state['values']['types'] = array(); + if (!$form_state['values']['all']) { - foreach ($form_state['values']['types'] as $type => $checked) { - if ($checked) { - $types[] = $type; - } + $keys = array_filter(array_keys($form_state['values']), "_delete_all_type_keys"); + foreach ($keys as $key) { + $types[] = $form_state['values'][$key]; } } @@ -294,3 +367,13 @@ drupal_goto('admin'); } +/** + * Private callback to determine if a variable starts with 'type_'. + * @param $var + * The string to test against. + * @return bool + * TRUE if $var begins with 'type_' + */ +function _delete_all_type_keys($var) { + return (strpos($var, 'type_') === 0 ? TRUE : FALSE); +} \ No newline at end of file