diff --git a/html/ajax_test.php b/html/ajax_test.php new file mode 100644 index 000000000..3ee7ad955 --- /dev/null +++ b/html/ajax_test.php @@ -0,0 +1,65 @@ +request('gDossier', "numeric",-1); +if ($gDossier==-1) +{ + echo " Vous devez donner le dossier avec paramètre gDossier dans l'url, exemple http://localhost/noalyss/html/test.php?gDossier=25"; + exit(); +} +$gDossierLogInput=$gDossier; +global $cn, $g_user, $g_succeed, $g_failed; +$cn=Dossier::connect(); + +$g_parameter=new Own($cn); +$g_user=new User($cn); + +if (!file_exists('authorized_debug')) +{ + echo "Pour pouvoir utiliser ce fichier vous devez creer un fichier nomme authorized_debug + dans le repertoire html du server"; + exit(); +} +define('ALLOWED', 1); +define('AJAX_TEST', 1); + +$w=$http->request("TestAjaxFile"); + +require_once $w; diff --git a/html/js/managetable.js b/html/js/managetable.js index 29e2f070c..84c2d88a8 100644 --- a/html/js/managetable.js +++ b/html/js/managetable.js @@ -74,7 +74,51 @@ var ManageTable = function (p_table_name) { this.callback = "ajax.php"; //!< File to call this.control = "dtr"; // p_element_row.cells[sort_column].getAttribute('sort_value')) { + var row=p_table.insertRow(i); + row.innerHTML=p_element_row.innerHTML; + row.id=p_element_row.id; + return; + } + } + p_table.appendChild(p_element_row); + } catch(e) { + console.log("insertRow failed with "+e.message); + throw e; + } + + }; var answer = {}; /** *@fn ManageTable.set_control @@ -170,7 +214,10 @@ var ManageTable = function (p_table_name) var new_row = new Element("tr"); new_row.id = answer['ctl_row']; new_row.innerHTML = answer['html']; - $("tb"+answer['ctl']).appendChild(new_row); + /** + * put the element at the right place + */ + here.insertRow($("tb"+answer['ctl']) , new_row,here.sort_column); } new Effect.Highlight(answer['ctl_row'] ,{startcolor: '#FAD4D4',endcolor: '#F78082' }); alternate_row_color("tb"+answer['ctl']); @@ -203,6 +250,7 @@ var ManageTable = function (p_table_name) this.param['action'] = 'delete'; this.param['ctl'] = p_ctl; var here=this; + $(p_ctl+"_"+p_id).addClassName("highlight"); smoke.confirm("Confirmez ?", function (e) { @@ -222,6 +270,9 @@ var ManageTable = function (p_table_name) } }); } + else { + $(p_ctl+"_"+p_id).removeClassName("highlight"); + } }) ; }; @@ -238,6 +289,7 @@ var ManageTable = function (p_table_name) this.param['ctl'] = p_ctl; var control = this.control; var here = this; + // display the form to enter data new Ajax.Request(this.callback, { parameters: this.param, @@ -249,7 +301,7 @@ var ManageTable = function (p_table_name) var obj = {id: control, "cssclass": "inner_box", "html": loading()}; add_div(obj); var pos = calcy(250); - $(obj.id).setStyle({position: "absolute", top: pos + 'px', width: "auto", "margin-left": "20%"}); + $(obj.id).setStyle({position: "fixed", top: '250px', width: "auto", "margin-left": "20%"}); $(obj.id).update(x['html']); } catch (e) { smoke.alert("ERREUR " + e.message); diff --git a/html/js/sorttable.js b/html/js/sorttable.js index d2de7e58c..2873e987f 100644 --- a/html/js/sorttable.js +++ b/html/js/sorttable.js @@ -39,7 +39,7 @@ var stIsIE = /*@cc_on!@*/false; -sorttable = { +var sorttable = { init: function () { // quit if this function has already been called if (arguments.callee.done) diff --git a/include/class/acc_plan_mtable.class.php b/include/class/acc_plan_mtable.class.php new file mode 100644 index 000000000..ef1af2723 --- /dev/null +++ b/include/class/acc_plan_mtable.class.php @@ -0,0 +1,120 @@ + + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/*** + * @file + * @brief + * + */ +require_once NOALYSS_INCLUDE.'/database/acc_plan_sql.class.php'; +require_once NOALYSS_INCLUDE.'/lib/manage_table_sql.class.php'; + +class Acc_Plan_MTable extends Manage_Table_SQL +{ + function __construct(Acc_Plan_SQL $p_table) + { + $this->table = $p_table; + parent::__construct($p_table); + //-------------------------------------------------------------- + //Set the table header + //-------------------------------------------------------------- + $this->set_col_label("pcm_val", _("Poste Comptable")); + $this->set_col_label("pcm_type", _("Type")); + $this->set_col_label("pcm_lib", _("Libellé")); + $this->set_col_label("parent_accounting", _("Dépend")); + $this->set_col_label("fiche_qcode", _("Fiche")); + //-------------------------------------------------------------- + $this->set_property_visible("id", FALSE); + $this->set_property_updatable("fiche_qcode", FALSE); + $this->set_col_type("pcm_type", "select", [ + ["label"=>_("Actif"),"value"=>"ACT"], + ["label"=>_("Actif inversé"),"value"=>"ACTINV"], + ["label"=>_("Passif"),"value"=>"PAS"], + ["label"=>_("Passif Inversé"),"value"=>"PASINV"], + ["label"=>_("Charge"),"value"=>"CHA"], + ["label"=>_("Charge inversé"),"value"=>"CHAINV"], + ["label"=>_("Produit"),"value"=>"PRO"], + ["label"=>_("Produit inversé"),"value"=>"PROINV"], + ["label"=>_("Contexte"),"value"=>"CON"] + ]); + } + /** + * Display a row + * @param type $p_row array of value key column=>value + */ + function display_row($p_row) + { + printf('', + $this->object_name, + $p_row[$this->table->primary_key]) + ; + + + $nb_order=count($this->a_order); + for ($i=0; $i<$nb_order; $i++) + { + $v=$this->a_order[$i]; + if ($v=="pcm_val") + { + $js=sprintf("onclick=\"%s.input('%s','%s');\"", $this->object_name, + $p_row[$this->table->primary_key], $this->object_name); + echo sprintf('%s', + htmlspecialchars($p_row[$v]), + HtmlInput::anchor($p_row[$v], "", $js)).''; + } + elseif ($v == "fiche_qcode") { + $count=$this->table->cn->get_value("select count(*) from fiche_detail where ad_id=5 and ad_value=$1",array($p_row['pcm_val'])); + if ($count == 0) echo td(""); + elseif ($count == 1 ) { echo td($p_row[$v]) ; } + elseif ($count > 1) { echo td($p_row[$v] . " ($count) ");} + } + else + { + if ( ! $this->get_property_visible($v)) continue; + echo td($p_row[$v]); + } + } + $this->display_icon_del($p_row); + + + + echo ''; + } + function check() + { + $cn=Dossier::connect(); + $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1 and id <> $2", + array($this->table->pcm_val,$this->table->id)); + if ($count > 0 ) { + $this->set_error("pcm_val", _("Poste comptable est unique")); + } + if ( trim($this->table->pcm_val) == "") { + $this->set_error("pcm_val", _("Poste comptable ne peut être vide")); + } + if ( trim($this->table->parent_accounting) == "") { + $this->set_error("parent_accounting", _("Poste comptable dépendant ne peut pas être vide")); + } + if ( count($this->aerror) > 0 ) return false; + return true; + } + + +} diff --git a/include/database/acc_plan_sql.class.php b/include/database/acc_plan_sql.class.php new file mode 100644 index 000000000..40d93b04a --- /dev/null +++ b/include/database/acc_plan_sql.class.php @@ -0,0 +1,162 @@ + + +/** + * @file + * @brief + * @param type $name Descriptionara + */ +require_once NOALYSS_INCLUDE."/lib/data_sql.class.php"; +require_once NOALYSS_INCLUDE."/database/tmp_pcmn_sql.class.php"; + +class Acc_Plan_SQL extends Data_SQL +{ + + private $limit_fiche_qcode; + function __construct($p_cn, $p_id=-1) + { + $this->table = "accounting_card"; + $this->primary_key = "id"; + $this->limit_fiche_qcode=0; + $this->name = array( + "id" => "id", + "pcm_val"=>"pcm_val", + "parent_accounting"=>"parent_accounting", + "pcm_lib"=>"pcm_lib", + "pcm_type"=>"pcm_type", + "fiche_qcode"=>"fiche_qcode" + ); + + $this->type = array( + "id" => "numeric", + "pcm_val" => "text", + "parent_accounting" => "text", + "pcm_lib" => "text", + "pcm_type" => "text", + "fiche_qcode"=>"string" + ); + + $this->default = array( + "id" => "auto", + "fiche_qcode"=>"auto" + ); + $this->sql=" + SELECT pcm_val, + pcm_lib, + pcm_val_parent as parent_accounting, + pcm_type, + id, + (select string_agg(m.fiche_qcode,' , ') + from (select a.ad_value as fiche_qcode + from fiche_detail as a + join fiche_detail as b on (a.ad_id=23 and a.f_id=b.f_id and b.ad_id=5) + where b.ad_value=pcm_val::text order by a.ad_value %s)as m) as fiche_qcode + FROM public.tmp_pcmn + +"; + parent::__construct($p_cn,$p_id); + } + + public function count($p_where="", $p_array=null) + { + throw new Exception("not implemented"); + } + + public function delete() + { + $obj=new Tmp_Pcmn_SQL($this->cn,$this->id); + return $obj->delete(); + + } + + public function exist() + { + $obj=new Tmp_Pcmn_SQL($this->cn,$this->id); + return $obj->exist(); + } + + public function insert() + { + $obj=new Tmp_Pcmn_SQL($this->cn); + $obj->set("pcm_val",$this->pcm_val); + $obj->set("pcm_lib",$this->pcm_lib); + $obj->set("pcm_type",$this->pcm_type); + $obj->set("pcm_val_parent",$this->parent_accounting); + $obj->insert(); + $this->id=$obj->id; + } + + public function load() + { + $pk=$this->primary_key; + if ( $this->get_limit_fiche_qcode() != 0 ) + { + $sql=sprintf($this->sql," limit ".$this->get_limit_fiche_qcode()); + } else + { + $sql=sprintf($this->sql," "); + } + $result=$this->cn->get_array($sql. " where id=$1",array ($this->$pk)); + if ($this->cn->count()==0) + { + $this->$pk=-1; + return; + } + + foreach ($result[0] as $key=> $value) + { + $this->$key=$value; + } + } + + public function seek($cond='', $p_array=null) + { + if ( $this->get_limit_fiche_qcode() != 0 ) + { + $sql=sprintf($this->sql," limit ".$this->get_limit_fiche_qcode()); + } else + { + $sql=sprintf($this->sql," "); + } + $ret=$this->cn->exec_sql($sql." ".$cond,$p_array); + return $ret; + } + + public function update() + { + $obj=new Tmp_Pcmn_SQL($this->cn,$this->id); + $obj->set("pcm_val",$this->pcm_val); + $obj->set("pcm_lib",$this->pcm_lib); + $obj->set("pcm_type",$this->pcm_type); + $obj->set("pcm_val_parent",$this->parent_accounting); + $obj->update(); + } + public function get_limit_fiche_qcode() + { + return $this->limit_fiche_qcode; + } + + public function set_limit_fiche_qcode($limit_fiche_qcode) + { + $this->limit_fiche_qcode=$limit_fiche_qcode; + } + +} \ No newline at end of file diff --git a/include/database/tmp_pcmn_sql.class.php b/include/database/tmp_pcmn_sql.class.php new file mode 100644 index 000000000..183886d02 --- /dev/null +++ b/include/database/tmp_pcmn_sql.class.php @@ -0,0 +1,63 @@ + + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/* * * + * @file + * @brief + * + */ +require_once NOALYSS_INCLUDE."/lib/noalyss_sql.class.php"; +class Tmp_Pcmn_SQL extends Noalyss_SQL +{ + + /** + * @brief manage table key_distribution_detail + */ + function __construct(&$p_cn, $p_id=-1) + { + + $this->table="public.tmp_pcmn"; + $this->primary_key="id"; + + $this->name=array( + "id"=>"id", + "pcm_val"=>"pcm_val", + "pcm_type"=>"pcm_type", + "pcm_val_parent"=>"pcm_val_parent", + "pcm_lib"=>"pcm_lib" + ); + + $this->type=array( + "id"=>"numeric", + "pcm_val"=>"text", + "pcm_type"=>"text", + "pcm_val_parent"=>"text", + "pcm_lib"=>"text" + ); + + $this->default=array( + "id"=>"auto" + ); + + parent::__construct($p_cn, $p_id); + } + +} diff --git a/include/lib/data_sql.class.php b/include/lib/data_sql.class.php new file mode 100644 index 000000000..99f1af68d --- /dev/null +++ b/include/lib/data_sql.class.php @@ -0,0 +1,306 @@ +table = "schema.table"; + $this->primary_key = "o_id"; + + $this->name=array( + "id"=>"o_id", + "program"=>"o_prog", + "date"=>"o_date", + "qcode"=>"o_qcode", + "fiche"=>"f_id", + + + ); + + $this->type = array( + "o_id"=>"numeric", + "o_prog"=>"numeric", + "o_date"=>"date", + "o_qcode"=>"text", + "f_id"=>"numeric", + + ); + + $this->default = array( + "o_id" => "auto", + ); + $this->date_format = "DD.MM.YYYY"; + global $cn; + + parent::__construct($cn,$p_id); + } + + } + * @endcode + * + */ +abstract class Data_SQL +{ + + function __construct($p_cn, $p_id=-1) + { + $this->cn=$p_cn; + $pk=$this->primary_key; + $this->$pk=$p_id; + // check that the definition is correct + if (count($this->name) != count($this->type) ){ + throw new Exception (__FILE__." $this->table Cannot instantiate"); + } + /* Initialize an empty object */ + foreach ($this->name as $key) + { + $this->$key=null; + } + $this->$pk=$p_id; + /* load it */ + if ($p_id != -1 )$this->load(); + } +/** + * Insert or update : if the row already exists, update otherwise insert + */ + public function save() + { + $count = $this->exist(); + + if ($count == 0) + $this->insert(); + else + $this->update(); + } + /** + *@brief get the value thanks the colum name and not the alias (name). + *@see getp + */ + public function get($p_string) + { + if (array_key_exists($p_string, $this->type)) { + return $this->$p_string; + } + else + throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string); + } + + /** + *@brief set the value thanks the colum name and not the alias (name) + *@see setp + */ + public function set($p_string, $p_value) + { + if (array_key_exists($p_string, $this->type)) { + $this->$idx=$p_value; + } else + throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string); + } + + /** + *@brief set the value thanks the alias name instead of the colum name + *@see get + */ + public function getp($p_string) + { + if (array_key_exists($p_string, $this->name)) { + $idx=$this->name[$p_string]; + return $this->$idx; + } + else + throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string); + } + + /** + *@brief set the value thanks the alias name instead of the colum name + *@see set + */ + public function setp($p_string, $p_value) + { + if (array_key_exists($p_string, $this->name)) { + $idx=$this->name[$p_string]; + $this->$idx=$p_value; + } else + throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string); + } + + abstract function insert(); + + abstract function delete(); + + abstract function update(); + + public function set_pk_value($p_value) + { + $pk=$this->primary_key; + $this->$pk=$p_value; + } + public function get_pk_value() + { + $pk=$this->primary_key; + return $this->$pk; + } + + abstract function load(); + + public function get_info() + { + return var_export($this, true); + } +/** + * @todo ajout vérification type (date, text ou numeric) + * @return int + */ + public function verify() + { + foreach ($this->name as $key) + { + if (trim($this->$key)=='') + $this->$key=null; + } + return 0; + } + + /** + * Transform an array into object + * @param type $p_array + * @return object + */ + public function from_array($p_array) + { + foreach ($this->name as $key=> $value) + { + if (isset($p_array[$value])) + { + $this->$value=$p_array[$value]; + } + else + { + $this->$value=null; + } + } + return $this; + } + /** + * Turn an object (row) into an array + * @return array + */ + public function to_array() + { + $array=array(); + foreach ($this->name as $key=> $value) + { + + $array[$key]=$this->$key; + } + return $array; + } + + /** + * @brief retrieve array of object thanks a condition + * @param $cond condition (where clause) (optional by default all the rows are fetched) + * you can use this parameter for the order or subselect + * @param $p_array array for the SQL stmt + * @see Database::exec_sql get_object Database::num_row + * @return the return value of exec_sql + */ + abstract function seek($cond='', $p_array=null); + + /** + * get_seek return the next object, the return of the query must have all the column + * of the object + * @param $p_ret is the return value of an exec_sql + * @param $idx is the index + * @see seek + * @return object + */ + public function next($ret, $i) + { + $array=$this->cn->fetch_array($ret, $i); + return $this->from_array($array); + } + + /** + * @see next + */ + public function get_object($p_ret, $idx) + { + return $this->next($p_ret, $idx); + } + + /** + * @brief return an array of objects. + * Do not use this function if they are too many objects, it takes a lot of memory, + * and could slow down your application. + * @param $cond condition, order... + * @param $p_array array to use for a condition + * @note this function could slow down your application. + */ + function collect_objects($cond='', $p_array=null) + { + if ($p_array != null && ! is_array($p_array) ) + { + throw new Exception(_("Erreur : exec_sql attend un array")); + } + $ret=$this->seek($cond, $p_array); + $max=Database::num_row($ret); + $a_return=array(); + for ($i=0; $i<$max; $i++) + { + $a_return[$i]=clone $this->next($ret, $i); + } + return $a_return; + } + abstract function count($p_where="",$p_array=null) ; + + /** + * Count the number of record with the id , + * @return integer 0 doesn't exist , 1 exists + */ + abstract function exist() ; +} + +?> diff --git a/include/lib/manage_table_sql.class.php b/include/lib/manage_table_sql.class.php index 0ea82c714..0021c5549 100644 --- a/include/lib/manage_table_sql.class.php +++ b/include/lib/manage_table_sql.class.php @@ -50,34 +50,42 @@ $objet->param_set($json); // Display the box - $xml=$objet->ajax_input(); + header('Content-type: text/xml; charset=UTF-8'); + $xml=$objet->ajax_input(); + echo $xml->save_XML(); @endcode * @see ManageTable.js */ class Manage_Table_SQL { - private $table; //!< Object Noalyss_SQL - private $a_label_displaid; //!< Label of the col. of the datarow - private $a_order; //!< order of the col - private $a_prop; //!< property for each col. - private $a_type; //!< Type of the column : date , select ... Only in input - private $a_select; //!< Possible value if a_type is a SELECT - private $object_name; //!< Object_name is used for the javascript - private $row_delete; //!< Flag to indicate if rows can be deleted - private $row_update; //!< Flag to indicate if rows can be updated - private $row_append; //!< Flag to indicate if rows can be added - private $json_parameter; //!< Default parameter to add (gDossier...) - private $aerror; //!< Array containing the error of the input data + protected $table; //!< Object Data_SQL + protected $a_label_displaid; //!< Label of the col. of the datarow + protected $a_order; //!< order of the col + protected $a_prop; //!< property for each col. + protected $a_type; //!< Type of the column : date , select ... Only in input + protected $a_select; //!< Possible value if a_type is a SELECT + protected $object_name; //!< Object_name is used for the javascript + protected $row_delete; //!< Flag to indicate if rows can be deleted + protected $row_update; //!< Flag to indicate if rows can be updated + protected $row_append; //!< Flag to indicate if rows can be added + protected $json_parameter; //!< Default parameter to add (gDossier...) + protected $aerror; //!< Array containing the error of the input data + protected $col_sort; //!< when inserting, it is the column to sort,-1 to disable it and append only + const UPDATABLE=1; const VISIBLE=2; + private $icon_mod; //!< place of right or left the icon update or mod, default right, accepted value=left,right,first column for mod + private $icon_del; //!< place of right or left the icon update or mod, default right, accepted value=left,right + /** * @brief Constructor : set the label to the column name, * the order of the column , set the properties and the * permission for updating or deleting row */ - function __construct(Noalyss_SQL $p_table) + + function __construct(Data_SQL $p_table) { $this->table=$p_table; $order=0; @@ -96,26 +104,62 @@ class Manage_Table_SQL $this->row_update=TRUE; $this->row_append=TRUE; $this->callback="ajax.php"; - $this->json=json_encode(array("gDossier"=>Dossier::id(), + $this->json_parameter=json_encode(array("gDossier"=>Dossier::id(), "op"=>"managetable")); $this->aerror=[]; + $this->icon_mod="right"; + $this->icon_del="right"; + $this->col_sort=0; } - function get_table() { + /** + * When adding an element , it is column we checked to insert before, + * @return none + */ + function get_col_sort() { + return $this->col_sort; + } + /** + * When adding an element ,we place it thanks the DOM Attribute sort_value + * set it to -1 if you want one to append + * @param numeric $pn_num + * @note you must be aware that the icon_mod or icon_del is in the first col, + * this column is skipped + */ + function set_col_sort($p_num) { + $this->col_sort=$p_num; + } + function get_icon_mod() + { + return $this->icon_mod; + } + function get_icon_del() + { + return $this->icon_del; + } + function get_table() + { return $this->table; } - function set_table(Noalyss_SQL $p_noalyss_sql) { + + function set_table(Data_SQL $p_noalyss_sql) + { $this->table=$p_noalyss_sql; } + function get_order() + { + return $this->a_order; + } /** * @brief set the error message for a wrong input * @param $p_col the column name * @param $p_message the error message * @see check */ - function set_error($p_col,$p_message) + function set_error($p_col, $p_message) { $this->aerror[$p_col]=$p_message; } + /** * @brief retrieve the error message * @param $p_col column name @@ -124,9 +168,11 @@ class Manage_Table_SQL */ function get_error($p_col) { - if ( isset ($this->aerror[$p_col])) return $this->aerror[$p_col]; + if (isset($this->aerror[$p_col])) + return $this->aerror[$p_col]; return ""; } + /** * This function can be overrided to check the data before * inserting , updating or removing, @@ -137,6 +183,7 @@ class Manage_Table_SQL { return true; } + /** * @brief set the type of a column , it will change in the input db box , the * select must supply an array of possible values [val=> , label=>] with @@ -157,6 +204,7 @@ class Manage_Table_SQL $this->a_type[$p_key]=$p_value; $this->a_select[$p_key]=$p_array; } + /** * @brief return the type of a column * @param $p_key col name @@ -180,19 +228,44 @@ class Manage_Table_SQL { return $this->object_name; } - + /** + * Add json parameter to the current one + */ + function add_json_param($p_attribute,$p_value) { + $x=json_decode($this->json_parameter,TRUE); + $x[$p_attribute]=$p_value; + $this->json_parameter=json_encode($x); + } + function get_json() + { + return $this->json_parameter; + } + function get_object_name() { + return $this->object_name; + } /** * Set the parameter of the object (gDossier, ac, plugin_code...) * @detail By default , only gDossier will be set . The default value * is given in the constructor * @param string with json format $p_json - * + * @deprecated since version 692 + * @see set_json */ function param_set($p_json) { - $this->json_parameter=$p_json; + $this->set_json($p_json); + } + /** + * Set the parameter of the object (gDossier, ac, plugin_code...) + * @detail By default , only gDossier will be set . The default value + * is given in the constructor + * @param string with json format $p_json + */ + function set_json($p_json) + { + $this->json_parameter=$p_json; + } - /** * @brief set the callback function that is passed to javascript * @param $p_file : callback file by default ajax.php @@ -213,6 +286,7 @@ class Manage_Table_SQL var {$this->object_name}=new ManageTable(\"{$this->table->table}\"); {$this->object_name}.set_callback(\"{$this->callback}\"); {$this->object_name}.param_add({$this->json_parameter}); + {$this->object_name}.set_sort({$this->get_col_sort()}); "; @@ -220,7 +294,8 @@ class Manage_Table_SQL /** * Set the object_name - * @param string $p_object_name name of the JS var, used in ajax response + * @param string $p_object_name name of the JS var, used in ajax response,id + * of the part of the id DOMElement to modify */ function set_object_name($p_object_name) { @@ -234,8 +309,10 @@ class Manage_Table_SQL */ function set_property_updatable($p_key, $p_value) { - if (!$this->a_prop[$p_key]) + if (! isset($this->a_prop[$p_key])) throw new Exception(__FILE__.":".__LINE__."$p_key invalid index"); + // if already done returns + if ( $this->get_property_updatable($p_key) == $p_value)return; if ($p_value==False) $this->a_prop[$p_key]=$this->a_prop[$p_key]-self::UPDATABLE; elseif ($p_value==True) @@ -252,7 +329,27 @@ class Manage_Table_SQL return $this->row_update; } - + /** + * Set the icon to modify at the right ,the first col or left of the row + * + * @param type $pString + * @throws Exception + */ + function set_icon_mod($pString) { + if ($pString != "right" && $pString != "left" && $pString!="first") + throw new Exception('set_icon_mod invalide '.$pString); + $this->icon_mod=$pString; + } + /** + * Set the icon to delete at the right or left of the row + * @param type $pString + * @throws Exception + */ + function set_icon_del($pString) { + if ($pString != "right" && $pString != "left" ) + throw new Exception('set_icon_del invalide '.$pString); + $this->icon_del=$pString; + } /** * @brief return false if the append of the row is forbidden */ @@ -322,8 +419,11 @@ class Manage_Table_SQL */ function set_property_visible($p_key, $p_value) { - if (!$this->a_prop[$p_key]) + if (!isset ($this->a_prop[$p_key]) ) throw new Exception(__FILE__.":".__LINE__."$p_key invalid index"); + // if already done return + if ( $this->get_property_visible($p_key) == $p_value)return; + if ($p_value==False) $this->a_prop[$p_key]=$this->a_prop[$p_key]-self::VISIBLE; elseif ($p_value==True) @@ -422,17 +522,24 @@ class Manage_Table_SQL /** * @brief display the data of the table + * @param $p_order is the cond or order of the rows, + * if empty the primary key will be used + * @param $p_array array of the bind variables + * @note the function create_js_script MUST be called before this function */ - function display_table() + function display_table($p_order="", $p_array=NULL) { - $ret=$this->table->seek("order by ".$this->table->primary_key); + if ($p_order=="") + { + $p_order="order by {$this->table->primary_key}"; + } + $ret=$this->table->seek($p_order, $p_array); $nb=Database::num_row($ret); if ($this->can_append_row()==TRUE) { echo HtmlInput::button_action(" "._("Ajout"), - sprintf("%s.input('-1','%s')", - $this->object_name, - $this->object_name),"xx","smallbutton",BUTTONADD); + sprintf("%s.input('-1','%s')", $this->object_name, + $this->object_name), "xx", "smallbutton", BUTTONADD); } $nb_order=count($this->a_order); $virg=""; $result=""; @@ -458,12 +565,12 @@ class Manage_Table_SQL echo ""; if ($this->can_append_row()==TRUE) { - echo HtmlInput::button_action(" "._("Ajout"), - sprintf("%s.input('-1','%s')", - $this->object_name, - $this->object_name),"xx","smallbutton",BUTTONADD); + echo HtmlInput::button_action(" "._("Ajout"), + sprintf("%s.input('-1','%s')", $this->object_name, + $this->object_name), "xx", "smallbutton", BUTTONADD); } - printf ('', $this->object_name); + printf('', + $this->object_name); } /** @@ -475,19 +582,29 @@ class Manage_Table_SQL $nb=count($this->a_order); echo ""; + if ($this->can_update_row() && $this->icon_mod=="left") + { + echo th(" ", 'style="width:40px"'); + } + if ($this->can_delete_row() && $this->icon_del=="left") + { + echo th(" ", 'style="width:40px"'); + } for ($i=0; $i<$nb; $i++) { $key=$this->a_order[$i]; if ($this->get_property_visible($key)==true) - echo th($this->a_label_displaid[$key]); + echo th("","",$this->a_label_displaid[$key]); } - if ($this->can_update_row()) { - echo th(" ",'style="width:40px"'); + if ($this->can_update_row() && $this->icon_mod=="right") + { + echo th(" ", 'style="width:40px"'); } - if ($this->can_delete_row()) { - echo th(" ",'style="width:40px"'); + if ($this->can_delete_row() && $this->icon_del=="right") + { + echo th(" ", 'style="width:40px"'); } echo ""; } @@ -511,53 +628,90 @@ class Manage_Table_SQL $nb=count($this->a_order); for ($i=0; $i<$nb; $i++) { - $v=HtmlInput::default_value_request($this->a_order[$i], ""); + $key=$this->a_order[$i]; - if ( $this->get_property_visible($key) == TRUE - && $this->get_property_updatable($key) == TRUE ) + if ($this->get_property_visible($key)==TRUE&&$this->get_property_updatable($key) + ==TRUE) { + $v=HtmlInput::default_value_request($this->a_order[$i], ""); $this->table->$key=strip_tags($v); } } } + function display_icon_mod($p_row) + { + if ($this->can_update_row()) + { + echo ""; + $js=sprintf("%s.input('%s','%s');", $this->object_name, + $p_row[$this->table->primary_key], $this->object_name + ); + echo HtmlInput::image_click("edit.png", $js, _("Modifier")); + echo ""; + } + } + + function display_icon_del($p_row) + { + if ($this->can_delete_row()) + { + echo ""; + $js=sprintf("%s.delete('%s','%s');", $this->object_name, + $p_row[$this->table->primary_key], $this->object_name + ); + echo HtmlInput::image_click("delete.gif", $js, _("Effacer")); + echo ""; + } + } + /** * @brief display a data row in the table, with the order defined * in a_order and depending of the visibility of the column * @see display_table */ - private function display_row($p_row) + function display_row($p_row) { printf('', $this->object_name, $p_row[$this->table->primary_key]) ; - + + if ($this->icon_mod=="left") + $this->display_icon_mod($p_row); + if ($this->icon_del=="left") + $this->display_icon_del($p_row); + $nb_order=count($this->a_order); for ($i=0; $i<$nb_order; $i++) { $v=$this->a_order[$i]; - if ($this->get_property_visible($v)) + if ($i==0&&$this->icon_mod=="first"&&$this->can_update_row()) + { + $js=sprintf("onclick=\"%s.input('%s','%s');\"", $this->object_name, + $p_row[$this->table->primary_key], $this->object_name); + $td=($i == $this->col_sort ) ? sprintf('',$p_row[$v]):""; + echo $td.HtmlInput::anchor($p_row[$v], "", $js).''; + } + elseif ( $i == $this->col_sort && $this->get_property_visible($v)) + { + echo td($p_row[$v],sprintf(' sort_value="X%s" ',$p_row[$v])); + } + elseif ( ! $this->get_property_visible($v)) { + continue; + } + else + { echo td($p_row[$v]); + } } - echo ""; - if ($this->can_update_row()) - { - $js=sprintf("%s.input('%s','%s');", $this->object_name, - $p_row[$this->table->primary_key], $this->object_name - ); - echo HtmlInput::image_click("edit.png",$js,_("Modifier")); - } - echo ""; - echo ""; - if ($this->can_delete_row()) - { - $js=sprintf("%s.delete('%s','%s');", $this->object_name, - $p_row[$this->table->primary_key], $this->object_name - ); - echo HtmlInput::image_click("delete.gif", $js,_("Effacer")); - } - echo ""; + if ($this->icon_mod=="right") + $this->display_icon_mod($p_row); + if ($this->icon_del=="right") + $this->display_icon_del($p_row); + + + echo ''; } @@ -587,7 +741,7 @@ class Manage_Table_SQL echo ""; if ($this->a_type[$key]=="select") { - $select = new ISelect($key); + $select=new ISelect($key); $select->value=$this->a_select[$key]; $select->selected=$value; echo $select->input(); @@ -647,10 +801,13 @@ class Manage_Table_SQL // Check if the data are valid , if not then display the // input values with the error message // - if ( $this->check() == false ) { + if ($this->check()==false) + { $xml=$this->ajax_input("NOK"); return $xml; - } else { + } + else + { // Data are valid so we can save them $this->save(); // compose the answer @@ -665,7 +822,7 @@ class Manage_Table_SQL $this->display_row($array); $html=ob_get_contents(); ob_end_clean(); - $s3=$xml->createElement("html" ); + $s3=$xml->createElement("html"); $t1=$xml->createTextNode($html); $s3->appendChild($t1); } @@ -681,7 +838,7 @@ class Manage_Table_SQL { $s1=$xml->createElement("status", "NOK"); $s2=$xml->createElement("ctl_row", - $this->object_name+"_"+$this->table->get_pk_value()); + $this->object_name+"_"+$this->table->get_pk_value()); $s4=$xml->createElement("ctl", $this->object_name); $s3=$xml->createElement("html", $ex->getTraceAsString()); $root=$xml->createElement("data"); @@ -710,7 +867,7 @@ class Manage_Table_SQL { $status=$p_status; ob_start(); - + echo HtmlInput::title_box("Donnée", "dtr"); printf('
', $this->object_name, $this->table->get_pk_value(), @@ -729,12 +886,12 @@ class Manage_Table_SQL HtmlInput::submit('update', _("OK")), '', '
  • ', - HtmlInput::button_action(_("Cancel"), $close,"","smallbutton"), + HtmlInput::button_action(_("Cancel"), $close, "", "smallbutton"), '
  • ', ''; echo "
    "; - - + + $html=ob_get_contents(); ob_end_clean(); @@ -742,7 +899,7 @@ class Manage_Table_SQL $ctl=$this->object_name."_".$this->table->get_pk_value(); $s2=$xml->createElement("ctl_row", $ctl); $s4=$xml->createElement("ctl", $this->object_name); - $s3=$xml->createElement("html" ); + $s3=$xml->createElement("html"); $t1=$xml->createTextNode($html); $s3->appendChild($t1); @@ -821,13 +978,13 @@ class Manage_Table_SQL } /** - * @brief save the Noalyss_SQL Object + * @brief save the Data_SQL Object * The noalyss_SQL is not empty * @see from_request */ function save() { - $this->table->save(); + $this->table->save(); } /** @@ -859,6 +1016,7 @@ class Manage_Table_SQL { $this->table->set($p_key, $p_value); } + /** * Display a list of the error collected * @see get_error set_error @@ -867,16 +1025,17 @@ class Manage_Table_SQL function display_error() { $nb_order=count($this->a_order); - if ( count($this->aerror) == 0)return; + if (count($this->aerror)==0) + return; echo "Liste erreurs :"; - for ($i=0; $i<$nb_order; $i++) + for ($i=0; $i<$nb_order; $i++) { - $key=$this->a_order[$i]; - $label=$this->a_label_displaid[$key]; - $error=$this->get_error($key); - $error=($error=="")?"":"".h($label)." : ".h($this->get_error($key)).""; - - echo $error; + $key=$this->a_order[$i]; + $label=$this->a_label_displaid[$key]; + $error=$this->get_error($key); + $error=($error=="")?"":"".h($label)." : ".h($this->get_error($key)).""; + + echo $error; } echo ""; } diff --git a/include/lib/noalyss_sql.class.php b/include/lib/noalyss_sql.class.php index 35b5677cd..043d659ea 100644 --- a/include/lib/noalyss_sql.class.php +++ b/include/lib/noalyss_sql.class.php @@ -23,9 +23,22 @@ /** * @file * @brief this wrapper is used to created easily a wrapper to a table + * You must create a class extending this one, in the constructor + * these variables have to be defined + * + * - table = name of the view or empty + * - select = name of the select + * - name = array of column name, match between logic and actual name + * - type = array , match between column and type of data + * - default = array of column with a default value + * - date_format = format of the date + * + * After you call the parent constructor + * @note the view or the table must include an unique key, otherwise the load + * doesn't work. * * @class Noalyss_SQL - * Match a table into an object, you need to add the code for each table + * Match a table or a view into an object, you need to add the code for each table * @note : the primary key must be an integer * * @code @@ -39,7 +52,7 @@ $this->name=array( "id"=>"o_id", - "dolibarr"=>"o_doli", + "program"=>"o_prog", "date"=>"o_date", "qcode"=>"o_qcode", "fiche"=>"f_id", @@ -49,7 +62,7 @@ $this->type = array( "o_id"=>"numeric", - "o_doli"=>"numeric", + "o_prog"=>"numeric", "o_date"=>"date", "o_qcode"=>"text", "f_id"=>"numeric", @@ -69,7 +82,9 @@ * @endcode * */ -abstract class Noalyss_SQL +require NOALYSS_INCLUDE."/lib/data_sql.class.php"; + +abstract class Noalyss_SQL extends Data_SQL { function __construct(&$p_cn, $p_id=-1) @@ -122,7 +137,7 @@ abstract class Noalyss_SQL public function set($p_string, $p_value) { if (array_key_exists($p_string, $this->type)) { - $this->$idx=$p_value; + $this->$p_string=$p_value; } else throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant '.$p_string); } @@ -361,7 +376,8 @@ abstract class Noalyss_SQL } /** - * @brief return an array of objects. Do not use this function if they are too many objects, it takes a lot of memory, + * @brief return an array of objects. + * Do not use this function if they are too many objects, it takes a lot of memory, * and could slow down your application. * @param $cond condition, order... * @param $p_array array to use for a condition diff --git a/scenario/ajax_acc_plan_mtable.php b/scenario/ajax_acc_plan_mtable.php new file mode 100644 index 000000000..16d640724 --- /dev/null +++ b/scenario/ajax_acc_plan_mtable.php @@ -0,0 +1,65 @@ + + +if (!defined('ALLOWED')) + die('Appel direct ne sont pas permis'); +if ( ! defined ("AJAX_TEST")) { + echo "Can not be called directly but via ajax_test.php"; + return; +} +/** + * @file + * @brief Test the ajax call from test_manage_table_sql.php + */ +$http=new HttpInput(); +try { +$table=$http->request('table'); +$action=$http->request('action'); +$p_id=$http->request('p_id', "numeric"); +$ctl_id=$http->request('ctl'); +} catch(Exception $e) { + + echo $e->getMessage(); +} +require_once NOALYSS_INCLUDE."/lib/manage_table_sql.class.php"; +require_once NOALYSS_INCLUDE."/class/acc_plan_mtable.class.php"; + +$obj=new Acc_Plan_SQL($cn); +$obj->set_limit_fiche_qcode(5); +$obj->set_pk_value($p_id); +$obj->load(); +$manage_table=new Acc_Plan_MTable($obj); +$manage_table->add_json_param("TestAjaxFile", + NOALYSS_HOME."/../scenario/ajax_acc_plan_mtable.php"); +$manage_table->set_object_name($ctl_id); +$manage_table->set_callback("ajax_test.php"); +if ($action=="input") +{ + header('Content-type: text/xml; charset=UTF-8'); + echo $manage_table->ajax_input()->saveXML(); + return; +} +elseif ($action == "save") +{ + $xml=$manage_table->ajax_save(); + header('Content-type: text/xml; charset=UTF-8'); + echo $xml->saveXML(); +} \ No newline at end of file diff --git a/scenario/ajax_manage_table_sql.php b/scenario/ajax_manage_table_sql.php index 8ae6e27ca..22c0e6498 100644 --- a/scenario/ajax_manage_table_sql.php +++ b/scenario/ajax_manage_table_sql.php @@ -21,7 +21,7 @@ if (!defined('ALLOWED')) die('Appel direct ne sont pas permis'); -if ( ! defined ("AJAX_TEST ")) { +if ( ! defined ("AJAX_TEST")) { echo "Can not be called directly but via AJAX"; return; } @@ -31,10 +31,10 @@ if ( ! defined ("AJAX_TEST ")) { */ $http=new HttpInput(); try { -$table=$http->get('table'); -$action=$http->get('action'); -$p_id=$http->get('p_id', "numeric"); -$ctl_id=$http->get('ctl'); +$table=$http->request('table'); +$action=$http->request('action'); +$p_id=$http->request('p_id', "numeric"); +$ctl_id=$http->request('ctl'); } catch(Exception $e) { echo $e->getMessage(); @@ -42,13 +42,18 @@ $ctl_id=$http->get('ctl'); require_once NOALYSS_INCLUDE."/lib/manage_table_sql.class.php"; require_once NOALYSS_INCLUDE."/database/acc_plan_sql.class.php"; -$obj=new Acc_Plan_SQL($cn, $p_id); -$manage_table=new Manage_Table_SQL($obj); -$manage_table->add_json_param("TestAjaxFile", - NOALYSS_HOME."/../scenario/ajax_manage_table_sql.php"); + if ($action=="input") { + $obj=new Acc_Plan_SQL($cn); + $obj->set_limit_fiche_qcode(5); + $obj->set_pk_value($p_id); + $obj->load(); + $manage_table=new Manage_Table_SQL($obj); + $manage_table->add_json_param("TestAjaxFile", + NOALYSS_HOME."/../scenario/ajax_manage_table_sql.php"); + $manage_table->set_object_name($ctl_id); $manage_table->set_col_label('pcm_val', "Poste"); $manage_table->set_col_label('parent_accounting', "Dépend"); @@ -58,4 +63,17 @@ if ($action=="input") header('Content-type: text/xml; charset=UTF-8'); echo $manage_table->ajax_input()->saveXML(); return; +} elseif ($action=="save") +{ +$obj=new Acc_Plan_SQL($cn); + $obj->set_limit_fiche_qcode(5); + $obj->set_pk_value($p_id); + $obj->load(); + $manage_table=new Manage_Table_SQL($obj); + $manage_table->set_object_name($ctl_id); + $manage_table->add_json_param("TestAjaxFile", + NOALYSS_HOME."/../scenario/ajax_manage_table_sql.php"); + header('Content-type: text/xml; charset=UTF-8'); + echo $manage_table->ajax_save()->saveXML(); + return; } \ No newline at end of file diff --git a/scenario/test_acc_plan_mtable.php b/scenario/test_acc_plan_mtable.php new file mode 100644 index 000000000..70a6383a1 --- /dev/null +++ b/scenario/test_acc_plan_mtable.php @@ -0,0 +1,50 @@ + + +if (!defined('ALLOWED')) + die('Appel direct ne sont pas permis'); + //@description:Test the class Acc_Plan_mtable , ajax and javascript + + $_GET=array ( +); +$_POST=array ( +); +$_POST['gDossier']=$gDossierLogInput; +$_GET['gDossier']=$gDossierLogInput; +$_REQUEST=array_merge($_GET,$_POST); +require_once NOALYSS_INCLUDE."/class/acc_plan_mtable.class.php"; +require_once NOALYSS_INCLUDE."/lib/manage_table_sql.class.php"; +/** + * @file + * @brief Test the Acc_Plan_MTable + */ +$obj=new Acc_Plan_SQL($cn); +/** + * Test $obj + */ + +$mtable=new Acc_Plan_MTable($obj); +$obj->set_limit_fiche_qcode(5); +$mtable->set_callback("ajax_test.php"); +$mtable->add_json_param("TestAjaxFile",NOALYSS_HOME."/../scenario/ajax_acc_plan_mtable.php"); +$mtable->create_js_script(); +echo $mtable->display_table(" where pcm_val::text like '4%' order by pcm_val::text limit 30"); + diff --git a/scenario/test_manage_table_sql.php b/scenario/test_manage_table_sql.php index d0a9f2bd3..fe08c5a6c 100644 --- a/scenario/test_manage_table_sql.php +++ b/scenario/test_manage_table_sql.php @@ -80,24 +80,24 @@ test_visible_update($manage_table, "parent_accounting", TRUE, TRUE); echo "

    "."Icon MODIFY place"."

    "; echo "

    "." Mod left"."

    "; $manage_table->set_icon_mod("left"); -$manage_table->display_table(" order by pcm_val::text limit 10"); +$manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); echo "

    "." Mod right"."

    "; $manage_table->set_icon_mod("right"); -$manage_table->display_table(" order by pcm_val::text limit 10"); +$manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); echo "

    "." Mod first"."

    "; $manage_table->set_icon_mod("first"); -$manage_table->display_table(" order by pcm_val::text limit 10"); +$manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); echo "

    "."Icon DELETE place"."

    "; echo "

    "." Delete left"."

    "; $manage_table->set_icon_del("left"); -$manage_table->display_table(" order by pcm_val::text limit 10"); +$manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); echo "

    "." Delete right"."

    "; $manage_table->set_icon_del("right"); -$manage_table->display_table(" order by pcm_val::text limit 10"); +$manage_table->display_table("where pcm_val::text >= '400' order by pcm_val::text limit 10"); ?> diff --git a/unit-test/README b/unit-test/README index a613b0087..119aedde6 100644 --- a/unit-test/README +++ b/unit-test/README @@ -25,3 +25,5 @@ Génération pour tous les fichiers ================================= Permet de voir les lignes de code qui ont été testées php /phpunit.phar --coverage-html html --bootstrap bootstrap.php include + +exemple depuis NETBEANS "/usr/bin/php" "/home/dany/Program/phpunit-skelgen.phar" "--ansi" "generate-test" "--bootstrap=/home/dany/developpement/phpcompta/noalyss/unit-test/bootstrap.php" "View_SQL" "/home/dany/developpement/phpcompta/noalyss/include/lib/view_sql.class.php" "View_SQLTest" "/home/dany/developpement/phpcompta/noalyss/unit-test/include/lib/view_sql.classTest.php"