[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/dblog/ -> dblog.admin.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Administrative page callbacks for the dblog module.
   6   */
   7  
   8  /**
   9   * dblog module settings form.
  10   *
  11   * @ingroup forms
  12   * @see system_settings_form()
  13   */
  14  function dblog_admin_settings() {
  15    $form['dblog_row_limit'] = array(
  16      '#type' => 'select',
  17      '#title' => t('Discard log entries above the following row limit'),
  18      '#default_value' => variable_get('dblog_row_limit', 1000),
  19      '#options' => drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
  20      '#description' => t('The maximum number of rows to keep in the database log. Older entries will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status')))
  21    );
  22  
  23    return system_settings_form($form);
  24  }
  25  
  26  /**
  27   * Menu callback; displays a listing of log messages.
  28   */
  29  function dblog_overview() {
  30    $filter = dblog_build_filter_query();
  31    $rows = array();
  32    $icons = array(
  33      WATCHDOG_DEBUG    => '',
  34      WATCHDOG_INFO     => '',
  35      WATCHDOG_NOTICE   => '',
  36      WATCHDOG_WARNING  => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
  37      WATCHDOG_ERROR    => theme('image', 'misc/watchdog-error.png', t('error'), t('error')),
  38      WATCHDOG_CRITICAL => theme('image', 'misc/watchdog-error.png', t('critical'), t('critical')),
  39      WATCHDOG_ALERT    => theme('image', 'misc/watchdog-error.png', t('alert'), t('alert')),
  40      WATCHDOG_EMERG    => theme('image', 'misc/watchdog-error.png', t('emergency'), t('emergency')),
  41    );
  42    $classes = array(
  43      WATCHDOG_DEBUG    => 'dblog-debug',
  44      WATCHDOG_INFO     => 'dblog-info',
  45      WATCHDOG_NOTICE   => 'dblog-notice',
  46      WATCHDOG_WARNING  => 'dblog-warning',
  47      WATCHDOG_ERROR    => 'dblog-error',
  48      WATCHDOG_CRITICAL => 'dblog-critical',
  49      WATCHDOG_ALERT    => 'dblog-alert',
  50      WATCHDOG_EMERG    => 'dblog-emerg',
  51    );
  52  
  53    $output = drupal_get_form('dblog_filter_form');
  54  
  55    $header = array(
  56      ' ',
  57      array('data' => t('Type'), 'field' => 'w.type'),
  58      array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
  59      t('Message'),
  60      array('data' => t('User'), 'field' => 'u.name'),
  61      array('data' => t('Operations')),
  62    );
  63  
  64    $sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.variables, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
  65    $tablesort = tablesort_sql($header);
  66    if (!empty($filter['where'])) {
  67      $result = pager_query($sql ." WHERE ". $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
  68    }
  69    else {
  70      $result = pager_query($sql . $tablesort, 50);
  71    }
  72  
  73    while ($dblog = db_fetch_object($result)) {
  74      $rows[] = array('data' =>
  75        array(
  76          // Cells
  77          $icons[$dblog->severity],
  78          t($dblog->type),
  79          format_date($dblog->timestamp, 'small'),
  80          l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE), 'admin/reports/event/'. $dblog->wid, array('html' => TRUE)),
  81          theme('username', $dblog),
  82          $dblog->link,
  83        ),
  84        // Attributes for tr
  85        'class' => "dblog-". preg_replace('/[^a-z]/i', '-', $dblog->type) .' '. $classes[$dblog->severity]
  86      );
  87    }
  88  
  89    if (!$rows) {
  90      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
  91    }
  92  
  93    $output .= theme('table', $header, $rows, array('id' => 'admin-dblog'));
  94    $output .= theme('pager', NULL, 50, 0);
  95  
  96    return $output;
  97  }
  98  
  99  /**
 100   * Menu callback; generic function to display a page of the most frequent
 101   * dblog events of a specified type.
 102   */
 103  function dblog_top($type) {
 104  
 105    $header = array(
 106      array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
 107      array('data' => t('Message'), 'field' => 'message')
 108    );
 109  
 110    $result = pager_query("SELECT COUNT(wid) AS count, message, variables FROM {watchdog} WHERE type = '%s' GROUP BY message, variables ". tablesort_sql($header), 30, 0, "SELECT COUNT(DISTINCT(message)) FROM {watchdog} WHERE type = '%s'", $type);
 111  
 112    $rows = array();
 113    while ($dblog = db_fetch_object($result)) {
 114      $rows[] = array($dblog->count, truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
 115    }
 116  
 117    if (empty($rows)) {
 118      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
 119    }
 120  
 121    $output  = theme('table', $header, $rows);
 122    $output .= theme('pager', NULL, 30, 0);
 123  
 124    return $output;
 125  }
 126  
 127  /**
 128   * Menu callback; displays details about a log message.
 129   */
 130  function dblog_event($id) {
 131    $severity = watchdog_severity_levels();
 132    $output = '';
 133    $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);
 134    if ($dblog = db_fetch_object($result)) {
 135      $rows = array(
 136        array(
 137          array('data' => t('Type'), 'header' => TRUE),
 138          t($dblog->type),
 139        ),
 140        array(
 141          array('data' => t('Date'), 'header' => TRUE),
 142          format_date($dblog->timestamp, 'large'),
 143        ),
 144        array(
 145          array('data' => t('User'), 'header' => TRUE),
 146          theme('username', $dblog),
 147        ),
 148        array(
 149          array('data' => t('Location'), 'header' => TRUE),
 150          l($dblog->location, $dblog->location),
 151        ),
 152        array(
 153          array('data' => t('Referrer'), 'header' => TRUE),
 154          l($dblog->referer, $dblog->referer),
 155        ),
 156        array(
 157          array('data' => t('Message'), 'header' => TRUE),
 158          _dblog_format_message($dblog),
 159        ),
 160        array(
 161          array('data' => t('Severity'), 'header' => TRUE),
 162          $severity[$dblog->severity],
 163        ),
 164        array(
 165          array('data' => t('Hostname'), 'header' => TRUE),
 166          check_plain($dblog->hostname),
 167        ),
 168        array(
 169          array('data' => t('Operations'), 'header' => TRUE),
 170          $dblog->link,
 171        ),
 172      );
 173      $attributes = array('class' => 'dblog-event');
 174      $output = theme('table', array(), $rows, $attributes);
 175    }
 176    return $output;
 177  }
 178  
 179  /**
 180   * Build query for dblog administration filters based on session.
 181   */
 182  function dblog_build_filter_query() {
 183    if (empty($_SESSION['dblog_overview_filter'])) {
 184      return;
 185    }
 186  
 187    $filters = dblog_filters();
 188  
 189    // Build query
 190    $where = $args = array();
 191    foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
 192      $filter_where = array();
 193      foreach ($filter as $value) {
 194        $filter_where[] = $filters[$key]['where'];
 195        $args[] = $value;
 196      }
 197      if (!empty($filter_where)) {
 198        $where[] = '('. implode(' OR ', $filter_where) .')';
 199      }
 200    }
 201    $where = !empty($where) ? implode(' AND ', $where) : '';
 202  
 203    return array(
 204      'where' => $where,
 205      'args' => $args,
 206    );
 207  }
 208  
 209  
 210  /**
 211   * List dblog administration filters that can be applied.
 212   */
 213  function dblog_filters() {
 214    $filters = array();
 215  
 216    foreach (_dblog_get_message_types() as $type) {
 217      $types[$type] = t($type);
 218    }
 219  
 220    if (!empty($types)) {
 221      $filters['type'] = array(
 222        'title' => t('Type'),
 223        'where' => "w.type = '%s'",
 224        'options' => $types,
 225      );
 226    }
 227  
 228    $filters['severity'] = array(
 229      'title' => t('Severity'),
 230      'where' => 'w.severity = %d',
 231      'options' => watchdog_severity_levels(),
 232    );
 233  
 234    return $filters;
 235  }
 236  
 237  /**
 238   * Formats a log message for display.
 239   *
 240   * @param $dblog
 241   *   An object with at least the message and variables properties
 242   */
 243  function _dblog_format_message($dblog) {
 244    // Legacy messages and user specified text
 245    if ($dblog->variables === 'N;') {
 246      return $dblog->message;
 247    }
 248    // Message to translate with injected variables
 249    else {
 250      return t($dblog->message, unserialize($dblog->variables));
 251    }
 252  }
 253  
 254  
 255  /**
 256   * Return form for dblog administration filters.
 257   *
 258   * @ingroup forms
 259   * @see dblog_filter_form_submit()
 260   * @see dblog_filter_form_validate()
 261   */
 262  function dblog_filter_form() {
 263    $session = &$_SESSION['dblog_overview_filter'];
 264    $session = is_array($session) ? $session : array();
 265    $filters = dblog_filters();
 266  
 267    $form['filters'] = array(
 268      '#type' => 'fieldset',
 269      '#title' => t('Filter log messages'),
 270      '#theme' => 'dblog_filters',
 271      '#collapsible' => TRUE,
 272      '#collapsed' => empty($session),
 273    );
 274    foreach ($filters as $key => $filter) {
 275      $form['filters']['status'][$key] = array(
 276        '#title' => $filter['title'],
 277        '#type' => 'select',
 278        '#multiple' => TRUE,
 279        '#size' => 8,
 280        '#options' => $filter['options'],
 281      );
 282      if (!empty($session[$key])) {
 283        $form['filters']['status'][$key]['#default_value'] = $session[$key];
 284      }
 285    }
 286  
 287    $form['filters']['buttons']['submit'] = array(
 288      '#type' => 'submit',
 289      '#value' => t('Filter'),
 290    );
 291    if (!empty($session)) {
 292      $form['filters']['buttons']['reset'] = array(
 293        '#type' => 'submit',
 294        '#value' => t('Reset')
 295      );
 296    }
 297  
 298    return $form;
 299  }
 300  
 301  /**
 302   * Validate result from dblog administration filter form.
 303   */
 304  function dblog_filter_form_validate($form, &$form_state) {
 305    if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
 306      form_set_error('type', t('You must select something to filter by.'));
 307    }
 308  }
 309  
 310  /**
 311   * Process result from dblog administration filter form.
 312   */
 313  function dblog_filter_form_submit($form, &$form_state) {
 314    $op = $form_state['values']['op'];
 315    $filters = dblog_filters();
 316    switch ($op) {
 317      case t('Filter'):
 318        foreach ($filters as $name => $filter) {
 319          if (isset($form_state['values'][$name])) {
 320            $_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
 321          }
 322        }
 323        break;
 324      case t('Reset'):
 325        $_SESSION['dblog_overview_filter'] = array();
 326        break;
 327    }
 328    return 'admin/reports/dblog';
 329  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7