2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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
14 use PMA\libraries\plugins\IOTransformationsPlugin
;
15 use PMA\libraries\Table
;
18 * Gets some core libraries
20 require_once 'libraries/common.inc.php';
23 * functions implementation for this script
25 require_once 'libraries/insert_edit.lib.php';
26 require_once 'libraries/transformations.lib.php';
29 PMA\libraries\Util
::checkParameters(array('db', 'table', 'goto'));
31 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
34 * Initializes some variables
36 $goto_include = false;
38 $response = PMA\libraries\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
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)
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 PMA_setSessionForEditNext($one_where_clause);
65 //get $goto_include for different cases
66 $goto_include = PMA_getGotoInclude($goto_include);
68 // Defines the url to return in case of failure of the query
69 $err_url = PMA_getErrorUrl($url_params);
72 * Prepares the update/insert of a row
74 list($loop_array, $using_key, $is_insert, $is_insertignore)
75 = PMA_getParamsForUpdateOrInsert();
78 $value_sets = array();
79 $func_no_param = array(
101 $func_optional_param = array(
106 $gis_from_text_functions = array(
117 $gis_from_wkb_functions = array(
128 //if some posted fields need to be transformed.
129 $mime_map = PMA_getMIME($GLOBALS['db'], $GLOBALS['table']);
130 if ($mime_map === false) {
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])) {
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
149 = isset($_REQUEST['fields']['multi_edit'][$rownumber])
150 ?
$_REQUEST['fields']['multi_edit'][$rownumber]
152 $multi_edit_columns_name
153 = isset($_REQUEST['fields_name']['multi_edit'][$rownumber])
154 ?
$_REQUEST['fields_name']['multi_edit'][$rownumber]
156 $multi_edit_columns_prev
157 = isset($_REQUEST['fields_prev']['multi_edit'][$rownumber])
158 ?
$_REQUEST['fields_prev']['multi_edit'][$rownumber]
161 = isset($_REQUEST['funcs']['multi_edit'][$rownumber])
162 ?
$_REQUEST['funcs']['multi_edit'][$rownumber]
165 = isset($_REQUEST['salt']['multi_edit'][$rownumber])
166 ?
$_REQUEST['salt']['multi_edit'][$rownumber]
168 $multi_edit_columns_type
169 = isset($_REQUEST['fields_type']['multi_edit'][$rownumber])
170 ?
$_REQUEST['fields_type']['multi_edit'][$rownumber]
172 $multi_edit_columns_null
173 = isset($_REQUEST['fields_null']['multi_edit'][$rownumber])
174 ?
$_REQUEST['fields_null']['multi_edit'][$rownumber]
176 $multi_edit_columns_null_prev
177 = isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber])
178 ?
$_REQUEST['fields_null_prev']['multi_edit'][$rownumber]
180 $multi_edit_auto_increment
181 = isset($_REQUEST['auto_increment']['multi_edit'][$rownumber])
182 ?
$_REQUEST['auto_increment']['multi_edit'][$rownumber]
185 // When a select field is nullified, it's not present in $_REQUEST
186 // so initialize it; this way, the foreach($multi_edit_columns) will process it
187 foreach ($multi_edit_columns_name as $key => $val) {
188 if (! isset($multi_edit_columns[$key])) {
189 $multi_edit_columns[$key] = '';
193 // Iterate in the order of $multi_edit_columns_name,
194 // not $multi_edit_columns, to avoid problems
195 // when inserting multiple entries
196 $insert_fail = false;
197 foreach ($multi_edit_columns_name as $key => $column_name) {
198 $current_value = $multi_edit_columns[$key];
199 // Note: $key is an md5 of the fieldname. The actual fieldname is
200 // available in $multi_edit_columns_name[$key]
202 $file_to_insert = new PMA\libraries\
File();
203 $file_to_insert->checkTblChangeForm($key, $rownumber);
205 $possibly_uploaded_val = $file_to_insert->getContent();
206 if ($possibly_uploaded_val !== false) {
207 $current_value = $possibly_uploaded_val;
209 // Apply Input Transformation if defined
210 if (!empty($mime_map[$column_name])
211 && !empty($mime_map[$column_name]['input_transformation'])
213 $filename = 'libraries/plugins/transformations/'
214 . $mime_map[$column_name]['input_transformation'];
215 if (is_file($filename)) {
216 include_once $filename;
217 $classname = PMA_getTransformationClassName($filename);
218 /** @var IOTransformationsPlugin $transformation_plugin */
219 $transformation_plugin = new $classname();
220 $transformation_options = PMA_Transformation_getOptions(
221 $mime_map[$column_name]['input_transformation_options']
223 $current_value = $transformation_plugin->applyTransformation(
224 $current_value, $transformation_options
226 // check if transformation was successful or not
227 // and accordingly set error messages & insert_fail
228 if (method_exists($transformation_plugin, 'isSuccess')
229 && !$transformation_plugin->isSuccess()
233 $insert_errors[] = sprintf(
234 __('Row: %1$s, Column: %2$s, Error: %3$s'),
235 $rownumber, $column_name,
236 $transformation_plugin->getError()
242 if ($file_to_insert->isError()) {
243 $message .= $file_to_insert->getError();
245 // delete $file_to_insert temporary variable
246 $file_to_insert->cleanUp();
248 $current_value = PMA_getCurrentValueForDifferentTypes(
249 $possibly_uploaded_val, $key, $multi_edit_columns_type,
250 $current_value, $multi_edit_auto_increment,
251 $rownumber, $multi_edit_columns_name, $multi_edit_columns_null,
252 $multi_edit_columns_null_prev, $is_insert,
253 $using_key, $where_clause, $table
256 $current_value_as_an_array = PMA_getCurrentValueAsAnArrayForMultipleEdit(
258 $multi_edit_salt, $gis_from_text_functions, $current_value,
259 $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key
262 list($query_values, $query_fields)
263 = PMA_getQueryValuesForInsertAndUpdateInMultipleEdit(
264 $multi_edit_columns_name, $multi_edit_columns_null, $current_value,
265 $multi_edit_columns_prev, $multi_edit_funcs, $is_insert,
266 $query_values, $query_fields, $current_value_as_an_array,
267 $value_sets, $key, $multi_edit_columns_null_prev
269 if (isset($multi_edit_columns_null[$key])) {
270 $multi_edit_columns[$key] = null;
274 // temporarily store rows not inserted
275 // so that they can be populated again.
277 $unsaved_values[$rownumber] = $multi_edit_columns;
279 if (!$insert_fail && count($query_values) > 0) {
281 $value_sets[] = implode(', ', $query_values);
283 // build update query
284 $query[] = 'UPDATE ' . PMA\libraries\Util
::backquote($GLOBALS['table'])
285 . ' SET ' . implode(', ', $query_values)
286 . ' WHERE ' . $where_clause
287 . ($_REQUEST['clause_is_unique'] ?
'' : ' LIMIT 1');
290 } // end foreach ($loop_array as $where_clause)
292 $multi_edit_columns_name, $multi_edit_columns_prev, $multi_edit_funcs,
293 $multi_edit_columns_type, $multi_edit_columns_null, $func_no_param,
294 $multi_edit_auto_increment, $current_value_as_an_array, $key, $current_value,
295 $loop_array, $where_clause, $using_key, $multi_edit_columns_null_prev,
299 // Builds the sql query
300 if ($is_insert && count($value_sets) > 0) {
301 $query = PMA_buildSqlQuery($is_insertignore, $query_fields, $value_sets);
302 } elseif (empty($query) && ! isset($_REQUEST['preview_sql']) && !$row_skipped) {
303 // No change -> move back to the calling script
305 // Note: logic passes here for inline edit
306 $message = PMA\libraries\Message
::success(__('No change'));
307 $active_page = $goto_include;
308 include '' . PMA_securePath($goto_include);
311 unset($multi_edit_columns, $is_insertignore);
313 // If there is a request for SQL previewing.
314 if (isset($_REQUEST['preview_sql'])) {
315 PMA_previewSQL($query);
319 * Executes the sql query and get the result, then move back to the calling
322 list ($url_params, $total_affected_rows, $last_messages, $warning_messages,
323 $error_messages, $return_to_sql_query)
324 = PMA_executeSqlQuery($url_params, $query);
326 if ($is_insert && (count($value_sets) > 0 ||
$row_skipped)) {
327 $message = PMA\libraries\Message
::getMessageForInsertedRows(
330 $unsaved_values = array_values($unsaved_values);
332 $message = PMA\libraries\Message
::getMessageForAffectedRows(
337 $goto_include = 'tbl_change.php';
338 $message->addMessages($insert_errors, '<br />');
339 $message->isError(true);
342 $message->addMessages($last_messages, '<br />');
344 if (! empty($warning_messages)) {
345 $message->addMessages($warning_messages, '<br />');
346 $message->isError(true);
348 if (! empty($error_messages)) {
349 $message->addMessages($error_messages);
350 $message->isError(true);
353 $error_messages, $warning_messages, $total_affected_rows,
354 $last_messages, $last_message, $row_skipped, $insert_errors
358 * The following section only applies to grid editing.
359 * However, verifying isAjax() is not enough to ensure we are coming from
360 * grid editing. If we are coming from the Edit or Copy link in Browse mode,
361 * ajax_page_request is present in the POST parameters.
363 if ($response->isAjax() && ! isset($_POST['ajax_page_request'])) {
365 * If we are in grid editing, we need to process the relational and
366 * transformed fields, if they were edited. After that, output the correct
367 * link/transformed value and exit
369 * Logic taken from libraries/DisplayResults.php
372 if (isset($_REQUEST['rel_fields_list']) && $_REQUEST['rel_fields_list'] != '') {
374 $map = PMA_getForeigners($db, $table, '', 'both');
376 $relation_fields = array();
377 parse_str($_REQUEST['rel_fields_list'], $relation_fields);
379 // loop for each relation cell
380 /** @var array $relation_fields */
381 foreach ($relation_fields as $cell_index => $curr_rel_field) {
382 foreach ($curr_rel_field as $relation_field => $relation_field_value) {
383 $where_comparison = "='" . $relation_field_value . "'";
384 $dispval = PMA_getDisplayValueForForeignTableColumn(
385 $where_comparison, $map, $relation_field
388 $extra_data['relations'][$cell_index]
389 = PMA_getLinkForRelationalDisplayField(
390 $map, $relation_field, $where_comparison,
391 $dispval, $relation_field_value
394 } // end of loop for each relation cell
396 if (isset($_REQUEST['do_transformations'])
397 && $_REQUEST['do_transformations'] == true
399 $edited_values = array();
400 parse_str($_REQUEST['transform_fields_list'], $edited_values);
402 if (! isset($extra_data)) {
403 $extra_data = array();
405 $transformation_types = array(
406 "input_transformation",
409 foreach ($mime_map as $transformation) {
410 $column_name = $transformation['column_name'];
411 foreach ($transformation_types as $type) {
412 $file = PMA_securePath($transformation[$type]);
413 $extra_data = PMA_transformEditedValues(
414 $db, $table, $transformation, $edited_values, $file,
415 $column_name, $extra_data, $type
418 } // end of loop for each $mime_map
421 // Need to check the inline edited value can be truncated by MySQL
422 // without informing while saving
423 $column_name = $_REQUEST['fields_name']['multi_edit'][0][0];
425 PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData(
426 $db, $table, $column_name, $extra_data
429 /**Get the total row count of the table*/
430 $_table = new Table($_REQUEST['table'], $_REQUEST['db']);
431 $extra_data['row_count'] = $_table->countRecords();
433 $extra_data['sql_query']
434 = PMA\libraries\Util
::getMessage($message, $GLOBALS['display_query']);
436 $response = PMA\libraries\Response
::getInstance();
437 $response->setRequestStatus($message->isSuccess());
438 $response->addJSON('message', $message);
439 $response->addJSON($extra_data);
443 if (! empty($return_to_sql_query)) {
444 $disp_query = $GLOBALS['sql_query'];
445 $disp_message = $message;
447 $GLOBALS['sql_query'] = $return_to_sql_query;
450 $scripts->addFile('tbl_change.js');
451 $scripts->addFile('big_ints.js');
453 $active_page = $goto_include;
456 * If user asked for "and then Insert another new row" we have to remove
457 * WHERE clause information so that tbl_change.php does not go back
458 * to the current record
460 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
461 unset($_REQUEST['where_clause']);
467 require '' . PMA_securePath($goto_include);