dblog_clear.module

Tracking 6.x-3.x branch
  1. drupal
    1. 6 contributions/util/contribs/dblog_ext/dblog_clear/dblog_clear.module

Functions & methods

NameDescription
dblog_clear_form_alterImplementation of hook_form_alter().
dblog_clear_helpImplementation of hook_help().
dblog_clear_submit

File

View source
  1. <?php
  2. /**
  3. * Implementation of hook_help().
  4. */
  5. function dblog_clear_help($path, $args) {
  6. switch ($path) {
  7. case 'admin/reports/dblog':
  8. // being able to access reports is not enough to allow deletion
  9. // the ones responsible for configuration owns the right
  10. // over log history preservation
  11. if (user_access('administer site configuration')) {
  12. return '<p>'. t('Within the "Filter log messages" field set there is a button that you may use to clear selected entries.') .'</p>';
  13. }
  14. }
  15. }
  16. /**
  17. * Implementation of hook_form_alter().
  18. */
  19. function dblog_clear_form_alter(&$form, $form_state, $form_id) {
  20. switch ($form_id) {
  21. // works with or without dblog_ext
  22. case 'dblog_filter_form': // no break;
  23. case 'dblog_ext_filter_form':
  24. drupal_add_css(drupal_get_path('module', 'dblog_clear') . '/dblog_clear.css');
  25. $form['filters']['buttons']['clear'] = array(
  26. '#type' => 'submit',
  27. '#value' => t('Clear'),
  28. '#submit' => array('dblog_clear_submit'),
  29. '#attributes' => array('class' => 'warning'),
  30. );
  31. break;
  32. }
  33. }
  34. function dblog_clear_submit() {
  35. // works with or without dblog_ext
  36. $filter = module_exists('dblog_ext') ? dblog_ext_build_filter_query() : dblog_build_filter_query();
  37. if ($filter) {
  38. $where = $filter['where'];
  39. $args = $filter['args'];
  40. // support filtering by user
  41. if (strpos($where, 'u.name') === false) {
  42. $where = str_replace('w.', '', $where);
  43. }
  44. else {
  45. $query = 'SELECT w.wid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid';
  46. $query .= ' WHERE ' . $where;
  47. $result = db_query($query, $args);
  48. $wids = array();
  49. while($row = db_fetch_array($result)) {
  50. $wids[] = $row['wid'];
  51. }
  52. $args = array(); // clear $args
  53. if (empty($wids)) unset($where);
  54. else $where = "wid IN ('" . implode("', '", $wids) . "')";
  55. }
  56. }
  57. $query = 'DELETE FROM {watchdog}';
  58. if ($where) $query .= ' WHERE ' . $where;
  59. if (db_query($query, $args) === FALSE) {
  60. $msg = t('Failed to clear logs.');
  61. drupal_set_message($msg, 'error');
  62. $msg .= ' ' . t('Performed SQL query: @query; with arguments: @args', array('@query' => $query, '@args' => implode(', ', $args)));
  63. watchdog('dblog_clear', $msg, NULL, WATCHDOG_ERROR);
  64. }
  65. else {
  66. $count = db_affected_rows();
  67. drupal_set_message(t('Deleted @count rows.', array('@count' => $count)));
  68. // clear the filter
  69. unset($_SESSION['dblog_overview_filter']);
  70. }
  71. }