2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functions for trigger management.
8 if (! defined('PHPMYADMIN')) {
13 * Sets required globals
15 function PMA_TRI_setGlobals()
17 global $action_timings, $event_manipulations;
19 // Some definitions for triggers
20 $action_timings = array('BEFORE',
22 $event_manipulations = array('INSERT',
28 * Main function for the triggers functionality
30 function PMA_TRI_main()
36 * Process all requests
38 PMA_TRI_handleEditor();
39 PMA_TRI_handleExport();
41 * Display a list of available triggers
43 $items = PMA_DBI_get_triggers($db, $table);
44 echo PMA_RTE_getList('trigger', $items);
46 * Display a link for adding a new trigger,
47 * if the user has the necessary privileges
49 echo PMA_TRI_getFooterLinks();
50 } // end PMA_TRI_main()
53 * Handles editor requests for adding or editing an item
55 function PMA_TRI_handleEditor()
57 global $_REQUEST, $_POST, $errors, $db, $table;
59 if (! empty($_REQUEST['editor_process_add'])
60 ||
! empty($_REQUEST['editor_process_edit'])
64 $item_query = PMA_TRI_getQueryFromRequest();
66 if (! count($errors)) { // set by PMA_RTN_getQueryFromRequest()
67 // Execute the created query
68 if (! empty($_REQUEST['editor_process_edit'])) {
69 // Backup the old trigger, in case something goes wrong
70 $trigger = PMA_TRI_getDataFromName($_REQUEST['item_original_name']);
71 $create_item = $trigger['create'];
72 $drop_item = $trigger['drop'] . ';';
73 $result = PMA_DBI_try_query($drop_item);
75 $errors[] = sprintf(__('The following query has failed: "%s"'), $drop_item) . '<br />'
76 . __('MySQL said: ') . PMA_DBI_getError(null);
78 $result = PMA_DBI_try_query($item_query);
80 $errors[] = sprintf(__('The following query has failed: "%s"'), $item_query) . '<br />'
81 . __('MySQL said: ') . PMA_DBI_getError(null);
82 // We dropped the old item, but were unable to create the new one
83 // Try to restore the backup query
84 $result = PMA_DBI_try_query($create_item);
86 // OMG, this is really bad! We dropped the query, failed to create a new one
87 // and now even the backup query does not execute!
88 // This should not happen, but we better handle this just in case.
89 $errors[] = __('Sorry, we failed to restore the dropped trigger.') . '<br />'
90 . __('The backed up query was:') . "\"$create_item\"" . '<br />'
91 . __('MySQL said: ') . PMA_DBI_getError(null);
94 $message = PMA_Message
::success(__('Trigger %1$s has been modified.'));
95 $message->addParam(PMA_backquote($_REQUEST['item_name']));
96 $sql_query = $drop_item . $item_query;
100 // 'Add a new item' mode
101 $result = PMA_DBI_try_query($item_query);
103 $errors[] = sprintf(__('The following query has failed: "%s"'), $item_query) . '<br /><br />'
104 . __('MySQL said: ') . PMA_DBI_getError(null);
106 $message = PMA_Message
::success(__('Trigger %1$s has been created.'));
107 $message->addParam(PMA_backquote($_REQUEST['item_name']));
108 $sql_query = $item_query;
113 if (count($errors)) {
114 $message = PMA_Message
::error(__('<b>One or more errors have occured while processing your request:</b>'));
115 $message->addString('<ul>');
116 foreach ($errors as $num => $string) {
117 $message->addString('<li>' . $string . '</li>');
119 $message->addString('</ul>');
122 $output = PMA_showMessage($message, $sql_query);
123 if ($GLOBALS['is_ajax_request']) {
124 $extra_data = array();
125 if ($message->isSuccess()) {
126 $items = PMA_DBI_get_triggers($db, $table, '');
128 foreach ($items as $key => $value) {
129 if ($value['name'] == $_REQUEST['item_name']) {
133 $extra_data['insert'] = false;
134 if (empty($table) ||
($trigger !== false && $table == $trigger['table'])) {
135 $extra_data['insert'] = true;
136 $extra_data['new_row'] = PMA_TRI_getRowForList($trigger);
137 $extra_data['name'] = htmlspecialchars(
138 strtoupper($_REQUEST['item_name'])
143 $response = $message;
145 PMA_ajaxResponse($response, $message->isSuccess(), $extra_data);
150 * Display a form used to add/edit a trigger, if necessary
152 if (count($errors) ||
( empty($_REQUEST['editor_process_add']) && empty($_REQUEST['editor_process_edit'])
153 && (! empty($_REQUEST['add_item']) ||
! empty($_REQUEST['edit_item']))) // FIXME: this must be simpler than that
155 // Get the data for the form (if any)
156 if (! empty($_REQUEST['add_item'])) {
157 $title = __("Create trigger");
158 $item = PMA_TRI_getDataFromRequest();
160 } else if (! empty($_REQUEST['edit_item'])) {
161 $title = __("Edit trigger");
162 if (! empty($_REQUEST['item_name'])
163 && empty($_REQUEST['editor_process_edit'])
165 $item = PMA_TRI_getDataFromName($_REQUEST['item_name']);
166 if ($item !== false) {
167 $item['item_original_name'] = $item['item_name'];
170 $item = PMA_TRI_getDataFromRequest();
174 if ($item !== false) {
176 $editor = PMA_TRI_getEditorForm($mode, $item);
177 if ($GLOBALS['is_ajax_request']) {
178 $extra_data = array('title' => $title);
179 PMA_ajaxResponse($editor, true, $extra_data);
181 echo "\n\n<h2>$title</h2>\n\n$editor";
183 require './libraries/footer.inc.php';
187 $message = __('Error in processing request') . ' : ';
189 PMA_RTE_getWord('not_found'),
190 htmlspecialchars(PMA_backquote($_REQUEST['item_name'])),
191 htmlspecialchars(PMA_backquote($db))
193 $message = PMA_message
::error($message);
194 if ($GLOBALS['is_ajax_request']) {
195 PMA_ajaxResponse($message, false);
201 } // end PMA_TRI_handleEditor()
204 * This function will generate the values that are required to for the editor
206 * @return array Data necessary to create the editor.
208 function PMA_TRI_getDataFromRequest()
211 $indices = array('item_name',
213 'item_original_name',
214 'item_action_timing',
215 'item_event_manipulation',
218 foreach ($indices as $key => $index) {
219 $retval[$index] = isset($_REQUEST[$index]) ?
$_REQUEST[$index] : '';
222 } // end PMA_TRI_getDataFromRequest()
225 * This function will generate the values that are required to complete
226 * the "Edit trigger" form given the name of a trigger.
228 * @param string $name The name of the trigger.
230 * @return array Data necessary to create the editor.
232 function PMA_TRI_getDataFromName($name)
234 global $db, $table, $_REQUEST;
237 $items = PMA_DBI_get_triggers($db, $table, '');
238 foreach ($items as $key => $value) {
239 if ($value['name'] == $name) {
247 $retval['create'] = $temp['create'];
248 $retval['drop'] = $temp['drop'];
249 $retval['item_name'] = $temp['name'];
250 $retval['item_table'] = $temp['table'];
251 $retval['item_action_timing'] = $temp['action_timing'];
252 $retval['item_event_manipulation'] = $temp['event_manipulation'];
253 $retval['item_definition'] = $temp['definition'];
254 $retval['item_definer'] = $temp['definer'];
257 } // end PMA_TRI_getDataFromName()
260 * Displays a form used to add/edit a trigger
262 * @param string $mode If the editor will be used edit a trigger
263 * or add a new one: 'edit' or 'add'.
264 * @param array $item Data for the trigger returned by
265 * PMA_TRI_getDataFromRequest() or
266 * PMA_TRI_getDataFromName()
268 * @return string HTML code for the editor.
270 function PMA_TRI_getEditorForm($mode, $item)
272 global $db, $table, $titles, $event_manipulations, $action_timings;
274 // Escape special characters
275 $need_escape = array(
276 'item_original_name',
281 foreach ($need_escape as $key => $index) {
282 $item[$index] = htmlentities($item[$index], ENT_QUOTES
);
285 if ($mode == 'edit') {
286 $original_data = "<input name='item_original_name' "
287 . "type='hidden' value='{$item['item_original_name']}'/>\n";
292 $retval .= "<!-- START " . strtoupper($mode) . " TRIGGER FORM -->\n\n";
293 $retval .= "<form class='rte_form' action='db_triggers.php' method='post'>\n";
294 $retval .= "<input name='{$mode}_item' type='hidden' value='1' />\n";
295 $retval .= $original_data;
296 $retval .= PMA_generate_common_hidden_inputs($db, $table) . "\n";
297 $retval .= "<fieldset>\n";
298 $retval .= "<legend>" . __('Details') . "</legend>\n";
299 $retval .= "<table class='rte_table' style='width: 100%'>\n";
301 $retval .= " <td style='width: 20%;'>" . __('Trigger name') . "</td>\n";
302 $retval .= " <td><input type='text' name='item_name' maxlength='64'\n";
303 $retval .= " value='{$item['item_name']}' /></td>\n";
304 $retval .= "</tr>\n";
306 $retval .= " <td>" . __('Table') . "</td>\n";
307 $retval .= " <td>\n";
308 $retval .= " <select name='item_table'>\n";
309 foreach (PMA_DBI_get_tables($db) as $key => $value) {
311 if ($value == $item['item_table']) {
312 $selected = " selected='selected'";
314 $retval .= " <option$selected>$value</option>\n";
316 $retval .= " </select>\n";
317 $retval .= " </td>\n";
318 $retval .= "</tr>\n";
320 $retval .= " <td>" . __('Time') . "</td>\n";
321 $retval .= " <td><select name='item_timing'>\n";
322 foreach ($action_timings as $key => $value) {
324 if (! empty($item['item_action_timing'])
325 && $item['item_action_timing'] == $value
327 $selected = " selected='selected'";
329 $retval .= "<option$selected>$value</option>";
331 $retval .= " </select></td>\n";
332 $retval .= "</tr>\n";
334 $retval .= " <td>" . __('Event') . "</td>\n";
335 $retval .= " <td><select name='item_event'>\n";
336 foreach ($event_manipulations as $key => $value) {
338 if (! empty($item['item_event_manipulation'])
339 && $item['item_event_manipulation'] == $value
341 $selected = " selected='selected'";
343 $retval .= "<option$selected>$value</option>";
345 $retval .= " </select></td>\n";
346 $retval .= "</tr>\n";
348 $retval .= " <td>" . __('Definition') . "</td>\n";
349 $retval .= " <td><textarea name='item_definition' rows='15' cols='40'>";
350 $retval .= $item['item_definition'];
351 $retval .= "</textarea></td>\n";
352 $retval .= "</tr>\n";
354 $retval .= " <td>" . __('Definer') . "</td>\n";
355 $retval .= " <td><input type='text' name='item_definer'\n";
356 $retval .= " value='{$item['item_definer']}' /></td>\n";
357 $retval .= "</tr>\n";
358 $retval .= "</table>\n";
359 $retval .= "</fieldset>\n";
360 if ($GLOBALS['is_ajax_request']) {
361 $retval .= "<input type='hidden' name='editor_process_{$mode}'\n";
362 $retval .= " value='true' />\n";
363 $retval .= "<input type='hidden' name='ajax_request' value='true' />\n";
365 $retval .= "<fieldset class='tblFooters'>\n";
366 $retval .= " <input type='submit' name='editor_process_{$mode}'\n";
367 $retval .= " value='" . __('Go') . "' />\n";
368 $retval .= "</fieldset>\n";
370 $retval .= "</form>\n\n";
371 $retval .= "<!-- END " . strtoupper($mode) . " TRIGGER FORM -->\n\n";
374 } // end PMA_TRI_getEditorForm()
377 * Composes the query necessary to create a trigger from an HTTP request.
379 * @return string The CREATE TRIGGER query.
381 function PMA_TRI_getQueryFromRequest()
383 global $_REQUEST, $db, $errors, $action_timings, $event_manipulations;
386 if (! empty($_REQUEST['item_definer'])) {
387 if (strpos($_REQUEST['item_definer'], '@') !== false) {
388 $arr = explode('@', $_REQUEST['item_definer']);
389 $query .= 'DEFINER=' . PMA_backquote($arr[0]);
390 $query .= '@' . PMA_backquote($arr[1]) . ' ';
392 $errors[] = __('The definer must be in the "username@hostname" format');
395 $query .= 'TRIGGER ';
396 if (! empty($_REQUEST['item_name'])) {
397 $query .= PMA_backquote($_REQUEST['item_name']) . ' ';
399 $errors[] = __('You must provide a trigger name');
401 if (! empty($_REQUEST['item_timing']) && in_array($_REQUEST['item_timing'], $action_timings)) {
402 $query .= $_REQUEST['item_timing'] . ' ';
404 $errors[] = __('You must provide a valid timing for the trigger');
406 if (! empty($_REQUEST['item_event']) && in_array($_REQUEST['item_event'], $event_manipulations)) {
407 $query .= $_REQUEST['item_event'] . ' ';
409 $errors[] = __('You must provide a valid event for the trigger');
412 if (! empty($_REQUEST['item_table']) && in_array($_REQUEST['item_table'], PMA_DBI_get_tables($db))) {
413 $query .= PMA_backQuote($_REQUEST['item_table']);
415 $errors[] = __('You must provide a valid table name');
417 $query .= ' FOR EACH ROW ';
418 if (! empty($_REQUEST['item_definition'])) {
419 $query .= $_REQUEST['item_definition'];
421 $errors[] = __('You must provide a trigger definition.');
425 } // end PMA_TRI_getQueryFromRequest()