[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 111/151: Improve Manage_Table_SQL : add funct
From: |
Dany De Bontridder |
Subject: |
[Noalyss-commit] [noalyss] 111/151: Improve Manage_Table_SQL : add functions for javascript , update or visible flag ... |
Date: |
Sat, 4 Feb 2017 17:14:32 +0000 (UTC) |
sparkyx pushed a commit to branch master
in repository noalyss.
commit 962221db783fa8590f3896c0995386c312a35cad
Author: Rachel <address@hidden>
Date: Thu Jan 12 14:00:19 2017 +0100
Improve Manage_Table_SQL : add functions for javascript , update or visible
flag ...
Signed-off-by: Dany De Bontridder <address@hidden>
---
include/database/class_manage_table_sql.php | 341 +++++++++++++++++++++++----
1 file changed, 296 insertions(+), 45 deletions(-)
diff --git a/include/database/class_manage_table_sql.php
b/include/database/class_manage_table_sql.php
index 4cfb103..e7e227d 100644
--- a/include/database/class_manage_table_sql.php
+++ b/include/database/class_manage_table_sql.php
@@ -1,30 +1,111 @@
<script>
-var ManageTable={
- add:function(p_table) {
- // display the form to enter data
- },
- save:function(form_id) {
+/**
+ address@hidden Javascript object to manage ajax calls to
+ * save , input or delete data row. The callback must
+respond with a XML file , the tag status for the result
+and data the HTML code to display
+ address@hidden p_table_name the data table on which we're working
+in javascript , to create an object to manipulate the table
+version.
address@hidden
+// Version will manage the table "version"
+var version=new ManageTable("version");
+
+// the file ajax_my.php will be called
+version.set_callback("ajax_my.php");
+
+// Add supplemental parameter to this file
+version.param_add ({"plugin_code":"OIC"};
+
+
- },
- delete:function (p_table,p_id) {
address@hidden
+ */
+var ManageTable=function(p_table_name)
+{
+ this.callback="ajax.php";
+ this.param={"table":p_table_name};
+ /**
+ address@hidden set the name of the callback file to
+ * call by default it is ajax.php
+ */
+ this.set_callback=function(p_new_callback) {
+ this.callback=p_new_callback;
+ };
+ /**
+ address@hidden By default send the json param variable
+ * you can add a json object to it in order to
+ * send it to the callback function
+ */
+ this.param_add= function(p_obj) {
+ var result={};
+ for ( var key in this.param) {
+ result[key]=this.param[key];
+ }
+ for ( var key in p_obj) {
+ result[key]=p_obj[key];
+ }
+ this.param=result;
+ return this.param;
+ };
+ /**
+ address@hidden call the ajax with the action save
+ */
+ this.save=function(form_id) {
+ var form=$F(form_id);
+ this.param_add(form);
- },
- input:function (p_table,p_id) {
- }
+ };
+ /**
+ address@hidden call the ajax with action delete
+ address@hidden id (pk) of the data row
+ */
+ this.delete=function (p_id) {
+ this.param['p_id']=p_id;
+ this.param['action']='delete';
+ };
+ /**
+ address@hidden display a dialog box with the information
+ * of the data row
+ address@hidden id (pk) of the data row
+ */
+ this.input=function (p_table,p_id) {
+ this.param['p_id']=p_id;
+ this.param['action']='input';
+ // display the form to enter data
+ new Ajax.Request(this.callback,{
+ parameters : this.param,
+ method:"get",
+ onSuccess:function (req) {
+ var a=new Element("div");
+ a.id="div_"+p_table+"_"+p_id;
+ a.update(req.responseText);
+ }
+ }
+ };
}
</script>
<?php
+/**
+ address@hidden
+ */
class Manage_Table_SQL
{
private $table ;
private $a_label_displaid;
- private $a_order; //!< order
- private $a_prop ; //!< property
- const UPDATABLE=1;
- const VISIBLE=2;
+ private $a_order; //!< order
+ private $a_prop ; //!< property
+ 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
+ const UPDATABLE=1;
+ const VISIBLE=2;
+ /**
+ address@hidden
+ */
function __construct(Noalyss_SQL $p_table)
{
$this->table=$p_table;
@@ -34,21 +115,137 @@ class Manage_Table_SQL
$this->a_label_displaid[$value]=$value;
$this->a_order[$order]=$value;
- $this->a_prop[$value]=self::UPDATABLE|self::VISIBLE;
+ $this->a_prop[$value]=self::UPDATABLE|self::VISIBLE;
$order++;
}
+ $this->object_name=uniqid("tbl");
+ $this->row_delete=TRUE;
+ $this->row_update=TRUE;
}
- function set_col_label($p_col_name,$p_display)
+ /**
+ address@hidden set the callback function that is passed to javascript
+ address@hidden $p_file : callback file by default ajax.php
+ */
+ function set_callback($p_file)
{
- $this->a_label_displaid[$p_col_name]=$p_display;
+ $this->callback=$p_file;
}
- function get_current_pos($p_col_name)
+ /**
+ address@hidden we must create first the javascript if we want to update
or delete
+ * row. It is the default script .
+ */
+ function create_js_script() {
+ echo "
+ <script>
+ var {$this->object_name}=new
ManageTable(\"{$this->table->tablename}\");
+ {$this->object_name}.set_callback(\"{$this->callback}\");
+ </script>
+
+ ";
+ }
+ /**
+ address@hidden set a column of the data row updatable or not
+ address@hidden $p_key data column
+ address@hidden $p_vallue 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";
+ }
+ /**
+ address@hidden return false if the update of the row is forbidden
+ */
+ function can_update_row() {
+
+ return $this->row_update;
+ }
+ /**
+ address@hidden Enable or disable the updating of rows
+ address@hidden $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;
+ }
+ /**
+ address@hidden return false if the delete of the row is forbidden
+ */
+ function can_delete_row() {
+ return $this->row_delete;
+
+ }
+ /**
+ address@hidden Enable or disable the deleting of rows
+ address@hidden $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;
+ }
+ /**
+ address@hidden return True if the column is updatable otherwise false
+ address@hidden $p_key data column
+ */
+ function get_property_updatable($p_key)
+ {
+ if ( $this->a_prop[$p_key] & self:UPDATABLE == 1) return true;
+ return false;
+
+ }
+ /**
+ address@hidden set a column of the data row visible or not
+ address@hidden $p_key data column
+ address@hidden $p_vallue 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";
+
+ }
+ /**
+ address@hidden return True if the column is visible otherwise false
+ address@hidden $p_key data column
+ */
+ function get_property_visible($p_key)
+ {
+ if ( $this->a_prop[$p_key] & self:VISIBLE == 1) return true;
+ return false;
+
+ }
+ /**
+ address@hidden set the name to display for a column
+ address@hidden $p_key data column
+ *
+ */
+ function set_col_label($p_key,$p_display)
+ {
+ $this->a_label_displaid[$p_key]=$p_display;
+ }
+ /**
+ address@hidden get the position of a column
+ address@hidden $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_col_name) return $i;
- throw new Exception ("COL INVAL ".$p_col_name);
+ if ( $this->a_order[$i]==$p_key) return $i;
+ throw new Exception ("COL INVAL ".$p_key);
}
/**
@@ -58,11 +255,12 @@ class Manage_Table_SQL
* 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]
+ address@hidden $p_key data column
*/
- function move($p_col_name,$p_idx)
+ function move($p_key,$p_idx)
{
- // get current position of p_col_name
- $cur_pos=$this->get_current_pos($p_col_name);
+ // get current position of p_key
+ $cur_pos=$this->get_current_pos($p_key);
if ( $cur_pos == $p_idx ) return ;
@@ -71,30 +269,33 @@ class Manage_Table_SQL
$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_col_name )
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_col_name;
+ $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_col_name )
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_col_name;
+ $this->a_order[$i-1]=$p_key;
}
}
}
+ /**
+ address@hidden display the data of the table
+ */
function display_table()
{
$ret=$this->table->seek();
@@ -108,24 +309,35 @@ class Manage_Table_SQL
$this->display_row($row);
}
}
+ /**
+ address@hidden display the column header excepted the not visible one
+ * and in the order defined with $this->a_order
+ */
function display_table_header()
{
$nb=count($this->a_order);
- echo "<tr>";
+ echo "<tr>";
for ($i=0;$i < $nb ; $i++ ) {
$key=$this->a_order[$i];
- echo th($this->a_label_displaid[$key]);
+ if ( $this->get_property_visible($key) )
+ echo th($this->a_label_displaid[$key]);
}
echo "</tr>";
}
+ /**
+ address@hidden set the id value of a data row
+ */
function set_pk($p_id)
{
$this->table->set_pk_value($p_id);
}
+ /**
+ address@hidden get the data from http request
+ */
function from_request()
{
$nb=count($this->a_order);
@@ -136,32 +348,47 @@ class Manage_Table_SQL
}
}
+ /**
+ address@hidden display a data row in the table, with the order defined
+ * in a_order and depending of the visibility of the column
+ address@hidden display_table
+ */
private function display_row($p_row)
{
printf ('<tr id="%s_%s">',
- $this->table->table,
+ $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);
- echo td($p_row[$v]);
- }
- $js=printf ("ManageTable.input('%s','%s');",
- $this->table->table,
- $p_row[$this->table->primary_key]
- );
- $js=printf ("ManageTable.delete('%s','%s');",
- $this->table->table,
- $p_row[$this->table->primary_key]
- );
-
- echo "<td>";
+ $v=$this->a_order($i);
+ if ( $this->get_property_visible($v) )
+ echo td($p_row[$v]);
+ }
+ echo "<td>";
+ if ( $this->can_update_row() ) {
+ $js=printf ("ManageTable.input('%s');",
+ $p_row[$this->table->primary_key]
+ );
+ }
+ echo "</td>";
+ echo "<td>";
+ if ( $this->can_delete_row() ) {
+ $js=printf ("ManageTable.delete('%s');",
+ $p_row[$this->table->primary_key]
+ );
+
+ }
+ echo "</td>";
echo '</tr>';
}
+ /**
+ address@hidden display into a dialog box the datarow in order
+ * to be appended or modified
+ */
function input()
{
$nb_order=count($this->a_order);
@@ -185,21 +412,45 @@ class Manage_Table_SQL
echo "</tr>";
echo "</table>";
}
+ /**
+ address@hidden delete a datarow , the id must be have set before
+ address@hidden from_request
+ */
function delete() {
$this->table->delete();
}
+ /**
+ address@hidden save the Noalyss_SQL Object
+ *The noalyss_SQL is not empty
+ address@hidden from_request
+ */
function save() {
$this->table->save();
}
+ /**
+ address@hidden insert a new value
+ address@hidden set_pk_value
+ address@hidden from_request
+ */
function insert() {
$this->table->insert();
}
+ /**
+ address@hidden
+ address@hidden set_pk_value
+ address@hidden from_request
+ */
function update() {
$this->table->update();
}
- function set_value($p_col_name,$p_value)
+ /**
+ address@hidden
+ address@hidden set_pk_value
+ address@hidden from_request
+ */
+ function set_value($p_key,$p_value)
{
- $this->table->set($p_col_name,$p_value);
+ $this->table->set($p_key,$p_value);
}
}
- [Noalyss-commit] [noalyss] 101/151: Deprecated HtmlInput::make_object must be replace by json_encode, (continued)
- [Noalyss-commit] [noalyss] 101/151: Deprecated HtmlInput::make_object must be replace by json_encode, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 99/151: Improve Sorttable, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 110/151: Add property in Manage_Table VISIBLE UPDATABLE, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 125/151: Test ManageTable ; fix parseXML function, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 144/151: Merge branch 'master' of ns3:/srv/git/noalyss, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 09/151: Experiment less, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 123/151: Test ManageTable ; fix parseXML function, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 126/151: Correct js : this of the object is not callable from a nested function, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 135/151: Fix bug : duplicate doesn't take in account the TVA_ID, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 148/151: Missing col in ANC_TABLE, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 111/151: Improve Manage_Table_SQL : add functions for javascript , update or visible flag ...,
Dany De Bontridder <=
- [Noalyss-commit] [noalyss] 147/151: Return must be after throw, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 141/151: ManageTable colorize the row in alternate, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 150/151: Improve Doxygen setting, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 133/151: Correct Manage Table , finish implementing, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 03/151: CSS : Update style mobile, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 57/151: CSS : Update style mobile, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 117/151: Indent properly, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 89/151: remove debug info, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 114/151: Move class_table_sql to lib , since it is usable without Noalyss, Dany De Bontridder, 2017/02/04
- [Noalyss-commit] [noalyss] 127/151: Add the noalyss version in the info page, Dany De Bontridder, 2017/02/04