/[drupal]/contributions/modules/delete_all/delete_all.module
ViewVC logotype

Diff of /contributions/modules/delete_all/delete_all.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2, Mon Aug 4 00:20:17 2008 UTC revision 1.2.2.1, Mon Aug 4 23:33:02 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: delete_all.module,v 1.1.2.9 2008/05/27 22:16:07 kbahey Exp $  // $Id: delete_all.module,v 1.2 2008/08/04 00:20:17 kbahey Exp $
3    
4  function delete_all_menu() {  function delete_all_menu() {
5    $items = array();    $items = array();
6    
7    $items['admin/content/delete_content'] = array(    $items['admin/content/delete_content'] = array(
8      'title'               => 'Delete all content',      'title'            => 'Delete all content',
9      'description'         => 'Delete all nodes and comments on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.',      'description'      => 'Delete all nodes and comments on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.',
10      'page callback'       => 'drupal_get_form',      'page callback'    => 'drupal_get_form',
11      'page arguments'      => array('delete_all_content'),      'page arguments'   => array('delete_all_content'),
12      'access arguments'    => array('administer nodes'),      'access arguments' => array('administer nodes'),
13      'type'                => MENU_NORMAL_ITEM);      'type'             => MENU_NORMAL_ITEM,
14      );
15    
16      $items['admin/content/delete_content/confirm'] = array(
17        'title'            => 'Confirm deletion of all content',
18        'page callback'    => 'drupal_get_form',
19        'page arguments'   => array('delete_all_content_confirm'),
20        'access arguments' => array('administer nodes'),
21        'type'             => MENU_CALLBACK,
22      );
23    
24    $items['admin/content/delete_users'] = array(    $items['admin/content/delete_users'] = array(
25      'title'               => 'Delete all users',      'title'            => 'Delete all users',
26      'description'         => 'Delete all users on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.',      'description'      => 'Delete all users on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.',
27      'page callback'       => 'drupal_get_form',      'page callback'    => 'drupal_get_form',
28      'page arguments'      => array('delete_all_users'),      'page arguments'   => array('delete_all_users'),
29      'access arguments'    => array('administer users'),      'access arguments' => array('administer users'),
30      'type'               => MENU_NORMAL_ITEM);      'type'             => MENU_NORMAL_ITEM,
31      );
32    
33    return $items;    return $items;
34  }  }
35    
36  function delete_all_content() {  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'),  
   );  
   
37    // count how many nodes we have of each type    // count how many nodes we have of each type
38    $result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type");    $result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type");
39    $count = array();    $count = array();
40    while ($data = db_fetch_object($result)) {    while ($data = db_fetch_object($result)) {
41      $count[$data->type] = $data->num;      $count[$data->type] = $data->num;
42    }    }
43    
44    // add the types to the form    // Add the types to the form. If there are no eligible types to delete,
45      // we don't need to render the form.
46    $types = array();    $types = array();
47    foreach (node_get_types() as $type => $info) {    foreach (node_get_types() as $type => $info) {
48      if ($count[$type] > 0) {      if ($count[$type] > 0) {
# Line 49  function delete_all_content() { Line 50  function delete_all_content() {
50      }      }
51    }    }
52    asort($types);    asort($types);
53    
54      if (empty($types)) {
55        $form = array();
56        $form['no_content_types'] = array(
57          '#prefix' => '<p>',
58          '#suffix' => '</p>',
59          '#value' => t('There are no content types with content available to delete. You must <a href="@node-add">create some content</a> in order to delete it.', array('@node-add' => url('node/add'))),
60        );
61    
62        if (module_exists('devel')) {
63          $form['generate_content_suggestion'] = array(
64            '#prefix' => '<p>',
65            '#suffix' => '</p>',
66            '#value' => t('You can generate content quickly at the <a href="@generate-content-page">generate content page</a>.', array('@generate-content-page' => url('admin/generate/content'))),
67          );
68        }
69        return $form;
70      }
71    
72      drupal_add_js(drupal_get_path('module', 'delete_all') .'/delete_all.js');
73      $form = array();
74      $form['all'] = array(
75        '#type' => 'checkbox',
76        '#default_value' => TRUE,
77        '#title' => t('Delete All Content'),
78        '#description' => t('Select to delete all content'),
79        '#attributes' => array('class' => 'delete-all'),
80      );
81    
82    $form['type-fieldset'] = array(    $form['type-fieldset'] = array(
83      '#type' => 'fieldset',      '#type' => 'fieldset',
84      '#title' => t('Types'),      '#title' => t('Types'),
# Line 74  function delete_all_content() { Line 104  function delete_all_content() {
104        '#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.'),        '#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.'),
105      ),      ),
106    );    );
   $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,  
   );  
107    $form['submit'] = array(    $form['submit'] = array(
108      '#type' => 'submit',      '#type' => 'submit',
109      '#value' => t('Delete'),      '#value' => t('Delete'),
110    );    );
111    $form['#submit'] = array('delete_all_content_submit');    $form['#action'] = url('admin/content/delete_content/confirm');
112    return $form;    return $form;
113  }  }
114    
# Line 123  function theme_delete_all_checkboxes($fo Line 147  function theme_delete_all_checkboxes($fo
147    return theme('table', array(), $rows);    return theme('table', array(), $rows);
148  }  }
149    
150  function delete_all_content_submit($form, &$form_state) {  function delete_all_content_confirm() {
151      $results = $_POST;
152    
153      $form = array();
154      $form['method'] = array(
155        '#type' => 'hidden',
156        '#value' => $results['method'],
157      );
158    
159      $form['all'] = array(
160        '#type' => 'hidden',
161        '#value' => $results['all'],
162      );
163      if ($results['all']) {
164        $form['all_warning'] = array(
165          '#prefix' => '<p>',
166          '#suffix' => '<p>',
167          '#value' => t('All content in all content types will be deleted. Be sure to have a backup of any important data!'),
168        );
169      }
170    
171      if (is_array($results['types'])) {
172        foreach($results['types'] as $key_type => $type) {
173          $form['type_' . $key_type] = array(
174            '#type' => 'hidden',
175            '#value' => $type,
176          );
177          $info = node_get_types('type', $type);
178          $form[$type . '_warning'] = array(
179            '#prefix' => '<p>',
180            '#suffix' => '<p>',
181            '#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)),
182          );
183        }
184      }
185    
186      $keys = array_filter(array_keys($results), "_delete_all_type_keys");
187    
188      foreach ($keys as $key) {
189        $form[$key] = array(
190          '#type' => 'hidden',
191          '#value' => $results[$key],
192        );
193      }
194    
195      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'));
196    }
197    
198    function delete_all_content_confirm_submit($form, &$form_state) {
199    $types = array();    $types = array();
200      $form_state['values']['types'] = array();
201    
202    if (!$form_state['values']['all']) {    if (!$form_state['values']['all']) {
203      foreach ($form_state['values']['types'] as $type => $checked) {      $keys = array_filter(array_keys($form_state['values']), "_delete_all_type_keys");
204        if ($checked) {      foreach ($keys as $key) {
205          $types[] = $type;        $types[] = $form_state['values'][$key];
       }  
206      }      }
207    }    }
208    
# Line 294  function delete_all_users_submit($form, Line 367  function delete_all_users_submit($form,
367    drupal_goto('admin');    drupal_goto('admin');
368  }  }
369    
370    /**
371     * Private callback to determine if a variable starts with 'type_'.
372     * @param $var
373     *   The string to test against.
374     * @return bool
375     *  TRUE if $var begins with 'type_'
376     */
377    function _delete_all_type_keys($var) {
378      return (strpos($var, 'type_') === 0 ? TRUE : FALSE);
379    }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.2.2.1

  ViewVC Help
Powered by ViewVC 1.1.2