Subversion Repositories Applications.bazar

Rev

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

Rev Author Line No. Line
468 mathias 1
<?php
2
 
3
/*
4
 * phpMyEdit - instant MySQL table editor and code generator
5
 *
6
 * extensions/phpMyEdit-messages.class.php - phpMyEdit messages 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-messages.class.php,v 1.15 2006-06-17 11:14:54 nepto Exp $ */
21
 
22
/* This extension is part of phpMyEzin: Content Management System project,
23
   where it handles discussion messages for particular articles. It depends on
24
   some phpMyEzin characteristics, thus extension should not and cannot be used
25
   outside this project. However there are planned some improvements for future
26
   to make this extension handle any kind of tree-structured data. */
27
 
28
require_once dirname(__FILE__).'/../phpMyEdit.class.php';
29
 
30
class phpMyEdit_messages extends phpMyEdit
31
{
32
	function phpMyEdit_messages($opts) /* {{{ */
33
	{
34
		$execute = 1;
35
		isset($opts['execute']) && $execute = $opts['execute'];
36
		$opts['execute'] = 0;
37
		parent::phpMyEdit($opts);
38
		$this->tb2         = $opts['tb2'];
39
		$this->format_date = $opts['format_date'];
40
 
41
		/* Preserved article ID in CGI environment. */
42
		/* TODO: change to $this->article_id or something like this */
43
		global $ezin_admin_article;
44
		$ezin_admin_article = $this->get_data_cgi_var('article_id');
45
 
46
		$execute && $this->execute();
47
	} /* }}} */
48
 
49
	function list_table() /* {{{ */
50
	{
51
		$ezin_admin_article_change_up = $this->get_sys_cgi_var('ezin_admin_article_change_up');
52
		$ezin_admin_article_change_down = $this->get_sys_cgi_var('ezin_admin_article_change_down');
53
		if (isset($ezin_admin_article_change_up)) {
54
			$ezin_admin_article = $this->get_sys_cgi_var('ezin_admin_article_up');
55
		} elseif (isset($ezin_admin_article_change_down)) {
56
			$ezin_admin_article = $this->get_sys_cgi_var('ezin_admin_article_down');
57
		}
58
		!isset($ezin_admin_article) && $ezin_admin_article = $this->get_data_cgi_var('article_id');
59
		$ezin_admin_article = intval($ezin_admin_article);
60
		$query = sprintf('SELECT article_id, count(id) AS messages FROM %s'
61
				.' GROUP BY article_id HAVING article_id = %d',
62
				$this->tb, intval($ezin_admin_article));
63
		if (($result = $this->myquery($query)) == false) {
64
			return false;
65
		}
66
		$row = @mysql_fetch_array($result, MYSQL_ASSOC);
67
		//$ezin_admin_article  = intval($row['article_id']);
68
		$ezin_admin_msgcount = intval($row['messages']);
69
		@mysql_free_result($result);
70
 
71
		echo '<form class="',$this->getCSSclass('form');
72
		echo '" action="',$page_name,'" method="POST">',"\n";
73
 
74
		if ($this->nav_up() || $ezin_admin_article <= 0) {
75
			$this->message_nav_buttons($ezin_admin_article, $ezin_admin_msgcount, 'up');
76
			echo '<hr class="',$this->getCSSclass('hr', 'up'),'">',"\n";
77
		}
78
 
79
		if ($ezin_admin_article > 0) {
80
			echo '<table class="',$this->getCSSclass('main'),'" summary="',$this->tb,'">',"\n";
81
			echo '<tr class="',$this->getCSSclass('header'),'">',"\n";
82
			foreach (array('ID', 'Subject', ' ', 'Author', 'Date & Time', 'IP addresses') as $str) {
83
				echo '<th class="',$this->getCSSclass('header'),'">';
84
				echo Platon::htmlspecialchars2($str),'</th>',"\n";
85
			}
86
			echo '</tr>',"\n";
87
			echo '<tr class="',$this->getCSSclass('header'),'">',"\n";
88
			echo '<th class="',$this->getCSSclass('header'),'" colspan="6">';
89
			echo 'Valid messages</td></tr>',"\n";
90
			$message_ids = $this->message_process($ezin_admin_article, 0, 0);
91
			$count_message_ids = count($message_ids);
92
			if ($count_message_ids == 0) {
93
				echo '<tr class="',$this->getCSSclass('row', null, 'next'),'">',"\n";
94
				echo '<td class="',$this->getCSSclass('cell', null, true),'" colspan="6">',"\n";
95
				echo '<i>There are no valid messages for this article.</i>';
96
				echo '</td></tr>',"\n";
97
			}
98
			$query = sprintf('SELECT id, parent, article_id, author,'
99
					.' email, homepage, subject, datetime, ip'
100
					.' FROM %s WHERE article_id = %d ORDER BY datetime ASC',
101
					$this->tb, intval($ezin_admin_article));
102
			if (($result = $this->myquery($query)) == false) {
103
				return false;
104
			}
105
			$all_ids = array();
106
			$parents = array();
107
			for ($i = 0; ($row = @mysql_fetch_array($result, MYSQL_ASSOC)); $i++) {
108
				$all_ids[]           = $row['id'];
109
				$parents[$row['id']] = $row['parent'];
110
			}
111
			@mysql_free_result($result);
112
			$all_ids = array_diff($all_ids, $message_ids);
113
			echo '<tr class="',$this->getCSSclass('header'),'">',"\n";
114
			echo '<th class="',$this->getCSSclass('header'),'" colspan="6">';
115
			echo 'Invalid messages</td></tr>',"\n";
116
			if (count($all_ids) > 0) {
117
				/* To force buttons */
118
				$count_message_ids = -1;
119
				while (count($all_ids) > 0) {
120
					//echo "<p>all_ids: "; var_dump($all_ids);echo '<br>';
121
					$sub_ids = $this->message_process($ezin_admin_article,
122
							$parents[array_shift($all_ids)], 0, true);
123
					$all_ids = array_diff($all_ids, $sub_ids);
124
				}
125
			} else {
126
				echo '<tr class="',$this->getCSSclass('row', null, 'next'),'">',"\n";
127
				echo '<td class="',$this->getCSSclass('cell', null, true),'" colspan="6">',"\n";
128
				echo '<i>There are no invalid messages for this article.</i>';
129
				echo '</td></tr>',"\n";
130
			}
131
			echo '</table>';
132
		}
133
		if ($this->nav_down() && $ezin_admin_article > 0) {
134
			echo '<hr class="',$this->getCSSclass('hr', 'down'),'">',"\n";
135
			$this->message_nav_buttons($ezin_admin_article, $ezin_admin_msgcount, 'down');
136
		}
137
		echo $this->htmlHiddenData('article_id', $ezin_admin_article);
138
		echo '</form>',"\n";
139
	} /* }}} */
140
 
141
	function message_process($article_id, $id, $level = 0, $parent = true) /* {{{ */
142
	{
143
		$id    = intval($id);
144
		$level = intval($level);
145
		$query = sprintf('SELECT id, parent, article_id, author,'
146
				.' email, homepage, subject, datetime, ip'
147
				.' FROM %s WHERE %s = %d AND article_id = %d'
148
				.' ORDER BY datetime ASC', $this->tb,
149
				$parent == true ? 'parent' : 'id', intval($id), intval($article_id));
150
		if (($result = $this->myquery($query)) == false) {
151
			return false;
152
		}
153
 
154
		$ar     = array();
155
		$ar_out = array();
156
		for ($i = 0; ($row = @mysql_fetch_array($result, MYSQL_ASSOC)); $i++) {
157
			$ar[$i]   = $row;
158
			$ar_out[] = $row['id'];
159
		}
160
		$checked = ! $level && $parent ? ' checked' : '';
161
		for ($i = 0; $i < count($ar); $i++) {
162
			echo '<tr class="',$this->getCSSclass('row', null, 'next'),'">',"\n";
163
			$css_class_name  = $this->getCSSclass('cell', null, true);
164
			$css_class_name2 = $this->getCSSclass('navigation', null, true);
165
			echo '<td class="',$css_class_name,'">',$ar[$i]['id'],'</td>',"\n";
166
			echo '<td class="',$css_class_name,'">';
167
			for ($j = 0; $j < $level; $j++) {
168
				echo '&nbsp;&nbsp;&nbsp;';
169
			}
170
			echo htmlspecialchars($ar[$i]['subject']);
171
			echo '</td>',"\n";
172
			echo '<td class="',$css_class_name2,'">';
173
			echo '<input',$checked,' class="',$css_class_name2,'"';
174
			echo ' type="radio" ','name="',$this->cgi['prefix']['sys'],'rec"';
175
			echo ' value="',$ar[$i]['id'],'" class="link"></td>',"\n";
176
			echo '<td class="',$css_class_name,'">',htmlspecialchars($ar[$i]['author']),  '</td>';
177
			echo '<td class="',$css_class_name,'">',htmlspecialchars($ar[$i]['datetime']),'</td>';
178
			// TODO: do resolving
179
			echo '<td class="',$css_class_name,'"><small>';
180
			// this shoud be global IP-adress-deliminator
181
			$output = false;
182
			$ar_ip  = preg_split('|([ ]*[ \\/,;]+[ ]*)|', $ar[$i]['ip'], -1, PREG_SPLIT_DELIM_CAPTURE);
183
			foreach ($ar_ip as $ip) {
184
				if (strlen($output) > 0) {
185
					$output = true;
186
				}
187
				$ip = htmlspecialchars($ip);
188
				if (preg_match('/^(\d{1,3}\.){3}\d{1,3}$/', $ip)) {
189
					echo '<a class="',$css_class_name,'" target="_blank" href="http://',$ip,'">';
190
					echo '<small>',$ip,'</small></a>';
191
				} else {
192
					echo $ip;
193
				}
194
			}
195
			if (! $output) {
196
				echo '&nbsp;';
197
			}
198
			echo '</small></td>',"\n";
199
			echo '</tr>',"\n";
200
			if ($parent) {
201
				$ar_out = array_merge($ar_out, $this->message_process(
202
							$article_id, $ar[$i]['id'], $level + 1));
203
			}
204
			strlen($checked) && $checked = '';
205
		}
206
		return $ar_out;
207
	} /* }}} */
208
 
209
	function message_nav_buttons($article_id, $messages_count, $position) /* {{{ */
210
	{
211
		echo '<table class="',$this->getCSSclass('navigation', $position),'">',"\n";
212
		echo '<tr class="',$this->getCSSclass('navigation', $position),'">',"\n";
213
		echo '<td class="',$this->getCSSclass('buttons', $position),'">',"\n";
214
		$this->print_article_select($article_id, 0, $position);
215
		echo '</td>',"\n";
216
		echo '<td class="',$this->getCSSclass('buttons2', $position),'">',"\n";
217
		if ($article_id > 0) {
218
			if ($this->add_enabled()) {
219
				echo $this->htmlSubmit('operation', 'Add', $this->getCSSclass('add', $position), false, false);
220
			}
221
			if ($this->view_enabled()) {
222
				echo '&nbsp;';
223
				echo $this->htmlSubmit('operation', 'View', $this->getCSSclass('view', $position),
224
						false, $messages_count <= 0);
225
			}
226
			if ($this->change_enabled()) {
227
				echo '&nbsp;';
228
				echo $this->htmlSubmit('operation', 'Change', $this->getCSSclass('change', $position),
229
						false, $messages_count <= 0);
230
			}
231
			if ($this->delete_enabled()) {
232
				echo '&nbsp;';
233
				echo $this->htmlSubmit('operation', 'Delete', $this->getCSSclass('delete', $position),
234
						false, $messages_count <= 0);
235
			}
236
		}
237
		echo '</td></tr></table>',"\n";
238
	} /* }}} */
239
 
240
	function display_record_buttons() /* {{{ */
241
	{
242
		echo '<table class="',$this->getCSSclass('navigation', $position),'">',"\n";
243
		echo '<tr class="',$this->getCSSclass('navigation', $position),'">',"\n";
244
		echo '<td class="',$this->getCSSclass('buttons', $position),'">',"\n";
245
		$this->print_article_select($article_id, 1, $position);
246
		echo '</td>',"\n";
247
		if (strlen(@$this->message) > 0) {
248
			echo '<td class="',$this->getCSSclass('message', $position),'">',$this->message,'</td>',"\n";
249
		}
250
		echo '<td class="',$this->getCSSclass('buttons2', $position),'">',"\n";
251
		if ($this->change_operation()) {
252
			echo $this->htmlSubmit('savechange', 'Save', $this->getCSSclass('save', $position), true), '&nbsp;';
253
			echo $this->htmlSubmit('morechange', 'Apply', $this->getCSSclass('more', $position), true), '&nbsp;';
254
			echo $this->htmlSubmit('cancelchange', 'Cancel', $this->getCSSclass('cancel', $position), false);
255
		} elseif ($this->add_operation()) {
256
			echo $this->htmlSubmit('saveadd', 'Save', $this->getCSSclass('save', $position), true), '&nbsp;';
257
			echo $this->htmlSubmit('moreadd', 'More', $this->getCSSclass('more', $position), true), '&nbsp;';
258
			echo $this->htmlSubmit('canceladd', 'Cancel', $this->getCSSclass('cancel', $position), false);
259
		} elseif ($this->delete_operation()) {
260
			echo $this->htmlSubmit('savedelete', 'Delete', $this->getCSSclass('save', $position), false), '&nbsp;';
261
			echo $this->htmlSubmit('canceldelete', 'Cancel', $this->getCSSclass('cancel', $position), false);
262
		} elseif ($this->view_operation()) {
263
			if ($this->change_enabled()) {
264
				echo $this->htmlSubmit('operation', 'Change', $this->getCSSclass('save', $position), false), '&nbsp;';
265
			}
266
			echo $this->htmlSubmit('cancelview', 'Cancel', $this->getCSSclass('cancel', $position), false);
267
		}
268
		// Message is now written here
269
		echo '</td>',"\n";
270
		echo '</tr></table>',"\n";
271
	} /* }}} */
272
 
273
	function print_article_select($selected_id, $disabled, $position) /* {{{ */
274
	{
275
		if ($selected_id <= 0) {
276
			$rec = intval($this->get_sys_cgi_var('rec'));
277
			if ($rec > 0) {
278
				$query  = sprintf('SELECT article_id FROM %s WHERE id = %d',
279
						$this->tb, $rec);
280
				$result = $this->myquery($query);
281
				if ($result != false) {
282
					$row = @mysql_fetch_array($result, MYSQL_NUM);
283
					$selected_id = $row[0];
284
				}
285
				@mysql_free_result($result);
286
			}
287
		}
288
		static $articles = null;
289
		if ($articles == null) {
290
			$articles = array();
291
			$query = 'SELECT id, title, atitle, UNIX_TIMESTAMP(datetime) AS date'
292
				.' FROM '.$this->tb2
293
				.' ORDER BY date DESC';
294
			if (($result = $this->myquery($query)) == false) {
295
				return false;
296
			}
297
			for ($k = 0; ($row = @mysql_fetch_array($result, MYSQL_ASSOC)); $k++) {
298
				$articles[] = $row;
299
			}
300
			@mysql_free_result($result);
301
		}
302
		echo '<select',($disabled ? ' disabled' : ''),' name="';
303
		echo $this->cgi['prefix']['sys'].'ezin_admin_article_',$position,'" size="1">',"\n";
304
		echo '<option value="0">-- Choose article --</option>',"\n";
305
		foreach ($articles as $row) {
306
			$row['title'] = empty($row['title']) ? $row['atitle'] : $row['title'];
307
			$row['title'] = Platon::pretty_substr(strip_tags($row['title']), 40);
308
			echo '<option'.($selected_id == $row['id'] ? ' selected' : '');
309
			echo ' value="',$row['id'],'">',$row['title'];
310
			if ($row['date'] > 0) {
311
				printf(' [%d] (%s)', $row['id'], date($this->format_date, $row['date']));
312
			}
313
			echo '</option>',"\n";
314
		}
315
		echo '</select>',"\n";
316
		if (! $disabled) {
317
			echo $this->htmlSubmit('ezin_admin_article_change_'.$position, ' &gt; ', $this->get_sys_cgi_var('change', $position)), '&nbsp;', "\n";
318
		}
319
		return true;
320
	} /* }}} */
321
 
322
}
323
 
324
/* Modeline for ViM {{{
325
 * vim:set ts=4:
326
 * vim600:fdm=marker fdl=0 fdc=0:
327
 * }}} */
328
 
329
?>