set_pk($p_id); $objet->set_object_name($objet_name); // Set the ajax to call $objet->set_callback("ajax.php"); // Build the json object for JS $plugin_code=HtmlInput::default_value_request("plugin_code",""); $ac=HtmlInput::default_value_request("ac",""); $sa=HtmlInput::default_value_request("sa",""); $aJson=array("gDossier"=>Dossier::id(), "ac"=>$ac, "plugin_code"=>$plugin_code, "sa"=>$sa, "sb"=>$sb ); $json=json_encode($aJson); $objet->param_set($json); // Display the box $xml=$objet->ajax_input(); @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...) const UPDATABLE=1; const VISIBLE=2; /** * @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) { $this->table=$p_table; $order=0; foreach ($this->table->name as $key=> $value) { $this->a_label_displaid[$value]=$value; $this->a_order[$order]=$value; $this->a_prop[$value]=self::UPDATABLE|self::VISIBLE; $this->a_type[$value]=$this->table->type[$value]; $this->a_select[$value]=null; $order++; } $this->object_name=uniqid("tbl"); $this->row_delete=TRUE; $this->row_update=TRUE; $this->row_append=TRUE; $this->callback="ajax.php"; $this->json=json_encode(array("gDossier"=>Dossier::id(), "op"=>"managetable")); } /** * @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 * the variable $this->key_name->a_value * @param $p_key col name * @param $p_type is SELECT NUMERIC TEXT or DATE * @param $p_array if type is SELECT an array is expected */ function set_col_type($p_key, $p_value, $p_array=NULL) { if (!isset($this->a_type[$p_key])) throw new Exception("invalid key $p_key"); if (!in_array($p_value, array("text", "numeric", "date", "select", "timestamp"))) throw new Exception("invalid type $p_value"); $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 * @see set_col_type */ function get_col_type($p_key) { if (!isset($this->a_type[$p_key])) throw new Exception("invalid key"); return $this->a_type[$p_key]; } /** * Get the object name * @details : return the object name , it is useful it * the javascript will return coded without the create_js_script function * @see create_js_script */ function get_js_variable() { 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 * */ function param_set($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 */ function set_callback($p_file) { $this->callback=$p_file; } /** * @brief we must create first the javascript if we want to update, insert * or delete rows. It is the default script . */ function create_js_script() { echo " "; } /** * Set the object_name * @param string $p_object_name name of the JS var, used in ajax response */ function set_object_name($p_object_name) { $this->object_name=$p_object_name; } /** * @brief set a column of the data row updatable or not * @param string $p_key data column * @param bool $p_value Boolean False or True */ function set_property_updatable($p_key, $p_value) { if (!$this->a_prop[$p_key]) throw new Exception(__FILE__.":".__LINE__."$p_key invalid index"); if ($p_value==False) $this->a_prop[$p_key]=$this->a_prop[$p_key]-self::UPDATABLE; elseif ($p_value==True) $this->a_prop[$p_key]=$this->a_prop[$p_key]|self::UPDATABLE; else throw new Exception("set_property_updatable [ $p_value ] incorrect"); } /** * @brief return false if the update of the row is forbidden */ function can_update_row() { return $this->row_update; } /** * @brief return false if the append of the row is forbidden */ function can_append_row() { return $this->row_append; } /** * @brief Enable or disable the deletion of rows * @param $p_value Boolean : true enable the row to be deleted */ function set_delete_row($p_value) { if ($p_value!==True&&$p_value!==False) throw new Exception("Valeur invalide set_delete_row [$p_value]"); $this->row_delete=$p_value; } /** * @brief Enable or disable the appending of rows * @param $p_value Boolean : true enable the row to be appended */ function set_append_row($p_value) { if ($p_value!==True&&$p_value!==False) throw new Exception("Valeur invalide set_append_row [$p_value]"); $this->row_append=$p_value; } /** * @brief Enable or disable the updating of rows * @param $p_value Boolean : true enable the row to be updated */ function set_update_row($p_value) { if ($p_value!==True&&$p_value!==False) throw new Exception("Valeur invalide set_update_row [$p_value]"); $this->row_update=$p_value; } /** * @brief return false if the delete of the row is forbidden */ function can_delete_row() { return $this->row_delete; } /** * @brief return True if the column is updatable otherwise false * @param $p_key data column */ function get_property_updatable($p_key) { $val=$this->a_prop[$p_key]&self::UPDATABLE; if ($val==self::UPDATABLE) return true; return false; } /** * @brief set a column of the data row visible or not * @param string $p_key data column * @param bool $p_value Boolean False or True */ function set_property_visible($p_key, $p_value) { if (!$this->a_prop[$p_key]) throw new Exception(__FILE__.":".__LINE__."$p_key invalid index"); if ($p_value==False) $this->a_prop[$p_key]=$this->a_prop[$p_key]-self::VISIBLE; elseif ($p_value==True) $this->a_prop[$p_key]=$this->a_prop[$p_key]|self::VISIBLE; else throw new Exception("set_property_updatable [ $p_value ] incorrect"); } /** * @brief return True if the column is visible otherwise false * @param $p_key data column */ function get_property_visible($p_key) { $val=$this->a_prop[$p_key]&self::VISIBLE; if ($val===self::VISIBLE) return true; return false; } /** * @brief set the name to display for a column * @param string $p_key data column * @param string $p_display Label to display * */ function set_col_label($p_key, $p_display) { $this->a_label_displaid[$p_key]=$p_display; } /** * @brief get the position of a column * @param $p_key data column */ function get_current_pos($p_key) { $nb_order=count($this->a_order); for ($i=0; $i<$nb_order; $i++) if ($this->a_order[$i]==$p_key) return $i; throw new Exception("COL INVAL ".$p_key); } /** * @brief if we change a column order , the order * of the other columns is impacted. * * With a_order[0,1,2,3]=[x,y,z,a] * if we move the column x (idx=0) to 2 * we must obtain [y,z,x,a] * @param string $p_key data column * @param integer $p_idx new location */ function move($p_key, $p_idx) { // get current position of p_key $cur_pos=$this->get_current_pos($p_key); if ($cur_pos==$p_idx) return; if ($cur_pos<$p_idx) { $nb_order=count($this->a_order); for ($i=0; $i<$nb_order; $i++) { // if col_name is not the searched one we continue if ($this->a_order[$i]!=$p_key) continue; if ($p_idx==$i) continue; // otherwise we swap with i+1 $old=$this->a_order[$i+1]; $this->a_order[$i]=$this->a_order[$i+1]; $this->a_order[$i+1]=$p_key; } } else { $nb_order=count($this->a_order)-1; for ($i=$nb_order; $i>0; $i--) { // if col_name is not the searched one we continue if ($this->a_order[$i]!=$p_key) continue; if ($p_idx==$i) continue; // otherwise we swap with i+1 $old=$this->a_order[$i-1]; $this->a_order[$i]=$this->a_order[$i-1]; $this->a_order[$i-1]=$p_key; } } } /** * @brief display the data of the table */ function display_table() { $ret=$this->table->seek("order by ".$this->table->primary_key); $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)); } $nb_order=count($this->a_order); $virg=""; $result=""; for ($e=0; $e<$nb_order; $e++) { if ($this->get_property_visible($this->a_order[$e])==TRUE) { $result.=$virg."$e"; $virg=","; } } echo HtmlInput::filter_table("tb".$this->object_name, $result, 1); printf('
| {$label} | "; if ($this->get_property_updatable($key)==TRUE) { echo ""; if ($this->a_type[$key]=="select") { $select = new ISelect($key); $select->value=$this->a_select[$key]; $select->selected=$value; echo $select->input(); } else { $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 );*/ } echo " | "; } else { printf('%s %s | ', h($value), HtmlInput::hidden($key, $value) ); } } echo "