From 5ddf93d6fd0672a9d3ab273d8f21655d792a17d9 Mon Sep 17 00:00:00 2001 From: Laurent Giovannoni <laurent.giovannoni@maarch.org> Date: Thu, 2 Feb 2012 13:51:22 +0000 Subject: [PATCH] evo: Export --- core/trunk/core/class/ExportControler.php | 186 +++++ core/trunk/core/class/class_request.php | 884 +++++++++++----------- 2 files changed, 630 insertions(+), 440 deletions(-) create mode 100755 core/trunk/core/class/ExportControler.php diff --git a/core/trunk/core/class/ExportControler.php b/core/trunk/core/class/ExportControler.php new file mode 100755 index 00000000000..fa7f30c3125 --- /dev/null +++ b/core/trunk/core/class/ExportControler.php @@ -0,0 +1,186 @@ +<?php +/* +* Copyright 2008,2009,2010 Maarch +* +* This file is part of Maarch Framework. +* +* Maarch Framework is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* Maarch Framework is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with Maarch Framework. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** +* @brief Contains the ExportControler Object +* +* +* @file +* @author Arnaud Veber +* @date $date$ +* @version $Revision$ +* @ingroup core +*/ + +//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(''); +} catch (Exception $e) { + 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() + { + $exportConf = simplexml_load_file('apps/maarch_entreprise/xml/export.xml'); + 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 397f0e6f097..87dfe2ae839 100644 --- a/core/trunk/core/class/class_request.php +++ b/core/trunk/core/class/class_request.php @@ -1,440 +1,444 @@ -<?php -/* -* Copyright 2008,2009 Maarch -* -* This file is part of Maarch Framework. -* -* Maarch Framework is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Maarch Framework is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Maarch Framework. If not, see <http://www.gnu.org/licenses/>. -*/ - -/** -* @brief Contains all the function to build a SQL query -* -* @file -* @author Loïc Vinet <dev@maarch.org> -* @author Claire Figueras <dev@maarch.org> -* @date $date$ -* @version $Revision$ -* @ingroup core -*/ - -/** -* @brief Contains all the function to build a SQL query (select, insert and update) -* -* @ingroup core -*/ -class request extends dbquery -{ - /** - * Constructs the select query and returns the results in an array - * - * @param $select array Query fields - * @param $where string Where clause of the query - * @param $other string Query complement (order by, ...) - * @param $database_type string Type of the database (MYSQL, POSTGRESQL, ...) - * @param $limit string Maximum numbers of results (500 by default) - * @param $left_join boolean Is the request is a left join ? (false by default) - * @param $first_join_table string Name of the first join table (empty by default) - * @param $second_join_table string Name of the second join table (empty by default) - * @param $join_key string Key of the join (empty by default) - * @param $add_security string Add the user security where clause or not (true by default) - * @param $distinct_argument Add the distinct parameters in the sql query (false by default) - * @return array Results of the built query - */ - public function select($select, $where, $other, $database_type, $limit="default", $left_join=false, $first_join_table="", $second_join_table="", $join_key="", $add_security = true, $catch_error = false, $distinct_argument = false) - { - if($limit == 0) - { - $limit=$_SESSION['config']['databasesearchlimit']; - } - elseif($limit == "default") - { - $limit=$_SESSION['config']['databasesearchlimit']; - } - //Extracts data in the first argument : $select. - $tab_field = array(); - $table = ''; - $table_string = ''; - $field_string = ''; - foreach (array_keys($select) as $value) - { - $table = $value; - $table_string .= $table.","; - foreach ($select[$value] as $subvalue) - { - $field = $subvalue; - $field_string .= $table.".".$field.","; - } - //Query fields and table names have been wrote in 2 strings - } - //Strings need to be cleaned - $table_string = substr($table_string, 0, -1); - $field_string = substr($field_string, 0, -1); - - //Extracts data from the second argument : the where clause - if (trim($where) <> "") - { - $where_string = " where ".$where; - } - else - { - $where_string = ""; - } - $join = ''; - if($left_join) - { - //Reste table string - $table_string = ""; - - //Add more table in join syntax - foreach (array_keys($select) as $value) - { - if ($value <> $first_join_table && $value <> $second_join_table) - { - $table_string = $value.","; - } - } - - $join = " left join "; - $table_string .= $first_join_table; - $join .= $second_join_table." on ".$second_join_table.".".$join_key." = ".$first_join_table.".".$join_key; - } - - if($add_security) - { -/* - for($i=0; $i < count($_SESSION['user']['security']); $i++) - { - if(isset($_SESSION['user']['security'][$i]['table']) && isset($_SESSION['user']['security'][$i]['coll_id'])) - { - if(preg_match('/'.$_SESSION['user']['security'][$i]['table'].'/',$table_string) || preg_match('/'.$_SESSION['user']['security'][$i]['view'].'/',$table_string) ) - { - if(empty($where_string)) - { - $where_string = " where ( ".$_SESSION['user']['security'][$i]['where']." ) "; - } - else - { - $where_string = ''.$where_string." and ( ".$_SESSION['user']['security'][$i]['where']." ) "; - } - break; - } - } - } -*/ - foreach(array_keys($_SESSION['user']['security']) as $coll) - { - if(isset($_SESSION['user']['security'][$coll]['DOC']['table'])) - { - if(preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['table'].'/',$table_string) || preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['view'].'/',$table_string) ) - { - if(empty($where_string)) - { - $where_string = " where ( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) "; - } - else - { - $where_string = ''.$where_string." and ( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) "; - } - break; - } - } - } - } - //Time to create the SQL Query - $query = ""; - $dist = ''; - if($distinct_argument == true) - { - $dist = " distinct "; - } - if($database_type == "SQLSERVER") - { - $query = "SELECT TOP ".$limit." ".$dist.$field_string." FROM ".$table_string." ".$join." ".$where_string." ".$other; - } - elseif($database_type == "MYSQL" ) - { - $query = "SELECT ".$dist.$field_string." FROM ".$table_string.' '.$join.' '.$where_string." ".$other." LIMIT 0,".$limit." "; - } - elseif($database_type == "POSTGRESQL" ) - { - $query = "SELECT ".$dist.$field_string." FROM ".$table_string.' '.$join.' '.$where_string." ".$other." OFFSET 0 LIMIT ".$limit." "; - } - elseif($database_type == "ORACLE" ) - { - if ($limit <> '') - { - $orcl_limit = $limit; - $orcl_limit = " rownum <= ".$orcl_limit; - if ($where_string <> '') { $orcl_limit = " and ".$orcl_limit; } else { $orcl_limit = " where ".$orcl_limit; } - } - $query = "SELECT ".$dist.$field_string." FROM ".$table_string.' '.$join.' '.$where_string." ".$orcl_limit." ".$other." "; - } - $this->connect(); - - $res_query = $this->query($query, $catch_error); - if($catch_error && !$res_query) - { - return false; - } - $result=array(); - while($line = $this->fetch_array()) - { - $temp= array(); - foreach (array_keys($line) as $resval) - { - if (!is_int($resval)) - { - if ($_SESSION['config']['databasetype'] == "ORACLE") - { - array_push($temp,array('column'=>strtolower($resval),'value'=>$line[$resval])); - } - else - { - array_push($temp,array('column'=>$resval,'value'=>$line[$resval])); - } - } - } - array_push($result,$temp); - } - if(count($result) == 0 && $catch_error) - { - return true; - } - return $result; - } - - /** - * Builds the insert query and sends it to the database - * - * @param string $table table to insert - * @param array $data data to insert - * @param array $database_type type of the database (MYSQL, POSTGRESQL, ...) - * @return bool True if the query was sent ok and processed by the database without error, False otherwise - */ - public function insert($table, $data, $database_type) - { - $field_string = "( "; - $value_string = "( "; - - for($i=0; $i < count($data);$i++) - { - $field_string .= $data[$i]['column'].","; - if($data[$i]['type'] == "string" || $data[$i]['type'] == "date") - { - $value_string .= "'".$data[$i]['value']."',"; - } - else - { - $value_string .= $data[$i]['value'].","; - } - } - $value_string = substr($value_string, 0, -1); - $field_string = substr($field_string, 0, -1); - - $value_string .= ")"; - $field_string .= ")"; - - //Time to create the SQL Query - $query = ""; - $query = "INSERT INTO ".$table." ".$field_string." VALUES ".$value_string ; - - $this->connect(); - return ($this->query($query, true)); - } - - /** - * Constructs the update query and sends it to the database - * - * @param $table string Table to update - * @param $data array Data to update - * @param $where array Where clause of the query - * @param $database_type array Type of the database (MYSQL, POSTGRESQL, ...) - */ - public function update($table, $data, $where, $databasetype) - { - $update_string = ""; - for($i=0; $i < count($data);$i++) - { - if($data[$i]['type'] == "string" || $data[$i]['type'] == "date") - { - if($databasetype == "POSTGRESQL" && $data[$i]['type'] == "date" && ($data[$i]['value'] == '' || $data[$i]['value'] == ' ')) - { - $update_string .= $data[$i]['column']."=NULL,"; - } - else - { - if(trim(strtoupper($data[$i]['value'])) == "SYSDATE") - { - $update_string .= $data[$i]['column']."=sysdate,"; - } - else - { - $update_string .= $data[$i]['column']."='".$data[$i]['value']."',"; - } - } - } - else - { - $update_string .= $data[$i]['column']."=".$data[$i]['value'].","; - } - } - $update_string = substr($update_string, 0, -1); - if ($where <> "") - { - $where_string = " WHERE ".$where; - } - else - { - $where_string = ""; - } - //Time to create the SQL Query - $query = ""; - $query = "UPDATE ".$table." SET ".$update_string.$where_string; - //echo $query; - $this->connect(); - return $this->query($query, true); - } - - /** - * Return current datetime instruction for each SQL database - * - * @author Loïc Vinet <dev@maarch.org> - */ - public function current_datetime() - { - - if($_SESSION['config']['databasetype'] == "SQLSERVER") - { - return ' getdate() '; - } - elseif( ($_SESSION['config']['databasetype'] == "MYSQL" || $_SESSION['config']['databasetype'] == "POSTGRESQL")) - { - return ' now() '; - } - elseif($_SESSION['config']['databasetype'] == "ORACLE") - { - return ' sysdate '; - } - } - /** - * Returns the correct SQL instruction (depending of the database type) for extracting a date or a date part from a datetime field - * - * @param $date_field String The name of the date field - * @param $arg String Date part : 'year', 'month', 'day', 'hour', 'minute' or 'second'; if empty return the all date, empty by default - * @return String SQL instruction - */ - public function extract_date($date_field, $arg = '') - { - - if($_SESSION['config']['databasetype'] == "SQLSERVER") - { - // TO DO - return $date_field; - } - elseif( $_SESSION['config']['databasetype'] == "MYSQL" || $_SESSION['config']['databasetype'] == "POSTGRESQL" ) - { - if(empty($arg)) - { - return ' date('.$date_field.')'; - } - else - { - if($_SESSION['config']['databasetype'] == "MYSQL") - { - switch($arg) - { - case 'year' : - return ' date_format('.$date_field.', %Y)'; - case 'month' : - return ' date_format('.$date_field.', %m)'; - case 'day' : - return ' date_format('.$date_field.', %d)'; - case 'hour' : - return ' date_format('.$date_field.', %k)'; - case 'minute' : - return ' date_format('.$date_field.', %i)'; - case 'second' : - return ' date_format('.$date_field.', %s)'; - default : - return ' date('.$date_field.')'; - } - } - else if($_SESSION['config']['databasetype'] == "POSTGRESQL") - { - switch($arg) - { - case 'year' : - return " date_part( 'year', ".$date_field.")"; - case 'month' : - return " date_part( 'month', ".$date_field.")"; - case 'day' : - return " date_part( 'day', ".$date_field.")"; - case 'hour' : - return " date_part( 'hour', ".$date_field.")"; - case 'minute' : - return " date_part( 'minute', ".$date_field.")"; - case 'second' : - return " date_part( 'second', ".$date_field.")"; - default : - return ' date('.$date_field.')'; - } - } - - } - } - elseif($_SESSION['config']['databasetype'] == "ORACLE") - { - switch($arg) - { - case 'year' : - return " to_char(".$date_field.", 'YYYY')"; - case 'month' : - return " to_char(".$date_field.", 'MM')"; - case 'day' : - return " to_char(".$date_field.", 'DD')"; - case 'hour' : - return " to_char(".$date_field.", 'HH24')"; - case 'minute' : - return " to_char(".$date_field.", 'MI')"; - case 'second' : - return " to_char(".$date_field.", 'SS')"; - default : - return " to_char(".$date_field.", 'DD/MM/YYYY')"; - } - } - } - - public function get_date_diff($date1, $date2) - { - if($_SESSION['config']['databasetype'] == "MYSQL") - { - return 'datediff('.$date1.', '.$date2.')'; - } - elseif($_SESSION['config']['databasetype'] == "POSTGRESQL") - { - return $this->extract_date($date1).' - '.$this->extract_date($date2); - } - elseif($_SESSION['config']['databasetype'] == "ORACLE") - { - return $this->extract_date($date1).' - '.$this->extract_date($date2); - } - else if($_SESSION['config']['databasetype'] == "SQLSERVER") - { - // TO DO - return ''; - } - } -} -?> +<?php +/* +* Copyright 2008,2009 Maarch +* +* This file is part of Maarch Framework. +* +* Maarch Framework is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* Maarch Framework is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with Maarch Framework. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** +* @brief Contains all the function to build a SQL query +* +* @file +* @author Loïc Vinet <dev@maarch.org> +* @author Claire Figueras <dev@maarch.org> +* @date $date$ +* @version $Revision$ +* @ingroup core +*/ + +/** +* @brief Contains all the function to build a SQL query (select, insert and update) +* +* @ingroup core +*/ +class request extends dbquery +{ + /** + * Constructs the select query and returns the results in an array + * + * @param $select array Query fields + * @param $where string Where clause of the query + * @param $other string Query complement (order by, ...) + * @param $database_type string Type of the database (MYSQL, POSTGRESQL, ...) + * @param $limit string Maximum numbers of results (500 by default) + * @param $left_join boolean Is the request is a left join ? (false by default) + * @param $first_join_table string Name of the first join table (empty by default) + * @param $second_join_table string Name of the second join table (empty by default) + * @param $join_key string Key of the join (empty by default) + * @param $add_security string Add the user security where clause or not (true by default) + * @param $distinct_argument Add the distinct parameters in the sql query (false by default) + * @return array Results of the built query + */ + public function select($select, $where, $other, $database_type, $limit="default", $left_join=false, $first_join_table="", $second_join_table="", $join_key="", $add_security = true, $catch_error = false, $distinct_argument = false) + { + if($limit == 0) + { + $limit=$_SESSION['config']['databasesearchlimit']; + } + elseif($limit == "default") + { + $limit=$_SESSION['config']['databasesearchlimit']; + } + //Extracts data in the first argument : $select. + $tab_field = array(); + $table = ''; + $table_string = ''; + $field_string = ''; + foreach (array_keys($select) as $value) + { + $table = $value; + $table_string .= $table.","; + foreach ($select[$value] as $subvalue) + { + $field = $subvalue; + $field_string .= $table.".".$field.","; + } + //Query fields and table names have been wrote in 2 strings + } + //Strings need to be cleaned + $table_string = substr($table_string, 0, -1); + $field_string = substr($field_string, 0, -1); + + //Extracts data from the second argument : the where clause + if (trim($where) <> "") + { + $where_string = " where ".$where; + } + else + { + $where_string = ""; + } + $join = ''; + if($left_join) + { + //Reste table string + $table_string = ""; + + //Add more table in join syntax + foreach (array_keys($select) as $value) + { + if ($value <> $first_join_table && $value <> $second_join_table) + { + $table_string = $value.","; + } + } + + $join = " left join "; + $table_string .= $first_join_table; + $join .= $second_join_table." on ".$second_join_table.".".$join_key." = ".$first_join_table.".".$join_key; + } + + if($add_security) + { +/* + for($i=0; $i < count($_SESSION['user']['security']); $i++) + { + if(isset($_SESSION['user']['security'][$i]['table']) && isset($_SESSION['user']['security'][$i]['coll_id'])) + { + if(preg_match('/'.$_SESSION['user']['security'][$i]['table'].'/',$table_string) || preg_match('/'.$_SESSION['user']['security'][$i]['view'].'/',$table_string) ) + { + if(empty($where_string)) + { + $where_string = " where ( ".$_SESSION['user']['security'][$i]['where']." ) "; + } + else + { + $where_string = ''.$where_string." and ( ".$_SESSION['user']['security'][$i]['where']." ) "; + } + break; + } + } + } +*/ + foreach(array_keys($_SESSION['user']['security']) as $coll) + { + if(isset($_SESSION['user']['security'][$coll]['DOC']['table'])) + { + if(preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['table'].'/',$table_string) || preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['view'].'/',$table_string) ) + { + if(empty($where_string)) + { + $where_string = " where ( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) "; + } + else + { + $where_string = ''.$where_string." and ( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) "; + } + break; + } + } + } + } + //Time to create the SQL Query + $query = ""; + $dist = ''; + if($distinct_argument == true) + { + $dist = " distinct "; + } + if($database_type == "SQLSERVER") + { + $query = "SELECT TOP ".$limit." ".$dist.$field_string." FROM ".$table_string." ".$join." ".$where_string." ".$other; + } + elseif($database_type == "MYSQL" ) + { + $query = "SELECT ".$dist.$field_string." FROM ".$table_string.' '.$join.' '.$where_string." ".$other." LIMIT 0,".$limit." "; + } + elseif($database_type == "POSTGRESQL" ) + { + $query = "SELECT ".$dist.$field_string." FROM ".$table_string.' '.$join.' '.$where_string." ".$other." OFFSET 0 LIMIT ".$limit." "; + } + elseif($database_type == "ORACLE" ) + { + if ($limit <> '') + { + $orcl_limit = $limit; + $orcl_limit = " rownum <= ".$orcl_limit; + if ($where_string <> '') { $orcl_limit = " and ".$orcl_limit; } else { $orcl_limit = " where ".$orcl_limit; } + } + $query = "SELECT ".$dist.$field_string." FROM ".$table_string.' '.$join.' '.$where_string." ".$orcl_limit." ".$other." "; + } + $this->connect(); + + if (preg_match('/res_view/i', $query)) { + $_SESSION['last_select_query'] = $query; + } + + $res_query = $this->query($query, $catch_error); + if($catch_error && !$res_query) + { + return false; + } + $result=array(); + while($line = $this->fetch_array()) + { + $temp= array(); + foreach (array_keys($line) as $resval) + { + if (!is_int($resval)) + { + if ($_SESSION['config']['databasetype'] == "ORACLE") + { + array_push($temp,array('column'=>strtolower($resval),'value'=>$line[$resval])); + } + else + { + array_push($temp,array('column'=>$resval,'value'=>$line[$resval])); + } + } + } + array_push($result,$temp); + } + if(count($result) == 0 && $catch_error) + { + return true; + } + return $result; + } + + /** + * Builds the insert query and sends it to the database + * + * @param string $table table to insert + * @param array $data data to insert + * @param array $database_type type of the database (MYSQL, POSTGRESQL, ...) + * @return bool True if the query was sent ok and processed by the database without error, False otherwise + */ + public function insert($table, $data, $database_type) + { + $field_string = "( "; + $value_string = "( "; + + for($i=0; $i < count($data);$i++) + { + $field_string .= $data[$i]['column'].","; + if($data[$i]['type'] == "string" || $data[$i]['type'] == "date") + { + $value_string .= "'".$data[$i]['value']."',"; + } + else + { + $value_string .= $data[$i]['value'].","; + } + } + $value_string = substr($value_string, 0, -1); + $field_string = substr($field_string, 0, -1); + + $value_string .= ")"; + $field_string .= ")"; + + //Time to create the SQL Query + $query = ""; + $query = "INSERT INTO ".$table." ".$field_string." VALUES ".$value_string ; + + $this->connect(); + return ($this->query($query, true)); + } + + /** + * Constructs the update query and sends it to the database + * + * @param $table string Table to update + * @param $data array Data to update + * @param $where array Where clause of the query + * @param $database_type array Type of the database (MYSQL, POSTGRESQL, ...) + */ + public function update($table, $data, $where, $databasetype) + { + $update_string = ""; + for($i=0; $i < count($data);$i++) + { + if($data[$i]['type'] == "string" || $data[$i]['type'] == "date") + { + if($databasetype == "POSTGRESQL" && $data[$i]['type'] == "date" && ($data[$i]['value'] == '' || $data[$i]['value'] == ' ')) + { + $update_string .= $data[$i]['column']."=NULL,"; + } + else + { + if(trim(strtoupper($data[$i]['value'])) == "SYSDATE") + { + $update_string .= $data[$i]['column']."=sysdate,"; + } + else + { + $update_string .= $data[$i]['column']."='".$data[$i]['value']."',"; + } + } + } + else + { + $update_string .= $data[$i]['column']."=".$data[$i]['value'].","; + } + } + $update_string = substr($update_string, 0, -1); + if ($where <> "") + { + $where_string = " WHERE ".$where; + } + else + { + $where_string = ""; + } + //Time to create the SQL Query + $query = ""; + $query = "UPDATE ".$table." SET ".$update_string.$where_string; + //echo $query; + $this->connect(); + return $this->query($query, true); + } + + /** + * Return current datetime instruction for each SQL database + * + * @author Loïc Vinet <dev@maarch.org> + */ + public function current_datetime() + { + + if($_SESSION['config']['databasetype'] == "SQLSERVER") + { + return ' getdate() '; + } + elseif( ($_SESSION['config']['databasetype'] == "MYSQL" || $_SESSION['config']['databasetype'] == "POSTGRESQL")) + { + return ' now() '; + } + elseif($_SESSION['config']['databasetype'] == "ORACLE") + { + return ' sysdate '; + } + } + /** + * Returns the correct SQL instruction (depending of the database type) for extracting a date or a date part from a datetime field + * + * @param $date_field String The name of the date field + * @param $arg String Date part : 'year', 'month', 'day', 'hour', 'minute' or 'second'; if empty return the all date, empty by default + * @return String SQL instruction + */ + public function extract_date($date_field, $arg = '') + { + + if($_SESSION['config']['databasetype'] == "SQLSERVER") + { + // TO DO + return $date_field; + } + elseif( $_SESSION['config']['databasetype'] == "MYSQL" || $_SESSION['config']['databasetype'] == "POSTGRESQL" ) + { + if(empty($arg)) + { + return ' date('.$date_field.')'; + } + else + { + if($_SESSION['config']['databasetype'] == "MYSQL") + { + switch($arg) + { + case 'year' : + return ' date_format('.$date_field.', %Y)'; + case 'month' : + return ' date_format('.$date_field.', %m)'; + case 'day' : + return ' date_format('.$date_field.', %d)'; + case 'hour' : + return ' date_format('.$date_field.', %k)'; + case 'minute' : + return ' date_format('.$date_field.', %i)'; + case 'second' : + return ' date_format('.$date_field.', %s)'; + default : + return ' date('.$date_field.')'; + } + } + else if($_SESSION['config']['databasetype'] == "POSTGRESQL") + { + switch($arg) + { + case 'year' : + return " date_part( 'year', ".$date_field.")"; + case 'month' : + return " date_part( 'month', ".$date_field.")"; + case 'day' : + return " date_part( 'day', ".$date_field.")"; + case 'hour' : + return " date_part( 'hour', ".$date_field.")"; + case 'minute' : + return " date_part( 'minute', ".$date_field.")"; + case 'second' : + return " date_part( 'second', ".$date_field.")"; + default : + return ' date('.$date_field.')'; + } + } + + } + } + elseif($_SESSION['config']['databasetype'] == "ORACLE") + { + switch($arg) + { + case 'year' : + return " to_char(".$date_field.", 'YYYY')"; + case 'month' : + return " to_char(".$date_field.", 'MM')"; + case 'day' : + return " to_char(".$date_field.", 'DD')"; + case 'hour' : + return " to_char(".$date_field.", 'HH24')"; + case 'minute' : + return " to_char(".$date_field.", 'MI')"; + case 'second' : + return " to_char(".$date_field.", 'SS')"; + default : + return " to_char(".$date_field.", 'DD/MM/YYYY')"; + } + } + } + + public function get_date_diff($date1, $date2) + { + if($_SESSION['config']['databasetype'] == "MYSQL") + { + return 'datediff('.$date1.', '.$date2.')'; + } + elseif($_SESSION['config']['databasetype'] == "POSTGRESQL") + { + return $this->extract_date($date1).' - '.$this->extract_date($date2); + } + elseif($_SESSION['config']['databasetype'] == "ORACLE") + { + return $this->extract_date($date1).' - '.$this->extract_date($date2); + } + else if($_SESSION['config']['databasetype'] == "SQLSERVER") + { + // TO DO + return ''; + } + } +} +?> -- GitLab