Index: modules/dblog/dblog.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/dblog/dblog.module,v
retrieving revision 1.2
diff -u -p -r1.2 dblog.module
--- modules/dblog/dblog.module 13 Apr 2007 08:27:18 -0000 1.2
+++ modules/dblog/dblog.module 21 Apr 2007 00:18:04 -0000
@@ -32,7 +32,10 @@ function dblog_help($section) {
*/
function dblog_theme() {
return array(
- 'dblog_form_overview' => array(
+ 'dblog_filter_form' => array(
+ 'arguments' => array('form' => NULL),
+ ),
+ 'dblog_filters' => array(
'arguments' => array('form' => NULL),
),
);
@@ -122,38 +125,22 @@ function dblog_user($op, &$edit, &$user)
}
}
-function dblog_form_overview() {
- $names['all'] = t('all messages');
- foreach (_dblog_get_message_types() as $type) {
- $names[$type] = t('!type messages', array('!type' => t($type)));
- }
-
- if (empty($_SESSION['dblog_overview_filter'])) {
- $_SESSION['dblog_overview_filter'] = 'all';
- }
-
- $form['filter'] = array(
- '#type' => 'select',
- '#title' => t('Filter by message type'),
- '#options' => $names,
- '#default_value' => $_SESSION['dblog_overview_filter']
- );
- $form['submit'] = array('#type' => 'submit', '#value' => t('Filter'));
- $form['#redirect'] = FALSE;
-
- return $form;
-}
/**
* Menu callback; displays a listing of log messages.
*/
function dblog_overview() {
+ $filter = dblog_build_filter_query();
$rows = array();
- $icons = array(WATCHDOG_NOTICE => '',
- WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
- WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
- $classes = array(WATCHDOG_NOTICE => 'dblog-notice', WATCHDOG_WARNING => 'dblog-warning', WATCHDOG_ERROR => 'dblog-error');
+ $icons = array(
+ WATCHDOG_NOTICE => '',
+ WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
+ WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
+ $classes = array(
+ WATCHDOG_NOTICE => 'dblog-notice',
+ WATCHDOG_WARNING => 'dblog-warning',
+ WATCHDOG_ERROR => 'dblog-error');
- $output = drupal_get_form('dblog_form_overview');
+ $output = drupal_get_form('dblog_filter_form');
$header = array(
' ',
@@ -166,9 +153,8 @@ function dblog_overview() {
$sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
$tablesort = tablesort_sql($header);
- $type = $_SESSION['dblog_overview_filter'];
- if ($type != 'all') {
- $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
+ if (!empty($filter['where'])) {
+ $result = pager_query($sql . " WHERE ". $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
}
else {
$result = pager_query($sql . $tablesort, 50);
@@ -228,19 +214,11 @@ function dblog_top($type) {
return $output;
}
-function theme_dblog_form_overview($form) {
- return '
'. drupal_render($form) .'
';
-}
-
-function dblog_form_overview_submit($form_id, $form_values) {
- $_SESSION['dblog_overview_filter'] = $form_values['filter'];
-}
-
/**
* Menu callback; displays details about a log message.
*/
function dblog_event($id) {
- $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
+ $severity = _dblog_get_severity_list();
$output = '';
$result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d', $id);
if ($dblog = db_fetch_object($result)) {
@@ -319,3 +297,196 @@ function dblog_watchdog($log = array())
db_set_active($current_db);
}
}
+
+/**
+ * Return form for dblog administration filters.
+ */
+function dblog_filter_form() {
+ $session = &$_SESSION['dblog_overview_filter'];
+ $session = is_array($session) ? $session : array();
+ $filters = dblog_filters();
+
+ $i = 0;
+
+ $form['filters'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Show only log messages where'),
+ '#theme' => 'dblog_filters',
+ );
+
+ foreach ($session as $filter) {
+ list($type, $value) = $filter;
+ $string = ($i++ ? 'and where %a is %b' : '%a is %b');
+ $form['filters']['current'][] = array('#value' => t($string, array('%a' => $filters[$type]['title'] , '%b' => $filters[$type]['options'][$value])));
+ }
+ foreach ($filters as $key => $filter) {
+ $names[$key] = $filter['title'];
+ $form['filters']['status'][$key] = array(
+ '#type' => 'select',
+ '#options' => $filter['options'],
+ );
+ }
+
+ $form['filters']['filter'] = array(
+ '#type' => 'radios',
+ '#options' => $names,
+ );
+
+ $form['filters']['buttons']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => (count($session) ? t('Refine') : t('Filter'))
+ );
+ if (count($session)) {
+ $form['filters']['buttons']['undo'] = array(
+ '#type' => 'submit',
+ '#value' => t('Undo')
+ );
+ $form['filters']['buttons']['reset'] = array(
+ '#type' => 'submit',
+ '#value' => t('Reset')
+ );
+ }
+
+ return $form;
+}
+/**
+ * Theme dblog administration filter form.
+ */
+function theme_dblog_filter_form($form) {
+ $output = '';
+ $output .= drupal_render($form['filters']);
+ $output .= '
';
+ $output .= drupal_render($form);
+ return $output;
+}
+
+/**
+ * Theme dblog administration filter selector.
+ */
+function theme_dblog_filters($form) {
+ $output = '';
+
+ return $output;
+}
+
+/**
+ * Process result from dblog administration filter form.
+ */
+function dblog_filter_form_validate($form_id, $form_values) {
+ if ($form_values['op'] == t('Filter') && empty($form_values['filter'])) {
+ form_set_error('filter', t('You must select what kind of filter to apply.'));
+ }
+}
+
+/**
+ * Process result from dblog administration filter form.
+ */
+function dblog_filter_form_submit($form_id, $form_values) {
+ $op = $form_values['op'];
+ $filters = dblog_filters();
+ switch ($op) {
+ case t('Filter'):
+ case t('Refine'):
+ if (isset($form_values['filter'])) {
+ $filter = $form_values['filter'];
+ if (isset($filters[$filter]['options'][$form_values[$filter]])) {
+ $_SESSION['dblog_overview_filter'][] = array($filter, $form_values[$filter]);
+ }
+ }
+ break;
+ case t('Undo'):
+ array_pop($_SESSION['dblog_overview_filter']);
+ break;
+ case t('Reset'):
+ $_SESSION['dblog_overview_filter'] = array();
+ break;
+ case t('Update'):
+ return;
+ }
+
+ return 'admin/logs/dblog';
+}
+
+/**
+ * List dblog administration filters that can be applied.
+ */
+function dblog_filters() {
+ $filters = array();
+
+ foreach(_dblog_get_message_types() as $type) {
+ $types[$type] = $type;
+ }
+
+ if (!empty($types)) {
+ $filters['type'] = array(
+ 'title' => t('type'),
+ 'where' => "w.type = '%s'",
+ 'options' => $types,
+ );
+ }
+
+ $filters['severity'] = array(
+ 'title' => t('severity'),
+ 'where' => 'w.severity = %d',
+ 'options' => _dblog_get_severity_list(),
+ );
+
+ return $filters;
+}
+
+/**
+ * Build query for dblog administration filters based on session.
+ */
+function dblog_build_filter_query() {
+ $filters = dblog_filters();
+
+ // Build query
+ $where = $args = array();
+ foreach ($_SESSION['dblog_overview_filter'] as $filter) {
+ list($key, $value) = $filter;
+ $where[] = $filters[$key]['where'];
+ $args[] = $value;
+ }
+ $where = count($where) ? implode(' AND ', $where) : '';
+
+ return array(
+ 'where' => $where,
+ 'args' => $args,
+ );
+}
+
+function _dblog_get_severity_list() {
+ return array(
+ WATCHDOG_EMERG => t('Emergency'),
+ WATCHDOG_ALERT => t('Alert'),
+ WATCHDOG_CRITICAL => t('Critical'),
+ WATCHDOG_ERROR => t('Error'),
+ WATCHDOG_WARNING => t('Warning'),
+ WATCHDOG_NOTICE => t('Notice'),
+ WATCHDOG_INFO => t('Info'),
+ WATCHDOG_DEBUG => t('Debug'),
+ );
+}
+