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
16 * Gets some core libraries
18 require_once 'libraries/common.inc.php';
21 * functions implementation for this script
23 require_once 'libraries/insert_edit.lib.php';
24 require_once 'libraries/transformations.lib.php';
27 PMA_Util
::checkParameters(array('db', 'table', 'goto'));
29 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
32 * Initializes some variables
34 $goto_include = false;
36 $response = PMA_Response
::getInstance();
37 $header = $response->getHeader();
38 $scripts = $header->getScripts();
39 $scripts->addFile('makegrid.js');
40 // Needed for generation of Inline Edit anchors
41 $scripts->addFile('sql.js');
42 $scripts->addFile('indexes.js');
43 $scripts->addFile('gis_data_editor.js');
45 // check whether insert row mode, if so include tbl_change.php
48 $after_insert_actions = array('new_insert', 'same_insert', 'edit_next');
49 if (isset($_REQUEST['after_insert'])
50 && in_array($_REQUEST['after_insert'], $after_insert_actions)
52 $url_params['after_insert'] = $_REQUEST['after_insert'];
53 if (isset($_REQUEST['where_clause'])) {
54 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
55 if ($_REQUEST['after_insert'] == 'same_insert') {
56 $url_params['where_clause'][] = $one_where_clause;
57 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
58 PMA_setSessionForEditNext($one_where_clause);
63 //get $goto_include for different cases
64 $goto_include = PMA_getGotoInclude($goto_include);
66 // Defines the url to return in case of failure of the query
67 $err_url = PMA_getErrorUrl($url_params);
70 * Prepares the update/insert of a row
72 list($loop_array, $using_key, $is_insert, $is_insertignore)
73 = PMA_getParamsForUpdateOrInsert();
76 $value_sets = array();
77 $func_no_param = array(
99 $func_optional_param = array(
104 $gis_from_text_functions = array(
115 $gis_from_wkb_functions = array(
126 // to create an object of PMA_File class
127 require_once './libraries/File.class.php';
129 //if some posted fields need to be transformed.
130 $mime_map = PMA_getMIME($GLOBALS['db'], $GLOBALS['table']);
131 if ($mime_map === false) {
135 $query_fields = array();
136 $insert_errors = array();
137 $row_skipped = false;
138 $unsaved_values = array();
139 foreach ($loop_array as $rownumber => $where_clause) {
140 // skip fields to be ignored
141 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
145 // Defines the SET part of the sql query
146 $query_values = array();
148 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
150 = isset($_REQUEST['fields']['multi_edit'][$rownumber])
151 ?
$_REQUEST['fields']['multi_edit'][$rownumber]
153 $multi_edit_columns_name
154 = isset($_REQUEST['fields_name']['multi_edit'][$rownumber])
155 ?
$_REQUEST['fields_name']['multi_edit'][$rownumber]
157 $multi_edit_columns_prev
158 = isset($_REQUEST['fields_prev']['multi_edit'][$rownumber])
159 ?
$_REQUEST['fields_prev']['multi_edit'][$rownumber]
162 = isset($_REQUEST['funcs']['multi_edit'][$rownumber])
163 ?
$_REQUEST['funcs']['multi_edit'][$rownumber]
166 = isset($_REQUEST['salt']['multi_edit'][$rownumber])
167 ?
$_REQUEST['salt']['multi_edit'][$rownumber]
169 $multi_edit_columns_type
170 = isset($_REQUEST['fields_type']['multi_edit'][$rownumber])
171 ?
$_REQUEST['fields_type']['multi_edit'][$rownumber]
173 $multi_edit_columns_null
174 = isset($_REQUEST['fields_null']['multi_edit'][$rownumber])
175 ?
$_REQUEST['fields_null']['multi_edit'][$rownumber]
177 $multi_edit_columns_null_prev
178 = isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber])
179 ?
$_REQUEST['fields_null_prev']['multi_edit'][$rownumber]
181 $multi_edit_auto_increment
182 = isset($_REQUEST['auto_increment']['multi_edit'][$rownumber])
183 ?
$_REQUEST['auto_increment']['multi_edit'][$rownumber]
186 // When a select field is nullified, it's not present in $_REQUEST
187 // so initialize it; this way, the foreach($multi_edit_columns) will process it
188 foreach ($multi_edit_columns_name as $key => $val) {
189 if (! isset($multi_edit_columns[$key])) {
190 $multi_edit_columns[$key] = '';
194 // Iterate in the order of $multi_edit_columns_name,
195 // not $multi_edit_columns, to avoid problems
196 // when inserting multiple entries
197 $insert_fail = false;
198 foreach ($multi_edit_columns_name as $key => $column_name) {
199 $current_value = $multi_edit_columns[$key];
200 // Note: $key is an md5 of the fieldname. The actual fieldname is
201 // available in $multi_edit_columns_name[$key]
203 $file_to_insert = new PMA_File();
204 $file_to_insert->checkTblChangeForm($key, $rownumber);
206 $possibly_uploaded_val = $file_to_insert->getContent();
207 if ($possibly_uploaded_val !== false) {
208 $current_value = $possibly_uploaded_val;
210 // Apply Input Transformation if defined
211 if (!empty($mime_map[$column_name])
212 && !empty($mime_map[$column_name]['input_transformation'])
214 $filename = 'libraries/plugins/transformations/'
215 . $mime_map[$column_name]['input_transformation'];
216 if (is_file($filename)) {
217 include_once $filename;
218 $classname = PMA_getTransformationClassName(
219 $mime_map[$column_name]['input_transformation']
221 $transformation_plugin = new $classname();
222 $transformation_options = PMA_Transformation_getOptions(
223 $mime_map[$column_name]['input_transformation_options']
225 $current_value = $transformation_plugin->applyTransformation(
226 $current_value, $transformation_options
228 // check if transformation was successful or not
229 // and accordingly set error messages & insert_fail
230 if (method_exists($transformation_plugin, 'isSuccess')
231 && !$transformation_plugin->isSuccess()
235 $insert_errors[] = sprintf(
236 __('Row: %1$s, Column: %2$s, Error: %3$s'),
237 $rownumber, $column_name,
238 $transformation_plugin->getError()
244 if ($file_to_insert->isError()) {
245 $message .= $file_to_insert->getError();
247 // delete $file_to_insert temporary variable
248 $file_to_insert->cleanUp();
250 $current_value = PMA_getCurrentValueForDifferentTypes(
251 $possibly_uploaded_val, $key, $multi_edit_columns_type,
252 $current_value, $multi_edit_auto_increment,
253 $rownumber, $multi_edit_columns_name, $multi_edit_columns_null,
254 $multi_edit_columns_null_prev, $is_insert,
255 $using_key, $where_clause, $table
258 $current_value_as_an_array = PMA_getCurrentValueAsAnArrayForMultipleEdit(
260 $multi_edit_salt, $gis_from_text_functions, $current_value,
261 $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key
264 list($query_values, $query_fields)
265 = PMA_getQueryValuesForInsertAndUpdateInMultipleEdit(
266 $multi_edit_columns_name, $multi_edit_columns_null, $current_value,
267 $multi_edit_columns_prev, $multi_edit_funcs, $is_insert,
268 $query_values, $query_fields, $current_value_as_an_array,
269 $value_sets, $key, $multi_edit_columns_null_prev
271 if (isset($multi_edit_columns_null[$key])) {
272 $multi_edit_columns[$key] = null;
276 // temporarily store rows not inserted
277 // so that they can be populated again.
279 $unsaved_values[$rownumber] = $multi_edit_columns;
281 if (!$insert_fail && count($query_values) > 0) {
283 $value_sets[] = implode(', ', $query_values);
285 // build update query
286 $query[] = 'UPDATE ' . PMA_Util
::backquote($GLOBALS['table'])
287 . ' SET ' . implode(', ', $query_values)
288 . ' WHERE ' . $where_clause
289 . ($_REQUEST['clause_is_unique'] ?
'' : ' LIMIT 1');
292 } // end foreach ($loop_array as $where_clause)
293 unset($multi_edit_columns_name, $multi_edit_columns_prev, $multi_edit_funcs,
294 $multi_edit_columns_type, $multi_edit_columns_null, $func_no_param,
295 $multi_edit_auto_increment, $current_value_as_an_array, $key, $current_value,
296 $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_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_Message
::getMessageForInsertedRows($total_affected_rows);
328 $unsaved_values = array_values($unsaved_values);
330 $message = PMA_Message
::getMessageForAffectedRows($total_affected_rows);
333 $goto_include = 'tbl_change.php';
334 $message->addMessages($insert_errors, '<br />');
335 $message->isError(true);
338 $message->addMessages($last_messages, '<br />');
340 if (! empty($warning_messages)) {
341 $message->addMessages($warning_messages, '<br />');
342 $message->isError(true);
344 if (! empty($error_messages)) {
345 $message->addMessages($error_messages);
346 $message->isError(true);
349 $error_messages, $warning_messages, $total_affected_rows,
350 $last_messages, $last_message, $row_skipped, $insert_errors
354 * The following section only applies to grid editing.
355 * However, verifying isAjax() is not enough to ensure we are coming from
356 * grid editing. If we are coming from the Edit or Copy link in Browse mode,
357 * ajax_page_request is present in the POST parameters.
359 if ($response->isAjax() && ! isset($_POST['ajax_page_request'])) {
361 * If we are in grid editing, we need to process the relational and
362 * transformed fields, if they were edited. After that, output the correct
363 * link/transformed value and exit
365 * Logic taken from libraries/DisplayResults.class.php
368 if (isset($_REQUEST['rel_fields_list']) && $_REQUEST['rel_fields_list'] != '') {
370 $map = PMA_getForeigners($db, $table, '', 'both');
372 $relation_fields = array();
373 parse_str($_REQUEST['rel_fields_list'], $relation_fields);
375 // loop for each relation cell
376 /** @var array $relation_fields */
377 foreach ($relation_fields as $cell_index => $curr_rel_field) {
378 foreach ($curr_rel_field as $relation_field => $relation_field_value) {
379 $where_comparison = "='" . $relation_field_value . "'";
380 $dispval = PMA_getDisplayValueForForeignTableColumn(
381 $where_comparison, $map, $relation_field
384 $extra_data['relations'][$cell_index]
385 = PMA_getLinkForRelationalDisplayField(
386 $map, $relation_field, $where_comparison,
387 $dispval, $relation_field_value
390 } // end of loop for each relation cell
392 if (isset($_REQUEST['do_transformations'])
393 && $_REQUEST['do_transformations'] == true
395 $edited_values = array();
396 parse_str($_REQUEST['transform_fields_list'], $edited_values);
398 if (! isset($extra_data)) {
399 $extra_data = array();
401 $transformation_types = array(
402 "input_transformation",
405 foreach ($mime_map as $transformation) {
406 $column_name = $transformation['column_name'];
407 foreach ($transformation_types as $type) {
408 $file = PMA_securePath($transformation[$type]);
409 $extra_data = PMA_transformEditedValues(
410 $db, $table, $transformation, $edited_values, $file,
411 $column_name, $extra_data, $type
414 } // end of loop for each $mime_map
417 // Need to check the inline edited value can be truncated by MySQL
418 // without informing while saving
419 $column_name = $_REQUEST['fields_name']['multi_edit'][0][0];
421 PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData(
422 $db, $table, $column_name, $extra_data
425 /**Get the total row count of the table*/
426 $_table = new PMA_Table($_REQUEST['table'], $_REQUEST['db']);
427 $extra_data['row_count'] = $_table->countRecords();
429 $extra_data['sql_query']
430 = PMA_Util
::getMessage($message, $GLOBALS['display_query']);
432 $response = PMA_Response
::getInstance();
433 $response->isSuccess($message->isSuccess());
434 $response->addJSON('message', $message);
435 $response->addJSON($extra_data);
439 if (! empty($return_to_sql_query)) {
440 $disp_query = $GLOBALS['sql_query'];
441 $disp_message = $message;
443 $GLOBALS['sql_query'] = $return_to_sql_query;
446 $scripts->addFile('tbl_change.js');
447 $scripts->addFile('big_ints.js');
449 $active_page = $goto_include;
452 * If user asked for "and then Insert another new row" we have to remove
453 * WHERE clause information so that tbl_change.php does not go back
454 * to the current record
456 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
457 unset($_REQUEST['where_clause']);
463 require '' . PMA_securePath($goto_include);