From 8f0b5487144c000562419d4bf8d6858576387d22 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 20 Nov 2012 15:56:44 +0100 Subject: [PATCH] Debug export filters. Seems to be enougth stable so it is now enabled by default. --- htdocs/exports/class/export.class.php | 31 +++---- htdocs/exports/export.php | 124 ++++++++++---------------- test/phpunit/ExportTest.php | 14 +-- 3 files changed, 66 insertions(+), 103 deletions(-) diff --git a/htdocs/exports/class/export.class.php b/htdocs/exports/class/export.class.php index c737d38ca3c..f44722d8de6 100644 --- a/htdocs/exports/class/export.class.php +++ b/htdocs/exports/class/export.class.php @@ -202,10 +202,9 @@ class Export * @param int $indice Indice of export * @param array $array_selected Filter on array of fields to export * @param array $array_filterValue Filter on array of fields to export - * @param array $array_filtered Array with filters values * @return string SQL String. Example "select s.rowid as r_rowid, s.status as s_status from ..." */ - function build_sql($indice, $array_selected, $array_filterValue, $array_filtered) + function build_sql($indice, $array_selected, $array_filterValue) { // Build the sql request $sql=$this->array_export_sql_start[$indice]; @@ -225,16 +224,13 @@ class Export $sql.=$this->array_export_sql_end[$indice]; //construction du filtrage si le parametrage existe - if (is_array($array_filtered)) + if (is_array($array_filterValue)) { $sqlWhere=''; // pour ne pas a gerer le nombre de condition - foreach ($array_filtered as $key => $value) + foreach ($array_filterValue as $key => $value) { - if ($array_filterValue[$key]) - { - $sqlWhere.=" and ".$this->build_filterQuery($this->array_export_TypeFields[0][$key], $key, $array_filterValue[$key]); - } + $sqlWhere.=" and ".$this->build_filterQuery($this->array_export_TypeFields[0][$key], $key, $array_filterValue[$key]); } $sql.=$sqlWhere; } @@ -297,8 +293,10 @@ class Export $szFilterQuery=" ".$NameField.substr($ValueField,0,1).substr($ValueField,1); } break; - case 'Status': case 'Boolean': + $szFilterQuery=" ".$NameField."=".(is_numeric($ValueField) ? $ValueField : ($ValueField =='yes' ? 1: 0) ); + break; + case 'Status': case 'List': if (is_numeric($ValueField)) $szFilterQuery=" ".$NameField."=".$ValueField; @@ -350,18 +348,18 @@ class Export $szFilterField='"; break; case 'Boolean': - $szFilterField="'; $szFilterField.=''; $szFilterField.=''; $szFilterField.=""; break; case 'List': @@ -466,11 +464,10 @@ class Export * @param string $datatoexport Name of dataset to export * @param array $array_selected Filter on array of fields to export * @param array $array_filterValue Filter on array of fields with a filter - * @param array $array_filtered Values of filters * @param string $sqlquery If set, transmit a sql query instead of building it from arrays * @return int <0 if KO, >0 if OK */ - function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $array_filtered, $sqlquery = '') + function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery = '') { global $conf,$langs; @@ -494,7 +491,7 @@ class Export $objmodel = new $classname($this->db); if (! empty($sqlquery)) $sql = $sqlquery; - else $sql=$this->build_sql($indice, $array_selected, $array_filterValue, $array_filtered); + else $sql=$this->build_sql($indice, $array_selected, $array_filterValue); // Run the sql $this->sqlusedforexport=$sql; diff --git a/htdocs/exports/export.php b/htdocs/exports/export.php index 8ec3e73f425..f96eb57d648 100644 --- a/htdocs/exports/export.php +++ b/htdocs/exports/export.php @@ -94,7 +94,7 @@ $entitytolang = array( ); $array_selected=isset($_SESSION["export_selected_fields"])?$_SESSION["export_selected_fields"]:array(); -$array_filtered=isset($_SESSION["export_filtered_fields"])?$_SESSION["export_filtered_fields"]:array(); +//$array_filtered=isset($_SESSION["export_filtered_fields"])?$_SESSION["export_filtered_fields"]:array(); $array_filtervalue=isset($_SESSION["export_FilterValue_fields"])?$_SESSION["export_FilterValue_fields"]:array(); $datatoexport=GETPOST("datatoexport"); $action=GETPOST('action', 'alpha'); @@ -116,8 +116,8 @@ $sqlusedforexport=''; $upload_dir = $conf->export->dir_temp.'/'.$user->id; -$usefilters=($conf->global->MAIN_FEATURES_LEVEL > 1); -//$usefilters=1; +//$usefilters=($conf->global->MAIN_FEATURES_LEVEL > 1); +$usefilters=1; /* @@ -189,53 +189,6 @@ if ($action=='unselectfield') } } -/* -if ($action=='selectFilterfield') -{ - if ($_GET["field"]=='all') - { - $fieldsarray=$objexport->array_export_TypeFields[0]; - foreach($fieldsarray as $key=>$val) - { - if (! empty($array_filtered[$key])) continue; // If already selected, select next - $array_filtered[$key]=count($array_filtered)+1; - //print_r($array_selected); - $_SESSION["export_filtered_fields"]=$array_filtered; - } - } - else - { - $array_filtered[$_GET["field"]]=count($array_filtered)+1; - //print_r($array_selected); - $_SESSION["export_filtered_fields"]=$array_filtered; - } -} - -if ($action=='unselectFilterfield') -{ - if ($_GET["field"]=='all') - { - $array_filtered=array(); - $_SESSION["export_filtered_fields"]=$array_filtered; - } - else - { - unset($array_filtered[$_GET["field"]]); - // Renumber fields of array_selected (from 1 to nb_elements) - asort($array_filtered); - $i=0; - $array_filterted_save=$array_filtered; - foreach($array_filtered as $code=>$value) - { - $i++; - $array_filtered[$code]=$i; - //print "x $code x $i y
"; - } - $_SESSION["export_filtered_fields"]=$array_filtered; - } -} -*/ - if ($action=='downfield' || $action=='upfield') { $pos=$array_selected[$_GET["field"]]; @@ -263,7 +216,7 @@ if ($action=='downfield' || $action=='upfield') if ($step == 1 || $action == 'cleanselect') { $_SESSION["export_selected_fields"]=array(); - $_SESSION["export_FilterValue_fields"]=array(); + //$_SESSION["export_FilterValue_fields"]=array(); $_SESSION["export_filtered_fields"]=array(); $array_selected=array(); $array_filtervalue=array(); @@ -273,7 +226,7 @@ if ($step == 1 || $action == 'cleanselect') if ($action == 'builddoc') { // Build export file - $result=$objexport->build_file($user, $_POST['model'], $datatoexport, $array_selected, $array_filtervalue, $array_filtered); + $result=$objexport->build_file($user, $_POST['model'], $datatoexport, $array_selected, $array_filtervalue); if ($result < 0) { $mesg='
'.$objexport->error.'
'; @@ -360,7 +313,7 @@ if ($action == 'add_export_model') if ($step == 2 && $action == 'select_model') { $_SESSION["export_selected_fields"]=array(); - $_SESSION["export_filtered_fields"]=array(); + //$_SESSION["export_filtered_fields"]=array(); $_SESSION["export_FilterValue_fields"]=array(); $array_selected=array(); @@ -388,25 +341,34 @@ if ($step == 2 && $action == 'select_model') $array_filtervalue[$val]=$fieldsarrayvalue[$i-1]; $i++; } - $_SESSION["export_filtered_fields"]=$array_filtered; + //$_SESSION["export_filtered_fields"]=$array_filtered; $_SESSION["export_FilterValue_fields"]=$array_filtervalue; } } -// recuperation du filtrage issu du formulaire +// Get form with filters if ($step == 4 && $action == 'submitFormField') { // on boucle sur les champs selectionne pour recuperer la valeur if (is_array($objexport->array_export_TypeFields[0])) { $_SESSION["export_FilterValue_fields"]=array(); - foreach($array_filtered as $code=>$value) + var_dump($_POST); + foreach($objexport->array_export_TypeFields[0] as $code => $type) // $code: s.fieldname $value: Text|Boolean|List:ccc { - //print $code."=".$_POST[$objexport->array_export_fields[0][$code]]; - $objexport->array_export_FilterValue[0][$code] = (isset($objexport->array_export_fields[0][$code])?$_POST[$objexport->array_export_fields[0][$code]]:''); + $newcode=(string) preg_replace('/\./','_',$code); + //print 'xxx'.$code."=".$newcode."=".$type."=".$_POST[$newcode]."\n
"; + $filterqualified=1; + if (! isset($_POST[$newcode]) || $_POST[$newcode] == '') $filterqualified=0; + elseif (preg_match('/^List/',$type) && $_POST[$newcode] <= 0) $filterqualified=0; + if ($filterqualified) + { + //print 'Filter on '.$newcode.' type='.$type.' value='.$_POST[$newcode]."\n"; + $objexport->array_export_FilterValue[0][$code] = $_POST[$newcode]; + } } - $_SESSION["export_FilterValue_fields"]=(! empty($objexport->array_export_FilterValue[0])?$objexport->array_export_FilterValue[0]:''); $array_filtervalue=(! empty($objexport->array_export_FilterValue[0])?$objexport->array_export_FilterValue[0]:''); + $_SESSION["export_FilterValue_fields"]=$array_filtervalue; } } @@ -415,7 +377,6 @@ if ($step == 4 && $action == 'submitFormField') * View */ - if ($step == 1 || ! $datatoexport) { llxHeader('',$langs->trans("NewExport"),'EN:Module_Exports_En|FR:Module_Exports|ES:Módulo_Exportaciones'); @@ -566,7 +527,7 @@ if ($step == 2 && $datatoexport) // Champs exportables $fieldsarray=$objexport->array_export_fields[0]; // Select request if all fields are selected - $sqlmaxforexport=$objexport->build_sql(0, array(), array(), array()); + $sqlmaxforexport=$objexport->build_sql(0, array(), array()); // $this->array_export_module[0]=$module; // $this->array_export_code[0]=$module->export_code[$r]; @@ -741,7 +702,7 @@ if ($step == 3 && $datatoexport) // valeur des filtres $ValueFiltersarray=(! empty($objexport->array_export_FilterValue[0])?$objexport->array_export_FilterValue[0]:''); // Select request if all fields are selected - $sqlmaxforexport=$objexport->build_sql(0, array(), array(), array()); + $sqlmaxforexport=$objexport->build_sql(0, array(), array()); $var=true; $i = 0; @@ -868,7 +829,7 @@ if ($step == 4 && $datatoexport) print $objexport->array_export_label[0]; print ''; - // Nbre champs exportes + // List of exported fields print ''.$langs->trans("ExportedFields").''; $list=''; foreach($array_selected as $code=>$value) @@ -876,28 +837,31 @@ if ($step == 4 && $datatoexport) $list.=($list?', ':''); $list.=$langs->trans($objexport->array_export_fields[0][$code]); } - print ''.$list.''; + print ''.$list.''; + print ''; - // Number of filtered fields + // List of filtered fiels if (isset($objexport->array_export_TypeFields[0]) && is_array($objexport->array_export_TypeFields[0])) { print ''.$langs->trans("FilteredFields").''; $list=''; - foreach($array_filtered as $code=>$value) + foreach($array_filtervalue as $code=>$value) { - if (isset($objexport->array_export_fields[0][$code])) { + if (isset($objexport->array_export_fields[0][$code])) + { $list.=($list?', ':''); - $list.="[".$langs->trans($objexport->array_export_fields[0][$code])."]='".(isset($array_filtervalue[$code])?$array_filtervalue[$code]:'')."'"; + $list.=$langs->trans($objexport->array_export_fields[0][$code])."='".(isset($array_filtervalue[$code])?$array_filtervalue[$code]:'')."'"; } } - print ''.$list.''; + print ''.($list?$list:$langs->trans("None")).''; + print ''; } print ''; print '
'; // Select request if all fields are selected - $sqlmaxforexport=$objexport->build_sql(0, array(), array(), array()); + $sqlmaxforexport=$objexport->build_sql(0, array(), array()); print $langs->trans("ChooseFieldsOrdersAndTitle").'
'; @@ -1098,7 +1062,7 @@ if ($step == 5 && $datatoexport) print $objexport->array_export_label[0]; print ''; - // Nbre champs exportes + // List of exported fields print ''.$langs->trans("ExportedFields").''; $list=''; foreach($array_selected as $code=>$label) @@ -1108,19 +1072,21 @@ if ($step == 5 && $datatoexport) } print ''.$list.''; - // Nbre champs filtres - if (is_array($objexport->array_export_TypeFields[0])) + // List of filtered fiels + if (isset($objexport->array_export_TypeFields[0]) && is_array($objexport->array_export_TypeFields[0])) { print ''.$langs->trans("FilteredFields").''; $list=''; - foreach($array_filtered as $code=>$value) + foreach($array_filtervalue as $code=>$value) { - if (isset($objexport->array_export_fields[0][$code])) { + if (isset($objexport->array_export_fields[0][$code])) + { $list.=($list?', ':''); - $list.="[".$langs->trans($objexport->array_export_fields[0][$code])."]='".(isset($array_filtervalue[$code])?$array_filtervalue[$code]:'')."'"; + $list.=$langs->trans($objexport->array_export_fields[0][$code])."='".(isset($array_filtervalue[$code])?$array_filtervalue[$code]:'')."'"; } } - print ''.$list.''; + print ''.($list?$list:$langs->trans("None")).''; + print ''; } print ''; @@ -1182,10 +1148,10 @@ if ($step == 5 && $datatoexport) print '
'; -$db->close(); - llxFooter(); +$db->close(); + /** * Return table name of an alias. For this, we look for the "tablename as alias" in sql string. diff --git a/test/phpunit/ExportTest.php b/test/phpunit/ExportTest.php index 860e6cc09e6..103869e1850 100755 --- a/test/phpunit/ExportTest.php +++ b/test/phpunit/ExportTest.php @@ -144,21 +144,21 @@ class ExportTest extends PHPUnit_Framework_TestCase $model='csv'; // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), array(), $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult); $model='tsv'; // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), array(), $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult); $model='excel'; // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), array(), $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult); @@ -195,21 +195,21 @@ class ExportTest extends PHPUnit_Framework_TestCase $model='csv'; // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $array_filtered, $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult); $model='tsv'; // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $array_filtered, $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult); $model='excel'; // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $array_filtered, $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult); @@ -234,7 +234,7 @@ class ExportTest extends PHPUnit_Framework_TestCase $result=$objexport->load_arrays($user,$datatoexport); // Build export file - $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), array(), $sql); + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql); $expectedresult=1; $this->assertEquals($result,$expectedresult);