From b1d071a964b4feee11ea7400332191ab15137282 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 25 May 2023 12:25:06 +0200 Subject: [PATCH] Better error message --- htdocs/core/class/html.form.class.php | 15 ++++++--------- htdocs/core/customreports.php | 17 ++++++++++------- htdocs/core/lib/functions.lib.php | 25 +++++++++++++++++++------ 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index e9ffa3d4bfc..61df8a70a4c 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -10431,11 +10431,11 @@ class Form /** * Output the component to make advanced search criteries * - * @param array $arrayofcriterias Array of available search criterias. Example: array($object->element => $object->fields, 'otherfamily' => otherarrayoffields, ...) - * @param array $search_component_params Array of selected search criterias - * @param array $arrayofinputfieldsalreadyoutput Array of input fields already inform. The component will not generate a hidden input field if it is in this list. - * @param string $search_component_params_hidden String with $search_component_params criterias - * @return string HTML component for advanced search + * @param array $arrayofcriterias Array of available search criterias. Example: array($object->element => $object->fields, 'otherfamily' => otherarrayoffields, ...) + * @param array $search_component_params Array of selected search criterias + * @param array $arrayofinputfieldsalreadyoutput Array of input fields already inform. The component will not generate a hidden input field if it is in this list. + * @param string $search_component_params_hidden String with $search_component_params criterias + * @return string HTML component for advanced search */ public function searchComponent($arrayofcriterias, $search_component_params, $arrayofinputfieldsalreadyoutput = array(), $search_component_params_hidden = '') { @@ -10456,13 +10456,10 @@ class Form $ret .= '
'; if ($search_component_params_hidden) { - if (!preg_match('/^\(.*\)$/', $search_component_params_hidden)) { // If $search_component_params_hidden does not start and end with () - $search_component_params_hidden .= '(' . $search_component_params_hidden . ')'; - } $errormessage = ''; $searchtags = forgeSQLFromUniversalSearchCriteria($search_component_params_hidden, $errormessage); if ($errormessage) { - print 'ERROR in parsing search string: ' . dol_escape_htmltag($errormessage); + $this->error = 'ERROR in parsing search string: '.$errormessage; } //var_dump($searchtags); $ret .= 'x ' . dol_escape_htmltag($searchtags) . ''; diff --git a/htdocs/core/customreports.php b/htdocs/core/customreports.php index 9595a285f71..0cae5beab64 100644 --- a/htdocs/core/customreports.php +++ b/htdocs/core/customreports.php @@ -572,6 +572,8 @@ print ''; // Generate the SQL request $sql = ''; if (!empty($search_measures) && !empty($search_xaxis)) { + $errormessage = ''; + $fieldid = 'rowid'; $sql = "SELECT "; @@ -663,7 +665,7 @@ if (!empty($search_measures) && !empty($search_xaxis)) { } } } else { - dol_print_error($db, 'Found a key into search_xaxis not found into arrayofxaxis'); + $errormessage = 'Found a key into search_xaxis not found into arrayofxaxis'; } } @@ -690,7 +692,7 @@ if (!empty($search_measures) && !empty($search_xaxis)) { } } } else { - dol_print_error($db, 'Found a key into search_groupby not found into arrayofgroupby'); + $errormessage = 'Found a key into search_groupby not found into arrayofgroupby'; } } @@ -717,7 +719,7 @@ if (!empty($search_measures) && !empty($search_xaxis)) { } } } else { - dol_print_error($db, 'Found a key into search_measures not found into arrayofmesures'); + $errormessage = 'Found a key into search_measures not found into arrayofmesures'; } } @@ -728,11 +730,7 @@ if (!empty($search_measures) && !empty($search_xaxis)) { // Add the where here $sqlfilters = $search_component_params_hidden; if ($sqlfilters) { - $errormessage = ''; $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); - if ($errormessage) { - print dol_escape_htmltag($errormessage); - } } $sql .= " GROUP BY "; foreach ($search_xaxis as $key => $val) { @@ -797,6 +795,11 @@ if (!empty($search_measures) && !empty($search_xaxis)) { } //print $sql; +if ($errormessage) { + print dol_escape_htmltag($errormessage); + $sql = ''; +} + $legend = array(); foreach ($search_measures as $key => $val) { $legend[] = $langs->trans($arrayofmesures[$val]['label']); diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index e35c9e86e3e..79e295869d7 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -11849,16 +11849,25 @@ function jsonOrUnserialize($stringtodecode) * @param string $filter String with universal search string. Must be (aaa:bbb:...) with * aaa is a field name (with alias or not) and * bbb is one of this operator '=', '<', '>', '<=', '>=', '!=', 'in', 'notin', 'like', 'notlike', 'is', 'isnot'. - * @param string $error Error message + * @param string $errorstr Error message string * @param int $noand 0=Default, 1=Do not add the AND before the condition string. + * @param int $noerror 1=If search criteria is not valid, does not return an error string but invalidate the SQL * @return string Return forged SQL string */ -function forgeSQLFromUniversalSearchCriteria($filter, &$error = '', $noand = 0) +function forgeSQLFromUniversalSearchCriteria($filter, &$errorstr = '', $noand = 0, $noerror = 0) { + if (!preg_match('/^\(.*\)$/', $filter)) { // If $search_component_params_hidden does not start and end with () + $filter = '(' . $filter . ')'; + } + $regexstring = '\(([a-zA-Z0-9_\.]+:[<>!=insotlke]+:[^\(\)]+)\)'; // Must be (aaa:bbb:...) with aaa is a field name (with alias or not) and bbb is one of this operator '=', '<', '>', '<=', '>=', '!=', 'in', 'notin', 'like', 'notlike', 'is', 'isnot' - if (!dolCheckFilters($filter, $error)) { - return '1 = 2'; // Bad balance of parenthesis, we force a SQL not found + if (!dolCheckFilters($filter, $errorstr)) { + if ($noerror) { + return '1 = 2'; + } else { + return 'Filter syntax error - '.$errorstr; // Bad balance of parenthesis, we return an error message or force a SQL not found + } } // Test the filter syntax @@ -11866,8 +11875,12 @@ function forgeSQLFromUniversalSearchCriteria($filter, &$error = '', $noand = 0) $t = str_replace(array('and','or','AND','OR',' '), '', $t); // Remove the only strings allowed between each () criteria // If the string result contains something else than '()', the syntax was wrong if (preg_match('/[^\(\)]/', $t)) { - $error = 'Bad syntax of the search string, filter criteria is invalidated'; - return 'Filter syntax error'; // Bad syntax of the search string, we force a SQL not found + $errorstr = 'Bad syntax of the search string'; + if ($noerror) { + return '1 = 2'; + } else { + return 'Filter syntax error - '.$errorstr; // Bad syntax of the search string, we return an error message or force a SQL not found + } } return ($noand ? "" : " AND ")."(".preg_replace_callback('/'.$regexstring.'/i', 'dolForgeCriteriaCallback', $filter).")";