2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Displays form for editing and inserting new table rows
6 * register_globals_save (mark this file save for disabling register globals)
12 * Gets the variables sent or posted to this script and displays the header
14 require_once 'libraries/common.inc.php';
17 * Ensures db and table are valid, else moves to the "parent" script
19 require_once 'libraries/db_table_exists.lib.php';
22 * functions implementation for this script
24 require_once 'libraries/insert_edit.lib.php';
27 * Sets global variables.
28 * Here it's better to use a if, instead of the '?' operator
29 * to avoid setting a variable to '' when it's not present in $_REQUEST
32 if (isset($_REQUEST['where_clause'])) {
33 $where_clause = $_REQUEST['where_clause'];
35 if (isset($_SESSION['edit_next'])) {
36 $where_clause = $_SESSION['edit_next'];
37 unset($_SESSION['edit_next']);
38 $after_insert = 'edit_next';
40 if (isset($_REQUEST['ShowFunctionFields'])) {
41 $cfg['ShowFunctionFields'] = $_REQUEST['ShowFunctionFields'];
43 if (isset($_REQUEST['ShowFieldTypesInDataEditView'])) {
44 $cfg['ShowFieldTypesInDataEditView'] = $_REQUEST['ShowFieldTypesInDataEditView'];
46 if (isset($_REQUEST['after_insert'])) {
47 $after_insert = $_REQUEST['after_insert'];
52 require_once 'libraries/file_listing.lib.php';
56 * Defines the url to return to in case of error in a sql statement
57 * (at this point, $GLOBALS['goto'] will be set but could be empty)
59 if (empty($GLOBALS['goto'])) {
61 // avoid a problem (see bug #2202709)
62 $GLOBALS['goto'] = 'tbl_sql.php';
64 $GLOBALS['goto'] = 'db_sql.php';
68 * @todo check if we could replace by "db_|tbl_" - please clarify!?
72 'sql_query' => $_REQUEST['sql_query']
75 if (preg_match('@^tbl_@', $GLOBALS['goto'])) {
76 $_url_params['table'] = $table;
79 $err_url = $GLOBALS['goto'] . PMA_generate_common_url($_url_params);
84 * Sets parameters for links
85 * where is this variable used?
86 * replace by PMA_generate_common_url($url_params);
88 $url_query = PMA_generate_common_url($url_params, 'html', '');
91 * get table information
92 * @todo should be done by a Table object
94 require_once 'libraries/tbl_info.inc.php';
97 * Get comments for table fileds/columns
99 $comments_map = array();
101 if ($GLOBALS['cfg']['ShowPropertyComments']) {
102 $comments_map = PMA_getComments($db, $table);
106 * START REGULAR OUTPUT
110 * Load JavaScript files
112 $response = PMA_Response
::getInstance();
113 $header = $response->getHeader();
114 $scripts = $header->getScripts();
115 $scripts->addFile('functions.js');
116 $scripts->addFile('tbl_change.js');
117 $scripts->addFile('jquery/jquery-ui-timepicker-addon.js');
118 $scripts->addFile('gis_data_editor.js');
121 * Displays the query submitted and its result
123 * @todo where does $disp_message and $disp_query come from???
125 if (! empty($disp_message)) {
126 if (! isset($disp_query)) {
129 $response->addHTML(PMA_Util
::getMessage($disp_message, $disp_query));
133 * Get the analysis of SHOW CREATE TABLE for this table
135 $analyzed_sql = PMA_Table
::analyzeStructure($db, $table);
138 * Get the list of the fields of the current table
140 PMA_DBI_select_db($db);
141 $table_fields = array_values(PMA_DBI_get_columns($db, $table));
143 $paramTableDbArray = array($table, $db);
146 * Determine what to do, edit or insert?
148 if (isset($where_clause)) {
150 $insert_mode = false;
151 $where_clause_array = PMA_getWhereClauseArray($where_clause);
152 list($where_clauses, $result, $rows, $found_unique_key)
153 = PMA_analyzeWhereClauses($where_clause_array, $table, $db);
157 $where_clause = null;
158 list($result, $rows) = PMA_loadFirstRow($table, $db);
159 $where_clauses = null;
160 $where_clause_array = null;
161 $found_unique_key = false;
164 // Copying a row - fetched data will be inserted as a new row,
165 // therefore the where clause is needless.
166 if (isset($_REQUEST['default_action']) && $_REQUEST['default_action'] === 'insert') {
167 $where_clause = $where_clauses = null;
170 // retrieve keys into foreign fields, if any
171 $foreigners = PMA_getForeigners($db, $table);
173 // Retrieve form parameters for insert/edit form
174 $_form_params = PMA_getFormParametersForInsertForm(
175 $db, $table, $where_clauses, $where_clause_array, $err_url
181 // autocomplete feature of IE kills the "onchange" event handler and it
182 // must be replaced by the "onpropertychange" one in this case
183 $chg_evt_handler = (PMA_USR_BROWSER_AGENT
== 'IE'
184 && PMA_USR_BROWSER_VER
>= 5
185 && PMA_USR_BROWSER_VER
< 7
189 // Had to put the URI because when hosted on an https server,
190 // some browsers send wrongly this form to the http server.
193 // Set if we passed the first timestamp field
194 $timestamp_seen = false;
195 $columns_cnt = count($table_fields);
198 $tabindex_for_function = +
3000;
199 $tabindex_for_null = +
6000;
200 $tabindex_for_value = 0;
202 $biggest_max_file_size = 0;
204 $url_params['db'] = $db;
205 $url_params['table'] = $table;
206 $url_params = PMA_urlParamsInEditMode(
207 $url_params, $where_clause_array, $where_clause
211 //If table has blob fields we have to disable ajax.
212 $has_blob_field = false;
213 foreach ($table_fields as $column) {
214 if (PMA_isColumnBlob($column)) {
215 $has_blob_field = true;
219 $html_output .='<form id="insertForm" ';
220 if ($has_blob_field && $is_upload) {
221 $html_output .='class="disableAjax" ';
223 $html_output .='method="post" action="tbl_replace.php" name="insertForm" ';
225 $html_output .= ' enctype="multipart/form-data"';
228 $html_output .= PMA_generate_common_hidden_inputs($_form_params);
230 $titles['Browse'] = PMA_Util
::getIcon('b_browse.png', __('Browse foreign values'));
232 // user can toggle the display of Function column and column types
233 // (currently does not work for multi-edits)
234 if (! $cfg['ShowFunctionFields'] ||
! $cfg['ShowFieldTypesInDataEditView']) {
235 $html_output .= __('Show');
238 if (! $cfg['ShowFunctionFields']) {
239 $html_output .= PMA_showFunctionFieldsInEditMode($url_params, false);
242 if (! $cfg['ShowFieldTypesInDataEditView']) {
243 $html_output .= PMA_showColumnTypesInDataEditView($url_params, false);
246 foreach ($rows as $row_id => $current_row) {
247 if ($current_row === false) {
252 $rownumber_param = '&rownumber=' . $row_id;
253 $vkey = '[multi_edit][' . $jsvkey . ']';
255 $current_result = (isset($result) && is_array($result) && isset($result[$row_id])
258 if ($insert_mode && $row_id > 0) {
259 $html_output .= '<input type="checkbox" checked="checked"'
260 . ' name="insert_ignore_' . $row_id . '"'
261 . ' id="insert_ignore_' . $row_id . '" />'
262 .'<label for="insert_ignore_' . $row_id . '">'
264 . '</label><br />' . "\n";
267 $html_output .= PMA_getHeadAndFootOfInsertRowTable($url_params)
270 // Sets a multiplier used for input-field counts
271 // (as zero cannot be used, advance the counter plus one)
272 $m_rows = $o_rows +
1;
273 //store the default value for CharEditing
274 $default_char_editing = $cfg['CharEditing'];
277 for ($i = 0; $i < $columns_cnt; $i++
) {
278 if (! isset($table_fields[$i]['processed'])) {
279 $column = $table_fields[$i];
280 $column = PMA_analyzeTableColumnsArray(
281 $column, $comments_map, $timestamp_seen
285 $extracted_columnspec
286 = PMA_Util
::extractColumnSpec($column['Type']);
288 if (-1 === $column['len']) {
289 $column['len'] = PMA_DBI_field_len($current_result, $i);
290 // length is unknown for geometry fields,
291 // make enough space to edit very simple WKTs
292 if (-1 === $column['len']) {
296 //Call validation when the form submited...
297 $unnullify_trigger = $chg_evt_handler
298 . "=\"return verificationsAfterFieldChange('"
299 . PMA_escapeJsString($column['Field_md5']) . "', '"
300 . PMA_escapeJsString($jsvkey) . "','".$column['pma_type'] . "')\"";
302 // Use an MD5 as an array index to avoid having special characters
303 // in the name atttibute (see bug #1746964 )
304 $column_name_appendix = $vkey . '[' . $column['Field_md5'] . ']';
306 if ($column['Type'] == 'datetime'
307 && ! isset($column['Default'])
308 && ! is_null($column['Default'])
309 && ($insert_mode ||
! isset($current_row[$column['Field']]))
312 // UPDATE case with an NULL value
313 $current_row[$column['Field']] = date('Y-m-d H:i:s', time());
316 $html_output .= '<tr class="noclick ' . ($odd_row ?
'odd' : 'even' ) . '">'
317 . '<td ' . ($cfg['LongtextDoubleTextarea'] && strstr($column['True_Type'], 'longtext') ?
'rowspan="2"' : '') . 'class="center">'
318 . $column['Field_title']
319 . '<input type="hidden" name="fields_name' . $column_name_appendix . '" value="' . $column['Field_html'] . '"/>'
321 if ($cfg['ShowFieldTypesInDataEditView']) {
322 $html_output .= '<td class="center' . $column['wrap'] . '">'
323 . '<span class="column_type">' . $column['pma_type'] . '</span>'
327 // Get a list of GIS data types.
328 $gis_data_types = PMA_Util
::getGISDatatypes();
330 // Prepares the field value
331 $real_null_value = false;
332 $special_chars_encoded = '';
333 if (isset($current_row)) {
336 $real_null_value, $special_chars_encoded, $special_chars,
339 = PMA_getSpecialCharsAndBackupFieldForExistingRow(
340 $current_row, $column, $extracted_columnspec,
341 $real_null_value, $gis_data_types, $column_name_appendix
344 // (we are inserting)
345 // display default values
346 list($real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded)
347 = PMA_getSpecialCharsAndBackupFieldForInsertingMode($column, $real_null_value);
350 $idindex = ($o_rows * $columns_cnt) +
$i +
1;
351 $tabindex = $idindex;
353 // Get a list of data types that are not yet supported.
354 $no_support_types = PMA_Util
::unsupportedDatatypes();
356 // The function column
357 // -------------------
358 if ($cfg['ShowFunctionFields']) {
359 $html_output .= PMA_getFunctionColumn(
360 $column, $is_upload, $column_name_appendix,
361 $unnullify_trigger, $no_support_types, $tabindex_for_function,
362 $tabindex, $idindex, $insert_mode
368 $foreignData = PMA_getForeignData(
369 $foreigners, $column['Field'], false, '', ''
371 $html_output .= PMA_getNullColumn(
372 $column, $column_name_appendix, $real_null_value,
373 $tabindex, $tabindex_for_null, $idindex, $vkey, $foreigners,
377 // The value column (depends on type)
379 // See bug #1667887 for the reason why we don't use the maxlength
381 $html_output .= ' <td>' . "\n";
382 // Will be used by js/tbl_change.js to set the default value
383 // for the "Continue insertion" feature
384 $html_output .= '<span class="default_value hide">'
385 . $special_chars . '</span>';
387 $html_output .= PMA_getValueColumn(
388 $column, $backup_field, $column_name_appendix, $unnullify_trigger,
389 $tabindex, $tabindex_for_value, $idindex, $data, $special_chars,
390 $foreignData, $odd_row, $paramTableDbArray, $rownumber_param, $titles,
391 $text_dir, $special_chars_encoded, $vkey, $is_upload,
392 $biggest_max_file_size, $default_char_editing,
393 $no_support_types, $gis_data_types, $extracted_columnspec
396 $html_output .= '</td>'
399 $odd_row = !$odd_row;
402 $html_output .= ' </tbody>'
404 } // end foreach on multi-edit
406 $html_output .='<div id="gis_editor"></div>'
407 . '<div id="popup_background"></div>'
410 if (! isset($after_insert)) {
411 $after_insert = 'back';
415 $html_output .= PMA_getActionsPanel(
416 $where_clause, $after_insert, $tabindex,
417 $tabindex_for_value, $found_unique_key
420 if ($biggest_max_file_size > 0) {
422 . PMA_Util
::generateHiddenMaxFileSize(
423 $biggest_max_file_size
426 $html_output .= '</form>';
427 // end Insert/Edit form
430 //Continue insertion form
431 $html_output .= PMA_getContinueInsertionForm(
432 $table, $db, $where_clause_array, $err_url
435 $response->addHTML($html_output);