Translated using Weblate (Thai)
[phpmyadmin.git] / tbl_change.php
blob608893e4ccfd0f738c38391802a1461fa0b751ad
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays form for editing and inserting new table rows
6 * register_globals_save (mark this file save for disabling register globals)
8 * @package PhpMyAdmin
9 */
11 /**
12 * Gets the variables sent or posted to this script and displays the header
14 require_once 'libraries/common.inc.php';
16 /**
17 * Ensures db and table are valid, else moves to the "parent" script
19 require_once 'libraries/db_table_exists.lib.php';
21 /**
22 * functions implementation for this script
24 require_once 'libraries/insert_edit.lib.php';
26 /**
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'];
49 /**
50 * file listing
52 require_once 'libraries/file_listing.lib.php';
55 /**
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'])) {
60 if (strlen($table)) {
61 // avoid a problem (see bug #2202709)
62 $GLOBALS['goto'] = 'tbl_sql.php';
63 } else {
64 $GLOBALS['goto'] = 'db_sql.php';
67 /**
68 * @todo check if we could replace by "db_|tbl_" - please clarify!?
70 $_url_params = array(
71 'db' => $db,
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);
80 unset($_url_params);
83 /**
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', '');
90 /**
91 * get table information
92 * @todo should be done by a Table object
94 require_once 'libraries/tbl_info.inc.php';
96 /**
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)) {
127 $disp_query = null;
129 $response->addHTML(PMA_Util::getMessage($disp_message, $disp_query));
133 * Get the analysis of SHOW CREATE TABLE for this table
134 * @todo should be handled by class Table
136 $show_create_table = PMA_DBI_fetch_value(
137 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table),
138 0, 1
140 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
141 unset($show_create_table);
144 * Get the list of the fields of the current table
146 PMA_DBI_select_db($db);
147 $table_fields = array_values(PMA_DBI_get_columns($db, $table));
149 $paramTableDbArray = array($table, $db);
152 * Determine what to do, edit or insert?
154 if (isset($where_clause)) {
155 // we are editing
156 $insert_mode = false;
157 $where_clause_array = PMA_getWhereClauseArray($where_clause);
158 list($where_clauses, $result, $rows, $found_unique_key)
159 = PMA_analyzeWhereClauses($where_clause_array, $table, $db);
160 } else {
161 // we are inserting
162 $insert_mode = true;
163 $where_clause = null;
164 list($result, $rows) = PMA_loadFirstRow($table, $db);
165 $where_clauses = null;
166 $where_clause_array = null;
167 $found_unique_key = false;
170 // Copying a row - fetched data will be inserted as a new row,
171 // therefore the where clause is needless.
172 if (isset($_REQUEST['default_action']) && $_REQUEST['default_action'] === 'insert') {
173 $where_clause = $where_clauses = null;
176 // retrieve keys into foreign fields, if any
177 $foreigners = PMA_getForeigners($db, $table);
179 // Retrieve form parameters for insert/edit form
180 $_form_params = PMA_getFormParametersForInsertForm(
181 $db, $table, $where_clauses, $where_clause_array, $err_url
185 * Displays the form
187 // autocomplete feature of IE kills the "onchange" event handler and it
188 // must be replaced by the "onpropertychange" one in this case
189 $chg_evt_handler = (PMA_USR_BROWSER_AGENT == 'IE'
190 && PMA_USR_BROWSER_VER >= 5
191 && PMA_USR_BROWSER_VER < 7
193 ? 'onpropertychange'
194 : 'onchange';
195 // Had to put the URI because when hosted on an https server,
196 // some browsers send wrongly this form to the http server.
198 $html_output = '';
199 // Set if we passed the first timestamp field
200 $timestamp_seen = false;
201 $columns_cnt = count($table_fields);
203 $tabindex = 0;
204 $tabindex_for_function = +3000;
205 $tabindex_for_null = +6000;
206 $tabindex_for_value = 0;
207 $o_rows = 0;
208 $biggest_max_file_size = 0;
210 $url_params['db'] = $db;
211 $url_params['table'] = $table;
212 $url_params = PMA_urlParamsInEditMode(
213 $url_params, $where_clause_array, $where_clause
216 //Insert/Edit form
217 //If table has blob fields we have to disable ajax.
218 $has_blob_field = false;
219 foreach ($table_fields as $column) {
220 if (PMA_isColumnBlob($column)) {
221 $has_blob_field = true;
222 break;
225 $html_output .='<form id="insertForm" ';
226 if ($has_blob_field && $is_upload) {
227 $html_output .='class="disableAjax" ';
229 $html_output .='method="post" action="tbl_replace.php" name="insertForm" ';
230 if ($is_upload) {
231 $html_output .= ' enctype="multipart/form-data"';
233 $html_output .= '>';
234 $html_output .= PMA_generate_common_hidden_inputs($_form_params);
236 $titles['Browse'] = PMA_Util::getIcon('b_browse.png', __('Browse foreign values'));
238 // user can toggle the display of Function column and column types
239 // (currently does not work for multi-edits)
240 if (! $cfg['ShowFunctionFields'] || ! $cfg['ShowFieldTypesInDataEditView']) {
241 $html_output .= __('Show');
244 if (! $cfg['ShowFunctionFields']) {
245 $html_output .= PMA_showFunctionFieldsInEditMode($url_params, false);
248 if (! $cfg['ShowFieldTypesInDataEditView']) {
249 $html_output .= PMA_showColumnTypesInDataEditView($url_params, false);
252 foreach ($rows as $row_id => $current_row) {
253 if ($current_row === false) {
254 unset($current_row);
257 $jsvkey = $row_id;
258 $rownumber_param = '&amp;rownumber=' . $row_id;
259 $vkey = '[multi_edit][' . $jsvkey . ']';
261 $current_result = (isset($result) && is_array($result) && isset($result[$row_id])
262 ? $result[$row_id]
263 : $result);
264 if ($insert_mode && $row_id > 0) {
265 $html_output .= '<input type="checkbox" checked="checked"'
266 . ' name="insert_ignore_' . $row_id . '"'
267 . ' id="insert_ignore_' . $row_id . '" />'
268 .'<label for="insert_ignore_' . $row_id . '">'
269 . __('Ignore')
270 . '</label><br />' . "\n";
273 $html_output .= PMA_getHeadAndFootOfInsertRowTable($url_params)
274 . '<tbody>';
276 // Sets a multiplier used for input-field counts
277 // (as zero cannot be used, advance the counter plus one)
278 $m_rows = $o_rows + 1;
279 //store the default value for CharEditing
280 $default_char_editing = $cfg['CharEditing'];
282 $odd_row = true;
283 for ($i = 0; $i < $columns_cnt; $i++) {
284 if (! isset($table_fields[$i]['processed'])) {
285 $column = $table_fields[$i];
286 $column = PMA_analyzeTableColumnsArray(
287 $column, $comments_map, $timestamp_seen
291 $extracted_columnspec
292 = PMA_Util::extractColumnSpec($column['Type']);
294 if (-1 === $column['len']) {
295 $column['len'] = PMA_DBI_field_len($current_result, $i);
296 // length is unknown for geometry fields,
297 // make enough space to edit very simple WKTs
298 if (-1 === $column['len']) {
299 $column['len'] = 30;
302 //Call validation when the form submited...
303 $unnullify_trigger = $chg_evt_handler
304 . "=\"return verificationsAfterFieldChange('"
305 . PMA_escapeJsString($column['Field_md5']) . "', '"
306 . PMA_escapeJsString($jsvkey) . "','".$column['pma_type'] . "')\"";
308 // Use an MD5 as an array index to avoid having special characters
309 // in the name atttibute (see bug #1746964 )
310 $column_name_appendix = $vkey . '[' . $column['Field_md5'] . ']';
312 if ($column['Type'] == 'datetime'
313 && ! isset($column['Default'])
314 && ! is_null($column['Default'])
315 && ($insert_mode || ! isset($current_row[$column['Field']]))
317 // INSERT case or
318 // UPDATE case with an NULL value
319 $current_row[$column['Field']] = date('Y-m-d H:i:s', time());
322 $html_output .= '<tr class="noclick ' . ($odd_row ? 'odd' : 'even' ) . '">'
323 . '<td ' . ($cfg['LongtextDoubleTextarea'] && strstr($column['True_Type'], 'longtext') ? 'rowspan="2"' : '') . 'class="center">'
324 . $column['Field_title']
325 . '<input type="hidden" name="fields_name' . $column_name_appendix . '" value="' . $column['Field_html'] . '"/>'
326 . '</td>';
327 if ($cfg['ShowFieldTypesInDataEditView']) {
328 $html_output .= '<td class="center' . $column['wrap'] . '">'
329 . '<span class="column_type">' . $column['pma_type'] . '</span>'
330 . '</td>';
331 } //End if
333 // Get a list of GIS data types.
334 $gis_data_types = PMA_Util::getGISDatatypes();
336 // Prepares the field value
337 $real_null_value = false;
338 $special_chars_encoded = '';
339 if (isset($current_row)) {
340 // (we are editing)
341 list(
342 $real_null_value, $special_chars_encoded, $special_chars,
343 $data, $backup_field
345 = PMA_getSpecialCharsAndBackupFieldForExistingRow(
346 $current_row, $column, $extracted_columnspec,
347 $real_null_value, $gis_data_types, $column_name_appendix
349 } else {
350 // (we are inserting)
351 // display default values
352 list($real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded)
353 = PMA_getSpecialCharsAndBackupFieldForInsertingMode($column, $real_null_value);
356 $idindex = ($o_rows * $columns_cnt) + $i + 1;
357 $tabindex = $idindex;
359 // Get a list of data types that are not yet supported.
360 $no_support_types = PMA_Util::unsupportedDatatypes();
362 // The function column
363 // -------------------
364 if ($cfg['ShowFunctionFields']) {
365 $html_output .= PMA_getFunctionColumn(
366 $column, $is_upload, $column_name_appendix,
367 $unnullify_trigger, $no_support_types, $tabindex_for_function,
368 $tabindex, $idindex, $insert_mode
372 // The null column
373 // ---------------
374 $foreignData = PMA_getForeignData(
375 $foreigners, $column['Field'], false, '', ''
377 $html_output .= PMA_getNullColumn(
378 $column, $column_name_appendix, $real_null_value,
379 $tabindex, $tabindex_for_null, $idindex, $vkey, $foreigners,
380 $foreignData
383 // The value column (depends on type)
384 // ----------------
385 // See bug #1667887 for the reason why we don't use the maxlength
386 // HTML attribute
387 $html_output .= ' <td>' . "\n";
388 // Will be used by js/tbl_change.js to set the default value
389 // for the "Continue insertion" feature
390 $html_output .= '<span class="default_value hide">'
391 . $special_chars . '</span>';
393 $html_output .= PMA_getValueColumn(
394 $column, $backup_field, $column_name_appendix, $unnullify_trigger,
395 $tabindex, $tabindex_for_value, $idindex, $data, $special_chars,
396 $foreignData, $odd_row, $paramTableDbArray, $rownumber_param, $titles,
397 $text_dir, $special_chars_encoded, $vkey, $is_upload,
398 $biggest_max_file_size, $default_char_editing,
399 $no_support_types, $gis_data_types, $extracted_columnspec
402 $html_output .= '</td>'
403 . '</tr>';
405 $odd_row = !$odd_row;
406 } // end for
407 $o_rows++;
408 $html_output .= ' </tbody>'
409 . '</table><br />';
410 } // end foreach on multi-edit
412 $html_output .='<div id="gis_editor"></div>'
413 . '<div id="popup_background"></div>'
414 . '<br />';
416 if (! isset($after_insert)) {
417 $after_insert = 'back';
420 //action panel
421 $html_output .= PMA_getActionsPanel(
422 $where_clause, $after_insert, $tabindex,
423 $tabindex_for_value, $found_unique_key
426 if ($biggest_max_file_size > 0) {
427 $html_output .= ' '
428 . PMA_Util::generateHiddenMaxFileSize(
429 $biggest_max_file_size
430 ) . "\n";
432 $html_output .= '</form>';
433 // end Insert/Edit form
435 if ($insert_mode) {
436 //Continue insertion form
437 $html_output .= PMA_getContinueInsertionForm(
438 $table, $db, $where_clause_array, $err_url
441 $response->addHTML($html_output);