* @author Loic Vinet * @author Jimmy Cedric Ndoumba */ include("session.php"); require("class_functions.php"); require_once("class_db.php"); $source = $_SESSION['config']['tmpdir']; //----------------------- Field selection in the export.xml file-----------------------// $table_users = false; $table_doctypes = false; $table_sender = false; $table_models = false; $table_document = false; $table_services = false; $xmlfile = simplexml_load_file($_SESSION['config']['exportlist']); $i_map = 0; foreach($xmlfile->TAG as $TAG) { $tab_export_name[$i_map] = $TAG->NAME; $tab_export_value[$i_map] = $TAG->VALUE; $tab_export_table[$i_map] = $TAG->TABLE; $i_map++; } //-----------------------Déclaration des tables nécessaires--------------------------------// for ($t=0; $t < count($tab_export_table); $t++) { if ($tab_export_table[$t] == $_SESSION['ressources'][0]['tablename']) { $table_document = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['doctypes']) { $table_doctypes = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['senders']) { $table_sender = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['models']) { $table_models = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['users']) { $table_users = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['services']) { $table_services = true; } } $tables = array(); if ($table_document){ $tables[$_SESSION['ressources'][0]['tablename']] = 'r'; } if ($table_doctypes){ $tables[$_SESSION['tablename']['doctypes']] = 'd'; } if ($table_sender){ $tables[$_SESSION['tablename']['senders']] = 's'; } //if ($table_models){ $tables[$_SESSION['tablename']['models']] = 'm'; } if ($table_users){ $tables[$_SESSION['tablename']['users']] = 'u'; } if ($table_services){ $tables[$_SESSION['tablename']['services']] = 'se'; } //-----------------------Query execution----------------------------------------------------------// $db = new dbquery(); $db->connect(); $select = ""; // Fields positioning for ($i_query=0; $i_query < $i_map; $i_query++) { //$select .= substr($tab_export_table[$i_query], 0, 1).".".$tab_export_value[$i_query]." ,"; $tempName = (string)$tab_export_table[$i_query]; $select .= $tables[$tempName].".".$tab_export_value[$i_query]." AS '".$tab_export_name[$i_query]."',"; } $select = substr($select, 0, -1); $from = ''; foreach ($tables as $tableName => $tableAlias) { $from .= $tableName.' '.$tableAlias.', '; } $from = substr($from, 0, -2); $where = $_SESSION['export_where_request']; //Création des jointures si nécessaire if ($table_doctypes) { $where .= "and ".$tables[$_SESSION['ressources'][0]['tablename']].".TYPE_ID = ".$tables[$_SESSION['tablename']['doctypes']].".TYPE_ID "; } if ($table_sender) { $where .= "and ".$tables[$_SESSION['ressources'][0]['tablename']].".AUTHOR = ".$tables[$_SESSION['tablename']['senders']].".NAME "; } if ($table_users) { $where .= "and ((".$tables[$_SESSION['ressources'][0]['tablename']].".DEST_USER = 'courrier') or (".$tables[$_SESSION['ressources'][0]['tablename']].".DEST_USER = ".$tables[$_SESSION['tablename']['users']].".USER_ID)) "; } if ($table_services) { $where .= "and ".$tables[$_SESSION['ressources'][0]['tablename']].".DESTINATION = ".$tables[$_SESSION['tablename']['services']].".ID "; } if($_SESSION['copies'] == 'true') { $db->query("SELECT ".$select." FROM ".$from." , listinstance l WHERE l.res_id = r.res_id and l.sequence > 1 and (".$where.")"); } else { $db->query("SELECT ".$select." FROM ".$from." WHERE (".$where.")"); } //$db->show(); //----------------------- Result list making ----------------------------------------// for ($i_title = 0; $i_title < $i_map; $i_title++) { $the_line .= $tab_export_name[$i_title].";"; } $csv .= $the_line."\r\n"; while ($result = $db->fetch_object()) { $the_line = ""; for ($i_reg = 0; $i_reg< $i_map; $i_reg++) { $tmp_value_csv = utf8_decode($result->$tab_export_name[$i_reg]); $tmp_value_csv = str_replace(CHR(10), "", $tmp_value_csv); $tmp_value_csv = str_replace(CHR(13), "", $tmp_value_csv); $tmp_value_csv = str_replace(";", ",", $tmp_value_csv); $tmp_value_csv = str_replace("\\", ",", $tmp_value_csv); $the_line .= $tmp_value_csv.";"; } $csv .= $the_line."\r\n"; } //----------------------- Index file making ----------------------------------------// /*if ($_SESSION['config']['system'] == "true") { $slh = "/"; } else { $slh = "\\"; }*/ $slh = DIRECTORY_SEPARATOR ; $monfichier = fopen($source.$slh."export_".$_SESSION['user']['UserId'].".csv","w+,"); fwrite($monfichier, $csv); header("Pragma: no-cach"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=maarch.csv;"); readfile($source.$slh."export_".$_SESSION['user']['UserId'].".csv");