interface_50_modMailmanspip_Mailmanspipsynchro.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  25. * Class of triggers for MailmanSpip module
  26. */
  27. class InterfaceMailmanSpipsynchro extends DolibarrTriggers
  28. {
  29. public $family = 'mailmanspip';
  30. public $description = "Triggers of this module allows to synchronize Mailman an Spip.";
  31. /**
  32. * Version of the trigger
  33. * @var string
  34. */
  35. public $version = self::VERSION_DOLIBARR;
  36. /**
  37. * @var string Image of the trigger
  38. */
  39. public $picto = 'technic';
  40. /**
  41. * Function called when a Dolibarrr business event is done.
  42. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  43. *
  44. * @param string $action Event action code
  45. * @param Object $object Object
  46. * @param User $user Object user
  47. * @param Translate $langs Object langs
  48. * @param conf $conf Object conf
  49. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  50. */
  51. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  52. {
  53. if (empty($conf->mailmanspip->enabled)) return 0; // Module not active, we do nothing
  54. require_once DOL_DOCUMENT_ROOT."/mailmanspip/class/mailmanspip.class.php";
  55. require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
  56. if ($action == 'CATEGORY_LINK')
  57. {
  58. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  59. // We add subscription if we change category (new category may means more mailing-list to subscribe)
  60. if (is_object($object->context['linkto']) && method_exists($object->context['linkto'], 'add_to_abo') && $object->context['linkto']->add_to_abo() < 0)
  61. {
  62. $this->error=$object->context['linkto']->error;
  63. $this->errors=$object->context['linkto']->errors;
  64. $return=-1;
  65. }
  66. else
  67. {
  68. $return=1;
  69. }
  70. return $return;
  71. }
  72. elseif ($action == 'CATEGORY_UNLINK')
  73. {
  74. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  75. // We remove subscription if we change category (lessw category may means less mailing-list to subscribe)
  76. if (is_object($object->context['unlinkoff']) && method_exists($object->context['unlinkoff'], 'del_to_abo') && $object->context['unlinkoff']->del_to_abo() < 0)
  77. {
  78. $this->error=$object->context['unlinkoff']->error;
  79. $this->errors=$object->context['unlinkoff']->errors;
  80. $return=-1;
  81. }
  82. else
  83. {
  84. $return=1;
  85. }
  86. return $return;
  87. }
  88. // Members
  89. elseif ($action == 'MEMBER_VALIDATE' || $action == 'MEMBER_MODIFY')
  90. {
  91. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  92. $return=0;
  93. // Add user into some linked tools (mailman, spip, etc...)
  94. if (($object->oldcopy->email != $object->email) || ($object->oldcopy->typeid != $object->typeid)) // TODO Do del/add also if type change
  95. {
  96. if (is_object($object->oldcopy) && ($object->oldcopy->email != $object->email)) // If email has changed we delete mailman subscription for old email
  97. {
  98. if ($object->oldcopy->del_to_abo() < 0)
  99. {
  100. if (! empty($object->oldcopy->error)) $this->error=$object->oldcopy->error;
  101. $this->errors=$object->oldcopy->errors;
  102. $return=-1;
  103. }
  104. else
  105. {
  106. $return=1;
  107. }
  108. }
  109. // We add subscription if new email or new type (new type may means more mailing-list to subscribe)
  110. if ($object->add_to_abo() < 0)
  111. {
  112. if (! empty($object->error)) $this->error=$object->error;
  113. $this->errors=$object->errors;
  114. $return=-1;
  115. }
  116. else
  117. {
  118. $return=1;
  119. }
  120. }
  121. return $return;
  122. }
  123. elseif ($action == 'MEMBER_RESILIATE' || $action == 'MEMBER_DELETE')
  124. {
  125. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  126. $return=0;
  127. // Remove from external tools (mailman, spip, etc...)
  128. if ($object->del_to_abo() < 0)
  129. {
  130. if (! empty($object->error)) $this->error=$object->error;
  131. $this->errors=$object->errors;
  132. $return=-1;
  133. }
  134. else
  135. {
  136. $return=1;
  137. }
  138. return $return;
  139. }
  140. return 0;
  141. }
  142. }