interface_50_modMailmanspip_Mailmanspipsynchro.class.php 5.6 KB

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