tbl_change.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Displays form for editing and inserting new table rows
  5. *
  6. * register_globals_save (mark this file save for disabling register globals)
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. /**
  11. * Gets the variables sent or posted to this script and displays the header
  12. */
  13. require_once 'libraries/common.inc.php';
  14. /**
  15. * Ensures db and table are valid, else moves to the "parent" script
  16. */
  17. require_once 'libraries/db_table_exists.lib.php';
  18. /**
  19. * functions implementation for this script
  20. */
  21. require_once 'libraries/insert_edit.lib.php';
  22. /**
  23. * Sets global variables.
  24. * Here it's better to use a if, instead of the '?' operator
  25. * to avoid setting a variable to '' when it's not present in $_REQUEST
  26. */
  27. if (isset($_REQUEST['where_clause'])) {
  28. $where_clause = $_REQUEST['where_clause'];
  29. }
  30. if (isset($_SESSION['edit_next'])) {
  31. $where_clause = $_SESSION['edit_next'];
  32. unset($_SESSION['edit_next']);
  33. $after_insert = 'edit_next';
  34. }
  35. if (isset($_REQUEST['ShowFunctionFields'])) {
  36. $cfg['ShowFunctionFields'] = $_REQUEST['ShowFunctionFields'];
  37. }
  38. if (isset($_REQUEST['ShowFieldTypesInDataEditView'])) {
  39. $cfg['ShowFieldTypesInDataEditView'] = $_REQUEST['ShowFieldTypesInDataEditView'];
  40. }
  41. if (isset($_REQUEST['after_insert'])) {
  42. $after_insert = $_REQUEST['after_insert'];
  43. }
  44. /**
  45. * file listing
  46. */
  47. require_once 'libraries/file_listing.lib.php';
  48. /**
  49. * Defines the url to return to in case of error in a sql statement
  50. * (at this point, $GLOBALS['goto'] will be set but could be empty)
  51. */
  52. if (empty($GLOBALS['goto'])) {
  53. if (strlen($table)) {
  54. // avoid a problem (see bug #2202709)
  55. $GLOBALS['goto'] = 'tbl_sql.php';
  56. } else {
  57. $GLOBALS['goto'] = 'db_sql.php';
  58. }
  59. }
  60. /**
  61. * @todo check if we could replace by "db_|tbl_" - please clarify!?
  62. */
  63. $_url_params = array(
  64. 'db' => $db,
  65. 'sql_query' => $_REQUEST['sql_query']
  66. );
  67. if (preg_match('@^tbl_@', $GLOBALS['goto'])) {
  68. $_url_params['table'] = $table;
  69. }
  70. $err_url = $GLOBALS['goto'] . PMA_generate_common_url($_url_params);
  71. unset($_url_params);
  72. /**
  73. * Sets parameters for links
  74. * where is this variable used?
  75. * replace by PMA_generate_common_url($url_params);
  76. */
  77. $url_query = PMA_generate_common_url($url_params, 'html', '');
  78. /**
  79. * get table information
  80. * @todo should be done by a Table object
  81. */
  82. require_once 'libraries/tbl_info.inc.php';
  83. /**
  84. * Get comments for table fileds/columns
  85. */
  86. $comments_map = array();
  87. if ($GLOBALS['cfg']['ShowPropertyComments']) {
  88. $comments_map = PMA_getComments($db, $table);
  89. }
  90. /**
  91. * START REGULAR OUTPUT
  92. */
  93. /**
  94. * Load JavaScript files
  95. */
  96. $response = PMA_Response::getInstance();
  97. $header = $response->getHeader();
  98. $scripts = $header->getScripts();
  99. $scripts->addFile('functions.js');
  100. $scripts->addFile('tbl_change.js');
  101. $scripts->addFile('jquery/jquery-ui-timepicker-addon.js');
  102. $scripts->addFile('gis_data_editor.js');
  103. /**
  104. * Displays the query submitted and its result
  105. *
  106. * @todo where does $disp_message and $disp_query come from???
  107. */
  108. if (! empty($disp_message)) {
  109. if (! isset($disp_query)) {
  110. $disp_query = null;
  111. }
  112. $response->addHTML(PMA_Util::getMessage($disp_message, $disp_query));
  113. }
  114. /**
  115. * Get the analysis of SHOW CREATE TABLE for this table
  116. */
  117. $analyzed_sql = PMA_Table::analyzeStructure($db, $table);
  118. /**
  119. * Get the list of the fields of the current table
  120. */
  121. PMA_DBI_select_db($db);
  122. $table_fields = array_values(PMA_DBI_get_columns($db, $table));
  123. $paramTableDbArray = array($table, $db);
  124. /**
  125. * Determine what to do, edit or insert?
  126. */
  127. if (isset($where_clause)) {
  128. // we are editing
  129. $insert_mode = false;
  130. $where_clause_array = PMA_getWhereClauseArray($where_clause);
  131. list($where_clauses, $result, $rows, $found_unique_key)
  132. = PMA_analyzeWhereClauses($where_clause_array, $table, $db);
  133. } else {
  134. // we are inserting
  135. $insert_mode = true;
  136. $where_clause = null;
  137. list($result, $rows) = PMA_loadFirstRow($table, $db);
  138. $where_clauses = null;
  139. $where_clause_array = null;
  140. $found_unique_key = false;
  141. }
  142. // Copying a row - fetched data will be inserted as a new row,
  143. // therefore the where clause is needless.
  144. if (isset($_REQUEST['default_action']) && $_REQUEST['default_action'] === 'insert') {
  145. $where_clause = $where_clauses = null;
  146. }
  147. // retrieve keys into foreign fields, if any
  148. $foreigners = PMA_getForeigners($db, $table);
  149. // Retrieve form parameters for insert/edit form
  150. $_form_params = PMA_getFormParametersForInsertForm(
  151. $db, $table, $where_clauses, $where_clause_array, $err_url
  152. );
  153. /**
  154. * Displays the form
  155. */
  156. // autocomplete feature of IE kills the "onchange" event handler and it
  157. // must be replaced by the "onpropertychange" one in this case
  158. $chg_evt_handler = (PMA_USR_BROWSER_AGENT == 'IE'
  159. && PMA_USR_BROWSER_VER >= 5
  160. && PMA_USR_BROWSER_VER < 7
  161. )
  162. ? 'onpropertychange'
  163. : 'onchange';
  164. // Had to put the URI because when hosted on an https server,
  165. // some browsers send wrongly this form to the http server.
  166. $html_output = '';
  167. // Set if we passed the first timestamp field
  168. $timestamp_seen = false;
  169. $columns_cnt = count($table_fields);
  170. $tabindex = 0;
  171. $tabindex_for_function = +3000;
  172. $tabindex_for_null = +6000;
  173. $tabindex_for_value = 0;
  174. $o_rows = 0;
  175. $biggest_max_file_size = 0;
  176. $url_params['db'] = $db;
  177. $url_params['table'] = $table;
  178. $url_params = PMA_urlParamsInEditMode(
  179. $url_params, $where_clause_array, $where_clause
  180. );
  181. //Insert/Edit form
  182. //If table has blob fields we have to disable ajax.
  183. $has_blob_field = false;
  184. foreach ($table_fields as $column) {
  185. if (PMA_isColumnBlob($column)) {
  186. $has_blob_field = true;
  187. break;
  188. }
  189. }
  190. $html_output .='<form id="insertForm" ';
  191. if ($has_blob_field && $is_upload) {
  192. $html_output .='class="disableAjax" ';
  193. }
  194. $html_output .='method="post" action="tbl_replace.php" name="insertForm" ';
  195. if ($is_upload) {
  196. $html_output .= ' enctype="multipart/form-data"';
  197. }
  198. $html_output .= '>';
  199. $html_output .= PMA_generate_common_hidden_inputs($_form_params);
  200. $titles['Browse'] = PMA_Util::getIcon('b_browse.png', __('Browse foreign values'));
  201. // user can toggle the display of Function column and column types
  202. // (currently does not work for multi-edits)
  203. if (! $cfg['ShowFunctionFields'] || ! $cfg['ShowFieldTypesInDataEditView']) {
  204. $html_output .= __('Show');
  205. }
  206. if (! $cfg['ShowFunctionFields']) {
  207. $html_output .= PMA_showFunctionFieldsInEditMode($url_params, false);
  208. }
  209. if (! $cfg['ShowFieldTypesInDataEditView']) {
  210. $html_output .= PMA_showColumnTypesInDataEditView($url_params, false);
  211. }
  212. foreach ($rows as $row_id => $current_row) {
  213. if ($current_row === false) {
  214. unset($current_row);
  215. }
  216. $jsvkey = $row_id;
  217. $rownumber_param = '&amp;rownumber=' . $row_id;
  218. $vkey = '[multi_edit][' . $jsvkey . ']';
  219. $current_result = (isset($result) && is_array($result) && isset($result[$row_id])
  220. ? $result[$row_id]
  221. : $result);
  222. if ($insert_mode && $row_id > 0) {
  223. $html_output .= '<input type="checkbox" checked="checked"'
  224. . ' name="insert_ignore_' . $row_id . '"'
  225. . ' id="insert_ignore_' . $row_id . '" />'
  226. .'<label for="insert_ignore_' . $row_id . '">'
  227. . __('Ignore')
  228. . '</label><br />' . "\n";
  229. }
  230. $html_output .= PMA_getHeadAndFootOfInsertRowTable($url_params)
  231. . '<tbody>';
  232. // Sets a multiplier used for input-field counts
  233. // (as zero cannot be used, advance the counter plus one)
  234. $m_rows = $o_rows + 1;
  235. //store the default value for CharEditing
  236. $default_char_editing = $cfg['CharEditing'];
  237. $odd_row = true;
  238. for ($i = 0; $i < $columns_cnt; $i++) {
  239. if (! isset($table_fields[$i]['processed'])) {
  240. $column = $table_fields[$i];
  241. $column = PMA_analyzeTableColumnsArray(
  242. $column, $comments_map, $timestamp_seen
  243. );
  244. }
  245. $extracted_columnspec
  246. = PMA_Util::extractColumnSpec($column['Type']);
  247. if (-1 === $column['len']) {
  248. $column['len'] = PMA_DBI_field_len($current_result, $i);
  249. // length is unknown for geometry fields,
  250. // make enough space to edit very simple WKTs
  251. if (-1 === $column['len']) {
  252. $column['len'] = 30;
  253. }
  254. }
  255. //Call validation when the form submited...
  256. $unnullify_trigger = $chg_evt_handler
  257. . "=\"return verificationsAfterFieldChange('"
  258. . PMA_escapeJsString($column['Field_md5']) . "', '"
  259. . PMA_escapeJsString($jsvkey) . "','".$column['pma_type'] . "')\"";
  260. // Use an MD5 as an array index to avoid having special characters
  261. // in the name atttibute (see bug #1746964 )
  262. $column_name_appendix = $vkey . '[' . $column['Field_md5'] . ']';
  263. if ($column['Type'] == 'datetime'
  264. && ! isset($column['Default'])
  265. && ! is_null($column['Default'])
  266. && ($insert_mode || ! isset($current_row[$column['Field']]))
  267. ) {
  268. // INSERT case or
  269. // UPDATE case with an NULL value
  270. $current_row[$column['Field']] = date('Y-m-d H:i:s', time());
  271. }
  272. $html_output .= '<tr class="noclick ' . ($odd_row ? 'odd' : 'even' ) . '">'
  273. . '<td ' . ($cfg['LongtextDoubleTextarea'] && strstr($column['True_Type'], 'longtext') ? 'rowspan="2"' : '') . 'class="center">'
  274. . $column['Field_title']
  275. . '<input type="hidden" name="fields_name' . $column_name_appendix . '" value="' . $column['Field_html'] . '"/>'
  276. . '</td>';
  277. if ($cfg['ShowFieldTypesInDataEditView']) {
  278. $html_output .= '<td class="center' . $column['wrap'] . '">'
  279. . '<span class="column_type">' . $column['pma_type'] . '</span>'
  280. . '</td>';
  281. } //End if
  282. // Get a list of GIS data types.
  283. $gis_data_types = PMA_Util::getGISDatatypes();
  284. // Prepares the field value
  285. $real_null_value = false;
  286. $special_chars_encoded = '';
  287. if (isset($current_row)) {
  288. // (we are editing)
  289. list(
  290. $real_null_value, $special_chars_encoded, $special_chars,
  291. $data, $backup_field
  292. )
  293. = PMA_getSpecialCharsAndBackupFieldForExistingRow(
  294. $current_row, $column, $extracted_columnspec,
  295. $real_null_value, $gis_data_types, $column_name_appendix
  296. );
  297. } else {
  298. // (we are inserting)
  299. // display default values
  300. list($real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded)
  301. = PMA_getSpecialCharsAndBackupFieldForInsertingMode($column, $real_null_value);
  302. }
  303. $idindex = ($o_rows * $columns_cnt) + $i + 1;
  304. $tabindex = $idindex;
  305. // Get a list of data types that are not yet supported.
  306. $no_support_types = PMA_Util::unsupportedDatatypes();
  307. // The function column
  308. // -------------------
  309. if ($cfg['ShowFunctionFields']) {
  310. $html_output .= PMA_getFunctionColumn(
  311. $column, $is_upload, $column_name_appendix,
  312. $unnullify_trigger, $no_support_types, $tabindex_for_function,
  313. $tabindex, $idindex, $insert_mode
  314. );
  315. }
  316. // The null column
  317. // ---------------
  318. $foreignData = PMA_getForeignData(
  319. $foreigners, $column['Field'], false, '', ''
  320. );
  321. $html_output .= PMA_getNullColumn(
  322. $column, $column_name_appendix, $real_null_value,
  323. $tabindex, $tabindex_for_null, $idindex, $vkey, $foreigners,
  324. $foreignData
  325. );
  326. // The value column (depends on type)
  327. // ----------------
  328. // See bug #1667887 for the reason why we don't use the maxlength
  329. // HTML attribute
  330. $html_output .= ' <td>' . "\n";
  331. // Will be used by js/tbl_change.js to set the default value
  332. // for the "Continue insertion" feature
  333. $html_output .= '<span class="default_value hide">'
  334. . $special_chars . '</span>';
  335. $html_output .= PMA_getValueColumn(
  336. $column, $backup_field, $column_name_appendix, $unnullify_trigger,
  337. $tabindex, $tabindex_for_value, $idindex, $data, $special_chars,
  338. $foreignData, $odd_row, $paramTableDbArray, $rownumber_param, $titles,
  339. $text_dir, $special_chars_encoded, $vkey, $is_upload,
  340. $biggest_max_file_size, $default_char_editing,
  341. $no_support_types, $gis_data_types, $extracted_columnspec
  342. );
  343. $html_output .= '</td>'
  344. . '</tr>';
  345. $odd_row = !$odd_row;
  346. } // end for
  347. $o_rows++;
  348. $html_output .= ' </tbody>'
  349. . '</table><br />';
  350. } // end foreach on multi-edit
  351. $html_output .='<div id="gis_editor"></div>'
  352. . '<div id="popup_background"></div>'
  353. . '<br />';
  354. if (! isset($after_insert)) {
  355. $after_insert = 'back';
  356. }
  357. //action panel
  358. $html_output .= PMA_getActionsPanel(
  359. $where_clause, $after_insert, $tabindex,
  360. $tabindex_for_value, $found_unique_key
  361. );
  362. if ($biggest_max_file_size > 0) {
  363. $html_output .= ' '
  364. . PMA_Util::generateHiddenMaxFileSize(
  365. $biggest_max_file_size
  366. ) . "\n";
  367. }
  368. $html_output .= '</form>';
  369. // end Insert/Edit form
  370. if ($insert_mode) {
  371. //Continue insertion form
  372. $html_output .= PMA_getContinueInsertionForm(
  373. $table, $db, $where_clause_array, $err_url
  374. );
  375. }
  376. $response->addHTML($html_output);
  377. ?>