Projekt

Obecné

Profil

Stáhnout (8.56 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3
	/**
4
	 * List triggers on a table
5
	 *
6
	 * $Id: triggers.php,v 1.28 2005/10/18 03:45:16 chriskl Exp $
7
	 */
8

    
9
	// Include application functions
10
	include_once('./libraries/lib.inc.php');
11
	include_once('./classes/class.select.php');
12
	
13
	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
14
	$PHP_SELF = $_SERVER['PHP_SELF'];
15

    
16
	/** 
17
	 * Function to save after altering a trigger
18
	 */
19
	function doSaveAlter() {
20
		global $data, $lang;
21
		
22
		$status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']);
23
		if ($status == 0)
24
			doDefault($lang['strtriggeraltered']);
25
		else
26
			doAlter($lang['strtriggeralteredbad']);
27
	}
28

    
29
	/**
30
	 * Function to allow altering of a trigger
31
	 */
32
	function doAlter($msg = '') {
33
		global $data, $misc;
34
		global $PHP_SELF, $lang;
35
		
36
		$misc->printTrail('trigger');
37
		$misc->printTitle($lang['stralter'],'pg.trigger.alter');
38
		$misc->printMsg($msg);
39
		
40
		$triggerdata = $data->getTrigger($_REQUEST['table'], $_REQUEST['trigger']);
41
		
42
		if ($triggerdata->recordCount() > 0) {
43
			
44
			if (!isset($_POST['name'])) $_POST['name'] = $triggerdata->f['tgname'];
45
			
46
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
47
			echo "<table>\n";
48
			echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
49
			echo "<td class=\"data1\">";
50
			echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", 
51
				htmlspecialchars($_POST['name']), "\" />\n";
52
			echo "</table>\n";
53
			echo "<p><input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
54
			echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
55
			echo "<input type=\"hidden\" name=\"trigger\" value=\"", htmlspecialchars($_REQUEST['trigger']), "\" />\n";
56
			echo $misc->form;
57
			echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['strok']}\" />\n";
58
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
59
			echo "</form>\n";
60
		}
61
		else echo "<p>{$lang['strnodata']}</p>\n";
62
	}
63
	
64
	/**
65
	 * Show confirmation of drop and perform actual drop
66
	 */
67
	function doDrop($confirm) {
68
		global $data, $misc;
69
		global $PHP_SELF, $lang;
70

    
71
		if ($confirm) {
72
			$misc->printTrail('trigger');
73
			$misc->printTitle($lang['strdrop'],'pg.trigger.drop');
74

    
75
			echo "<p>", sprintf($lang['strconfdroptrigger'], $misc->printVal($_REQUEST['trigger']),
76
				$misc->printVal($_REQUEST['table'])), "</p>\n";
77

    
78
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
79
			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
80
			echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
81
			echo "<input type=\"hidden\" name=\"trigger\" value=\"", htmlspecialchars($_REQUEST['trigger']), "\" />\n";
82
			echo $misc->form;
83
			// Show cascade drop option if supportd
84
			if ($data->hasDropBehavior()) {
85
				echo "<p><input type=\"checkbox\" name=\"cascade\" /> {$lang['strcascade']}</p>\n";
86
			}
87
			echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
88
			echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
89
			echo "</form>\n";
90
		}
91
		else {
92
			$status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade']));
93
			if ($status == 0)
94
				doDefault($lang['strtriggerdropped']);
95
			else
96
				doDefault($lang['strtriggerdroppedbad']);
97
		}
98

    
99
	}
100

    
101
	/**
102
	 * Let them create s.th.
103
	 */
104
	function doCreate($msg = '') {
105
		global $data, $misc;
106
		global $PHP_SELF, $lang;
107
		
108
		$misc->printTrail('table');
109
		$misc->printTitle($lang['strcreatetrigger'],'pg.trigger.create');
110
		$misc->printMsg($msg);
111
		
112
		// Get all the functions that can be used in triggers
113
		$funcs = $data->getTriggerFunctions();
114
		if ($funcs->recordCount() == 0) {
115
			doDefault($lang['strnofunctions']);
116
			return;
117
		}
118

    
119
		/* Populate functions */
120
		$sel0 = new XHTML_Select('formFunction');
121
		while (!$funcs->EOF) {
122
			$sel0->add(new XHTML_Option($funcs->f['proname']));
123
			$funcs->moveNext();
124
		}
125

    
126
		/* Populate times */
127
		$sel1 = new XHTML_Select('formExecTime');
128
		$sel1->set_data($data->triggerExecTimes);
129

    
130
		/* Populate events */
131
		$sel2 = new XHTML_Select('formEvent');
132
		$sel2->set_data($data->triggerEvents);
133
		
134
		/* Populate occurences */
135
		$sel3 = new XHTML_Select('formFrequency');
136
		$sel3->set_data($data->triggerFrequency);
137
		
138
		echo "<form action=\"$PHP_SELF\" method=\"POST\">\n";
139
		echo "<table>\n";
140
		echo "<tr>\n";
141
		echo "		<th class=\"data\">{$lang['strname']}</th>\n";
142
		echo "		<th class=\"data\">{$lang['strwhen']}</th>\n";
143
		echo "</tr>\n";
144
		echo "<tr>\n";
145
		echo "		<td class=\"data1\"> <input type=\"text\" name=\"formTriggerName\" size=\"32\" /></td>\n";
146
		echo "		<td class=\"data1\"> ", $sel1->fetch(), "</td>\n";
147
		echo "</tr>\n";
148
		echo "<tr>\n";
149
		echo "    <th class=\"data\">{$lang['strevent']}</th>\n";
150
		echo "    <th class=\"data\">{$lang['strforeach']}</th>\n";
151
		echo "</tr>\n";
152
		echo "<tr>\n";
153
		echo "     <td class=\"data1\"> ", $sel2->fetch(), "</td>\n";
154
		echo "     <td class=\"data1\"> ", $sel3->fetch(), "</td>\n";
155
		echo "</tr>\n";
156
		echo "<tr><th class=\"data\"> {$lang['strfunction']}</th>\n";
157
		echo "<th class=\"data\"> {$lang['strarguments']}</th></tr>\n";
158
		echo "<tr><td class=\"data1\">", $sel0->fetch(), "</td>\n";
159
		echo "<td class=\"data1\">(<input type=\"text\" name=\"formTriggerArgs\" size=\"32\" />)</td>\n";
160
		echo "</tr></table>\n";
161
		echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
162
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
163
		echo "<input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
164
		echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
165
		echo $misc->form;
166
		echo "</form>\n";
167
	}
168
	
169
	/**
170
	 * Actually creates the new trigger in the database
171
	 */
172
	function doSaveCreate() {
173
		global $data;
174
		global $PHP_SELF, $lang;		
175
	
176
		// Check that they've given a name and a definition
177

    
178
		if ($_POST['formFunction'] == '')
179
			doCreate($lang['strtriggerneedsfunc']);
180
		elseif ($_POST['formTriggerName'] == '')
181
			doCreate($lang['strtriggerneedsname']);
182
		elseif ($_POST['formEvent'] == '') 
183
			doCreate();
184
		else {		 
185
			$status = $data->createTrigger($_POST['formTriggerName'], $_POST['table'],
186
					$_POST['formFunction'], $_POST['formExecTime'], $_POST['formEvent'],
187
					$_POST['formFrequency'], $_POST['formTriggerArgs']);
188
			if ($status == 0)
189
				doDefault($lang['strtriggercreated']);
190
			else
191
				doCreate($lang['strtriggercreatedbad']);
192
		}
193
	}	
194

    
195
	/**
196
	 * List all the triggers on the table
197
	 */
198
	function doDefault($msg = '') {
199
		global $data, $misc, $database;
200
		global $PHP_SELF;
201
		global $lang;
202

    
203
		function tgPre(&$rowdata) {
204
			global $data, $lang;
205
			// Nasty hack to support pre-7.4 PostgreSQL
206
			$rowdata->f['+tgdef'] = $rowdata->f['tgdef'] !== null
207
									? $rowdata->f['tgdef']
208
									: $data->getTriggerDef($rowdata->f);
209
		}
210
		
211
		$misc->printTrail('table');
212
		$misc->printTabs('table','triggers');
213
		$misc->printMsg($msg);
214

    
215
		$triggers = $data->getTriggers($_REQUEST['table']);
216

    
217
		$columns = array(
218
			'trigger' => array(
219
				'title' => $lang['strname'],
220
				'field' => 'tgname',
221
			),
222
			'definition' => array(
223
				'title' => $lang['strdefinition'],
224
				'field' => '+tgdef',
225
			),
226
			'actions' => array(
227
				'title' => $lang['stractions'],
228
			),
229
		);
230

    
231
		$actions = array(
232
			'alter' => array(
233
				'title' => $lang['stralter'],
234
				'url'   => "{$PHP_SELF}?action=confirm_alter&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
235
				'vars'  => array('trigger' => 'tgname'),
236
			),
237
			'drop' => array(
238
				'title' => $lang['strdrop'],
239
				'url'   => "{$PHP_SELF}?action=confirm_drop&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
240
				'vars'  => array('trigger' => 'tgname'),
241
			),
242
		);
243

    
244
		if (!$data->hasAlterTrigger()) unset($actions['alter']);
245
		
246
		$misc->printTable($triggers, $columns, $actions, $lang['strnotriggers'], 'tgPre');
247
		
248
		echo "<p><a class=\"navlink\" href=\"$PHP_SELF?action=create&amp;{$misc->href}&amp;table=", urlencode($_REQUEST['table']), "\">{$lang['strcreatetrigger']}</a></p>\n";
249
	}
250

    
251
	$misc->printHeader($lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strtriggers']);
252
	$misc->printBody();
253

    
254
	switch ($action) {
255
		case 'alter':
256
			if (isset($_POST['alter'])) doSaveAlter();
257
			else doDefault();
258
			break;
259
		case 'confirm_alter':
260
			doAlter();
261
			break;
262
		case 'save_create':
263
			if (isset($_POST['cancel'])) doDefault();
264
			else doSaveCreate();
265
			break;
266
		case 'create':
267
			doCreate();
268
			break;
269
		case 'drop':
270
			if (isset($_POST['yes'])) doDrop(false);
271
			else doDefault();
272
			break;
273
		case 'confirm_drop':
274
			doDrop(true);
275
			break;
276
		default:
277
			doDefault();
278
			break;
279
	}
280
	
281
	$misc->printFooter();
282

    
283
?>
(49-49/53)