Subversion Repositories eFlore/Applications.cel

Rev

Rev 419 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 aurelien 1
<?php
2
 
3
/*
4
 * phpMyEdit - instant MySQL table editor and code generator
5
 *
6
 * extensions/phpMyEdit-report.class.php - phpMyEdit report extension
7
 * ____________________________________________________________
8
 *
9
 * Developed by Ondrej Jombik <nepto@platon.sk>
10
 * Copyright (c) 2002-2006 Platon Group, http://platon.sk/
11
 * All rights reserved.
12
 *
13
 * See README file for more information about this software.
14
 * See COPYING file for license information.
15
 *
16
 * Download the latest version from
17
 * http://platon.sk/projects/phpMyEdit/
18
 */
19
 
20
/* $Platon: phpMyEdit/extensions/phpMyEdit-report.class.php,v 1.12 2006-01-22 21:44:23 nepto Exp $ */
21
 
22
/* Extension TODO:
23
 
24
   - allow user to enable/disable particular field in reporting (maybe 'X' flag
25
     for indicating that field is forbidden is good idea)
26
   - support for ['help'] in select fields screen
27
   - make extension's option for selecting "Select fields" link or button
28
 */
29
 
30
require_once dirname(__FILE__).'/../phpMyEdit.class.php';
31
 
32
class phpMyEdit_report extends phpMyEdit
33
{
34
 
35
	function phpMyEdit_report($opts) /* {{{ */
36
	{
37
		$opts['options'] = 'L';
38
		$execute = 1;
39
		isset($opts['execute']) && $execute = $opts['execute'];
40
		$opts['execute'] = 0;
41
		parent::phpMyEdit($opts);
42
		$execute && $this->execute();
43
	} /* }}} */
44
 
45
	function make_language_labels($language) /* {{{ */
46
	{
47
		$ret = parent::make_language_labels($language);
48
		strlen($ret['Make report'])        <= 0 && $ret['Make report']        = 'Make report';
49
		strlen($ret['Select fields'])      <= 0 && $ret['Select fields']      = 'Select fields';
50
		strlen($ret['Records per screen']) <= 0 && $ret['Records per screen'] = 'Records per screen';
51
		return $ret;
52
	} /* }}} */
53
 
54
	function get_cgi_cookie_var($name, $default_value = null) /* {{{ */
55
	{
56
		$ret = $this->get_cgi_var($name, null);
57
		if ($ret === null) {
58
			global $HTTP_COOKIE_VARS;
59
			$ret = @$HTTP_COOKIE_VARS[$name.'_'.$this->tb.'_cookie'];
60
			if (! isset($ret)) {
61
				$ret = $default_value;
62
			}
63
		}
64
		return $ret;
65
	} /* }}} */
66
 
67
	function display_list_table_buttons($total_recs, $position) /* {{{ */
68
	{	/* This is mostly copy/paste from core class. */
69
		$listall = $this->inc <= 0; // Are we doing a listall?
70
		echo '<table class="',$this->getCSSclass('navigation', $position),'">',"\n";
71
		echo '<tr class="',$this->getCSSclass('navigation', $position),'">',"\n";
72
		echo '<td class="',$this->getCSSclass('buttons', $position),'">',"\n";
73
		echo '<input class="',$this->getCSSclass('fields-select', $position);
74
		echo '" type="submit" name="fields_select" value="',$this->labels['Select fields'],'">&nbsp;';
75
		// Note that <input disabled isn't valid HTML, but most browsers support it
76
		$disabled = ($this->fm > 0 && ! $listall) ? '' : ' disabled';
77
		echo '<input',$disabled,' class="',$this->getCSSclass('prev', $position);
78
		echo '" type="submit" name="',ltrim($disabled),'prev" value="',$this->labels['Prev'],'">&nbsp;';
79
		$disabled = ($this->fm + $this->inc < $total_recs && ! $listall) ? '' : ' disabled';
80
		echo '<input',$disabled,' class="',$this->getCSSclass('next', $position);
81
		echo '" type="submit" name="',ltrim($disabled),'next" value="',$this->labels['Next'],'">';
82
		// Message is now written here
83
		echo '</td>',"\n";
84
		if (strlen(@$this->message) > 0) {
85
			echo '<td class="',$this->getCSSclass('message', $position),'">',$this->message,'</td>',"\n";
86
		}
87
		// Display page and records statistics
88
		echo '<td class="',$this->getCSSclass('stats', $position),'">',"\n";
89
		if ($listall) {
90
			echo $this->labels['Page'],':&nbsp;1&nbsp;',$this->labels['of'],'&nbsp;1';
91
		} else {
92
			echo $this->labels['Page'],':&nbsp;',($this->fm / $this->inc) + 1;
93
			echo '&nbsp;',$this->labels['of'],'&nbsp;',max(1, ceil($total_recs / abs($this->inc)));
94
		}
95
		echo '&nbsp; ',$this->labels['Records'],':&nbsp;',$total_recs;
96
		echo '</td></tr></table>',"\n";
97
	} /* }}} */
98
 
99
	function display_report_selection_buttons($position) /* {{{ */
100
	{
101
		echo '<table class="',$this->getCSSclass('navigation', $position),'">',"\n";
102
		echo '<tr class="',$this->getCSSclass('navigation', $position),'">',"\n";
103
		echo '<td class="',$this->getCSSclass('buttons', $position),'">',"\n";
104
		echo '<input class="',$this->getCSSclass('make-report', $position);
105
		echo '" type="submit" name="prepare_filter" value="',$this->labels['Make report'],'">',"\n";
106
		echo '</td></tr></table>',"\n";
107
	} /* }}} */
108
 
109
	function get_select_fields_link() /* {{{ */
110
	{
111
		$link = '<a href="'.htmlspecialchars($this->page_name).'?fields_select=1';
112
		for ($i = 0; $i < count($table_cols); $i++) {
113
			$varname = 'qf'.$i;
114
			$value   = $this->get_cgi_cookie_var($varname);
115
			if (! empty($value)) {
116
				$link .= htmlspecialchars(
117
						'&'.rawurlencode($varname).
118
						'='.rawurlencode($value));
119
			}
120
		}
121
		$link .= htmlspecialchars($this->cgi['persist']);
122
		$link .= '">'.$this->labels['Select fields'].'</a>';
123
		return $link;
124
	} /* }}} */
125
 
126
	function execute() /* {{{ */
127
	{
128
		global $HTTP_GET_VARS;
129
		global $HTTP_POST_VARS;
130
 
131
		/*
132
		 * Extracting field names
133
		 */
134
 
135
		$table_cols     = array();
136
		$all_table_cols = array();
137
 
138
		if ($this->connect() == false) {
139
			return false;
140
		}
141
		$query_parts = array(
142
				'type'   => 'select',
143
				'select' => '*',
144
				'from'   => $this->tb,
145
				'limit'  => '1');
146
		$result = $this->myquery($this->get_SQL_query($query_parts), __LINE__);
147
		$all_table_cols = array_keys(@mysql_fetch_array($result, MYSQL_ASSOC));
148
		if (count($all_table_cols) <= 0) {
149
			$this->error('database fetch error');
150
			return false;
151
		}
152
		foreach (array_keys($this->fdd) as $field_name) {
153
			if (preg_match('/^\d*$/', $field_name))
154
				continue;
155
			if (($idx = array_search($field_name, $all_table_cols)) !== false)
156
				$table_cols[$field_name] = mysql_field_len($result, $idx);
157
		}
158
		@mysql_free_result($result);
159
		unset($all_table_cols);
160
 
161
		/*
162
		 * Preparing variables
163
		 */
164
 
165
		$fields_select  = $this->get_cgi_var('fields_select');
166
		$filter         = $this->get_cgi_var('filter');
167
		$prepare_filter = $this->get_cgi_var('prepare_filter');
168
		$this->inc      = intval($this->get_cgi_cookie_var('inc'));
169
		$force_select   = true;
170
		$none_displayed = true;
171
		$expire_time    = time() + (3600 * 24 * 30 * 12 * 5); // five years
172
		$headers_sent   = @headers_sent();
173
 
174
		foreach (array_merge(array('@inc'), array_keys($table_cols)) as $col) {
175
			$varname = ($col[0] == '@' ? substr($col, 1) : 'have_'.$col);
176
			if (isset($HTTP_POST_VARS[$varname]) || isset($HTTP_GET_VARS[$varname])) {
177
				$value = $HTTP_POST_VARS[$varname];
178
				if (isset($HTTP_GET_VARS[$varname])) {
179
					$value = $HTTP_GET_VARS[$varname];
180
				}
181
				if ($varname != 'inc' && ! empty($value)) {
182
					$force_select = false;
183
				}
184
				$headers_sent || setcookie($varname.'_'.$this->tb.'_cookie', $value, $expire_time);
185
				$this->cgi['persist'] .= '&'.urlencode($varname);
186
				$this->cgi['persist'] .= '='.urlencode($value);
187
			} else {
188
				$headers_sent || setcookie($varname.'_'.$this->tb.'_cookie', '', time() - 10000);
189
			}
190
		}
191
 
192
		$i = -1;
193
		foreach (array_keys($this->fdd) as $key) {
194
			$i++;
195
			if (preg_match('/^\d*$/', $key))
196
				continue;
197
			$varname = 'have_'.$key;
198
			$value   = @$this->get_cgi_cookie_var($varname, '');
199
			$options = @$value ? 'LV' : '';
200
			$this->fdd[$i]['options']   = $options;
201
			$this->fdd[$key]['options'] = $options;
202
			$this->displayed[$i] = @$value ? true : false;
203
			$value && $none_displayed = false;
204
		}
205
 
206
		/*
207
		 * Redirecting when neccessary
208
		 * (hackity hack with unregistering/unchecking fields)
209
		 */
210
 
211
		if ($prepare_filter && ! $headers_sent) {
212
			$this->execute_redirect();
213
			exit;
214
		}
215
 
216
		/*
217
		 * Check if field selection report screen has to be displayed
218
		 */
219
 
220
		if (isset($fields_select) || $force_select || $none_displayed) {
221
			$this->execute_report_screen($table_cols);
222
			return true;
223
		}
224
 
225
		if (0) {
226
			$this->message .= $this->get_select_fields_link();
227
		}
228
 
229
		// parent class call
230
		return parent::execute();
231
	} /* }}} */
232
 
233
	function execute_redirect() /* {{{ */
234
	{
235
		global $HTTP_SERVER_VARS;
236
		global $HTTP_GET_VARS;
237
		global $HTTP_POST_VARS;
238
		$redirect_url = 'http://'.$HTTP_SERVER_VARS['HTTP_HOST'].$HTTP_SERVER_VARS['SCRIPT_NAME'];
239
		$delim = '?';
240
		foreach ($HTTP_POST_VARS + $HTTP_GET_VARS as $cgi_var_name => $cgi_var_value) {
241
			$cgi_var_name == 'prepare_filter' && $cgi_var_name = 'filter';
242
			$redirect_url .= $delim;
243
			$redirect_url .= rawurlencode($cgi_var_name).'='.rawurlencode($cgi_var_value);
244
			$delim == '?' && $delim = '&';
245
		}
246
		$redirect_url .= $this->cgi['persist'];
247
		header('Location: '.$redirect_url);
248
		exit;
249
	} /* }}} */
250
 
251
	function execute_report_screen($table_cols) /* {{{ */
252
	{
253
		echo '<form class="',$this->getCSSclass('form'),'" action="';
254
		echo htmlspecialchars($this->page_name),'" method="POST">',"\n";
255
		if ($this->nav_up()) {
256
			$this->display_report_selection_buttons('up');
257
			echo '<hr class="',$this->getCSSclass('hr', 'up'),'">',"\n";
258
		}
259
		echo '<table class="',$this->getCSSclass('main'),'" summary="',$this->tb,'">',"\n";
260
 
261
		$i = 0;
262
		foreach ($table_cols as $key => $val) {
263
			$css_postfix    = @$this->fdd[$key]['css']['postfix'];
264
			$css_class_name = $this->getCSSclass('input', null, true, $css_postfix);
265
			$varname        = 'have_'.$key;
266
			$value          = $this->get_cgi_cookie_var($varname);
267
			$checked        = @$value ? ' checked' : '';
268
			echo '<tr class="',$this->getCSSclass('row', null, 'next', $css_postfix),'">',"\n";
269
			echo '<td class="',$this->getCSSclass('key', null, true, $css_postfix),'">';
270
			echo $this->fdd[$i]['name'],'</td>',"\n";
271
			echo '<td class="',$this->getCSSclass('check', null, true, $css_postfix),'">';
272
			echo '<input class="',$css_class_name,'" type="checkbox" name="';
273
			echo htmlspecialchars($varname),'"',$checked,'>';
274
			echo '</td>',"\n";
275
			echo '<td class="',$this->getCSSclass('value', null, true, $css_postfix),'"';
276
			echo $this->getColAttributes($key),">\n";
277
			$varname = 'qf'.$i;
278
			$value   = $this->get_cgi_cookie_var($varname);
279
			if ($this->fdd[$key]['select'] == 'D' || $this->fdd[$key]['select'] == 'M') {
280
				$from_table = ! $this->col_has_values($key) || isset($this->fdd[$key]['values']['table']);
281
				$selected   = $value;
282
				$value      = $this->set_values($key, array('*' => '*'), null, $from_table);
283
				$multiple   = $this->col_has_multiple_select($key);
284
				$multiple  |= $this->fdd[$key]['select'] == 'M';
285
				$readonly   = false;
286
				$strip_tags = true;
287
				$escape     = true;
288
				echo $this->htmlSelect($varname.'_id', $css_class_name, $value, $selected,
289
						$multiple, $readonly, $strip_tags, $escape);
290
			} else {
291
				echo '<input class="',$css_class_name,'" type=text name="';
292
				echo htmlspecialchars($varname),'" value="',htmlspecialchars($value),'" size="';
293
				echo min(40, $val),'" maxlength="',min(40, max(10, $val)),'">';
294
			}
295
			echo '</td>',"\n",'</tr>',"\n";
296
			$i++;
297
		}
298
		echo '<tr class="',$this->getCSSclass('row', null, 'next', $css_postfix),'">',"\n";
299
		echo '<td class="',$this->getCSSclass('key', null, true, $css_postfix),'" colspan="2">';
300
		echo $this->labels['Records per screen'],'</td>';
301
		echo '<td class="',$this->getCSSclass('value', null, true, $css_postfix),'">';
302
		echo '<input class="',$css_class_name,'" type="text" name="inc" value="',$this->inc.'">';
303
		echo '</td></tr>',"\n";
304
		echo '</table>',"\n";
305
		if ($this->nav_down()) {
306
			echo '<hr class="',$this->getCSSclass('hr', 'down'),'">',"\n";
307
			$this->display_report_selection_buttons('down');
308
		}
309
		echo '</form>';
310
	} /* }}} */
311
 
312
}
313
 
314
/* Modeline for ViM {{{
315
 * vim:set ts=4:
316
 * vim600:fdm=marker fdl=0 fdc=0:
317
 * }}} */
318
 
319
?>