interface_50_modMailmanspip_Mailmanspipsynchro.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /* Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/triggers/interface_50_modMailmanspip_Mailmanspipsynchro.class.php
  20. * \ingroup core
  21. * \brief File to manage triggers Mailman and Spip
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  24. require_once DOL_DOCUMENT_ROOT."/mailmanspip/class/mailmanspip.class.php";
  25. require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
  26. /**
  27. * Class of triggers for MailmanSpip module
  28. */
  29. class InterfaceMailmanSpipsynchro extends DolibarrTriggers
  30. {
  31. public $family = 'ldap';
  32. public $description = "Triggers of this module allows to synchronize Mailman an Spip.";
  33. public $version = self::VERSION_DOLIBARR;
  34. public $picto = 'technic';
  35. /**
  36. * Function called when a Dolibarrr business event is done.
  37. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  38. *
  39. * @param string $action Event action code
  40. * @param Object $object Object
  41. * @param User $user Object user
  42. * @param Translate $langs Object langs
  43. * @param conf $conf Object conf
  44. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  45. */
  46. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  47. {
  48. if (empty($conf->mailmanspip->enabled)) return 0; // Module not active, we do nothing
  49. if (! function_exists('ldap_connect'))
  50. {
  51. dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
  52. return 0;
  53. }
  54. if ($action == 'CATEGORY_LINK')
  55. {
  56. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  57. // We add subscription if we change category (new category may means more mailing-list to subscribe)
  58. if (is_object($object->linkto) && method_exists($object->linkto, 'add_to_abo') && $object->linkto->add_to_abo() < 0)
  59. {
  60. $this->error=$object->linkto->error;
  61. $this->errors=$object->linkto->errors;
  62. $return=-1;
  63. }
  64. else
  65. {
  66. $return=1;
  67. }
  68. return $return;
  69. }
  70. elseif ($action == 'CATEGORY_UNLINK')
  71. {
  72. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  73. // We remove subscription if we change category (lessw category may means less mailing-list to subscribe)
  74. if (is_object($object->unlinkoff) && method_exists($object->unlinkoff, 'del_to_abo') && $object->unlinkoff->del_to_abo() < 0)
  75. {
  76. $this->error=$object->unlinkoff->error;
  77. $this->errors=$object->unlinkoff->errors;
  78. $return=-1;
  79. }
  80. else
  81. {
  82. $return=1;
  83. }
  84. return $return;
  85. }
  86. // Members
  87. elseif ($action == 'MEMBER_VALIDATE' || $action == 'MEMBER_MODIFY')
  88. {
  89. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  90. $return=0;
  91. // Add user into some linked tools (mailman, spip, etc...)
  92. if (($object->oldcopy->email != $object->email) || ($object->oldcopy->typeid != $object->typeid)) // TODO Do del/add also if type change
  93. {
  94. if (is_object($object->oldcopy) && ($object->oldcopy->email != $object->email)) // If email has changed we delete mailman subscription for old email
  95. {
  96. if ($object->oldcopy->del_to_abo() < 0)
  97. {
  98. if (! empty($object->oldcopy->error)) $this->error=$object->oldcopy->error;
  99. $this->errors=$object->oldcopy->errors;
  100. $return=-1;
  101. }
  102. else
  103. {
  104. $return=1;
  105. }
  106. }
  107. // We add subscription if new email or new type (new type may means more mailing-list to subscribe)
  108. if ($object->add_to_abo() < 0)
  109. {
  110. if (! empty($object->error)) $this->error=$object->error;
  111. $this->errors=$object->errors;
  112. $return=-1;
  113. }
  114. else
  115. {
  116. $return=1;
  117. }
  118. }
  119. return $return;
  120. }
  121. elseif ($action == 'MEMBER_RESILIATE' || $action == 'MEMBER_DELETE')
  122. {
  123. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  124. $return=0;
  125. // Remove from external tools (mailman, spip, etc...)
  126. if ($object->del_to_abo() < 0)
  127. {
  128. if (! empty($object->error)) $this->error=$object->error;
  129. $this->errors=$object->errors;
  130. $return=-1;
  131. }
  132. else
  133. {
  134. $return=1;
  135. }
  136. return $return;
  137. }
  138. return 0;
  139. }
  140. }