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

Diff of /contributions/modules/trash/trash.module

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

revision 1.1, Thu Feb 21 17:33:17 2008 UTC revision 1.2, Thu Feb 21 17:42:15 2008 UTC
# Line 32  define('TRASH_LIST_SIZE', 50); Line 32  define('TRASH_LIST_SIZE', 50);
32  /**  /**
33   * Implementation of hook_help().   * Implementation of hook_help().
34   */   */
35  function trash_help($section) {  function trash_help($path, $arg) {
36    switch ($section) {    switch ($path) {
37      case 'admin/help#trash':      case 'admin/help#trash':
38        $output = '<p>' . t('The trash module gives you a node trash. Deleted nodes will be moved into the trash and can be restored or permanently deleted afterwards') . '</p>';        $output = '<p>' . t('The trash module gives you a node trash. Deleted nodes will be moved into the trash and can be restored or permanently deleted afterwards') . '</p>';
39        return $output;        return $output;
# Line 51  function trash_perm() { Line 51  function trash_perm() {
51  /**  /**
52   * Implementation of hook_menu().   * Implementation of hook_menu().
53   */   */
54  function trash_menu($may_cache) {  function trash_menu() {
55    $items = array();    $items = array();
56    if ($may_cache) {    $items['trash'] = array(
57      $items[] = array(      'title' => t('Trash'),
58        'path' => 'trash',      'description' => t('Node trash.'),
59        'title' => t('Trash'),      'page callback' => 'trash_view',
60        'description' => t('Node trash.'),      'access arguments' => array('view trash'),
61        'access' => user_access('view trash'),      'type' => MENU_NORMAL_ITEM
62        'callback' => 'trash_view',    );
63        'type' => MENU_NORMAL_ITEM    $items['trash/empty'] = array(
64      );      'page callback' => 'drupal_get_form',
65      $items[] = array(      'page arguments' => array('trash_empty_trash_confirm'),
66        'path' => 'trash/empty',      'access arguments' => array('purge trash'),
67        'access' => user_access('purge trash'),      'type' => MENU_CALLBACK
68        'callback' => 'drupal_get_form',    );
69        'callback arguments' => array('trash_empty_trash_confirm'),    // View node from trash
70        'type' => MENU_CALLBACK    $items['trash/%node'] = array(
71      );      'title' => t('View'),
72        'page callback' => 'trash_view_node',
73        'page arguments' => array(1),
74        'access arguments' => array('view trash'),
75        'type' => MENU_CALLBACK,
76      );
77      $items['trash/%node/view'] = array(
78        'title' => t('View'),
79        'type' => MENU_DEFAULT_LOCAL_TASK,
80        'weight' => -10,
81      );
82      // Restore node from trash
83      $items['trash/%node/restore'] = array(
84        'title' => t('Restore'),
85        'page callback' => 'drupal_get_form',
86        'page arguments' => array('trash_restore_node_confirm', 1),
87        'access arguments' => array('restore trash'),
88        'type' => MENU_LOCAL_TASK,
89        'weight' => 0,
90      );
91      // Delete node from trash
92      $items['trash/%node/delete'] = array(
93        'title' => t('Delete'),
94        'page callback' => 'drupal_get_form',
95        'page arguments' => array('trash_delete_node_confirm', 1),
96        'access arguments' => array('purge trash'),
97        'type' => MENU_LOCAL_TASK,
98        'weight' => 10,
99      );
100      // Move to trash tab
101      $items['node/%node/trash'] = array(
102        'title' => t('Move to trash'),
103        'page callback' => 'drupal_get_form',
104        'page arguments' => array('node_delete_confirm', 1),
105        'access callback' => '_trash_move_tab_access',
106        'access arguments' => array(1),
107        'weight' => 1,
108        'type' => MENU_LOCAL_TASK,
109        'file' => 'node.pages.inc',
110        'file path' => drupal_get_path('module', 'node')
111      );
112      return $items;
113    }
114    
115    /**
116     * Implementation of hook_menu_alter().
117     */
118    function trash_menu_alter(&$callbacks) {
119      $callbacks['node/%node']['page callback'] = '_trash_node_page_view';
120      $callbacks['node/%node/edit']['page callback'] = '_trash_node_page_edit';
121      $callbacks['node/%node/delete']['page callback'] = '_trash_node_page_delete';
122    }
123    
124    /**
125     * Access callback for 'Move to trash' tab.
126     */
127    function _trash_move_tab_access($node) {
128      return $node->nid && trash_enabled($node->type) && trash_move_to_trash_tab_enabled($node->type);
129    }
130    
131    /**
132     * Overridden page callback for 'node/%node' path.
133     */
134    function _trash_node_page_view($node, $cid = NULL) {
135      if ($node->status == STATUS_TRASH) {
136        return trash_deleted_message($node);
137    }    }
138    else {    else {
139      if (arg(0) == 'trash' && is_numeric(arg(1))) {      return node_page_view($node, $cid);
140        $node = node_load(arg(1));    }
141        if ($node->nid) {  }
142          // View node from trash  
143          $items[] = array(  /**
144            'path' => 'trash/'. arg(1),   * Overridden page callback for 'node/%node/edit' path.
145            'title' => t('View'),   */
146            'callback' => 'trash_view_node',  function _trash_node_page_edit($node) {
147            'callback arguments' => array($node),    if ($node->status == STATUS_TRASH) {
148            'access' => user_access('view trash'),      return trash_deleted_message($node);
149            'type' => MENU_CALLBACK,    }
150          );    else {
151          $items[] = array(      return node_page_edit($node);
152            'path' => 'trash/'. arg(1) .'/view',    }
153            'title' => t('View'),  }
154            'type' => MENU_DEFAULT_LOCAL_TASK,  
155            'weight' => -10,  /**
156          );   * Overridden page callback for 'node/%node/delete' path.
157          // Restore node from trash   */
158          $items[] = array(  function _trash_node_page_delete($node) {
159            'path' => 'trash/'. arg(1) .'/restore',    if ($node->status == STATUS_TRASH) {
160            'title' => t('Restore'),      return trash_deleted_message($node);
161            'callback' => 'drupal_get_form',    }
162            'callback arguments' => array('trash_restore_node_confirm', $node),    else {
163            'access' => user_access('restore trash'),      return drupal_get_form('node_delete_confirm', $node);
           'type' => MENU_LOCAL_TASK,  
           'weight' => 0,  
         );  
         // Delete node from trash  
         $items[] = array(  
           'path' => 'trash/'. arg(1) .'/delete',  
           'title' => t('Delete'),  
           'callback' => 'drupal_get_form',  
           'callback arguments' => array('trash_delete_node_confirm', $node),  
           'access' => user_access('purge trash'),  
           'type' => MENU_LOCAL_TASK,  
           'weight' => 10,  
         );  
       }  
     }  
     elseif (arg(0) == 'node' && is_numeric(arg(1))) {  
       $node = node_load(arg(1));  
       if ($node->nid && $node->status == STATUS_TRASH) {  
         // It's a trashed node, disable 'view' access  
         $items[] = array(  
           'path' => 'node/'. arg(1),  
           'callback' => 'trash_deleted_message',  
           'callback arguments' => array($node)  
         );  
         $items[] = array(  
           'path' => 'node/'. arg(1) .'/edit',  
           'callback' => 'trash_deleted_message',  
           'callback arguments' => array($node)  
         );  
         $items[] = array(  
           'path' => 'node/'. arg(1) .'/delete',  
           'callback' => 'trash_deleted_message',  
           'callback arguments' => array($node)  
         );  
       }  
       elseif ($node->nid && trash_enabled($node->type) && trash_move_to_trash_tab_enabled($node->type)) {  
         $items[] = array(  
           'path' => 'node/'. arg(1) .'/trash',  
           'title' => t('Move to trash'),  
           'callback' => 'drupal_get_form',  
           'callback arguments' => array('node_delete_confirm', $node),  
           'access' => node_access('delete', $node),  
           'weight' => 1,  
           'type' => MENU_LOCAL_TASK  
         );  
       }  
     }  
164    }    }
   return $items;  
165  }  }
166    
167  /**  /**
# Line 158  function trash_form_alter(&$form, $form_ Line 175  function trash_form_alter(&$form, $form_
175          // Node is edited.          // Node is edited.
176          // Remove delete button as there is a 'Move to trash' tab now.          // Remove delete button as there is a 'Move to trash' tab now.
177          if (trash_delete_button_removing_enabled($form['type']['#value'])) {          if (trash_delete_button_removing_enabled($form['type']['#value'])) {
178            unset($form['delete']);            unset($form['buttons']['delete']);
179          }          }
180          else {          else {
181            $form['delete']['#value'] = t('Move to trash');            $form['buttons']['delete']['#value'] = t('Move to trash');
182          }          }
183        }        }
184      }      }
# Line 181  function trash_form_alter(&$form, $form_ Line 198  function trash_form_alter(&$form, $form_
198        '#default_value' => variable_get('trash_settings_'. $form['#node_type']->type, array()),        '#default_value' => variable_get('trash_settings_'. $form['#node_type']->type, array()),
199      );      );
200    }    }
   print_r($form_id);  
201    if ($form_id == 'node_delete_confirm') {    if ($form_id == 'node_delete_confirm') {
   print_r($form);  
202      $node = node_load($form['nid']['#value']);      $node = node_load($form['nid']['#value']);
203      if (trash_enabled($node->type)) {      if (trash_enabled($node->type)) {
204        drupal_set_title(t('Are you sure you want to move %title to the trash?', array('%title' => $node->title)));        drupal_set_title(t('Are you sure you want to move %title to the trash?', array('%title' => $node->title)));
# Line 196  function trash_form_alter(&$form, $form_ Line 211  function trash_form_alter(&$form, $form_
211        $form['description']['#value'] = t('The node will be moved to the !trash where it can be restored or permanently deleted.', array('!trash' => l(t('trash'), 'trash')));        $form['description']['#value'] = t('The node will be moved to the !trash where it can be restored or permanently deleted.', array('!trash' => l(t('trash'), 'trash')));
212        $form['actions']['submit']['#value'] = t('Move to trash');        $form['actions']['submit']['#value'] = t('Move to trash');
213        unset($form['#submit']);        unset($form['#submit']);
214        $form['#submit']['trash_node_delete_confirm_submit'] = array();        $form['#submit'] = array('trash_node_delete_confirm_submit');
215      }      }
216    }    }
217  }  }
# Line 319  function trash_view_node($node) { Line 334  function trash_view_node($node) {
334  /**  /**
335   * Restore node confirmation form.   * Restore node confirmation form.
336   */   */
337  function trash_restore_node_confirm($node) {  function trash_restore_node_confirm(&$form_state, $node) {
338    $form['nid'] = array('#type' => 'value', '#value' => $node->nid);    $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
339    $form['log'] = array(    $form['log'] = array(
340      '#type' => 'textfield',      '#type' => 'textfield',
# Line 337  function trash_restore_node_confirm($nod Line 352  function trash_restore_node_confirm($nod
352  /**  /**
353   * Restore node confirmation form submit handler.   * Restore node confirmation form submit handler.
354   */   */
355  function trash_restore_node_confirm_submit($form_id, $form_values) {  function trash_restore_node_confirm_submit($form, &$form_state) {
356    if ($form_values['confirm']) {    if ($form_state['values']['confirm']) {
357      $node = node_load($form_values['nid']);      $node = node_load($form_state['values']['nid']);
358      // Save node as a published again.      // Save node as a published again.
359      $node->status = 1;      $node->status = 1;
360      $node->revision = TRUE;      $node->revision = TRUE;
361      $node->log = $form_values['log'];      $node->log = $form_state['values']['log'];
362    
363      // Call nodeapi hook      // Call nodeapi hook
364      node_invoke_nodeapi($node, 'restore from trash');      node_invoke_nodeapi($node, 'restore from trash');
# Line 351  function trash_restore_node_confirm_subm Line 366  function trash_restore_node_confirm_subm
366      // Save node      // Save node
367      node_save($node);      node_save($node);
368    
369      // Go to trash page      // Go to restored node
370      return 'node/' . $node->nid;      $form_state['redirect'] = 'node/' . $node->nid;
371      }
372      else {
373        // Go to trash list
374        $form_state['redirect'] = 'trash';
375    }    }
   return 'trash';  
376  }  }
377    
378  /**  /**
379   * Permanently delete node confirmation form.   * Permanently delete node confirmation form.
380   */   */
381  function trash_delete_node_confirm($node) {  function trash_delete_node_confirm(&$form_state, $node) {
382   $form['nid'] = array('#type' => 'value', '#value' => $node->nid);   $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
383    
384   return confirm_form($form,   return confirm_form($form,
# Line 373  function trash_delete_node_confirm($node Line 391  function trash_delete_node_confirm($node
391  /**  /**
392   * Permanently delete node confirmation form submit handler.   * Permanently delete node confirmation form submit handler.
393   */   */
394  function trash_delete_node_confirm_submit($form_id, $form_values) {  function trash_delete_node_confirm_submit($form, &$form_state) {
395    if ($form_values['confirm']) {    if ($form_state['values']['confirm']) {
396      // Delete the node.      // Delete the node.
397      node_delete($form_values['nid']);      node_delete($form_state['values']['nid']);
398    }    }
399    return 'trash';    // Go to trash list
400      $form_state['redirect'] = 'trash';
401  }  }
402    
403  /**  /**
404   * Submit handler for the 'node_delete_confirm' form of the node module.   * Submit handler for the 'node_delete_confirm' form of the node module.
405   * This is called instead of the original submit handler if the trash is enabled.   * This is called instead of the original submit handler if the trash is enabled.
406   */   */
407  function trash_node_delete_confirm_submit($form_id, $form_values) {  function trash_node_delete_confirm_submit($form, &$form_state) {
408    if ($form_values['confirm']) {    if ($form_state['values']['confirm']) {
409      $node = node_load($form_values['nid']);      $node = node_load($form_state['values']['nid']);
410      if (trash_enabled($node->type)) {      if (trash_enabled($node->type)) {
411        // Save node as a new revision with status set to TRASH.        // Save node as a new revision with status set to TRASH.
412        $node->status = STATUS_TRASH;        $node->status = STATUS_TRASH;
413        $node->revision = TRUE;        $node->revision = TRUE;
414        $node->log = $form_values['log'];        $node->log = $form_state['values']['log'];
415        // Call nodeapi hook        // Call nodeapi hook
416        node_invoke_nodeapi($node, 'move to trash');        node_invoke_nodeapi($node, 'move to trash');
417        // Save node        // Save node
418        node_save($node);        node_save($node);
419        // Go to trash page        // Go to trash list
420        return 'trash';        $form_state['redirect'] = 'trash';
421      }      }
422      else {      else {
423        // Call original submit handler        // Call original submit handler
424        return node_delete_confirm_submit($form_id, $form_values);        node_delete_confirm_submit($form, $form_state);
425      }      }
426    }    }
   return '<front>';  
427  }  }
428    
429  /**  /**
430   * Empty trash confirmation form.   * Empty trash confirmation form.
431   */   */
432  function trash_empty_trash_confirm() {  function trash_empty_trash_confirm(&$form_state) {
433   return confirm_form($form,   return confirm_form($form,
434     t('Are you sure you want to empty the trash?', array('%title' => $node->title)),     t('Are you sure you want to empty the trash?', array('%title' => $node->title)),
435     isset($_GET['destination']) ? $_GET['destination'] : 'trash',     isset($_GET['destination']) ? $_GET['destination'] : 'trash',
436     t('This action cannot be undone.'),     t('This action cannot be undone.'),
437     t('Delete'), t('Cancel'));     t('Empty trash'), t('Cancel'));
438  }  }
439    
440  /**  /**
441   * Empty trash confirmation form submit handler.   * Empty trash confirmation form submit handler.
442   */   */
443  function trash_empty_trash_confirm_submit($form_id, &$form_values) {  function trash_empty_trash_confirm_submit($form, &$form_state) {
444    if ($form_values['confirm']) {    if ($form_state['values']['confirm']) {
445      $result = db_query("SELECT n.nid FROM {node} n WHERE n.status = %d", STATUS_TRASH);      $result = db_query("SELECT n.nid FROM {node} n WHERE n.status = %d", STATUS_TRASH);
446      while ($node = db_fetch_object($result)) {      while ($node = db_fetch_object($result)) {
447        node_delete($node->nid);        node_delete($node->nid);
448      }      }
449    }    }
450    return 'trash';    $form_state['redirect'] = 'trash';
451  }  }
   
 /**  
  * Implementation of hook_node_access_records().  
  */  
 // Not needed anymore? (since the node_view page is overwritten)  
 // function trash_node_access_records($node) {  
 //   if (!trash_enabled($node->type)) {  
 //     return;  
 //   }  
 //  
 //   // Look if the status is set to 'trash'  
 //   if ($node->status == STATUS_TRASH) {  
 //     // If yes, remove all rights for this node.  
 //     $grants = array();  
 //     $grants[] = array(  
 //       'realm' => 'all',  
 //       'gid' => FALSE,  
 //       'grant_view' => FALSE,  
 //       'grant_update' => FALSE,  
 //       'grant_delete' => FALSE,  
 //       'priority' => 0  
 //     );  
 //     return $grants;  
 //   }  
 // }  

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

  ViewVC Help
Powered by ViewVC 1.1.2