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

Contents of /contributions/modules/watchdog_live/watchdog_live.module

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


Revision 1.6 - (show annotations) (download) (as text)
Wed Nov 4 20:19:36 2009 UTC (3 weeks, 1 day ago) by naxoc
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +8 -9 lines
File MIME type: text/x-php
Removed popups-api soft dependency - it did not make much sense. Fixed the mini window to work with themes other than Garland and removed the filter fieldset on the mini window (#619716). And did some code cleanup..
1 <?php
2 // $Id: watchdog_live.module,v 1.5 2009/05/30 15:29:46 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 if ('watchdogmini' == arg(2)) {
77 unset($form['filters']);
78 }
79 }
80 }
81
82 /**
83 * Callback used to retrieve the updated table.
84 */
85 function watchdog_live_callback($page_name) {
86
87 // Make sure we need to update the table.
88 if (watchdog_live_update_needed($page_name)) {
89
90 require_once drupal_get_path('module', 'dblog') .'/dblog.admin.inc';
91 $output['content'] = preg_replace('#<form.*</form>\s+#s', '', dblog_overview());
92
93 // Fix url
94 $output['content'] = preg_replace('#admin/logs/watchdog/live/callback/'. $page_name .'#', 'admin/reports/dblog', $output['content']);
95 }
96 else {
97 $output['error'] = TRUE;
98 }
99
100 print drupal_to_js($output);
101 exit;
102 }
103
104 /**
105 * Update the settings.
106 */
107 function watchdog_live_settings() {
108 // Handle interval.
109 if (isset($_POST['interval']) && intval($_POST['interval']) && user_access('administer site configuration')) {
110 variable_set('watchdog_live_interval', $_POST['interval']);
111 }
112
113 // Allow users to turn off live updating for a session.
114 if (isset($_POST['disabled'])) {
115 $_SESSION['watchdog_live_disabled'] = $_POST['disabled'];
116 }
117
118 // Give it something.
119 print 1;
120 exit;
121 }
122
123 /**
124 * @param $page_name
125 * The page calling to check for updates
126 */
127 function watchdog_live_update_needed($page_name) {
128 $session_string = 'watchdog_live_latest_wid_'. $page_name;
129
130 $latest_wid = db_result(db_query_range("SELECT wid FROM {watchdog} ORDER BY wid DESC", 0, 1));
131 if ($_SESSION[$session_string] < $latest_wid) {
132 $_SESSION[$session_string] = $latest_wid;
133 return TRUE;
134 }
135 return FALSE;
136 }
137
138 function watchdog_live_popup() {
139 require_once drupal_get_path('module', 'dblog') .'/dblog.admin.inc';
140
141 // Suppress admin_menu.
142 module_invoke('admin_menu', 'suppress');
143
144 // Fix url
145 $output = preg_replace('#admin/reports/watchdogmini#', 'admin/reports/dblog', dblog_overview());
146
147 print theme('watchdog_live_popup', $output);
148 return;
149 }
150
151 /**
152 * Implementation of hook_theme().
153 */
154 function watchdog_live_theme() {
155 return array('watchdog_live_popup' => array(
156 'arguments' => array('content' => NULL),
157 'template' => 'watchdog-live-popup',
158 )
159 );
160 return $hooks;
161 }
162
163 /**
164 * Set some variables for the popup page
165 */
166 function template_preprocess_watchdog_live_popup(&$variables) {
167
168 // Add favicon.
169 if (theme_get_setting('toggle_favicon')) {
170 drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />');
171 }
172
173 // Construct page title.
174 if (drupal_get_title()) {
175 $head_title[] = strip_tags(drupal_get_title());
176 }
177 $head_title[] = variable_get('site_name', 'Drupal');
178
179 $variables['head_title'] = implode(' | ', $head_title);
180 $variables['base_path'] = base_path();
181 $variables['front_page'] = url();
182 $variables['head'] = drupal_get_html_head();
183 $variables['language'] = $GLOBALS['language'];
184 $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
185 $variables['messages'] = theme('status_messages');
186
187 $variables['styles'] = drupal_get_css();
188 $variables['scripts'] = drupal_get_js();
189 $variables['title'] = drupal_get_title();
190 // Closure should be filled last.
191 $variables['closure'] = theme('closure');
192 }

  ViewVC Help
Powered by ViewVC 1.1.2