interface_90_modSociete_ContactRoles.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2017 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011-2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2013 Cedric GROSS <c.gross@kreiz-it.fr>
  7. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  8. * Copyright (C) 2015 Bahfir Abbes <bafbes@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/core/triggers/interface_90_modSociete_ContactRoles.class.php
  25. * \ingroup agenda
  26. * \brief Trigger file for company - contactroles
  27. */
  28. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  29. /**
  30. * Class of triggered functions for agenda module
  31. */
  32. class InterfaceContactRoles extends DolibarrTriggers
  33. {
  34. /**
  35. * Constructor
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. public function __construct($db)
  40. {
  41. $this->db = $db;
  42. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  43. $this->family = "agenda";
  44. $this->description = "Triggers of this module auto link contact to company.";
  45. // 'development', 'experimental', 'dolibarr' or version
  46. $this->version = self::VERSION_DOLIBARR;
  47. $this->picto = 'company';
  48. }
  49. /**
  50. * Function called when a Dolibarrr business event is done.
  51. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  52. *
  53. * Following properties may be set before calling trigger. The may be completed by this trigger to be used for writing the event into database:
  54. * $object->socid or $object->fk_soc(id of thirdparty)
  55. * $object->element (element type of object)
  56. *
  57. * @param string $action Event action code
  58. * @param Object $object Object
  59. * @param User $user Object user
  60. * @param Translate $langs Object langs
  61. * @param conf $conf Object conf
  62. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  63. */
  64. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  65. {
  66. if ($action === 'PROPAL_CREATE' || $action === 'ORDER_CREATE' || $action === 'BILL_CREATE'
  67. || $action === 'ORDER_SUPPLIER_CREATE' || $action === 'BILL_SUPPLIER_CREATE' || $action === 'PROPOSAL_SUPPLIER_CREATE'
  68. || $action === 'CONTRACT_CREATE' || $action === 'FICHINTER_CREATE' || $action === 'PROJECT_CREATE' || $action === 'TICKET_CREATE') {
  69. dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
  70. $socid = (property_exists($object, 'socid') ? $object->socid : $object->fk_soc);
  71. if (!empty($socid) && $socid > 0) {
  72. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  73. $contactdefault = new Contact($this->db);
  74. $contactdefault->socid = $socid;
  75. $TContact = array();
  76. if (method_exists($contactdefault, 'getContactRoles')) { // For backward compatibility
  77. $TContact = $contactdefault->getContactRoles($object->element);
  78. }
  79. if (is_array($TContact) && !empty($TContact)) {
  80. $TContactAlreadyLinked = array();
  81. if ($object->id > 0) {
  82. $TContactAlreadyLinked = array_merge($object->liste_contact(-1, 'external'), $object->liste_contact(-1, 'internal'));
  83. }
  84. foreach ($TContact as $i => $infos) {
  85. foreach ($TContactAlreadyLinked as $contactData) {
  86. if ($contactData['id'] == $infos['fk_socpeople'] && $contactData['fk_c_type_contact'] == $infos['type_contact']) {
  87. unset($TContact[$i]);
  88. }
  89. }
  90. }
  91. $nb = 0;
  92. foreach ($TContact as $infos) {
  93. $res = $object->add_contact($infos['fk_socpeople'], $infos['type_contact']);
  94. if ($res > 0) {
  95. $nb++;
  96. }
  97. }
  98. if ($nb > 0) {
  99. setEventMessages($langs->trans('ContactAddedAutomatically', $nb), null, 'mesgs');
  100. }
  101. }
  102. }
  103. }
  104. return 0;
  105. }
  106. }