eventorganization.modules.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /* Copyright (C) 2018-2018 Andre Schild <a.schild@aarboard.ch>
  3. * Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This file is an example to follow to add your own email selector inside
  7. * the Dolibarr email tool.
  8. * Follow instructions given in README file to know what to change to build
  9. * your own emailing list selector.
  10. * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
  11. */
  12. /**
  13. * \file htdocs/core/modules/mailings/eventorganization.modules.php
  14. * \ingroup mailing
  15. * \brief Example file to provide a list of recipients for mailing module
  16. */
  17. // Load Dolibarr Environment
  18. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  19. /**
  20. * Class to manage a list of personalised recipients for mailing feature
  21. */
  22. class mailing_eventorganization extends MailingTargets
  23. {
  24. // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
  25. public $name = 'AttendeesOfOrganizedEvent';
  26. public $desc = "Attendees of an organized event";
  27. public $require_admin = 0;
  28. public $require_module = array(); // This module allows to select by categories must be also enabled if category module is not activated
  29. /**
  30. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  31. */
  32. public $picto = 'conferenceorbooth';
  33. /**
  34. * @var DoliDB Database handler.
  35. */
  36. public $db;
  37. public $enabled = 'isModEnabled("eventorganization")';
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. public function __construct($db)
  44. {
  45. global $conf, $langs;
  46. $langs->load('companies');
  47. $this->db = $db;
  48. }
  49. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  50. /**
  51. * This is the main function that returns the array of emails
  52. *
  53. * @param int $mailing_id Id of mailing. No need to use it.
  54. * @return int <0 if error, number of emails added if ok
  55. */
  56. public function add_to_target($mailing_id)
  57. {
  58. // phpcs:enable
  59. global $conf, $langs;
  60. $cibles = array();
  61. $addDescription = '';
  62. $sql = "SELECT p.ref, p.entity, e.rowid as id, e.fk_project, e.email as email, e.email_company as company_name, e.firstname as firstname, e.lastname as lastname,";
  63. $sql .= " 'eventorganizationattendee' as source";
  64. $sql .= " FROM ".MAIN_DB_PREFIX."eventorganization_conferenceorboothattendee as e,";
  65. $sql .= " ".MAIN_DB_PREFIX."projet as p";
  66. $sql .= " WHERE e.email <> ''";
  67. $sql .= " AND e.fk_project = p.rowid";
  68. $sql .= " AND p.entity IN (".getEntity('project').")";
  69. $sql .= " AND e.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  70. $sql .= " AND e.fk_project = ".((int) GETPOST('filter_eventorganization', 'int'));
  71. $sql .= " ORDER BY e.email";
  72. // Stock recipients emails into targets table
  73. $result = $this->db->query($sql);
  74. if ($result) {
  75. $num = $this->db->num_rows($result);
  76. $i = 0;
  77. $j = 0;
  78. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  79. $old = '';
  80. while ($i < $num) {
  81. $obj = $this->db->fetch_object($result);
  82. if ($old <> $obj->email) {
  83. $otherTxt = ($obj->ref ? $langs->transnoentities("Project").'='.$obj->ref : '');
  84. if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
  85. $otherTxt .= ";";
  86. }
  87. $otherTxt .= $addDescription;
  88. $cibles[$j] = array(
  89. 'email' => $obj->email,
  90. 'fk_project' => $obj->fk_project,
  91. 'lastname' => $obj->lastname,
  92. 'firstname' => $obj->firstname,
  93. 'other' => $otherTxt,
  94. 'source_url' => $this->url($obj->id, $obj->source),
  95. 'source_id' => $obj->id,
  96. 'source_type' => $obj->source
  97. );
  98. $old = $obj->email;
  99. $j++;
  100. }
  101. $i++;
  102. }
  103. } else {
  104. dol_syslog($this->db->error());
  105. $this->error = $this->db->error();
  106. return -1;
  107. }
  108. return parent::addTargetsToDatabase($mailing_id, $cibles);
  109. }
  110. /**
  111. * On the main mailing area, there is a box with statistics.
  112. * If you want to add a line in this report you must provide an
  113. * array of SQL request that returns two field:
  114. * One called "label", One called "nb".
  115. *
  116. * @return array Array with SQL requests
  117. */
  118. public function getSqlArrayForStats()
  119. {
  120. // CHANGE THIS: Optionnal
  121. //var $statssql=array();
  122. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  123. return array();
  124. }
  125. /**
  126. * Return here number of distinct emails returned by your selector.
  127. * For example if this selector is used to extract 500 different
  128. * emails from a text file, this function must return 500.
  129. *
  130. * @param string $sql Requete sql de comptage
  131. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  132. */
  133. public function getNbOfRecipients($sql = '')
  134. {
  135. global $conf;
  136. $sql = "SELECT COUNT(DISTINCT(e.email)) as nb";
  137. $sql .= " FROM ".MAIN_DB_PREFIX."eventorganization_conferenceorboothattendee as e, ";
  138. $sql .= " ".MAIN_DB_PREFIX."projet as p";
  139. $sql .= " WHERE e.email <> ''";
  140. $sql .= " AND e.fk_project = p.rowid";
  141. $sql .= " AND p.entity IN (".getEntity('project').")";
  142. //print $sql;
  143. // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
  144. return parent::getNbOfRecipients($sql);
  145. }
  146. /**
  147. * This is to add a form filter to provide variant of selector
  148. * If used, the HTML select must be called "filter"
  149. *
  150. * @return string A html select zone
  151. */
  152. public function formFilter()
  153. {
  154. global $conf, $langs;
  155. $langs->load("companies");
  156. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  157. $formproject = new FormProjets($this->db);
  158. $s = $formproject->select_projects(-1, 0, "filter_eventorganization", 0, 0, 1, 1, 0, 0, 0, '', 1, 0, '', '', 'usage_organize_event=1');
  159. return $s;
  160. }
  161. /**
  162. * Can include an URL link on each record provided by selector shown on target page.
  163. *
  164. * @param int $id ID
  165. * @param string $sourcetype Source type
  166. * @return string Url link
  167. */
  168. public function url($id, $sourcetype = 'thirdparty')
  169. {
  170. if ($sourcetype == 'project') {
  171. return '<a href="'.DOL_URL_ROOT.'/eventorganization/conferenceorboothattendee_card.php?id='.((int) $id).'">'.img_object('', "eventorganization").'</a>';
  172. }
  173. return '';
  174. }
  175. }