Translated using Weblate (Indonesian)
[phpmyadmin.git] / tbl_replace.php
blob533064d188a2001d120bf59a171e62fd4bdb77d6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Manipulation of table data like inserting, replacing and updating
6 * Usually called as form action from tbl_change.php to insert or update table rows
8 * @todo 'edit_next' tends to not work as expected if used ...
9 * at least there is no order by it needs the original query
10 * and the row number and than replace the LIMIT clause
12 * @package PhpMyAdmin
15 use PhpMyAdmin\Core;
16 use PhpMyAdmin\InsertEdit;
17 use PhpMyAdmin\Plugins\IOTransformationsPlugin;
18 use PhpMyAdmin\Relation;
19 use PhpMyAdmin\Response;
20 use PhpMyAdmin\Table;
21 use PhpMyAdmin\Transformations;
23 /**
24 * Gets some core libraries
26 require_once 'libraries/common.inc.php';
28 // Check parameters
29 PhpMyAdmin\Util::checkParameters(array('db', 'table', 'goto'));
31 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
33 /**
34 * Initializes some variables
36 $goto_include = false;
38 $response = Response::getInstance();
39 $header = $response->getHeader();
40 $scripts = $header->getScripts();
41 $scripts->addFile('makegrid.js');
42 // Needed for generation of Inline Edit anchors
43 $scripts->addFile('sql.js');
44 $scripts->addFile('indexes.js');
45 $scripts->addFile('gis_data_editor.js');
47 // check whether insert row mode, if so include tbl_change.php
48 InsertEdit::isInsertRow();
50 $after_insert_actions = array('new_insert', 'same_insert', 'edit_next');
51 if (isset($_REQUEST['after_insert'])
52 && in_array($_REQUEST['after_insert'], $after_insert_actions)
53 ) {
54 $url_params['after_insert'] = $_REQUEST['after_insert'];
55 if (isset($_REQUEST['where_clause'])) {
56 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
57 if ($_REQUEST['after_insert'] == 'same_insert') {
58 $url_params['where_clause'][] = $one_where_clause;
59 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
60 InsertEdit::setSessionForEditNext($one_where_clause);
65 //get $goto_include for different cases
66 $goto_include = InsertEdit::getGotoInclude($goto_include);
68 // Defines the url to return in case of failure of the query
69 $err_url = InsertEdit::getErrorUrl($url_params);
71 /**
72 * Prepares the update/insert of a row
74 list($loop_array, $using_key, $is_insert, $is_insertignore)
75 = InsertEdit::getParamsForUpdateOrInsert();
77 $query = array();
78 $value_sets = array();
79 $func_no_param = array(
80 'CONNECTION_ID',
81 'CURRENT_USER',
82 'CURDATE',
83 'CURTIME',
84 'CURRENT_DATE',
85 'CURRENT_TIME',
86 'DATABASE',
87 'LAST_INSERT_ID',
88 'NOW',
89 'PI',
90 'RAND',
91 'SYSDATE',
92 'UNIX_TIMESTAMP',
93 'USER',
94 'UTC_DATE',
95 'UTC_TIME',
96 'UTC_TIMESTAMP',
97 'UUID',
98 'UUID_SHORT',
99 'VERSION',
101 $func_optional_param = array(
102 'RAND',
103 'UNIX_TIMESTAMP',
106 $gis_from_text_functions = array(
107 'GeomFromText',
108 'GeomCollFromText',
109 'LineFromText',
110 'MLineFromText',
111 'PointFromText',
112 'MPointFromText',
113 'PolyFromText',
114 'MPolyFromText',
117 $gis_from_wkb_functions = array(
118 'GeomFromWKB',
119 'GeomCollFromWKB',
120 'LineFromWKB',
121 'MLineFromWKB',
122 'PointFromWKB',
123 'MPointFromWKB',
124 'PolyFromWKB',
125 'MPolyFromWKB',
128 //if some posted fields need to be transformed.
129 $mime_map = Transformations::getMIME($GLOBALS['db'], $GLOBALS['table']);
130 if ($mime_map === false) {
131 $mime_map = array();
134 $query_fields = array();
135 $insert_errors = array();
136 $row_skipped = false;
137 $unsaved_values = array();
138 foreach ($loop_array as $rownumber => $where_clause) {
139 // skip fields to be ignored
140 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
141 continue;
144 // Defines the SET part of the sql query
145 $query_values = array();
147 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
148 $multi_edit_columns
149 = isset($_REQUEST['fields']['multi_edit'][$rownumber])
150 ? $_REQUEST['fields']['multi_edit'][$rownumber]
151 : array();
152 $multi_edit_columns_name
153 = isset($_REQUEST['fields_name']['multi_edit'][$rownumber])
154 ? $_REQUEST['fields_name']['multi_edit'][$rownumber]
155 : array();
156 $multi_edit_columns_prev
157 = isset($_REQUEST['fields_prev']['multi_edit'][$rownumber])
158 ? $_REQUEST['fields_prev']['multi_edit'][$rownumber]
159 : null;
160 $multi_edit_funcs
161 = isset($_REQUEST['funcs']['multi_edit'][$rownumber])
162 ? $_REQUEST['funcs']['multi_edit'][$rownumber]
163 : null;
164 $multi_edit_salt
165 = isset($_REQUEST['salt']['multi_edit'][$rownumber])
166 ? $_REQUEST['salt']['multi_edit'][$rownumber]
167 :null;
168 $multi_edit_columns_type
169 = isset($_REQUEST['fields_type']['multi_edit'][$rownumber])
170 ? $_REQUEST['fields_type']['multi_edit'][$rownumber]
171 : null;
172 $multi_edit_columns_null
173 = isset($_REQUEST['fields_null']['multi_edit'][$rownumber])
174 ? $_REQUEST['fields_null']['multi_edit'][$rownumber]
175 : null;
176 $multi_edit_columns_null_prev
177 = isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber])
178 ? $_REQUEST['fields_null_prev']['multi_edit'][$rownumber]
179 : null;
180 $multi_edit_auto_increment
181 = isset($_REQUEST['auto_increment']['multi_edit'][$rownumber])
182 ? $_REQUEST['auto_increment']['multi_edit'][$rownumber]
183 : null;
184 $multi_edit_virtual
185 = isset($_REQUEST['virtual']['multi_edit'][$rownumber])
186 ? $_REQUEST['virtual']['multi_edit'][$rownumber]
187 : null;
189 // When a select field is nullified, it's not present in $_REQUEST
190 // so initialize it; this way, the foreach($multi_edit_columns) will process it
191 foreach ($multi_edit_columns_name as $key => $val) {
192 if (! isset($multi_edit_columns[$key])) {
193 $multi_edit_columns[$key] = '';
197 // Iterate in the order of $multi_edit_columns_name,
198 // not $multi_edit_columns, to avoid problems
199 // when inserting multiple entries
200 $insert_fail = false;
201 foreach ($multi_edit_columns_name as $key => $column_name) {
202 $current_value = $multi_edit_columns[$key];
203 // Note: $key is an md5 of the fieldname. The actual fieldname is
204 // available in $multi_edit_columns_name[$key]
206 $file_to_insert = new PhpMyAdmin\File();
207 $file_to_insert->checkTblChangeForm($key, $rownumber);
209 $possibly_uploaded_val = $file_to_insert->getContent();
210 if ($possibly_uploaded_val !== false) {
211 $current_value = $possibly_uploaded_val;
213 // Apply Input Transformation if defined
214 if (!empty($mime_map[$column_name])
215 && !empty($mime_map[$column_name]['input_transformation'])
217 $filename = 'libraries/classes/Plugins/Transformations/'
218 . $mime_map[$column_name]['input_transformation'];
219 if (is_file($filename)) {
220 include_once $filename;
221 $classname = Transformations::getClassName($filename);
222 /** @var IOTransformationsPlugin $transformation_plugin */
223 $transformation_plugin = new $classname();
224 $transformation_options = Transformations::getOptions(
225 $mime_map[$column_name]['input_transformation_options']
227 $current_value = $transformation_plugin->applyTransformation(
228 $current_value, $transformation_options
230 // check if transformation was successful or not
231 // and accordingly set error messages & insert_fail
232 if (method_exists($transformation_plugin, 'isSuccess')
233 && !$transformation_plugin->isSuccess()
235 $insert_fail = true;
236 $row_skipped = true;
237 $insert_errors[] = sprintf(
238 __('Row: %1$s, Column: %2$s, Error: %3$s'),
239 $rownumber, $column_name,
240 $transformation_plugin->getError()
246 if ($file_to_insert->isError()) {
247 $insert_errors[] = $file_to_insert->getError();
249 // delete $file_to_insert temporary variable
250 $file_to_insert->cleanUp();
252 $current_value = InsertEdit::getCurrentValueForDifferentTypes(
253 $possibly_uploaded_val, $key, $multi_edit_columns_type,
254 $current_value, $multi_edit_auto_increment,
255 $rownumber, $multi_edit_columns_name, $multi_edit_columns_null,
256 $multi_edit_columns_null_prev, $is_insert,
257 $using_key, $where_clause, $table, $multi_edit_funcs
260 $current_value_as_an_array = InsertEdit::getCurrentValueAsAnArrayForMultipleEdit(
261 $multi_edit_funcs,
262 $multi_edit_salt, $gis_from_text_functions, $current_value,
263 $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key
266 if (! isset($multi_edit_virtual) || ! isset($multi_edit_virtual[$key])) {
267 list($query_values, $query_fields)
268 = InsertEdit::getQueryValuesForInsertAndUpdateInMultipleEdit(
269 $multi_edit_columns_name, $multi_edit_columns_null,
270 $current_value, $multi_edit_columns_prev, $multi_edit_funcs,
271 $is_insert, $query_values, $query_fields,
272 $current_value_as_an_array, $value_sets, $key,
273 $multi_edit_columns_null_prev
276 if (isset($multi_edit_columns_null[$key])) {
277 $multi_edit_columns[$key] = null;
279 } //end of foreach
281 // temporarily store rows not inserted
282 // so that they can be populated again.
283 if ($insert_fail) {
284 $unsaved_values[$rownumber] = $multi_edit_columns;
286 if (!$insert_fail && count($query_values) > 0) {
287 if ($is_insert) {
288 $value_sets[] = implode(', ', $query_values);
289 } else {
290 // build update query
291 $query[] = 'UPDATE ' . PhpMyAdmin\Util::backquote($GLOBALS['table'])
292 . ' SET ' . implode(', ', $query_values)
293 . ' WHERE ' . $where_clause
294 . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1');
297 } // end foreach ($loop_array as $where_clause)
298 unset(
299 $multi_edit_columns_name, $multi_edit_columns_prev, $multi_edit_funcs,
300 $multi_edit_columns_type, $multi_edit_columns_null, $func_no_param,
301 $multi_edit_auto_increment, $current_value_as_an_array, $key, $current_value,
302 $loop_array, $where_clause, $using_key, $multi_edit_columns_null_prev,
303 $insert_fail
306 // Builds the sql query
307 if ($is_insert && count($value_sets) > 0) {
308 $query = InsertEdit::buildSqlQuery($is_insertignore, $query_fields, $value_sets);
309 } elseif (empty($query) && ! isset($_REQUEST['preview_sql']) && !$row_skipped) {
310 // No change -> move back to the calling script
312 // Note: logic passes here for inline edit
313 $message = PhpMyAdmin\Message::success(__('No change'));
314 // Avoid infinite recursion
315 if ($goto_include == 'tbl_replace.php') {
316 $goto_include = 'tbl_change.php';
318 $active_page = $goto_include;
319 include '' . Core::securePath($goto_include);
320 exit;
322 unset($multi_edit_columns, $is_insertignore);
324 // If there is a request for SQL previewing.
325 if (isset($_REQUEST['preview_sql'])) {
326 Core::previewSQL($query);
330 * Executes the sql query and get the result, then move back to the calling
331 * page
333 list ($url_params, $total_affected_rows, $last_messages, $warning_messages,
334 $error_messages, $return_to_sql_query)
335 = InsertEdit::executeSqlQuery($url_params, $query);
337 if ($is_insert && (count($value_sets) > 0 || $row_skipped)) {
338 $message = PhpMyAdmin\Message::getMessageForInsertedRows(
339 $total_affected_rows
341 $unsaved_values = array_values($unsaved_values);
342 } else {
343 $message = PhpMyAdmin\Message::getMessageForAffectedRows(
344 $total_affected_rows
347 if ($row_skipped) {
348 $goto_include = 'tbl_change.php';
349 $message->addMessagesString($insert_errors, '<br />');
350 $message->isError(true);
353 $message->addMessages($last_messages, '<br />');
355 if (! empty($warning_messages)) {
356 $message->addMessagesString($warning_messages, '<br />');
357 $message->isError(true);
359 if (! empty($error_messages)) {
360 $message->addMessagesString($error_messages);
361 $message->isError(true);
363 unset(
364 $error_messages, $warning_messages, $total_affected_rows,
365 $last_messages, $last_message, $row_skipped, $insert_errors
369 * The following section only applies to grid editing.
370 * However, verifying isAjax() is not enough to ensure we are coming from
371 * grid editing. If we are coming from the Edit or Copy link in Browse mode,
372 * ajax_page_request is present in the POST parameters.
374 if ($response->isAjax() && ! isset($_POST['ajax_page_request'])) {
376 * If we are in grid editing, we need to process the relational and
377 * transformed fields, if they were edited. After that, output the correct
378 * link/transformed value and exit
380 * Logic taken from libraries/DisplayResults.php
383 if (isset($_REQUEST['rel_fields_list']) && $_REQUEST['rel_fields_list'] != '') {
385 $map = Relation::getForeigners($db, $table, '', 'both');
387 $relation_fields = array();
388 parse_str($_REQUEST['rel_fields_list'], $relation_fields);
390 // loop for each relation cell
391 /** @var array $relation_fields */
392 foreach ($relation_fields as $cell_index => $curr_rel_field) {
393 foreach ($curr_rel_field as $relation_field => $relation_field_value) {
394 $where_comparison = "='" . $relation_field_value . "'";
395 $dispval = InsertEdit::getDisplayValueForForeignTableColumn(
396 $where_comparison, $map, $relation_field
399 $extra_data['relations'][$cell_index]
400 = InsertEdit::getLinkForRelationalDisplayField(
401 $map, $relation_field, $where_comparison,
402 $dispval, $relation_field_value
405 } // end of loop for each relation cell
407 if (isset($_REQUEST['do_transformations'])
408 && $_REQUEST['do_transformations'] == true
410 $edited_values = array();
411 parse_str($_REQUEST['transform_fields_list'], $edited_values);
413 if (! isset($extra_data)) {
414 $extra_data = array();
416 $transformation_types = array(
417 "input_transformation",
418 "transformation"
420 foreach ($mime_map as $transformation) {
421 $column_name = $transformation['column_name'];
422 foreach ($transformation_types as $type) {
423 $file = Core::securePath($transformation[$type]);
424 $extra_data = InsertEdit::transformEditedValues(
425 $db, $table, $transformation, $edited_values, $file,
426 $column_name, $extra_data, $type
429 } // end of loop for each $mime_map
432 // Need to check the inline edited value can be truncated by MySQL
433 // without informing while saving
434 $column_name = $_REQUEST['fields_name']['multi_edit'][0][0];
436 InsertEdit::verifyWhetherValueCanBeTruncatedAndAppendExtraData(
437 $db, $table, $column_name, $extra_data
440 /**Get the total row count of the table*/
441 $_table = new Table($_REQUEST['table'], $_REQUEST['db']);
442 $extra_data['row_count'] = $_table->countRecords();
444 $extra_data['sql_query']
445 = PhpMyAdmin\Util::getMessage($message, $GLOBALS['display_query']);
447 $response->setRequestStatus($message->isSuccess());
448 $response->addJSON('message', $message);
449 $response->addJSON($extra_data);
450 exit;
453 if (! empty($return_to_sql_query)) {
454 $disp_query = $GLOBALS['sql_query'];
455 $disp_message = $message;
456 unset($message);
457 $GLOBALS['sql_query'] = $return_to_sql_query;
460 $scripts->addFile('tbl_change.js');
462 $active_page = $goto_include;
465 * If user asked for "and then Insert another new row" we have to remove
466 * WHERE clause information so that tbl_change.php does not go back
467 * to the current record
469 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
470 unset($_REQUEST['where_clause']);
474 * Load target page.
476 require '' . Core::securePath($goto_include);
477 exit;