| 1 |
<?php
|
| 2 |
// $Id: watchdog_live.module,v 1.4 2009/04/22 06:21:54 naxoc Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Adds auto update functionality to the watchdog table.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_menu().
|
| 10 |
*/
|
| 11 |
function watchdog_live_menu() {
|
| 12 |
$items = array();
|
| 13 |
$items['admin/logs/watchdog/live/callback/%'] = array(
|
| 14 |
'page callback' => 'watchdog_live_callback',
|
| 15 |
'page arguments' => array(5),
|
| 16 |
'access arguments' => array('access site reports'),
|
| 17 |
'type' => MENU_CALLBACK,
|
| 18 |
);
|
| 19 |
|
| 20 |
$items['admin/logs/watchdog/live/settings'] = array(
|
| 21 |
'page callback' => 'watchdog_live_settings',
|
| 22 |
'access arguments' => array('access site reports'),
|
| 23 |
'type' => MENU_CALLBACK,
|
| 24 |
);
|
| 25 |
|
| 26 |
$items['admin/reports/watchdogmini'] = array(
|
| 27 |
'title' => 'Watchdog mini',
|
| 28 |
'description' => 'A popup window with the site log live',
|
| 29 |
'page callback' => 'watchdog_live_popup',
|
| 30 |
'access arguments' => array('access site reports'),
|
| 31 |
);
|
| 32 |
|
| 33 |
return $items;
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_menu_link_alter().
|
| 38 |
*/
|
| 39 |
function watchdog_live_menu_link_alter(&$item, $menu) {
|
| 40 |
if ('admin/reports/watchdogmini' == $item['link_path']) {
|
| 41 |
// Make the menu link open in a new window
|
| 42 |
$item['options']['attributes']['onclick'] = "var w=window.open(this.href, 'watchdog_live_popup', 'width=820,height=400,scrollbars,resizable'); w.focus(); return false;";
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Implementation of hook_form_alter().
|
| 48 |
*/
|
| 49 |
function watchdog_live_form_alter(&$form, &$form_state , $form_id) {
|
| 50 |
if ($form_id == 'dblog_filter_form' && empty($_GET['page'])) {
|
| 51 |
|
| 52 |
// Include needed JavaScript.
|
| 53 |
$path = array(
|
| 54 |
'callback_url' => url('admin/logs/watchdog/live/callback/'. (arg(2) == 'watchdogmini' ? 'mini' : 'full')),
|
| 55 |
'setting_url' => url('admin/logs/watchdog/live/settings')
|
| 56 |
);
|
| 57 |
|
| 58 |
drupal_add_js(array('watchdogLive' => $path), 'setting');
|
| 59 |
drupal_add_js(drupal_get_path('module', 'watchdog_live') .'/watchdog_live.js');
|
| 60 |
|
| 61 |
$intervals = drupal_map_assoc(array(500, 1000, 2000, 3000, 5000, 10000), create_function('$a', 'return $a/1000 . " '. t('seconds') .'";'));
|
| 62 |
|
| 63 |
// Add configurable options right on the watchdog page so we can update instantly.. Just for kicks.
|
| 64 |
$form['watchdog_live'] = array('#type' => 'fieldset', '#title' => t('Live settings'), '#tree' => TRUE, '#collapsible' => TRUE, '#collapsed' => (arg(2) == 'watchdogmini' ? TRUE : FALSE));
|
| 65 |
|
| 66 |
if (user_access('administer site configuration')) {
|
| 67 |
$form['watchdog_live']['interval'] = array(
|
| 68 |
'#type' => 'select', '#title' => t('Update interval'),
|
| 69 |
'#options' => $intervals, '#default_value' => variable_get('watchdog_live_interval', 3000)
|
| 70 |
);
|
| 71 |
}
|
| 72 |
$form['watchdog_live']['disabled'] = array(
|
| 73 |
'#type' => 'checkbox', '#title' => t('Disable live updating'),
|
| 74 |
'#default_value' => $_SESSION['watchdog_live_disabled']
|
| 75 |
);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Callback used to retrieve the updated table.
|
| 81 |
*/
|
| 82 |
function watchdog_live_callback($page_name) {
|
| 83 |
|
| 84 |
// Make sure we need to update the table.
|
| 85 |
if (watchdog_live_update_needed($page_name)) {
|
| 86 |
|
| 87 |
require_once drupal_get_path('module', 'dblog') .'/dblog.admin.inc';
|
| 88 |
$output['content'] = preg_replace('#<form.*</form>\s+#s', '', dblog_overview());
|
| 89 |
|
| 90 |
// Fix url
|
| 91 |
$output['content'] = preg_replace('#admin/logs/watchdog/live/callback/'. $page_name .'#', 'admin/reports/dblog', $output['content']);
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
$output['error'] = TRUE;
|
| 95 |
}
|
| 96 |
|
| 97 |
print drupal_to_js($output);
|
| 98 |
exit;
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Update the settings.
|
| 103 |
*/
|
| 104 |
function watchdog_live_settings() {
|
| 105 |
// Handle interval.
|
| 106 |
if (isset($_POST['interval']) && intval($_POST['interval']) && user_access('administer site configuration')) {
|
| 107 |
variable_set('watchdog_live_interval', $_POST['interval']);
|
| 108 |
}
|
| 109 |
|
| 110 |
// Allow users to turn off live updating for a session.
|
| 111 |
if (isset($_POST['disabled'])) {
|
| 112 |
$_SESSION['watchdog_live_disabled'] = $_POST['disabled'];
|
| 113 |
}
|
| 114 |
|
| 115 |
// Give it something.
|
| 116 |
print 1;
|
| 117 |
exit;
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* @param $page_name
|
| 122 |
* The page calling to check for updates
|
| 123 |
*/
|
| 124 |
function watchdog_live_update_needed($page_name) {
|
| 125 |
$session_string = 'watchdog_live_latest_wid_'. $page_name;
|
| 126 |
|
| 127 |
$latest_wid = db_result(db_query_range("SELECT wid FROM {watchdog} ORDER BY wid DESC", 0, 1));
|
| 128 |
if ($_SESSION[$session_string] < $latest_wid) {
|
| 129 |
$_SESSION[$session_string] = $latest_wid;
|
| 130 |
return TRUE;
|
| 131 |
}
|
| 132 |
return FALSE;
|
| 133 |
}
|
| 134 |
|
| 135 |
function watchdog_live_popup() {
|
| 136 |
require_once drupal_get_path('module', 'dblog') .'/dblog.admin.inc';
|
| 137 |
|
| 138 |
// Suppress admin_menu.
|
| 139 |
module_invoke('admin_menu', 'suppress');
|
| 140 |
|
| 141 |
// Fix url
|
| 142 |
$output = preg_replace('#admin/reports/watchdogmini#', 'admin/reports/dblog', dblog_overview());
|
| 143 |
|
| 144 |
// If the popups api module is present - use it for nice popups
|
| 145 |
if (module_exists('popups')) {
|
| 146 |
popups_add_popups(array("#admin-dblog tr td a"));
|
| 147 |
}
|
| 148 |
print theme('watchdog_live_popup', $output);
|
| 149 |
return;
|
| 150 |
}
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Implementation of hook_theme().
|
| 154 |
*/
|
| 155 |
function watchdog_live_theme() {
|
| 156 |
return array('watchdog_live_popup' => array(
|
| 157 |
'arguments' => array('content' => NULL),
|
| 158 |
'template' => 'watchdog-live-popup',
|
| 159 |
)
|
| 160 |
);
|
| 161 |
return $hooks;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Set some variables for the popup page
|
| 166 |
*/
|
| 167 |
function template_preprocess_watchdog_live_popup(&$variables) {
|
| 168 |
|
| 169 |
// Add favicon.
|
| 170 |
if (theme_get_setting('toggle_favicon')) {
|
| 171 |
drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />');
|
| 172 |
}
|
| 173 |
|
| 174 |
// Construct page title.
|
| 175 |
if (drupal_get_title()) {
|
| 176 |
$head_title[] = strip_tags(drupal_get_title());
|
| 177 |
}
|
| 178 |
$head_title[] = variable_get('site_name', 'Drupal');
|
| 179 |
|
| 180 |
$variables['head_title'] = implode(' | ', $head_title);
|
| 181 |
$variables['base_path'] = base_path();
|
| 182 |
$variables['front_page'] = url();
|
| 183 |
$variables['head'] = drupal_get_html_head();
|
| 184 |
$variables['language'] = $GLOBALS['language'];
|
| 185 |
$variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
|
| 186 |
$variables['messages'] = theme('status_messages');
|
| 187 |
|
| 188 |
$variables['styles'] = drupal_get_css();
|
| 189 |
$variables['scripts'] = drupal_get_js();
|
| 190 |
$variables['title'] = drupal_get_title();
|
| 191 |
// Closure should be filled last.
|
| 192 |
$variables['closure'] = theme('closure');
|
| 193 |
}
|