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

Contents of /contributions/modules/logsearch/logsearch.module

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


Revision 1.3 - (show annotations) (download) (as text)
Fri Jan 2 16:17:27 2009 UTC (10 months, 3 weeks ago) by deekayen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +54 -17 lines
File MIME type: text/x-php
convert remaining drupal 4.6 form functions to fapi
1 <?php
2 // $Id: logsearch.module,v 1.2 2008/08/19 05:13:51 deekayen Exp $
3
4 /**
5 * @file
6 * Search recent log entries (watchdog).
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function logsearch_help($path, $arg) {
13 switch ($path) {
14 case 'admin/reports/logsearch':
15 return t('Fill out the form below to search watchdog log items. You can search by user, message, timestamp, hostname or item type.');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function logsearch_menu() {
23 $items = array();
24 $items['admin/reports/logsearch'] = array(
25 'title' => 'Search recent log',
26 'page callback' => 'drupal_get_form',
27 'page arguments' => array('logsearch_search_form'),
28 'access callback' => 'user_access',
29 'access arguments' => array('access site reports'),
30 'type' => MENU_NORMAL_ITEM
31 );
32 return $items;
33 }
34
35 function logsearch_theme() {
36 return array(
37 'logsearch_search_form' => array(
38 'arguments' => array('form' => NULL)
39 )
40 );
41 }
42
43 /**
44 * Returns HTML for a search form.
45 * @todo
46 * Fill in form items with previous search terms
47 */
48 function logsearch_search_form() {
49 $form = array();
50 // search usernames, message contains, types, link, hostname, time
51 // search any/all of the following
52 // use _watchdog_get_message_types
53 $form['logsearch_anyorall'] = array(
54 '#type' => 'radios',
55 '#title' => t('Match'),
56 '#default_value' => 'AND',
57 '#options' => array('AND' => t('ALL of the following conditions'),
58 'OR' => t('ANY of the following conditions')),
59 );
60
61 $types = array_merge(array(''), _dblog_get_message_types());
62
63 for ($i = 0; $i < sizeof($types); $i++) {
64 $types[$types[$i]] = $types[$i];
65 unset($types[$i]);
66 }
67 $form['logsearch_type'] = array(
68 '#type' => 'select',
69 '#title' => t('Type is'),
70 '#default_value' => $types[0],
71 '#options' => $types
72 );
73 $form['logsearch_message'] = array(
74 '#type' => 'textfield',
75 '#title' => t('Message contains'),
76 '#default_value' => '',
77 '#size' => 50,
78 '#maxlength' => 250
79 );
80 $form['logsearch_user'] = array(
81 '#type' => 'textfield',
82 '#title' => t('User contains'),
83 '#default_value' => '',
84 '#size' => 50,
85 '#maxlength' => 250
86 );
87 $form['logsearch_time_enable'] = array(
88 '#type' => 'checkbox',
89 '#title' => t('Time')
90 );
91 $form['logsearch_time_begin'] = array(
92 '#type' => 'textfield',
93 '#title' => t(''),
94 '#default_value' => format_date(time() - (60 * 60 * 24 * 7), 'small'),
95 '#size' => 16,
96 '#maxlength' => 250,
97 '#prefix' => t('is after')
98 );
99 $form['logsearch_time_end'] = array(
100 '#type' => 'textfield',
101 '#title' => t(''),
102 '#default_value' => format_date(time(), 'small'),
103 '#size' => 16,
104 '#maxlength' => 250,
105 '#prefix' => t('but before')
106 );
107 $form['logsearch_hostname'] = array(
108 '#type' => 'textfield',
109 '#title' => t('Hostname contains'),
110 '#default_value' => '',
111 '#size' => 50,
112 '#maxlength' => 100
113 );
114 $form['submit'] = array(
115 '#type' => 'submit',
116 '#value' => t('Search'),
117 '#weight' => 10
118 );
119 return $form;
120 }
121
122 function theme_logsearch_search_form($form) {
123 $rows = array();
124 $rows[] = array(
125 array('data' => drupal_render($form['logsearch_time_enable'])),
126 array('data' => drupal_render($form['logsearch_time_begin'])),
127 array('data' => drupal_render($form['logsearch_time_end']))
128 );
129 unset(
130 $form['logsearch_time_enable'],
131 $form['logsearch_time_begin'],
132 $form['logsearch_time_end']
133 );
134 $form['time_table'] = array(
135 '#value' => theme('table', array(), $rows),
136 '#weight' => 9
137 );
138 return drupal_render($form);
139 }
140
141 /**
142 * Assemble the search query from search terms.
143 */
144 function logsearch_search_form_submit($form, &$form_state) {
145 $andor = $form_state['values']['logsearch_anyorall'];
146 $conditions = array();
147 if (!empty($form_state['values']['logsearch_user'])) {
148 $logsearch_user = preg_replace('!\*+!', '%', $form_state['values']['logsearch_user']);
149 $conditions[] = 'LOWER(u.name) LIKE LOWER(\'%%'. $logsearch_user .'%%\')';
150 }
151 if ($form_state['values']['logsearch_time_enable'] == 1) {
152 $begin = strtotime($form_state['values']['logsearch_time_begin']);
153 $end = strtotime($form_state['values']['logsearch_time_end']);
154 $conditions[] = 'w.timestamp BETWEEN '. $begin .' AND '. $end;
155 }
156 foreach (array('message', 'type', 'hostname') as $field) {
157 if (!empty($form_state['values']['logsearch_'. $field])) {
158 $logsearch_field = preg_replace('!\*+!', '%', $form_state['values']['logsearch_'. $field]);
159 $conditions[] = 'LOWER('. $field .') LIKE LOWER(\'%%'. $logsearch_field .'%%\')';
160 }
161 }
162
163 $query = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u on u.uid = w.uid ';
164 if (sizeof($conditions) > 0) {
165 $query .= 'WHERE '. (implode(" $andor ", $conditions));
166 }
167 //return $query;
168 /*
169 }
170
171 /**
172 * Perform search and print results.
173 *
174 function logsearch_results() {
175 */
176 global $pager_total;
177 //$query = $_SESSION['logsearch_query'] ? $_SESSION['logsearch_query'] : logsearch_query();
178 $header = array(
179 array('data' => t('Type'), 'field' => 'w.type'),
180 array('data' => t('Date'), 'field' => 'w.timestamp', 'sort' => 'desc'),
181 array('data' => t('Message'), 'field' => 'w.message'),
182 array('data' => t('User'), 'field' => 'u.name'),
183 array('data' => t('Operations'), 'colspan' => '2')
184 );
185 $query .= tablesort_sql($header);
186 $output = '<h2>'. t('Search results: ') .'</h2>';
187 // $output .= '<p>' . $pager_total . t(' total results.</p>');
188 $result = pager_query($query, 50);
189 while ($watchdog = db_fetch_object($result)) {
190 $rows[] = array('data' =>
191 array(
192 // Cells
193 $watchdog->type,
194 format_date($watchdog->timestamp, 'small'),
195 truncate_utf8(strip_tags(t($watchdog->message, unserialize($watchdog->variables))), 64),
196 theme('username', $watchdog),
197 $watchdog->link,
198 l(t('details'), "admin/reports/view/$watchdog->wid")
199 ),
200 // Attributes for tr
201 'class' => "watchdog-$watchdog->type"
202 );
203 // die(var_dump($rows));
204 }
205 // return a new form if we didn't find anything
206 if (!$rows) {
207 drupal_set_message(t('Your search yielded no results.'));
208 $output = drupal_get_form('logsearch_search_form');
209 }
210 else { // otherwise print results
211 $pager = theme('pager', NULL, 50, 0);
212 if (!empty($pager)) {
213 // $pager = $pager_total[0] . t(' results returned.') . $pager;
214 $rows[] = array(array('data' => $pager, 'colspan' => 6));
215 }
216 $output = $output . theme('table', $header, $rows);
217 }
218 print theme('page', $output);
219 exit;
220 }
221
222 function _logsearch_get_username($name) {
223 return db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name));
224 }

  ViewVC Help
Powered by ViewVC 1.1.2