From c4bc10b8eb2e8806f3941aefcb38b985999ac792 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 21 Aug 2013 12:39:20 +0200 Subject: [PATCH] Qual: Clean code by moving synchro with mailman into mailman trigger New: A trigger can return an array of error strings instead of one error string. --- ChangeLog | 4 +- htdocs/adherents/class/adherent.class.php | 13 +- htdocs/adherents/fiche.php | 44 ---- htdocs/categories/categorie.php | 28 +- htdocs/categories/class/categorie.class.php | 16 +- htdocs/core/class/interfaces.class.php | 3 +- ...interface_50_modLdap_Ldapsynchro.class.php | 38 +-- ...odMailmanspip_Mailmanspipsynchro.class.php | 247 ++++++++++++++++++ 8 files changed, 302 insertions(+), 91 deletions(-) create mode 100644 htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php diff --git a/ChangeLog b/ChangeLog index e8b5cc4b203..9076616d183 100644 --- a/ChangeLog +++ b/ChangeLog @@ -68,12 +68,14 @@ For developers: key to use a specific language file. - New: Make some changes to allow usage of several alternative $dolibarr_main_url_root variables. Fix also several bugs with old code. -- Qual: All nowrap properties are now using CSS class nowrap. +- Qual: All nowrap properties are now using CSS class nowrap. +- Qual: Move hardcoded code of module mailmanspip into trigger. - New: Into POST forms, if you can add a parameter DOL_AUTOSET_COOKIE with a vlue that is list name, 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. +- New: A trigger can return an array of error strings instead of one error string. WARNING: Following change may create regression for some external modules, but was necessary to make Dolibarr better: diff --git a/htdocs/adherents/class/adherent.class.php b/htdocs/adherents/class/adherent.class.php index da94fa3ac02..2dae974d9c1 100644 --- a/htdocs/adherents/class/adherent.class.php +++ b/htdocs/adherents/class/adherent.class.php @@ -1419,8 +1419,7 @@ class Adherent extends CommonObject /** - * Fonction qui ajoute l'adherent aux abonnements automatiques mailing-list, spip, etc. - * TODO Move this into member creation trigger (trigger of mailmanspip module) + * Function to add member into external tools mailing-list, spip, etc. * * @return int <0 if KO, >0 if OK */ @@ -1439,7 +1438,7 @@ class Adherent extends CommonObject $result=$mailmanspip->add_to_mailman($this); if ($result < 0) { - $this->error=$mailmanspip->error; + if (! empty($mailmanspip->error)) $this->errors[]=$mailmanspip->error; $err+=1; } foreach ($mailmanspip->mladded_ko as $tmplist => $tmpemail) @@ -1460,7 +1459,7 @@ class Adherent extends CommonObject $result=$mailmanspip->add_to_spip($this); if ($result < 0) { - $this->error=$mailmanspip->error; + $this->errors[]=$mailmanspip->error; $err+=1; } } @@ -1476,8 +1475,7 @@ class Adherent extends CommonObject /** - * Fonction qui supprime l'adherent des abonnements automatiques mailing-list, spip, etc. - * TODO Move this into member deletion trigger (trigger of mailmanspip module) + * Function to delete a member from external tools like mailing-list, spip, etc. * * @return int <0 if KO, >0 if OK */ @@ -1496,7 +1494,7 @@ class Adherent extends CommonObject $result=$mailmanspip->del_to_mailman($this); if ($result < 0) { - $this->error=$mailmanspip->error; + if (! empty($mailmanspip->error)) $this->errors[]=$mailmanspip->error; $err+=1; } @@ -1517,6 +1515,7 @@ class Adherent extends CommonObject $result=$mailmanspip->del_to_spip($this); if ($result < 0) { + $this->errors[]=$mailmanspip->error; $err+=1; } } diff --git a/htdocs/adherents/fiche.php b/htdocs/adherents/fiche.php index 25ee691c453..66de54e804a 100644 --- a/htdocs/adherents/fiche.php +++ b/htdocs/adherents/fiche.php @@ -359,34 +359,6 @@ 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 - { - if ($object->oldcopy->del_to_abo() < 0) - { - if (! empty($object->oldcopy->error)) setEventMessage($langs->trans("ErrorFailedToRemoveToMailmanList").': '.$object->oldcopy->error, 'errors'); - setEventMessage($object->oldcopy->errors, 'errors'); - } - else - { - setEventMessage($object->oldcopy->mesgs,'mesgs'); - } - } - // We add subscription if new email or new type (new type may means more mailing-list to subscribe) - if ($object->add_to_abo() < 0) - { - if (! empty($object->error)) setEventMessage($langs->trans("ErrorFailedToAddToMailmanList").': '.$object->error, 'errors'); - setEventMessage($object->errors, 'errors'); - } - else - { - setEventMessage($object->mesgs, 'mesgs'); - } - } - $rowid=$object->id; $action=''; @@ -601,15 +573,6 @@ if ($user->rights->adherent->creer && $action == 'confirm_valid' && $confirm == $errmsg.=$object->error; } } - - // Add user to other systems (mailman, spip, etc...) - // TODO Move this into trigger on validate action - if (! $error && $object->add_to_abo() < 0) - { - $langs->load("errors"); - $error++; - $errmsg.= $langs->trans("ErrorFailedToAddToMailmanList").': '.$object->error." ".join(',',$object->errors)."
\n"; - } } else { @@ -648,13 +611,6 @@ if ($user->rights->adherent->supprimer && $action == 'confirm_resign') { $errmsg.=$object->error; } - - // supprime l'utilisateur des divers abonnements .. - if ($object->del_to_abo() < 0) - { - // error - $errmsg.=$langs->trans("FaildToRemoveFromMailmanList").': '.$object->error."
\n"; - } } else { diff --git a/htdocs/categories/categorie.php b/htdocs/categories/categorie.php index 7ec7f8fb889..f77c04b07d4 100644 --- a/htdocs/categories/categorie.php +++ b/htdocs/categories/categorie.php @@ -37,7 +37,6 @@ $socid = GETPOST('socid','int'); $id = GETPOST('id','int'); $ref = GETPOST('ref'); $type = GETPOST('type'); -$mesg = GETPOST('mesg'); $removecat = GETPOST('removecat','int'); $parent=GETPOST('parent','int'); @@ -147,6 +146,11 @@ if (empty($reshook)) $result=$cat->fetch($removecat); $result=$cat->del_type($object,$elementtype); + if ($result < 0) + { + setEventMessage($cat->error,'errors'); + setEventMessage($cat->errors,'errors'); + } } // Add object into a category @@ -191,12 +195,19 @@ if (empty($reshook)) $result=$cat->add_type($object,$elementtype); if ($result >= 0) { - $mesg='
'.$langs->trans("WasAddedSuccessfully",$cat->label).'
'; + setEventMessage($langs->trans("WasAddedSuccessfully",$cat->label)); } else { - if ($cat->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') $mesg='
'.$langs->trans("ObjectAlreadyLinkedToCategory").'
'; - else $mesg=$langs->trans("Error").' '.$cat->error; + if ($cat->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') + { + setEventMessage($langs->trans("ObjectAlreadyLinkedToCategory"),'warnings'); + } + else + { + setEventMessage($cat->error,'errors'); + setEventMessage($cat->errors,'errors'); + } } } } @@ -308,8 +319,6 @@ if ($socid) dol_fiche_end(); - dol_htmloutput_mesg($mesg); - if ($soc->client) formCategory($db,$soc,2,$socid); if ($soc->client && $soc->fournisseur) print '

'; @@ -368,8 +377,6 @@ else if ($id || $ref) dol_fiche_end(); - dol_htmloutput_mesg($mesg); - formCategory($db,$product,0); } @@ -449,8 +456,6 @@ else if ($id || $ref) dol_fiche_end(); - dol_htmloutput_mesg($mesg); - formCategory($db,$member,3); } if ($type == 4) @@ -597,8 +602,6 @@ else if ($id || $ref) dol_fiche_end(); - dol_htmloutput_mesg($mesg); - formCategory($db,$object,4); } } @@ -726,5 +729,6 @@ function formCategory($db,$object,$typeid,$socid=0) llxFooter(); + $db->close(); ?> diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 693c8036ada..ac71c22d612 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -384,12 +384,12 @@ class Categorie * Link an object to the category * * @param Object $obj Object to link to category - * @param string $type Type of category (member, supplier, product, customer) + * @param string $type Type of category (member, supplier, product, customer, contact) * @return int 1 : OK, -1 : erreur SQL, -2 : id not defined, -3 : Already linked */ function add_type($obj,$type) { - global $conf; + global $user,$langs,$conf; $error=0; @@ -407,10 +407,10 @@ 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."'"; + $sql.= " WHERE rowid = ".$this->id; dol_syslog(get_class($this)."::add_type sql=".$sql); $resql=$this->db->query($sql); @@ -435,13 +435,13 @@ class Categorie } // Save object we want to link category to into category instance to provide information to trigger - $this->linkto=$object; + $this->linkto=$obj; // 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); } + if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=$interface->error; } // Fin appel triggers if (! $error) return 1; @@ -471,6 +471,8 @@ class Categorie */ function del_type($obj,$type) { + global $user,$langs,$conf; + $error=0; if ($type == 'company') $type='societe'; @@ -493,7 +495,7 @@ class Categorie 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); } + if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=$this->error; } // Fin appel triggers if (! $error) return 1; diff --git a/htdocs/core/class/interfaces.class.php b/htdocs/core/class/interfaces.class.php index 9c27420fb89..a40c7250103 100644 --- a/htdocs/core/class/interfaces.class.php +++ b/htdocs/core/class/interfaces.class.php @@ -167,7 +167,8 @@ class Interfaces // Action KO $nbtotal++; $nbko++; - $this->errors[]=$objMod->error; + if (! empty($objMod->error)) $this->errors[]=$objMod->error; + if (! empty($objMod->errors)) $this->errors=array_merge($this->errors,$objMod->errors); } } else diff --git a/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php b/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php index d0a1bd45168..5af0f663a80 100644 --- a/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php +++ b/htdocs/core/triggers/interface_50_modLdap_Ldapsynchro.class.php @@ -110,7 +110,7 @@ class InterfaceLdapsynchro if ($action == 'USER_CREATE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -129,7 +129,7 @@ class InterfaceLdapsynchro elseif ($action == 'USER_MODIFY') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -166,7 +166,7 @@ class InterfaceLdapsynchro elseif ($action == 'USER_NEW_PASSWORD') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -207,7 +207,7 @@ class InterfaceLdapsynchro elseif ($action == 'USER_DELETE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -226,7 +226,7 @@ class InterfaceLdapsynchro elseif ($action == 'USER_SETINGROUP') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -264,7 +264,7 @@ class InterfaceLdapsynchro elseif ($action == 'USER_REMOVEFROMGROUP') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -304,7 +304,7 @@ class InterfaceLdapsynchro elseif ($action == 'GROUP_CREATE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -327,7 +327,7 @@ class InterfaceLdapsynchro elseif ($action == 'GROUP_MODIFY') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -364,7 +364,7 @@ class InterfaceLdapsynchro elseif ($action == 'GROUP_DELETE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') + if (! empty($conf->global->LDAP_SYNCHRO_ACTIVE) && $conf->global->LDAP_SYNCHRO_ACTIVE === 'dolibarr2ldap') { $ldap=new Ldap(); $ldap->connect_bind(); @@ -385,7 +385,7 @@ class InterfaceLdapsynchro elseif ($action == 'CONTACT_CREATE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_CONTACT_ACTIVE)) + if (! empty($conf->global->LDAP_CONTACT_ACTIVE)) { $ldap=new Ldap(); $ldap->connect_bind(); @@ -404,7 +404,7 @@ class InterfaceLdapsynchro elseif ($action == 'CONTACT_MODIFY') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_CONTACT_ACTIVE)) + if (! empty($conf->global->LDAP_CONTACT_ACTIVE)) { $ldap=new Ldap(); $ldap->connect_bind(); @@ -441,7 +441,7 @@ class InterfaceLdapsynchro elseif ($action == 'CONTACT_DELETE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_CONTACT_ACTIVE)) + if (! empty($conf->global->LDAP_CONTACT_ACTIVE)) { $ldap=new Ldap(); $ldap->connect_bind(); @@ -462,7 +462,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_CREATE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { $ldap=new Ldap(); $ldap->connect_bind(); @@ -481,7 +481,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_VALIDATE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { // If status field is setup to be synchronized if (! empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) @@ -505,7 +505,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_SUBSCRIPTION') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { // If subscriptions fields are setup to be synchronized if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE @@ -533,7 +533,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_MODIFY') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { $ldap=new Ldap(); $ldap->connect_bind(); @@ -570,7 +570,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_NEW_PASSWORD') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { // If password field is setup to be synchronized if ($conf->global->LDAP_FIELD_PASSWORD || $conf->global->LDAP_FIELD_PASSWORD_CRYPTED) @@ -594,7 +594,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_RESILIATE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { // If status field is setup to be synchronized if (! empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) @@ -618,7 +618,7 @@ class InterfaceLdapsynchro elseif ($action == 'MEMBER_DELETE') { dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); - if (! empty($conf->ldap->enabled) && ! empty($conf->global->LDAP_MEMBER_ACTIVE)) + if (! empty($conf->global->LDAP_MEMBER_ACTIVE)) { $ldap=new Ldap(); $ldap->connect_bind(); diff --git a/htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php b/htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php new file mode 100644 index 00000000000..03c6b9dbbd2 --- /dev/null +++ b/htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php @@ -0,0 +1,247 @@ + + * + * 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 3 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, see . + */ + +/** + * \file htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php + * \ingroup core + * \brief File to manage triggers Mailman and Spip + */ +require_once (DOL_DOCUMENT_ROOT."/mailmanspip/class/mailmanspip.class.php"); +require_once (DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php"); + + +/** + * Class of triggers for MailmanSpip module + */ +class InterfaceMailmanSpipsynchro +{ + var $db; + var $error; + + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + function __construct($db) + { + $this->db = $db; + + $this->name = preg_replace('/^Interface/i','',get_class($this)); + $this->family = "ldap"; + $this->description = "Triggers of this module allows to synchronize Mailman an Spip."; + $this->version = 'dolibarr'; // 'experimental' or 'dolibarr' or version + $this->picto = 'technic'; + } + + /** + * Return name of trigger file + * + * @return string Name of trigger file + */ + function getName() + { + return $this->name; + } + + /** + * Return description of trigger file + * + * @return string Description of trigger file + */ + function getDesc() + { + return $this->description; + } + + /** + * Return version of trigger file + * + * @return string Version of trigger file + */ + function getVersion() + { + global $langs; + $langs->load("admin"); + + if ($this->version == 'experimental') return $langs->trans("Experimental"); + elseif ($this->version == 'dolibarr') return DOL_VERSION; + elseif ($this->version) return $this->version; + else return $langs->trans("Unknown"); + } + + /** + * Function called when a Dolibarrr business event is done. + * All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers + * + * @param string $action Event action code + * @param Object $object Object + * @param User $user Object user + * @param Translate $langs Object langs + * @param conf $conf Object conf + * @return int <0 if KO, 0 if no triggered ran, >0 if OK + */ + function run_trigger($action,$object,$user,$langs,$conf) + { + if (empty($conf->mailmanspip->enabled)) return 0; // Module not active, we do nothing + + if (! function_exists('ldap_connect')) + { + dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING); + return 0; + } + + // Users + if ($action == 'USER_CREATE') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + + } + elseif ($action == 'USER_MODIFY') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + elseif ($action == 'USER_NEW_PASSWORD') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + elseif ($action == 'USER_ENABLEDISABLE') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + elseif ($action == 'USER_DELETE') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + elseif ($action == 'USER_SETINGROUP') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + elseif ($action == 'USER_REMOVEFROMGROUP') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + + elseif ($action == 'CATEGORY_LINK') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + + // We add subscription if new email or new type (new type may means more mailing-list to subscribe) + if ($object->linkto->add_to_abo() < 0) + { + $this->error=$object->linkto->error; + $this->errors=$object->linkto->errors; + $return=-1; + } + else + { + $return=1; + } + + return $return; + } + elseif ($action == 'CATEGORY_UNLINK') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + + if ($object->unlinkoff->del_to_abo() < 0) + { + $this->error=$object->unlinkoff->error; + $this->errors=$object->unlinkoff->errors; + $return=-1; + } + else + { + $return=1; + } + + return $return; + } + + // Members + elseif ($action == 'MEMBER_VALIDATE' || $action == 'MEMBER_MODIFY') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + + $return=0; + // Add user into some linked tools (mailman, spip, etc...) + if (($object->oldcopy->email != $object->email) || ($object->oldcopy->typeid != $object->typeid)) // TODO Do del/add also if type change + { + if ($object->oldcopy->email != $object->email) // If email has changed we delete mailman subscription for old email + { + if ($object->oldcopy->del_to_abo() < 0) + { + if (! empty($object->oldcopy->error)) $this->error=$object->oldcopy->error; + $this->errors=$object->oldcopy->errors; + $return=-1; + } + else + { + $return=1; + } + } + // We add subscription if new email or new type (new type may means more mailing-list to subscribe) + if ($object->add_to_abo() < 0) + { + if (! empty($object->error)) $this->error=$object->error; + $this->errors=$object->errors; + $return=-1; + } + else + { + $return=1; + } + } + + return $return; + } + elseif ($action == 'MEMBER_NEW_PASSWORD') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + } + elseif ($action == 'MEMBER_RESILIATE' || $action == 'MEMBER_DELETE') + { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + + $return=0; + // Remove from external tools (mailman, spip, etc...) + if ($object->del_to_abo() < 0) + { + if (! empty($object->error)) $this->error=$object->error; + $this->errors=$object->errors; + $return=-1; + } + else + { + $return=1; + } + } + + // If not found +/* + else + { + dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action."); + return -1; + } +*/ + return 0; + } + +} +?> \ No newline at end of file