New: Gestion des triggers sur la création, modification et suppression de sociétés

This commit is contained in:
Laurent Destailleur 2005-04-21 18:49:15 +00:00
parent bf4bdd5ef6
commit 9460f44724
3 changed files with 290 additions and 56 deletions

View File

@ -0,0 +1,119 @@
<?php
/* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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.
*
* $Id$
* $Source$
*
*/
/**
\file htdocs/includes/triggers/interface_demo.class.php
\ingroup core
\brief Fichier des actions de demo de workflow
\remarks Son propre fichier d'actions peut etre créés par recopie de celui-ci:
- Le nom du fichier doit etre interface_xxx.class.php
- Le fichier doit rester stocké dans includes/modules/triggers
- Le nom de la classe doit etre InterfaceXxx
*/
/**
\class interface_demo
\brief Classe de la fonction trigger des actions de workflow
*/
class InterfaceDemo
{
/**
* \brief Constructeur.
* \param DB handler d'accès base
*/
function InterfaceDemo($DB)
{
$this->db = $DB ;
}
/**
* \brief Fonction appelée lors du déclenchement d'un évènement Dolibarr.
* D'autres fonctions run_trigger peuvent etre présentes dans includes/triggers
* \param action Code de l'evenement
* \param object Objet concerné
* \param user Objet user
* \param lang Objet lang
* \param conf Objet conf
*/
function run_trigger($action,$object,$user,$lang,$conf)
{
// Mettre ici le code à exécuter en réaction de l'action
// Les données de l'action sont stockées dans $object
if ($action == 'COMPANY_CREATE')
{
dolibarr_syslog("Trigger for action '$action' launched. id=".$object->id);
}
elseif ($action == 'COMPANY_MODIFY')
{
dolibarr_syslog("Trigger for action '$action' launched. id=".$object->id);
}
elseif ($action == 'COMPANY_DELETE')
{
dolibarr_syslog("Trigger for action '$action' launched. id=".$object->id);
}
elseif ($action == 'BILL_CREATE')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'BILL_MODIFY')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'BILL_DELETE')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'PRODUCT_CREATE')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'PRODUCT_MODIFY')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'PRODUCT_DELETE')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'ORDER_CREATE')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'ORDER_MODIFY')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
elseif ($action == 'ORDER_DELETE')
{
dolibarr_syslog("Trigger for action '$action' launched");
}
else
{
dolibarr_syslog("A trigger for action '$action' was ran but no handler found.");
}
}
}
?>

View File

@ -0,0 +1,95 @@
<?php
/* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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.
*
* $Id$
* $Source$
*
*/
/**
\file htdocs/interfaces.class.php
\ingroup core
\brief Fichier de la classe de gestion des triggers
*/
/**
\class Interfaces
\brief Classe de la gestion des triggers
*/
class Interfaces
{
var $dir;
/**
* \brief Constructeur.
* \param DB handler d'accès base
*/
function Interfaces($DB)
{
$this->db = $DB ;
$this->dir = DOL_DOCUMENT_ROOT . "/includes/triggers";
}
/**
* \brief Fonction appelée lors du déclenchement d'un évènement Dolibarr.
* Cette fonction déclenche tous les triggers trouvés
* \param action Code de l'evenement
* \param object Objet concern
* \param user Objet user
* \param lang Objet lang
* \param conf Objet conf
* \return int Nbre de triggers déclenchés si pas d'erreurs. Nb en erreur sinon.
*/
function run_triggers($action,$object,$user,$lang,$conf)
{
$handle=opendir($this->dir);
$modules = array();
$nbok = $nbko = 0;
while (($file = readdir($handle))!==false)
{
if (is_readable($this->dir."/".$file) && eregi('interface_(.*).class.php',$file,$reg))
{
$modName = "Interface".ucfirst($reg[1]);
//print "file=$file"; print "modName=$modName"; exit;
if ($modName)
{
include_once($this->dir."/".$file);
$objMod = new $modName($db);
if ($objMod)
{
if ($objMod->run_trigger($action,$object,$user,$lang,$conf) > 0)
{
$nbok++;
}
else
{
$nbko++;
}
}
}
}
}
if ($nbko) return $nbko;
return $nbok;
}
}
?>

View File

@ -106,67 +106,74 @@ class Societe {
* \return 0 si ok, < 0 si erreur * \return 0 si ok, < 0 si erreur
*/ */
function create($user='') function create($user='')
{ {
global $langs; global $langs,$conf;
$this->nom=trim($this->nom); $this->nom=trim($this->nom);
$this->db->begin(); $this->db->begin();
$result = $this->verify();
if ($result >= 0) $result = $this->verify();
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (nom, datec, datea, fk_user_creat) "; if ($result >= 0)
$sql .= " VALUES ('".addslashes($this->nom)."', now(), now(), '".$user->id."')"; {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (nom, datec, datea, fk_user_creat) ";
$result=$this->db->query($sql); $sql .= " VALUES ('".addslashes($this->nom)."', now(), now(), '".$user->id."')";
if ($result)
{ $result=$this->db->query($sql);
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe"); if ($result)
{
$this->creation_bit = 1; $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe");
$ret = $this->update($this->id); $this->creation_bit = 1;
if ($ret == 0) $ret = $this->update($this->id);
{
$this->db->commit(); if ($ret == 0)
} {
else // Appel des triggers
{ include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
dolibarr_syslog("Societe::create echec update"); $interface=new Interfaces($this->db);
$this->db->rollback(); $interface->run_triggers('COMPANY_CREATE',$this,$user,$lang,$conf);
return -3; // Fin appel triggers
}
dolibarr_syslog("Societe::Create success id=".$this->id);
return $ret; $this->db->commit();
} }
else else
{
{ dolibarr_syslog("Societe::Create echec update");
if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS) $this->db->rollback();
{ return -3;
}
$this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->nom);
} return $ret;
}
else else
{
dolibarr_syslog("Societe::create echec insert sql=$sql"); {
} if ($this->db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
{
$this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->nom);
}
else
{
dolibarr_syslog("Societe::Create echec insert sql=$sql");
}
$this->db->rollback();
return -2;
}
}
else
{
$this->db->rollback(); $this->db->rollback();
return -2; dolibarr_syslog("Societe::Create echec verify sql=$sql");
} return -1;
}
} }
else
{
$this->db->rollback();
dolibarr_syslog("Societe::Create echec verify sql=$sql");
return -1;
}
}
/** /**
* \brief Verification lors de la modification * \brief Verification lors de la modification
@ -230,6 +237,7 @@ class Societe {
dolibarr_syslog("Societe::Update"); dolibarr_syslog("Societe::Update");
$this->id=$id;
$this->capital=trim($this->capital); $this->capital=trim($this->capital);
$this->nom=trim($this->nom); $this->nom=trim($this->nom);
$this->adresse=trim($this->adresse); $this->adresse=trim($this->adresse);
@ -354,6 +362,12 @@ class Societe {
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$interface->run_triggers('COMPANY_MODIFY',$this,$user,$lang,$conf);
// Fin appel triggers
$result = 0; $result = 0;
} }
else else
@ -564,6 +578,12 @@ class Societe {
if ($sqr == 3) if ($sqr == 3)
{ {
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$interface->run_triggers('COMPANY_DELETE',$this,$user,$lang,$conf);
// Fin appel triggers
$this->db->commit(); $this->db->commit();
// Suppression du répertoire document // Suppression du répertoire document