From 8e5537f1cea6f4201338025d8c0a3f9ec9614951 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 10 Aug 2013 02:40:11 +0200 Subject: [PATCH] New: Add trigger CATEGORY_LINK and CATEGORY_UNLINK. --- ChangeLog | 2 +- htdocs/adherents/fiche.php | 3 +- htdocs/categories/class/categorie.class.php | 56 +++++++++++++++------ 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index d83555879b3..3ae461decb3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -47,7 +47,6 @@ For users: - New: [ task #918 ] Stock replenishment - Fix: [ bug #992 ] Proforma invoices don't have a separated numeric count - For translators: - Qual: Normalized sort order of all languages files with english reference files. - New: Add language code files for South Africa. @@ -72,6 +71,7 @@ For developers: separated by a coma, of other POST parameters, Dolibarr will automatically save this parameters into user cookies. - New: Add hook addHomeSetup. +- New: Add trigger CATEGORY_LINK and CATEGORY_UNLINK. WARNING: Following change may create regression for some external modules, but was necessary to make Dolibarr better: diff --git a/htdocs/adherents/fiche.php b/htdocs/adherents/fiche.php index 7c92bd18623..25ee691c453 100644 --- a/htdocs/adherents/fiche.php +++ b/htdocs/adherents/fiche.php @@ -360,6 +360,7 @@ if ($action == 'update' && ! $_POST["cancel"] && $user->rights->adherent->creer) } // Rajoute l'utilisateur dans les divers abonnements (mailman, spip, etc...) + // TODO Move this into update trigger MEMBER_MODIFY if (($object->oldcopy->email != $object->email) || ($object->oldcopy->typeid != $object->typeid)) { if ($object->oldcopy->email != $object->email) // If email has changed we delete mailman subscription for old email @@ -1411,7 +1412,7 @@ else // EMail print ''.$langs->trans("EMail").''.dol_print_email($object->email,0,$object->fk_soc,1).''; - + // Password if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) { diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 78fa74d206e..693c8036ada 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -390,16 +390,16 @@ class Categorie function add_type($obj,$type) { global $conf; - + + $error=0; + if ($this->id == -1) return -2; - + if ($type == 'company') $type='societe'; if ($type == 'fournisseur') $type='societe'; - + $column_name=$type; - if ($type=='contact') { - $column_name='socpeople'; - } + if ($type=='contact') $column_name='socpeople'; $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_".$type." (fk_categorie, fk_".$column_name.")"; $sql .= " VALUES (".$this->id.", ".$obj->id.")"; @@ -407,10 +407,11 @@ class Categorie dol_syslog(get_class($this).'::add_type sql='.$sql); if ($this->db->query($sql)) { - if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) { + if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) + { $sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'categorie'; $sql.= " WHERE rowid = '".$this->id."'"; - + dol_syslog(get_class($this)."::add_type sql=".$sql); $resql=$this->db->query($sql); if ($resql) @@ -418,7 +419,7 @@ class Categorie if ($this->db->num_rows($resql) > 0) { $objparent = $this->db->fetch_object($resql); - + if (!empty($objparent->fk_parent)) { $cat = new Categorie($this->db); $cat->id=$objparent->fk_parent; @@ -428,12 +429,23 @@ class Categorie } else { - $this->error=$this->db->error().' sql='.$sql; + $this->error=$this->db->lasterror(); return -1; } } - - return 1; + + // Save object we want to link category to into category instance to provide information to trigger + $this->linkto=$object; + + // Appel des triggers + include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('CATEGORY_LINK',$this,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=join(',',$this->errors); } + // Fin appel triggers + + if (! $error) return 1; + else return -2; } else { @@ -444,7 +456,7 @@ class Categorie } else { - $this->error=$this->db->error().' sql='.$sql; + $this->error=$this->db->lasterror(); } return -1; } @@ -459,10 +471,11 @@ class Categorie */ function del_type($obj,$type) { + $error=0; if ($type == 'company') $type='societe'; if ($type == 'fournisseur') $type='societe'; - + $column_name=$type; if ($type=='contact') $column_name='socpeople'; @@ -473,11 +486,22 @@ class Categorie dol_syslog(get_class($this).'::del_type sql='.$sql); if ($this->db->query($sql)) { - return 1; + // Save object we want to unlink category off into category instance to provide information to trigger + $this->unlinkoff=$obj; + + // Appel des triggers + include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('CATEGORY_UNLINK',$this,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=join(',',$this->errors); } + // Fin appel triggers + + if (! $error) return 1; + else return -2; } else { - $this->error=$this->db->error().' sql='.$sql; + $this->error=$this->db->lasterror(); return -1; } }