2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Manipulation of table data like inserting, replacing and updating
6 * Usally 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';
26 PMA_Util
::checkParameters(array('db', 'table', 'goto'));
28 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
31 * Initializes some variables
33 $goto_include = false;
35 $response = PMA_Response
::getInstance();
36 $header = $response->getHeader();
37 $scripts = $header->getScripts();
38 $scripts->addFile('makegrid.js');
39 // Needed for generation of Inline Edit anchors
40 $scripts->addFile('sql.js');
41 $scripts->addFile('indexes.js');
42 $scripts->addFile('gis_data_editor.js');
44 // check whether insert row moode, if so include tbl_change.php
47 $after_insert_actions = array('new_insert', 'same_insert', 'edit_next');
48 if (isset($_REQUEST['after_insert'])
49 && in_array($_REQUEST['after_insert'], $after_insert_actions)
51 $url_params['after_insert'] = $_REQUEST['after_insert'];
52 if (isset($_REQUEST['where_clause'])) {
53 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
54 if ($_REQUEST['after_insert'] == 'same_insert') {
55 $url_params['where_clause'][] = $one_where_clause;
56 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
57 PMA_setSessionForEditNext($one_where_clause);
62 //get $goto_include for different cases
63 $goto_include = PMA_getGotoInclude($goto_include);
65 // Defines the url to return in case of failure of the query
66 $err_url = PMA_getErrorUrl($url_params);
69 * Prepares the update/insert of a row
71 list($loop_array, $using_key, $is_insert, $is_insertignore)
72 = PMA_getParamsForUpdateOrInsert();
75 $value_sets = array();
76 $func_no_param = array(
98 $func_optional_param = array(
103 $gis_from_text_functions = array(
114 $gis_from_wkb_functions = array(
125 // to create an object of PMA_File class
126 require_once './libraries/File.class.php';
128 $query_fields = array();
129 foreach ($loop_array as $rownumber => $where_clause) {
130 // skip fields to be ignored
131 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
135 // Defines the SET part of the sql query
136 $query_values = array();
138 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
140 = isset($_REQUEST['fields']['multi_edit'][$rownumber])
141 ?
$_REQUEST['fields']['multi_edit'][$rownumber]
143 $multi_edit_columns_name
144 = isset($_REQUEST['fields_name']['multi_edit'][$rownumber])
145 ?
$_REQUEST['fields_name']['multi_edit'][$rownumber]
147 $multi_edit_columns_prev
148 = isset($_REQUEST['fields_prev']['multi_edit'][$rownumber])
149 ?
$_REQUEST['fields_prev']['multi_edit'][$rownumber]
152 = isset($_REQUEST['funcs']['multi_edit'][$rownumber])
153 ?
$_REQUEST['funcs']['multi_edit'][$rownumber]
156 = isset($_REQUEST['salt']['multi_edit'][$rownumber])
157 ?
$_REQUEST['salt']['multi_edit'][$rownumber]
159 $multi_edit_columns_type
160 = isset($_REQUEST['fields_type']['multi_edit'][$rownumber])
161 ?
$_REQUEST['fields_type']['multi_edit'][$rownumber]
163 $multi_edit_columns_null
164 = isset($_REQUEST['fields_null']['multi_edit'][$rownumber])
165 ?
$_REQUEST['fields_null']['multi_edit'][$rownumber]
167 $multi_edit_columns_null_prev
168 = isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber])
169 ?
$_REQUEST['fields_null_prev']['multi_edit'][$rownumber]
171 $multi_edit_auto_increment
172 = isset($_REQUEST['auto_increment']['multi_edit'][$rownumber])
173 ?
$_REQUEST['auto_increment']['multi_edit'][$rownumber]
176 // When a select field is nullified, it's not present in $_REQUEST
177 // so initialize it; this way, the foreach($multi_edit_colummns) will process it
178 foreach ($multi_edit_columns_name as $key => $val) {
179 if (! isset($multi_edit_colummns[$key])) {
180 $multi_edit_colummns[$key] = '';
184 // Iterate in the order of $multi_edit_columns_name,
185 // not $multi_edit_colummns, to avoid problems
186 // when inserting multiple entries
187 foreach ($multi_edit_columns_name as $key => $colummn_name) {
188 $current_value = $multi_edit_colummns[$key];
189 // Note: $key is an md5 of the fieldname. The actual fieldname is
190 // available in $multi_edit_columns_name[$key]
192 $file_to_insert = new PMA_File();
193 $file_to_insert->checkTblChangeForm($key, $rownumber);
195 $possibly_uploaded_val = $file_to_insert->getContent();
197 if ($file_to_insert->isError()) {
198 $message .= $file_to_insert->getError();
200 // delete $file_to_insert temporary variable
201 $file_to_insert->cleanUp();
203 $current_value = PMA_getCurrentValueForDifferentTypes(
204 $possibly_uploaded_val, $key, $multi_edit_columns_type,
205 $current_value, $multi_edit_auto_increment,
206 $rownumber, $multi_edit_columns_name, $multi_edit_columns_null,
207 $multi_edit_columns_null_prev, $is_insert,
208 $using_key, $where_clause, $table
211 $current_value_as_an_array = PMA_getCurrentValueAsAnArrayForMultipleEdit(
212 $multi_edit_colummns, $multi_edit_columns_name, $multi_edit_funcs,
213 $multi_edit_salt, $gis_from_text_functions, $current_value,
214 $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key
217 list($query_values, $query_fields)
218 = PMA_getQueryValuesForInsertAndUpdateInMultipleEdit(
219 $multi_edit_columns_name, $multi_edit_columns_null, $current_value,
220 $multi_edit_columns_prev, $multi_edit_funcs, $is_insert,
221 $query_values, $query_fields, $current_value_as_an_array,
222 $value_sets, $key, $multi_edit_columns_null_prev
226 if (count($query_values) > 0) {
228 $value_sets[] = implode(', ', $query_values);
230 // build update query
231 $query[] = 'UPDATE ' . PMA_Util
::backquote($GLOBALS['db'])
232 . '.' . PMA_Util
::backquote($GLOBALS['table'])
233 . ' SET ' . implode(', ', $query_values)
234 . ' WHERE ' . $where_clause
235 . ($_REQUEST['clause_is_unique'] ?
'' : ' LIMIT 1');
238 } // end foreach ($loop_array as $where_clause)
239 unset($multi_edit_columns_name, $multi_edit_columns_prev, $multi_edit_funcs,
240 $multi_edit_columns_type, $multi_edit_columns_null, $func_no_param,
241 $multi_edit_auto_increment, $current_value_as_an_array, $key, $current_value,
242 $loop_array, $where_clause, $using_key, $multi_edit_columns_null_prev);
244 // Builds the sql query
245 if ($is_insert && count($value_sets) > 0) {
246 $query = PMA_buildSqlQuery($is_insertignore, $query_fields, $value_sets);
247 } elseif (empty($query)) {
248 // No change -> move back to the calling script
250 // Note: logic passes here for inline edit
251 $message = PMA_Message
::success(__('No change'));
252 $active_page = $goto_include;
253 include '' . PMA_securePath($goto_include);
256 unset($multi_edit_colummns, $is_insertignore);
259 * Executes the sql query and get the result, then move back to the calling
262 list ($url_params, $total_affected_rows, $last_messages, $warning_messages,
263 $error_messages, $return_to_sql_query)
264 = PMA_executeSqlQuery($url_params, $query);
266 if ($is_insert && count($value_sets) > 0) {
267 $message = PMA_Message
::getMessageForInsertedRows($total_affected_rows);
269 $message = PMA_Message
::getMessageForAffectedRows($total_affected_rows);
272 $message->addMessages($last_messages, '<br />');
274 if (! empty($warning_messages)) {
275 $message->addMessages($warning_messages, '<br />');
276 $message->isError(true);
278 if (! empty($error_messages)) {
279 $message->addMessages($error_messages);
280 $message->isError(true);
283 $error_messages, $warning_messages, $total_affected_rows,
284 $last_messages, $last_message
288 * The following section only applies to grid editing.
289 * However, verifying isAjax() is not enough to ensure we are coming from
290 * grid editing. If we are coming from the Edit or Copy link in Browse mode,
291 * ajax_page_request is present in the POST parameters.
293 if ($response->isAjax() && ! isset($_POST['ajax_page_request'])) {
295 * If we are in grid editing, we need to process the relational and
296 * transformed fields, if they were edited. After that, output the correct
297 * link/transformed value and exit
299 * Logic taken from libraries/DisplayResults.class.php
302 if (isset($_REQUEST['rel_fields_list']) && $_REQUEST['rel_fields_list'] != '') {
304 $map = PMA_getForeigners($db, $table, '', 'both');
306 $relation_fields = array();
307 parse_str($_REQUEST['rel_fields_list'], $relation_fields);
309 // loop for each relation cell
310 foreach ($relation_fields as $cell_index => $curr_cell_rel_field) {
311 foreach ($curr_cell_rel_field as $relation_field => $relation_field_value) {
312 $where_comparison = "='" . $relation_field_value . "'";
313 $dispval = PMA_getDisplayValueForForeignTableColumn(
314 $where_comparison, $relation_field_value, $map, $relation_field
317 $extra_data['relations'][$cell_index] = PMA_getLinkForRelationalDisplayField(
318 $map, $relation_field, $where_comparison,
319 $dispval, $relation_field_value
322 } // end of loop for each relation cell
324 if (isset($_REQUEST['do_transformations'])
325 && $_REQUEST['do_transformations'] == true
327 include_once 'libraries/transformations.lib.php';
328 //if some posted fields need to be transformed, generate them here.
329 $mime_map = PMA_getMIME($db, $table);
331 if ($mime_map === false) {
334 $edited_values = array();
335 parse_str($_REQUEST['transform_fields_list'], $edited_values);
337 if (! isset($extra_data)) {
338 $extra_data = array();
340 foreach ($mime_map as $transformation) {
341 $file = PMA_securePath($transformation['transformation']);
342 // if only an underscore in the file name, nothing to transform
344 $column_name = $transformation['column_name'];
345 $extra_data = PMA_transformEditedValues(
346 $db, $table, $transformation, $edited_values, $file,
347 $column_name, $extra_data
350 } // end of loop for each $mime_map
353 // Need to check the inline edited value can be truncated by MySQL
354 // without informing while saving
355 $column_name = $_REQUEST['fields_name']['multi_edit'][0][0];
357 PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData(
358 $db, $table, $column_name, $extra_data
361 /**Get the total row count of the table*/
362 $extra_data['row_count'] = PMA_Table
::countRecords(
363 $_REQUEST['db'], $_REQUEST['table']
366 $extra_data['sql_query']
367 = PMA_Util
::getMessage($message, $GLOBALS['display_query']);
369 $response = PMA_Response
::getInstance();
370 $response->isSuccess($message->isSuccess());
371 $response->addJSON('message', $message);
372 $response->addJSON($extra_data);
376 if (! empty($return_to_sql_query)) {
377 $disp_query = $GLOBALS['sql_query'];
378 $disp_message = $message;
380 $GLOBALS['sql_query'] = $return_to_sql_query;
383 $scripts->addFile('tbl_change.js');
385 $active_page = $goto_include;
388 * If user asked for "and then Insert another new row" we have to remove
389 * WHERE clause information so that tbl_change.php does not go back
390 * to the current record
392 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
393 unset($_REQUEST['where_clause']);
399 require '' . PMA_securePath($goto_include);