dblayer : allow to define the hook functions in the specification of the layer. Can be external ones

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2950 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2016-07-25 14:03:36 +00:00
parent a208873f34
commit 495ea91840

View File

@@ -64,6 +64,23 @@ class dblayer
public $verifyOneFunc; public $verifyOneFunc;
/** Define the name of the method to use to verify all entries */ /** Define the name of the method to use to verify all entries */
public $verifyAllFunc; public $verifyAllFunc;
/** Define the name of the method of hook before read */
public $hookprereadFunc;
/** Define the name of the method of hook after read */
public $hookpostreadFunc;
/** Define the name of the method of hook before insert */
public $hookpreinsertFunc;
/** Define the name of the method of hook after insert */
public $hookpostinsertFunc;
/** Define the name of the method of hook before update */
public $hookpreupdateFunc;
/** Define the name of the method of hook after update */
public $hookpostupdateFunc;
/** Define the name of the method of hook before delete */
public $hookpredeleteFunc;
/** Define the name of the method of hook after delete */
public $hookpostdeleteFunc;
/** The verify unitary stack /** The verify unitary stack
@param string $field The name of the field to test @param string $field The name of the field to test
@@ -104,6 +121,14 @@ class dblayer
{ {
$this->verifyOneFunc = array ($this, "verifyOne"); $this->verifyOneFunc = array ($this, "verifyOne");
$this->verifyAllFunc = array ($this, "verifyAll"); $this->verifyAllFunc = array ($this, "verifyAll");
$this->hookprereadFunc = array ($this, "hookpreread");
$this->hookpostreadFunc = array ($this, "hookpostread");
$this->hookpreinsertFunc = array ($this, "hookpreinsert");
$this->hookpostinsertFunc = array ($this, "hookpostinsert");
$this->hookpreupdateFunc = array ($this, "hookpreupdate");
$this->hookpostupdateFunc = array ($this, "hookpostupdate");
$this->hookpredeleteFunc = array ($this, "hookpredelete");
$this->hookpostdeleteFunc = array ($this, "hookpostdelete");
$driver = @explode (":", $dsn); $driver = @explode (":", $dsn);
if (! isset ($driver[0])) if (! isset ($driver[0]))
@@ -609,7 +634,7 @@ class dblayer
array_walk ($binds, function(&$value, $key) { array_walk ($binds, function(&$value, $key) {
$value = md5 ($value); $value = md5 ($value);
}); });
$dataOK = $this->hookpreinsert ($dataOK); $dataOK = call_user_func ($this->hookpreinsertFunc, $dataOK);
$req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep "; $req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep ";
$req .= "($this->sep". $req .= "($this->sep".
implode ("$this->sep,$this->sep", array_keys ($dataOK)). implode ("$this->sep,$this->sep", array_keys ($dataOK)).
@@ -646,7 +671,7 @@ class dblayer
exit; exit;
} }
$lastID = self::$instance[$this->dsn]->lastInsertId(); $lastID = self::$instance[$this->dsn]->lastInsertId();
$lastID = $this->hookpostinsert ($dataOK, $lastID); $lastID = call_user_func ($this->hookpostinsertFunc, $dataOK, $lastID);
return $lastID; return $lastID;
} }
@@ -707,7 +732,9 @@ class dblayer
foreach ($foreignSelect as $s) foreach ($foreignSelect as $s)
$foreignSelectCols[] = $s[0]; $foreignSelectCols[] = $s[0];
} }
$this->hookpreread ($select, $display, $order, $whereOr, $foreignSelect); call_user_func_array ($this->hookprereadFunc,
array (&$select, &$display, &$order,
&$whereOr, &$foreignSelect));
$req = "SELECT $this->sep"; $req = "SELECT $this->sep";
$req .= implode ("$this->sep,$this->sep", $display); $req .= implode ("$this->sep,$this->sep", $display);
$req .= "$this->sep "; $req .= "$this->sep ";
@@ -832,7 +859,7 @@ class dblayer
$res = array (); $res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC)) while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d; $res[] = $d;
$res = $this->hookpostread ($res); $res = call_user_func ($this->hookpostreadFunc, $res);
return $res; return $res;
} }
@@ -878,7 +905,7 @@ class dblayer
$dataOK[$field] = $data[$field]; $dataOK[$field] = $data[$field];
} }
$dataOK = $this->hookpreupdate ($updatekey, $dataOK); $dataOK = call_user_func ($this->hookpreupdateFunc, $updatekey, $dataOK);
$req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET "; $req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET ";
$i = 0; $i = 0;
foreach ($dataOK as $key=>$val) foreach ($dataOK as $key=>$val)
@@ -935,8 +962,8 @@ class dblayer
$st->execute (); $st->execute ();
$nbLinesUpdated = $st->rowCount (); $nbLinesUpdated = $st->rowCount ();
$nbLinesUpdated = $this->hookpostupdate ($updatekey, $dataOK, $nbLinesUpdated = call_user_func ($this->hookpostupdateFunc, $updatekey,
$nbLinesUpdated); $dataOK, $nbLinesUpdated);
return $nbLinesUpdated; return $nbLinesUpdated;
} }
@@ -952,7 +979,7 @@ class dblayer
throw new \Exception (dgettext("domframework", throw new \Exception (dgettext("domframework",
"No table name defined to delete in the table"), "No table name defined to delete in the table"),
500); 500);
$deletekey = $this->hookpredelete ($deletekey); $deletekey = call_user_func ($this->hookpredeleteFunc, $deletekey);
$req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep "; $req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep ";
$req .= "WHERE $this->primary = :primary"; $req .= "WHERE $this->primary = :primary";
$st = self::$instance[$this->dsn]->prepare ($req); $st = self::$instance[$this->dsn]->prepare ($req);
@@ -969,7 +996,8 @@ class dblayer
throw new \Exception ($e->getMessage (), 500); throw new \Exception ($e->getMessage (), 500);
} }
$nbLinesDeleted = $st->rowCount(); $nbLinesDeleted = $st->rowCount();
$nbLinesDeleted = $this->hookpostdelete ($deletekey, $nbLinesDeleted); $nbLinesDeleted = call_user_func ($this->hookpostdeleteFunc, $deletekey,
$nbLinesDeleted);
return $nbLinesDeleted; return $nbLinesDeleted;
} }