From e8bee6bd59e90814b605ee79f14b110292e0fcd4 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Sat, 9 Jun 2012 19:13:06 +0530 Subject: [PATCH 1/3] code improvement in tbl_replace-php --- libraries/insert_edit.lib.php | 127 +++++++++++++++++++++++++++++++++- tbl_change.php | 2 +- tbl_replace.php | 96 ++++--------------------- 3 files changed, 139 insertions(+), 86 deletions(-) diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php index 1a9678bf6e..4ef221561a 100644 --- a/libraries/insert_edit.lib.php +++ b/libraries/insert_edit.lib.php @@ -51,7 +51,7 @@ function PMA_getFormParametersForInsertForm($db, $table, $where_clauses, $where_ * @return type containing insert_mode,whereClauses, result array * where_clauses_array and found_unique_key boolean value */ -function PMA_getValuesForEditMode($where_clause, $table, $db) +function PMA_getStuffForEditMode($where_clause, $table, $db) { $found_unique_key = false; if (isset($where_clause)) { @@ -1665,6 +1665,7 @@ function PMA_buildSqlQuery($is_insertignore, $query_fields, $value_sets) */ function PMA_executeSqlQuery($url_params, $query) { + $return_to_sql_query = null; if (! empty($GLOBALS['sql_query'])) { $url_params['sql_query'] = $GLOBALS['sql_query']; $return_to_sql_query = $GLOBALS['sql_query']; @@ -1734,4 +1735,128 @@ function PMA_getWarningMessages() } return $warning_essages; } + +/** + * Field to display from the foreign table? + * + * @param string $where_comparison string that contain relation field value + * @param string $relation_field_value relation field value + * @param array $map all Relations to foreign tables for a + * given table or optionally a given column in a table + * @param string $relation_field relation field + * + * @return string $dispval display value from the foriegn table + */ +function PMA_displayForiengTableColumn($where_comparison, $relation_field_value, $map, $relation_field) +{ + $display_field = PMA_getDisplayField($map[$relation_field]['foreign_db'], $map[$relation_field]['foreign_table']); + // Field to display from the foreign table? + if (isset($display_field) && strlen($display_field)) { + $dispsql = 'SELECT ' . PMA_backquote($display_field) + . ' FROM ' . PMA_backquote($map[$relation_field]['foreign_db']) + . '.' . PMA_backquote($map[$relation_field]['foreign_table']) + . ' WHERE ' . PMA_backquote($map[$relation_field]['foreign_field']) + . $where_comparison; + $dispresult = PMA_DBI_try_query($dispsql, null, PMA_DBI_QUERY_STORE); + if ($dispresult && PMA_DBI_num_rows($dispresult) > 0) { + list($dispval) = PMA_DBI_fetch_row($dispresult, 0); + } + @PMA_DBI_free_result($dispresult); + return $dispval; + } + return ''; +} + +/** + * Display option in the cell according to user choises + * + * @param array $map all Relations to foreign tables for a + * given table or optionally a given column in a table + * @param string $relation_field relation field + * @param string $where_comparison string that contain relation field value + * @param string $dispval display value from the foriegn table + * @param string $relation_field_value relation field value + * + * @return string $output HTML tag + */ +function PMA_getLinkForRelationalDisplayField($map, $relation_field, $where_comparison, + $dispval, $relation_field_value +){ + if ('K' == $_SESSION['tmp_user_values']['relational_display']) { + // user chose "relational key" in the display options, so + // the title contains the display field + $title = (! empty($dispval))? ' title="' . htmlspecialchars($dispval) . '"' : ''; + } else { + $title = ' title="' . htmlspecialchars($relation_field_value) . '"'; + } + $_url_params = array( + 'db' => $map[$relation_field]['foreign_db'], + 'table' => $map[$relation_field]['foreign_table'], + 'pos' => '0', + 'sql_query' => 'SELECT * FROM ' + . PMA_backquote($map[$relation_field]['foreign_db']) . '.' . PMA_backquote($map[$relation_field]['foreign_table']) + . ' WHERE ' . PMA_backquote($map[$relation_field]['foreign_field']) . $where_comparison + ); + $output = ''; + + if ('D' == $_SESSION['tmp_user_values']['relational_display']) { + // user chose "relational display field" in the + // display options, so show display field in the cell + $output .= (!empty($dispval)) ? htmlspecialchars($dispval) : ''; + } else { + // otherwise display data in the cell + $output .= htmlspecialchars($relation_field_value); + } + $output .= ''; + return $output; +} + +/** + * Get transformation function and transformation options + * + * @param string $db db name + * @param string $table table name + * @param array $transformation mimetypes for all columns of a table + * [field_name][field_key] + * @param array $edited_values transform fields list + * @param array $extra_data extra data array + * + * @return array $extra_data + */ +function PMA_getTransformationFunctionAndTransformationOptions($db, $table, + $transformation, $edited_values, $extra_data +) { + foreach ($edited_values as $cell_index => $curr_cell_edited_values) { + if (isset($curr_cell_edited_values[$column_name])) { + $column_data = $curr_cell_edited_values[$column_name]; + + $_url_params = array( + 'db' => $db, + 'table' => $table, + 'where_clause' => $_REQUEST['where_clause'], + 'transform_key' => $column_name, + ); + + if (file_exists('libraries/transformations/' . $include_file)) { + $transformfunction_name = str_replace('.inc.php', '', $transformation['transformation']); + + include_once 'libraries/transformations/' . $include_file; + + if (function_exists('PMA_transformation_' . $transformfunction_name)) { + $transform_function = 'PMA_transformation_' . $transformfunction_name; + $transform_options = PMA_transformation_getOptions( + isset($transformation['transformation_options']) + ? $transformation['transformation_options'] + : '' + ); + $transform_options['wrapper_link'] = PMA_generate_common_url($_url_params); + } + } + + $extra_data['transformations'][$cell_index] = $transform_function($column_data, $transform_options); + } + } // end of loop for each transformation cell + return $extra_data; +} + ?> diff --git a/tbl_change.php b/tbl_change.php index 59addef9ad..50e3352984 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -156,7 +156,7 @@ if (! isset($where_clause)) { } //Retrieve values for data edit view list($insert_mode, $where_clauses, $result, $rows, $where_clause_array, $found_unique_key) - = PMA_getValuesForEditMode($where_clause, $table, $db); + = PMA_getStuffForEditMode($where_clause, $table, $db); // Copying a row - fetched data will be inserted as a new row, therefore the where clause is needless. if (isset($_REQUEST['default_action']) && $_REQUEST['default_action'] === 'insert') { diff --git a/tbl_replace.php b/tbl_replace.php index 8fc95e0182..245f328733 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -312,63 +312,18 @@ if ($GLOBALS['is_ajax_request'] == true) { $map = PMA_getForeigners($db, $table, '', 'both'); - $rel_fields = array(); - parse_str($_REQUEST['rel_fields_list'], $rel_fields); + $relation_fields = array(); + parse_str($_REQUEST['rel_fields_list'], $relation_fields); // loop for each relation cell - foreach ( $rel_fields as $cell_index => $curr_cell_rel_field) { + foreach ( $relation_fields as $cell_index => $curr_cell_rel_field) { + foreach ( $curr_cell_rel_field as $relation_field => $relation_field_value) { + $where_comparison = "='" . $relation_field_value . "'"; + $dispval = PMA_displayForiengTableColumn($where_comparison, $relation_field_value, $map, $relation_field); - foreach ( $curr_cell_rel_field as $rel_field => $rel_field_value) { - - $where_comparison = "='" . $rel_field_value . "'"; - $display_field = PMA_getDisplayField($map[$rel_field]['foreign_db'], $map[$rel_field]['foreign_table']); - - // Field to display from the foreign table? - if (isset($display_field) && strlen($display_field)) { - $dispsql = 'SELECT ' . PMA_backquote($display_field) - . ' FROM ' . PMA_backquote($map[$rel_field]['foreign_db']) - . '.' . PMA_backquote($map[$rel_field]['foreign_table']) - . ' WHERE ' . PMA_backquote($map[$rel_field]['foreign_field']) - . $where_comparison; - $dispresult = PMA_DBI_try_query($dispsql, null, PMA_DBI_QUERY_STORE); - if ($dispresult && PMA_DBI_num_rows($dispresult) > 0) { - list($dispval) = PMA_DBI_fetch_row($dispresult, 0); - } else { - //$dispval = __('Link not found'); - } - @PMA_DBI_free_result($dispresult); - } else { - $dispval = ''; - } // end if... else... - - if ('K' == $_SESSION['tmp_user_values']['relational_display']) { - // user chose "relational key" in the display options, so - // the title contains the display field - $title = (! empty($dispval))? ' title="' . htmlspecialchars($dispval) . '"' : ''; - } else { - $title = ' title="' . htmlspecialchars($rel_field_value) . '"'; - } - - $_url_params = array( - 'db' => $map[$rel_field]['foreign_db'], - 'table' => $map[$rel_field]['foreign_table'], - 'pos' => '0', - 'sql_query' => 'SELECT * FROM ' - . PMA_backquote($map[$rel_field]['foreign_db']) . '.' . PMA_backquote($map[$rel_field]['foreign_table']) - . ' WHERE ' . PMA_backquote($map[$rel_field]['foreign_field']) . $where_comparison - ); - $output = ''; - - if ('D' == $_SESSION['tmp_user_values']['relational_display']) { - // user chose "relational display field" in the - // display options, so show display field in the cell - $output .= (!empty($dispval)) ? htmlspecialchars($dispval) : ''; - } else { - // otherwise display data in the cell - $output .= htmlspecialchars($rel_field_value); - } - $output .= ''; - $extra_data['relations'][$cell_index] = $output; + $extra_data['relations'][$cell_index] = PMA_getLinkForRelationalDisplayField( + $map, $relation_field, $where_comparison, $dispval, $relation_field_value + ); } } // end of loop for each relation cell } @@ -388,35 +343,8 @@ if ($GLOBALS['is_ajax_request'] == true) { foreach ($mime_map as $transformation) { $include_file = PMA_securePath($transformation['transformation']); $column_name = $transformation['column_name']; - - foreach ($edited_values as $cell_index => $curr_cell_edited_values) { - if (isset($curr_cell_edited_values[$column_name])) { - $column_data = $curr_cell_edited_values[$column_name]; - - $_url_params = array( - 'db' => $db, - 'table' => $table, - 'where_clause' => $_REQUEST['where_clause'], - 'transform_key' => $column_name, - ); - - if (file_exists('libraries/transformations/' . $include_file)) { - $transformfunction_name = str_replace('.inc.php', '', $transformation['transformation']); - - include_once 'libraries/transformations/' . $include_file; - - if (function_exists('PMA_transformation_' . $transformfunction_name)) { - $transform_function = 'PMA_transformation_' . $transformfunction_name; - $transform_options = PMA_transformation_getOptions( - isset($transformation['transformation_options']) ? $transformation['transformation_options'] : '' - ); - $transform_options['wrapper_link'] = PMA_generate_common_url($_url_params); - } - } - - $extra_data['transformations'][$cell_index] = $transform_function($column_data, $transform_options); - } - } // end of loop for each transformation cell + $extra_data = PMA_getTransformationFunctionAndTransformationOptions($db, $table, + $transformation, $edited_values, $include_file, $column_name, $extra_data); } // end of loop for each $mime_map } @@ -443,7 +371,7 @@ $active_page = $goto_include; * to the current record */ if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) { - unset($_REQUEST['where_clause']); + unset($_REQUEST['where_clause']); } /** From 90abdfe593fcdd95eaccbb463814e799450abcf7 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 13 Jun 2012 11:21:54 +0530 Subject: [PATCH 2/3] add PMA_getCurrentValueForMultipleEdit function to insert_edit-lib --- libraries/insert_edit.lib.php | 48 ++++++++++++++++++++++++++++++++--- tbl_replace.php | 28 +++----------------- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php index 4ef221561a..1c08162d09 100644 --- a/libraries/insert_edit.lib.php +++ b/libraries/insert_edit.lib.php @@ -1737,7 +1737,7 @@ function PMA_getWarningMessages() } /** - * Field to display from the foreign table? + * Column to display from the foreign table? * * @param string $where_comparison string that contain relation field value * @param string $relation_field_value relation field value @@ -1747,7 +1747,7 @@ function PMA_getWarningMessages() * * @return string $dispval display value from the foriegn table */ -function PMA_displayForiengTableColumn($where_comparison, $relation_field_value, $map, $relation_field) +function PMA_getDisplayValueForForiengTableColumn($where_comparison, $relation_field_value, $map, $relation_field) { $display_field = PMA_getDisplayField($map[$relation_field]['foreign_db'], $map[$relation_field]['foreign_table']); // Field to display from the foreign table? @@ -1824,7 +1824,7 @@ function PMA_getLinkForRelationalDisplayField($map, $relation_field, $where_comp * @return array $extra_data */ function PMA_getTransformationFunctionAndTransformationOptions($db, $table, - $transformation, $edited_values, $extra_data + $transformation, $edited_values, $extra_data, $include_file ) { foreach ($edited_values as $cell_index => $curr_cell_edited_values) { if (isset($curr_cell_edited_values[$column_name])) { @@ -1859,4 +1859,46 @@ function PMA_getTransformationFunctionAndTransformationOptions($db, $table, return $extra_data; } +/** + * Get current value in multi edit mode + * + * @param array $multi_edit_colummns multiple edit column array + * @param array $multi_edit_columns_name multiple edit columns name array + * @param array $multi_edit_funcs multiple edit functions array + * @param array $gis_from_text_functions array that contains gis from text functions + * @param string $val + * @param array $gis_from_wkb_functions initialy $val is $multi_edit_colummns[$key] + * @param array $func_optional_param array('RAND','UNIX_TIMESTAMP') + * @param array $func_no_param array of set of string + * @param string $key an md5 of the fieldname + * + * @return array $val, $cur_value + */ +function PMA_getCurrentValueForMultipleEdit($multi_edit_colummns, $multi_edit_columns_name, $multi_edit_funcs, + $gis_from_text_functions, $val, $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key +) { + if (empty($multi_edit_funcs[$key])) { + $cur_value = $val; + } elseif ('UUID' === $multi_edit_funcs[$key]) { + /* This way user will know what UUID new row has */ + $uuid = PMA_DBI_fetch_value('SELECT UUID()'); + $cur_value = "'" . $uuid . "'"; + } elseif ((in_array($multi_edit_funcs[$key], $gis_from_text_functions) + && substr($val, 0, 3) == "'''") + || in_array($multi_edit_funcs[$key], $gis_from_wkb_functions) + ) { + // Remove enclosing apostrophes + $val = substr($val, 1, strlen($val) - 2); + // Remove escaping apostrophes + $val = str_replace("''", "'", $val); + $cur_value = $multi_edit_funcs[$key] . '(' . $val . ')'; + } elseif (! in_array($multi_edit_funcs[$key], $func_no_param) + || ($val != "''" && in_array($multi_edit_funcs[$key], $func_optional_param))) { + $cur_value = $multi_edit_funcs[$key] . '(' . $val . ')'; + } else { + $cur_value = $multi_edit_funcs[$key] . '()'; + } + return array($val, $cur_value); +} + ?> diff --git a/tbl_replace.php b/tbl_replace.php index 245f328733..e81e146db7 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -176,32 +176,12 @@ foreach ($loop_array as $rownumber => $where_clause) { // when inserting multiple entries foreach ($multi_edit_columns_name as $key => $colummn_name) { $val = $multi_edit_colummns[$key]; - // Note: $key is an md5 of the fieldname. The actual fieldname is available in $multi_edit_columns_name[$key] include 'libraries/tbl_replace_fields.inc.php'; - - if (empty($multi_edit_funcs[$key])) { - $cur_value = $val; - } elseif ('UUID' === $multi_edit_funcs[$key]) { - /* This way user will know what UUID new row has */ - $uuid = PMA_DBI_fetch_value('SELECT UUID()'); - $cur_value = "'" . $uuid . "'"; - } elseif ((in_array($multi_edit_funcs[$key], $gis_from_text_functions) - && substr($val, 0, 3) == "'''") - || in_array($multi_edit_funcs[$key], $gis_from_wkb_functions) - ) { - // Remove enclosing apostrophes - $val = substr($val, 1, strlen($val) - 2); - // Remove escaping apostrophes - $val = str_replace("''", "'", $val); - $cur_value = $multi_edit_funcs[$key] . '(' . $val . ')'; - } elseif (! in_array($multi_edit_funcs[$key], $func_no_param) - || ($val != "''" && in_array($multi_edit_funcs[$key], $func_optional_param))) { - $cur_value = $multi_edit_funcs[$key] . '(' . $val . ')'; - } else { - $cur_value = $multi_edit_funcs[$key] . '()'; - } + list($val, $cur_value) = PMA_getCurrentValueForMultipleEdit($multi_edit_colummns, + $multi_edit_columns_name, $multi_edit_funcs, $gis_from_text_functions, $val, + $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key); // i n s e r t if ($is_insert) { @@ -344,7 +324,7 @@ if ($GLOBALS['is_ajax_request'] == true) { $include_file = PMA_securePath($transformation['transformation']); $column_name = $transformation['column_name']; $extra_data = PMA_getTransformationFunctionAndTransformationOptions($db, $table, - $transformation, $edited_values, $include_file, $column_name, $extra_data); + $transformation, $edited_values, $include_file, $column_name, $extra_data, $include_file); } // end of loop for each $mime_map } From 7bcd92c51256c8623ba53359d423512bb522b7d1 Mon Sep 17 00:00:00 2001 From: Thilina Buddika Date: Wed, 13 Jun 2012 18:54:42 +0530 Subject: [PATCH 3/3] implement PMA_getQueryValuesForInsertAndUpdateInMultipleEdit function in insert_edit-lib --- libraries/insert_edit.lib.php | 57 ++++++++++++++++++++++++++++++++++- tbl_replace.php | 37 +++-------------------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php index 1c08162d09..1f9fb7fd12 100644 --- a/libraries/insert_edit.lib.php +++ b/libraries/insert_edit.lib.php @@ -1870,7 +1870,7 @@ function PMA_getTransformationFunctionAndTransformationOptions($db, $table, * @param array $gis_from_wkb_functions initialy $val is $multi_edit_colummns[$key] * @param array $func_optional_param array('RAND','UNIX_TIMESTAMP') * @param array $func_no_param array of set of string - * @param string $key an md5 of the fieldname + * @param string $key an md5 of the column name * * @return array $val, $cur_value */ @@ -1901,4 +1901,59 @@ function PMA_getCurrentValueForMultipleEdit($multi_edit_colummns, $multi_edit_co return array($val, $cur_value); } +/** + * Get query values array and query fileds array for insert and update in multi edit + * + * @param array $multi_edit_columns_name multiple edit columns name array + * @param array $multi_edit_columns_null multiple edit columns name array + * @param string $val + * @param array $multi_edit_columns_prev multiple edit previous columns array + * @param array $multi_edit_funcs multiple edit functions array + * @param boolean $is_insert boolean value whether insert or not + * @param array $query_values SET part of the sql query + * @param array $query_fields array of query fileds + * @param string $current_value current value in the column in loop + * @param array $value_sets array of valu sets + * @param string $key an md5 of the column name + * @param array $multi_edit_columns_null_prev array of multiple edit columnd null previous + * + * @return array ($query_values, $query_fields) + */ +function PMA_getQueryValuesForInsertAndUpdateInMultipleEdit($multi_edit_columns_name, + $multi_edit_columns_null, $val, $multi_edit_columns_prev, $multi_edit_funcs,$is_insert, + $query_values, $query_fields, $current_value, $value_sets, $key, $multi_edit_columns_null_prev +) { + // i n s e r t + if ($is_insert) { + // no need to add column into the valuelist + if (strlen($current_value)) { + $query_values[] = $current_value; + // first inserted row so prepare the list of fields + if (empty($value_sets)) { + $query_fields[] = PMA_backquote($multi_edit_columns_name[$key]); + } + } + // u p d a t e + } elseif (!empty($multi_edit_columns_null_prev[$key]) + && ! isset($multi_edit_columns_null[$key])) { + // field had the null checkbox before the update + // field no longer has the null checkbox + $query_values[] = PMA_backquote($multi_edit_columns_name[$key]) . ' = ' . $current_value; + } elseif (empty($multi_edit_funcs[$key]) + && isset($multi_edit_columns_prev[$key]) + && ("'" . PMA_sqlAddSlashes($multi_edit_columns_prev[$key]) . "'" == $val)) { + // No change for this column and no MySQL function is used -> next column + } elseif (! empty($val)) { + // avoid setting a field to NULL when it's already NULL + // (field had the null checkbox before the update + // field still has the null checkbox) + if (empty($multi_edit_columns_null_prev[$key]) + || empty($multi_edit_columns_null[$key]) + ) { + $query_values[] = PMA_backquote($multi_edit_columns_name[$key]) . ' = ' . $current_value; + } + } + return array($query_values, $query_fields); +} + ?> diff --git a/tbl_replace.php b/tbl_replace.php index e81e146db7..00b1393253 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -112,6 +112,7 @@ $gis_from_wkb_functions = array( 'MPolyFromWKB', ); +$query_fields = array(); foreach ($loop_array as $rownumber => $where_clause) { // skip fields to be ignored if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) { @@ -183,38 +184,10 @@ foreach ($loop_array as $rownumber => $where_clause) { $multi_edit_columns_name, $multi_edit_funcs, $gis_from_text_functions, $val, $gis_from_wkb_functions, $func_optional_param, $func_no_param, $key); - // i n s e r t - if ($is_insert) { - // no need to add column into the valuelist - if (strlen($cur_value)) { - $query_values[] = $cur_value; - // first inserted row so prepare the list of fields - if (empty($value_sets)) { - $query_fields[] = PMA_backquote($multi_edit_columns_name[$key]); - } - } - - // u p d a t e - } elseif (!empty($multi_edit_columns_null_prev[$key]) - && ! isset($multi_edit_columns_null[$key])) { - // field had the null checkbox before the update - // field no longer has the null checkbox - $query_values[] = PMA_backquote($multi_edit_columns_name[$key]) . ' = ' . $cur_value; - } elseif (empty($multi_edit_funcs[$key]) - && isset($multi_edit_columns_prev[$key]) - && ("'" . PMA_sqlAddSlashes($multi_edit_columns_prev[$key]) . "'" == $val)) { - // No change for this column and no MySQL function is used -> next column - continue; - } elseif (! empty($val)) { - // avoid setting a field to NULL when it's already NULL - // (field had the null checkbox before the update - // field still has the null checkbox) - if (empty($multi_edit_columns_null_prev[$key]) - || empty($multi_edit_columns_null[$key]) - ) { - $query_values[] = PMA_backquote($multi_edit_columns_name[$key]) . ' = ' . $cur_value; - } - } + list($query_values, $query_fields) = PMA_getQueryValuesForInsertAndUpdateInMultipleEdit( + $multi_edit_columns_name,$multi_edit_columns_null, $val, $multi_edit_columns_prev, + $multi_edit_funcs,$is_insert,$query_values, $query_fields, $cur_value, $value_sets, + $key, $multi_edit_columns_null_prev); } // end foreach ($multi_edit_colummns as $key => $val) if (count($query_values) > 0) {