diff --git a/html/js/infobulle.js b/html/js/infobulle.js index 525585ff7..e87b19b1f 100644 --- a/html/js/infobulle.js +++ b/html/js/infobulle.js @@ -52,3 +52,10 @@ function hideBulle(p_ctl) var d=document.getElementById('bulle'); d.style.visibility="hidden"; } +function displayBulle(p_comment) { + var d=document.getElementById('bulle'); + d.innerHTML=p_comment; + d.style.top=posY+offsetY+"px"; + d.style.left=posX+offsetX+"px"; + d.style.visibility="visible"; +} \ No newline at end of file diff --git a/html/js/managetable.js b/html/js/managetable.js index 1e84a54c7..29e2f070c 100644 --- a/html/js/managetable.js +++ b/html/js/managetable.js @@ -172,16 +172,19 @@ var ManageTable = function (p_table_name) new_row.innerHTML = answer['html']; $("tb"+answer['ctl']).appendChild(new_row); } - new Effect.Highlight(answer['ctl_row'] ,{ startcolor: '#ABCBF7',endcolor: '#ffffff' }); + new Effect.Highlight(answer['ctl_row'] ,{startcolor: '#FAD4D4',endcolor: '#F78082' }); + alternate_row_color("tb"+answer['ctl']); + remove_waiting_box(); + $("dtr").hide(); } else { + remove_waiting_box(); smoke.alert("Changement impossible"); - throw "error in save"; + $("dtr").update(answer['html']); + } - alternate_row_color("tb"+answer['ctl']); - remove_waiting_box(); - $("dtr").hide(); - } catch (e) { + } + catch (e) { alert(e.message); return false; } diff --git a/include/database/class_noalyss_sql.php b/include/database/class_noalyss_sql.php index 34eb68948..35b5677cd 100644 --- a/include/database/class_noalyss_sql.php +++ b/include/database/class_noalyss_sql.php @@ -95,8 +95,7 @@ abstract class Noalyss_SQL */ public function save() { - $pk=$this->primary_key; - $count=$this->cn->get_value('select count(*) from '.$this->table.' where '.$this->primary_key.'=$1',array($this->$pk)); + $count = $this->exist(); if ($count == 0) $this->insert(); diff --git a/include/lib/class_html_input.php b/include/lib/class_html_input.php index a367f7aa2..6d5a06358 100755 --- a/include/lib/class_html_input.php +++ b/include/lib/class_html_input.php @@ -228,6 +228,15 @@ class HtmlInput $r='Δ'; return $r; } + static function errorbulle($p_comment) + { + global $g_failed; + $r=sprintf(' %s ', + $p_comment, + $p_comment, + $g_failed); + return $r; + } /** * return a string containing the html code for calling the modifyOperation */ @@ -476,7 +485,7 @@ class HtmlInput */ static function image_click($p_image,$p_js,$p_message) { - $ret=sprintf('', + $ret=sprintf('', $p_message,$p_image,$p_js); return $ret; diff --git a/include/lib/class_manage_table_sql.php b/include/lib/class_manage_table_sql.php index f670f4129..32506ec9b 100644 --- a/include/lib/class_manage_table_sql.php +++ b/include/lib/class_manage_table_sql.php @@ -68,7 +68,7 @@ class Manage_Table_SQL 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 const UPDATABLE=1; const VISIBLE=2; @@ -98,8 +98,42 @@ class Manage_Table_SQL $this->callback="ajax.php"; $this->json=json_encode(array("gDossier"=>Dossier::id(), "op"=>"managetable")); + $this->aerror=[]; + } + function get_table() { + return $this->table; + } + /** + * @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) + { + $this->aerror[$p_col]=$p_message; + } + /** + * @brief retrieve the error message + * @param $p_col column name + * @return string with message or empty if no error + * @see input + */ + function get_error($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, + * @see set_error get_error + * @return boolean + */ + function check() + { + 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 @@ -454,7 +488,7 @@ class Manage_Table_SQL } /** - * @brief set the id value of a data row + * @brief set the id value of a data row and load from the db */ function set_pk($p_id) { @@ -463,7 +497,9 @@ class Manage_Table_SQL } /** - * @brief get the data from http request + * @brief get the data from http request strip the not update or not visible data to their + * initial value. Before saving , it is important to set the pk and load from db + * @see set_pk */ function from_request() { @@ -472,7 +508,11 @@ class Manage_Table_SQL { $v=HtmlInput::default_value_request($this->a_order[$i], ""); $key=$this->a_order[$i]; - $this->table->$key=strip_tags($v); + if ( $this->get_property_visible($key) == TRUE + && $this->get_property_updatable($key) == TRUE ) + { + $this->table->$key=strip_tags($v); + } } } @@ -510,7 +550,7 @@ class Manage_Table_SQL $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 HtmlInput::image_click("delete.gif", $js,_("Effacer")); } echo ""; echo ''; @@ -530,11 +570,12 @@ class Manage_Table_SQL $key=$this->a_order[$i]; $label=$this->a_label_displaid[$key]; $value=$this->table->get($key); - + $error=$this->get_error($key); + $error=($error=="")?"":HtmlInput::errorbulle($error); if ($this->get_property_visible($key)===TRUE) { // Label - echo " {$label} "; + echo " {$label} {$error}"; if ($this->get_property_updatable($key)==TRUE) { @@ -546,16 +587,29 @@ class Manage_Table_SQL $select->selected=$value; echo $select->input(); } - else + elseif ($this->a_type[$key]=="text") { $text=new IText($key); $text->value=$value; $min_size=(strlen($value)<30)?30:strlen($value)+5; $text->size=$min_size; echo $text->input(); - /* printf('', - $label, $value, $key, $key - );*/ + } + elseif ($this->a_type[$key]=="numeric") + { + $text=new INum($key); + $text->value=$value; + $min_size=(strlen($value)<10)?10:strlen($value)+1; + $text->size=$min_size; + echo $text->input(); + } + elseif ($this->a_type[$key]=="date") + { + $text=new IDate($key); + $text->value=$value; + $min_size=10; + $text->size=$min_size; + echo $text->input(); } echo ""; } @@ -585,30 +639,38 @@ class Manage_Table_SQL { // fill up object with $_REQUEST $this->from_request(); - // save the object - $this->save(); - // compose the answer - $status="OK"; - $s1=$xml->createElement("status", $status); - $ctl=$this->object_name."_".$this->table->get_pk_value(); - $s2=$xml->createElement("ctl_row", $ctl); - $s4=$xml->createElement("ctl", $this->object_name); - ob_start(); - $this->table->load(); - $array=$this->table->to_array(); - $this->display_row($array); - $html=ob_get_contents(); - ob_end_clean(); - $s3=$xml->createElement("html" ); - $t1=$xml->createTextNode($html); - $s3->appendChild($t1); - + // Check if the data are valid , if not then display the + // input values with the error message + // + if ( $this->check() == false ) { + $xml=$this->ajax_input("NOK"); + return $xml; + } else { + // Data are valid so we can save them + $this->save(); + // compose the answer + $status="OK"; + $s1=$xml->createElement("status", $status); + $ctl=$this->object_name."_".$this->table->get_pk_value(); + $s2=$xml->createElement("ctl_row", $ctl); + $s4=$xml->createElement("ctl", $this->object_name); + ob_start(); + $this->table->load(); + $array=$this->table->to_array(); + $this->display_row($array); + $html=ob_get_contents(); + ob_end_clean(); + $s3=$xml->createElement("html" ); + $t1=$xml->createTextNode($html); + $s3->appendChild($t1); + } $root=$xml->createElement("data"); $root->appendChild($s1); $root->appendChild($s2); $root->appendChild($s3); $root->appendChild($s4); + $xml->appendChild($root); } catch (Exception $ex) { @@ -622,8 +684,8 @@ class Manage_Table_SQL $root->appendChild($s2); $root->appendChild($s3); $root->appendChild($s4); + $xml->appendChild($root); } - $xml->appendChild($root); return $xml; } @@ -635,13 +697,13 @@ class Manage_Table_SQL * - content : Html answer * @return DomDocument */ - function ajax_input() + function ajax_input($p_status="OK") { $xml=new DOMDocument("1.0", "UTF-8"); - $xml->createElement("status", "OK"); + $xml->createElement("status", $p_status); try { - $status="OK"; + $status=$p_status; ob_start(); echo HtmlInput::title_box("DonnĂ©e", "dtr"); @@ -655,6 +717,8 @@ class Manage_Table_SQL echo HtmlInput::hidden("p_id", $this->table->get_pk_value()); // button Submit and cancel $close=sprintf("\$('%s').remove()", "dtr"); + // display error if any + $this->display_error(); echo ''; echo ""; - + + $html=ob_get_contents(); ob_end_clean(); @@ -757,14 +822,7 @@ class Manage_Table_SQL */ function save() { - if ($this->table->exist()==0) - { - $this->table->insert(); - } - else - { - $this->table->update(); - } + $this->table->save(); } /** @@ -796,5 +854,26 @@ class Manage_Table_SQL { $this->table->set($p_key, $p_value); } + /** + * Display a list of the error collected + * @see get_error set_error + * + */ + function display_error() + { + $nb_order=count($this->a_order); + if ( count($this->aerror) == 0)return; + echo "Liste erreurs :"; + 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; + } + echo ""; + } }