diff --git a/core/trunk/core/class/ExportControler.php b/core/trunk/core/class/ExportControler.php index a48142cbcc64f4643b920699341dcde8e597a45b..d80eacfe8c264ecf510caba4e970f9df2eafb3d4 100755 --- a/core/trunk/core/class/ExportControler.php +++ b/core/trunk/core/class/ExportControler.php @@ -31,160 +31,160 @@ //Loads the require class try { - require_once('core/class/class_functions.php'); - require_once('core/class/class_db.php'); - require_once('core/class/class_history.php'); - //require_once(''); + require_once('core/class/class_functions.php'); + require_once('core/class/class_db.php'); + require_once('core/class/class_history.php'); + //require_once(''); } catch (Exception $e) { - echo $e->getMessage() . ' // '; + echo $e->getMessage() . ' // '; } class ExportControler -{ - private static $db; - private static $db2; - private static $db3; - private static $coll; - private static $functions; - private static $return_loadXMLConf; - - function __construct() - { - $this->coll = $_SESSION['collection_id_choice']; - $this->functions = new functions(); - $this->return_loadXMLConf = $this->functions->object2array($this->loadXMLConf()); - $this->db = new dbquery(); - $this->db->connect(); - $this->db2 = new dbquery(); - $this->db2->connect(); - $this->db3 = new dbquery(); - $this->db3->connect(); - $this->export(); - } - - private function export() - { - $return_createQuery = $this->createQuery(); - //echo $return_createQuery;exit; - if (!$this->db->query($return_createQuery, true)) { - $_SESSION['error'] = 'Erreur SQL 1'; - } - $resultQuery[0] = $this->getHeaderOfCsv(); - $cpt = 1; - while ($return_dbQuery = $this->db->fetch_object()) { - - $return_dbQuery->subject = utf8_decode($return_dbQuery->subject); - $resultQuery[$cpt] = $this->functions->object2array($return_dbQuery); - if ( isset($this->return_loadXMLConf[$this->coll]['FUNCTIONS']['COPIES']) - && !empty($this->return_loadXMLConf[$this->coll]['FUNCTIONS']['COPIES'])) { - $resultQuery[$cpt][$this->return_loadXMLConf[$this->coll]['FUNCTIONS']['COPIES']] = substr($this->functions_copies($return_dbQuery->res_id), 0, -2); - } - - $resultQuery[$cpt]['commentaire'] = ''; - $cpt++; - } - - $return_array2CSV = $this->array2CSV($resultQuery); - $_SESSION['export']['filename'] = $return_array2CSV; - } - - private function createQuery() - { - $query = 'SELECT '; - for ($i=0; $i<count($this->return_loadXMLConf[$this->coll]['FIELD']); $i++) { - $query .= $this->return_loadXMLConf[$this->coll]['FIELD'][$i]['DATABASE_FIELD']; - if ($i <> (count($this->return_loadXMLConf[$this->coll]['FIELD']) - 1)) { - $query .= ', '; - } - } - - $query .= ' '.substr($_SESSION['last_select_query'], stripos($_SESSION['last_select_query'], 'FROM')); - - return $query; - } - - private function array2CSV($resultQuery) - { - do { - $csvName = $_SESSION['user']['UserId'] . '-' . md5(date('Y-m-d H:i:s')) . '.csv'; - if (isset($pathToCsv) && !empty($pathToCsv)) { - $csvName = $_SESSION['user']['UserId'] . '-' . md5($pathToCsv) . '.csv'; - } - $pathToCsv = $_SESSION['config']['tmppath'] . $csvName; - } while (file_exists($pathToCsv)); - - $csvFile = fopen($pathToCsv, 'a+'); - - foreach ($resultQuery as $fields) { - fputcsv($csvFile, $fields, ';'); - } - - fclose($csvFile); - - return $csvName; - } - - private function getHeaderOfCsv() - { - //echo '<pre>'.print_r($loadXMLConf, true).'</pre>';exit; - for($i=0; $i < count($this->return_loadXMLConf[$this->coll]['FIELD']); $i++) { - - if (!empty($this->return_loadXMLConf[$this->coll]['FIELD'][$i]['LIBELLE'])) { - $tabToReturn[$i] = $this->return_loadXMLConf[$this->coll]['FIELD'][$i]['LIBELLE']; - } else { - $tabToReturn[$i] = $this->return_loadXMLConf[$this->coll]['FIELD'][$i]['DATABASE_FIELD']; - } - } - - $temp = array_keys($this->return_loadXMLConf[$this->coll]['FUNCTIONS']); - for ($k=0; $k<count($temp); $k++) { - $j = $i+$k; - $tabToReturn[$j] = $this->return_loadXMLConf[$this->coll]['FUNCTIONS'][$temp[$k]]; - } - if ($k == 0) { - $j = $i; - } - - $tabToReturn[$j+1] = $this->return_loadXMLConf[$this->coll]['FIXE']; - - return $tabToReturn; - } - - private function loadXMLConf() - { - $path = 'apps/maarch_entreprise/xml/export.xml'; - if (file_exists('custom/'.$_SESSION['custom_override_id'].'/apps/maarch_entreprise/xml/export.xml')) { - $path = 'custom/'.$_SESSION['custom_override_id'].'/apps/maarch_entreprise/xml/export.xml'; - } - $exportConf = simplexml_load_file($path); - return $exportConf; - } - - private function functions_copies($res_id) - { - $return_functionsCopies = ''; - $queryListinstance = "SELECT item_id, item_type FROM listinstance WHERE res_id = ".$res_id." AND coll_id = '".$this->coll."' AND item_mode = 'cc'"; - if (!$this->db2->query($queryListinstance, true)) { - $_SESSION['error'] = 'Erreur SQL 2'; - } - $j = 0; - while ($return_dbQueryListinstance = $this->db2->fetch_object()) { - if ($return_dbQueryListinstance->item_type == 'user_id') { - $queryUsersEntities = "SELECT entity_id FROM users_entities WHERE user_id = '".$return_dbQueryListinstance->item_id."' AND primary_entity = 'Y'"; - if (!$this->db3->query($queryUsersEntities, true)) { - $_SESSION['error'] = 'Erreur SQL 3'; - } - while ($return_dbQueryUsersEntities = $this->db3->fetch_object()) { - $usersEntities = $return_dbQueryListinstance->item_id.' : '.$return_dbQueryUsersEntities->entity_id; - } - } else { - $usersEntities = $return_dbQueryListinstance->item_id; - } - - $return_functionsCopies .= $usersEntities . ' | '; - $j++; - } - - return $return_functionsCopies; - } +{ + private static $db; + private static $db2; + private static $db3; + private static $coll; + private static $functions; + private static $return_loadXMLConf; + + function __construct() + { + $this->coll = $_SESSION['collection_id_choice']; + $this->functions = new functions(); + $this->return_loadXMLConf = $this->functions->object2array($this->loadXMLConf()); + $this->db = new dbquery(); + $this->db->connect(); + $this->db2 = new dbquery(); + $this->db2->connect(); + $this->db3 = new dbquery(); + $this->db3->connect(); + $this->export(); + } + + private function export() + { + $return_createQuery = $this->createQuery(); + //echo $return_createQuery;exit; + if (!$this->db->query($return_createQuery, true)) { + $_SESSION['error'] = 'Erreur SQL 1'; + } + $resultQuery[0] = $this->getHeaderOfCsv(); + $cpt = 1; + while ($return_dbQuery = $this->db->fetch_object()) { + + $return_dbQuery->subject = utf8_decode($return_dbQuery->subject); + $resultQuery[$cpt] = $this->functions->object2array($return_dbQuery); + if ( isset($this->return_loadXMLConf[$this->coll]['FUNCTIONS']['COPIES']) + && !empty($this->return_loadXMLConf[$this->coll]['FUNCTIONS']['COPIES'])) { + $resultQuery[$cpt][$this->return_loadXMLConf[$this->coll]['FUNCTIONS']['COPIES']] = substr($this->functions_copies($return_dbQuery->res_id), 0, -2); + } + + $resultQuery[$cpt]['commentaire'] = ''; + $cpt++; + } + + $return_array2CSV = $this->array2CSV($resultQuery); + $_SESSION['export']['filename'] = $return_array2CSV; + } + + private function createQuery() + { + $query = 'SELECT '; + for ($i=0; $i<count($this->return_loadXMLConf[$this->coll]['FIELD']); $i++) { + $query .= $this->return_loadXMLConf[$this->coll]['FIELD'][$i]['DATABASE_FIELD']; + if ($i <> (count($this->return_loadXMLConf[$this->coll]['FIELD']) - 1)) { + $query .= ', '; + } + } + + $query .= ' '.substr($_SESSION['last_select_query'], stripos($_SESSION['last_select_query'], 'FROM')); + + return $query; + } + + private function array2CSV($resultQuery) + { + do { + $csvName = $_SESSION['user']['UserId'] . '-' . md5(date('Y-m-d H:i:s')) . '.csv'; + if (isset($pathToCsv) && !empty($pathToCsv)) { + $csvName = $_SESSION['user']['UserId'] . '-' . md5($pathToCsv) . '.csv'; + } + $pathToCsv = $_SESSION['config']['tmppath'] . $csvName; + } while (file_exists($pathToCsv)); + + $csvFile = fopen($pathToCsv, 'a+'); + + foreach ($resultQuery as $fields) { + fputcsv($csvFile, $fields, ';'); + } + + fclose($csvFile); + + return $csvName; + } + + private function getHeaderOfCsv() + { + //echo '<pre>'.print_r($loadXMLConf, true).'</pre>';exit; + for($i=0; $i < count($this->return_loadXMLConf[$this->coll]['FIELD']); $i++) { + + if (!empty($this->return_loadXMLConf[$this->coll]['FIELD'][$i]['LIBELLE'])) { + $tabToReturn[$i] = $this->return_loadXMLConf[$this->coll]['FIELD'][$i]['LIBELLE']; + } else { + $tabToReturn[$i] = $this->return_loadXMLConf[$this->coll]['FIELD'][$i]['DATABASE_FIELD']; + } + } + + $temp = array_keys($this->return_loadXMLConf[$this->coll]['FUNCTIONS']); + for ($k=0; $k<count($temp); $k++) { + $j = $i+$k; + $tabToReturn[$j] = $this->return_loadXMLConf[$this->coll]['FUNCTIONS'][$temp[$k]]; + } + if ($k == 0) { + $j = $i; + } + + $tabToReturn[$j+1] = $this->return_loadXMLConf[$this->coll]['FIXE']; + + return $tabToReturn; + } + + private function loadXMLConf() + { + $path = 'apps/maarch_entreprise/xml/export.xml'; + if (file_exists('custom/'.$_SESSION['custom_override_id'].'/apps/maarch_entreprise/xml/export.xml')) { + $path = 'custom/'.$_SESSION['custom_override_id'].'/apps/maarch_entreprise/xml/export.xml'; + } + $exportConf = simplexml_load_file($path); + return $exportConf; + } + + private function functions_copies($res_id) + { + $return_functionsCopies = ''; + $queryListinstance = "SELECT item_id, item_type FROM listinstance WHERE res_id = ".$res_id." AND coll_id = '".$this->coll."' AND item_mode = 'cc'"; + if (!$this->db2->query($queryListinstance, true)) { + $_SESSION['error'] = 'Erreur SQL 2'; + } + $j = 0; + while ($return_dbQueryListinstance = $this->db2->fetch_object()) { + if ($return_dbQueryListinstance->item_type == 'user_id') { + $queryUsersEntities = "SELECT entity_id FROM users_entities WHERE user_id = '".$return_dbQueryListinstance->item_id."' AND primary_entity = 'Y'"; + if (!$this->db3->query($queryUsersEntities, true)) { + $_SESSION['error'] = 'Erreur SQL 3'; + } + while ($return_dbQueryUsersEntities = $this->db3->fetch_object()) { + $usersEntities = $return_dbQueryListinstance->item_id.' : '.$return_dbQueryUsersEntities->entity_id; + } + } else { + $usersEntities = $return_dbQueryListinstance->item_id; + } + + $return_functionsCopies .= $usersEntities . ' | '; + $j++; + } + + return $return_functionsCopies; + } } diff --git a/core/trunk/core/class/class_request.php b/core/trunk/core/class/class_request.php index 12bea0bdd0c79e5eea89f086365c49aa7fee0cc8..43e5ffdd970f3915baa4cfa2463580d1c1d43bdd 100644 --- a/core/trunk/core/class/class_request.php +++ b/core/trunk/core/class/class_request.php @@ -160,7 +160,11 @@ class request extends dbquery } $query = $this->limit_select(0, $limit, $field_string, $table_string." ".$join, $where_string, $other, $dist); - + + if (preg_match('/res_view/i', $query)) { + $_SESSION['last_select_query'] = $query; + } + $this->connect(); $res_query = $this->query($query, $catch_error);