interface_50_modMailmanspip_Mailmanspipsynchro.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /* Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php
  19. * \ingroup core
  20. * \brief File to manage triggers Mailman and Spip
  21. */
  22. require_once (DOL_DOCUMENT_ROOT."/mailmanspip/class/mailmanspip.class.php");
  23. require_once (DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php");
  24. /**
  25. * Class of triggers for MailmanSpip module
  26. */
  27. class InterfaceMailmanSpipsynchro
  28. {
  29. var $db;
  30. var $error;
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. function __construct($db)
  37. {
  38. $this->db = $db;
  39. $this->name = preg_replace('/^Interface/i','',get_class($this));
  40. $this->family = "ldap";
  41. $this->description = "Triggers of this module allows to synchronize Mailman an Spip.";
  42. $this->version = 'dolibarr'; // 'experimental' or 'dolibarr' or version
  43. $this->picto = 'technic';
  44. }
  45. /**
  46. * Return name of trigger file
  47. *
  48. * @return string Name of trigger file
  49. */
  50. function getName()
  51. {
  52. return $this->name;
  53. }
  54. /**
  55. * Return description of trigger file
  56. *
  57. * @return string Description of trigger file
  58. */
  59. function getDesc()
  60. {
  61. return $this->description;
  62. }
  63. /**
  64. * Return version of trigger file
  65. *
  66. * @return string Version of trigger file
  67. */
  68. function getVersion()
  69. {
  70. global $langs;
  71. $langs->load("admin");
  72. if ($this->version == 'experimental') return $langs->trans("Experimental");
  73. elseif ($this->version == 'dolibarr') return DOL_VERSION;
  74. elseif ($this->version) return $this->version;
  75. else return $langs->trans("Unknown");
  76. }
  77. /**
  78. * Function called when a Dolibarrr business event is done.
  79. * All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers
  80. *
  81. * @param string $action Event action code
  82. * @param Object $object Object
  83. * @param User $user Object user
  84. * @param Translate $langs Object langs
  85. * @param conf $conf Object conf
  86. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  87. */
  88. function run_trigger($action,$object,$user,$langs,$conf)
  89. {
  90. if (empty($conf->mailmanspip->enabled)) return 0; // Module not active, we do nothing
  91. if (! function_exists('ldap_connect'))
  92. {
  93. dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
  94. return 0;
  95. }
  96. // Users
  97. if ($action == 'USER_CREATE')
  98. {
  99. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  100. }
  101. elseif ($action == 'USER_MODIFY')
  102. {
  103. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  104. }
  105. elseif ($action == 'USER_NEW_PASSWORD')
  106. {
  107. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  108. }
  109. elseif ($action == 'USER_ENABLEDISABLE')
  110. {
  111. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  112. }
  113. elseif ($action == 'USER_DELETE')
  114. {
  115. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  116. }
  117. elseif ($action == 'USER_SETINGROUP')
  118. {
  119. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  120. }
  121. elseif ($action == 'USER_REMOVEFROMGROUP')
  122. {
  123. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  124. }
  125. elseif ($action == 'CATEGORY_LINK')
  126. {
  127. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  128. // We add subscription if we change category (new category may means more mailing-list to subscribe)
  129. if (is_object($object->linkto) && method_exists($object->linkto, 'add_to_abo') && $object->linkto->add_to_abo() < 0)
  130. {
  131. $this->error=$object->linkto->error;
  132. $this->errors=$object->linkto->errors;
  133. $return=-1;
  134. }
  135. else
  136. {
  137. $return=1;
  138. }
  139. return $return;
  140. }
  141. elseif ($action == 'CATEGORY_UNLINK')
  142. {
  143. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  144. // We remove subscription if we change category (lessw category may means less mailing-list to subscribe)
  145. if (is_object($object->unlinkoff) && method_exists($object->unlinkoff, 'del_to_abo') && $object->unlinkoff->del_to_abo() < 0)
  146. {
  147. $this->error=$object->unlinkoff->error;
  148. $this->errors=$object->unlinkoff->errors;
  149. $return=-1;
  150. }
  151. else
  152. {
  153. $return=1;
  154. }
  155. return $return;
  156. }
  157. // Members
  158. elseif ($action == 'MEMBER_VALIDATE' || $action == 'MEMBER_MODIFY')
  159. {
  160. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  161. $return=0;
  162. // Add user into some linked tools (mailman, spip, etc...)
  163. if (($object->oldcopy->email != $object->email) || ($object->oldcopy->typeid != $object->typeid)) // TODO Do del/add also if type change
  164. {
  165. if (is_object($object->oldcopy) && ($object->oldcopy->email != $object->email)) // If email has changed we delete mailman subscription for old email
  166. {
  167. if ($object->oldcopy->del_to_abo() < 0)
  168. {
  169. if (! empty($object->oldcopy->error)) $this->error=$object->oldcopy->error;
  170. $this->errors=$object->oldcopy->errors;
  171. $return=-1;
  172. }
  173. else
  174. {
  175. $return=1;
  176. }
  177. }
  178. // We add subscription if new email or new type (new type may means more mailing-list to subscribe)
  179. if ($object->add_to_abo() < 0)
  180. {
  181. if (! empty($object->error)) $this->error=$object->error;
  182. $this->errors=$object->errors;
  183. $return=-1;
  184. }
  185. else
  186. {
  187. $return=1;
  188. }
  189. }
  190. return $return;
  191. }
  192. elseif ($action == 'MEMBER_NEW_PASSWORD')
  193. {
  194. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  195. }
  196. elseif ($action == 'MEMBER_RESILIATE' || $action == 'MEMBER_DELETE')
  197. {
  198. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  199. $return=0;
  200. // Remove from external tools (mailman, spip, etc...)
  201. if ($object->del_to_abo() < 0)
  202. {
  203. if (! empty($object->error)) $this->error=$object->error;
  204. $this->errors=$object->errors;
  205. $return=-1;
  206. }
  207. else
  208. {
  209. $return=1;
  210. }
  211. }
  212. // If not found
  213. /*
  214. else
  215. {
  216. dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
  217. return -1;
  218. }
  219. */
  220. return 0;
  221. }
  222. }
  223. ?>