partnership.modules.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/partnership.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_partnership 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 = 'PartnershipThirdpartiesOrMembers';
  26. public $desc = "Thirdparties or members included into a partnership program";
  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 = 'partnership';
  33. /**
  34. * @var DoliDB Database handler.
  35. */
  36. public $db;
  37. public $enabled = 'isModEnabled("partnership")';
  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 s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, pt.label as label, 'thirdparty' as source";
  63. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
  64. $sql .= " WHERE s.email <> ''";
  65. $sql .= " AND s.entity IN (".getEntity('societe').")";
  66. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  67. $sql .= " AND p.fk_soc = s.rowid";
  68. $sql .= " AND pt.rowid = p.fk_type";
  69. if (GETPOST('filter', 'int') > 0) {
  70. $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
  71. }
  72. $sql .= " UNION ";
  73. $sql .= "SELECT s.rowid as id, s.email as email, s.lastname as name, null as fk_contact, s.firstname as firstname, pt.label as label, 'member' as source";
  74. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
  75. $sql .= " WHERE s.email <> ''";
  76. $sql .= " AND s.entity IN (".getEntity('member').")";
  77. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  78. $sql .= " AND p.fk_member = s.rowid";
  79. $sql .= " AND pt.rowid = p.fk_type";
  80. if (GETPOST('filter', 'int') > 0) {
  81. $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
  82. }
  83. $sql .= " ORDER BY email";
  84. // Stock recipients emails into targets table
  85. $result = $this->db->query($sql);
  86. if ($result) {
  87. $num = $this->db->num_rows($result);
  88. $i = 0;
  89. $j = 0;
  90. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  91. $old = '';
  92. while ($i < $num) {
  93. $obj = $this->db->fetch_object($result);
  94. if ($old <> $obj->email) {
  95. $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
  96. if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
  97. $otherTxt .= ";";
  98. }
  99. $otherTxt .= $addDescription;
  100. $cibles[$j] = array(
  101. 'email' => $obj->email,
  102. 'fk_contact' => $obj->fk_contact,
  103. 'lastname' => $obj->name, // For a thirdparty, we must use name
  104. 'firstname' => '', // For a thirdparty, lastname is ''
  105. 'other' => $otherTxt,
  106. 'source_url' => $this->url($obj->id, $obj->source),
  107. 'source_id' => $obj->id,
  108. 'source_type' => $obj->source
  109. );
  110. $old = $obj->email;
  111. $j++;
  112. }
  113. $i++;
  114. }
  115. } else {
  116. dol_syslog($this->db->error());
  117. $this->error = $this->db->error();
  118. return -1;
  119. }
  120. return parent::addTargetsToDatabase($mailing_id, $cibles);
  121. }
  122. /**
  123. * On the main mailing area, there is a box with statistics.
  124. * If you want to add a line in this report you must provide an
  125. * array of SQL request that returns two field:
  126. * One called "label", One called "nb".
  127. *
  128. * @return array Array with SQL requests
  129. */
  130. public function getSqlArrayForStats()
  131. {
  132. // CHANGE THIS: Optionnal
  133. //var $statssql=array();
  134. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  135. return array();
  136. }
  137. /**
  138. * Return here number of distinct emails returned by your selector.
  139. * For example if this selector is used to extract 500 different
  140. * emails from a text file, this function must return 500.
  141. *
  142. * @param string $sql Requete sql de comptage
  143. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  144. */
  145. public function getNbOfRecipients($sql = '')
  146. {
  147. global $conf;
  148. $sql = "SELECT count(distinct(s.email)) as nb";
  149. $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
  150. $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
  151. $sql .= " AND s.entity IN (".getEntity('societe').")";
  152. $sql .= " UNION ";
  153. $sql .= "SELECT count(distinct(s.email)) as nb";
  154. $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
  155. $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
  156. $sql .= " AND s.entity IN (".getEntity('member').")";
  157. //print $sql;
  158. // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
  159. return parent::getNbOfRecipients($sql);
  160. }
  161. /**
  162. * This is to add a form filter to provide variant of selector
  163. * If used, the HTML select must be called "filter"
  164. *
  165. * @return string A html select zone
  166. */
  167. public function formFilter()
  168. {
  169. global $conf, $langs;
  170. $langs->load("companies");
  171. $s = '<select id="filter_partnership" name="filter" class="flat">';
  172. // Show categories
  173. $sql = "SELECT rowid, label, code, active";
  174. $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
  175. $sql .= " WHERE active = 1";
  176. $sql .= " AND entity = ".$conf->entity;
  177. $sql .= " ORDER BY label";
  178. //print $sql;
  179. $resql = $this->db->query($sql);
  180. if ($resql) {
  181. $num = $this->db->num_rows($resql);
  182. if (empty($conf->partnership->enabled)) {
  183. $num = 0; // Force empty list if category module is not enabled
  184. }
  185. if ($num) {
  186. $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
  187. }
  188. $i = 0;
  189. while ($i < $num) {
  190. $obj = $this->db->fetch_object($resql);
  191. $s .= '<option value="'.$obj->rowid.'">'.dol_escape_htmltag($obj->label);
  192. $s .= '</option>';
  193. $i++;
  194. }
  195. $s .= ajax_combobox("filter_partnership");
  196. } else {
  197. dol_print_error($this->db);
  198. }
  199. $s .= '</select> ';
  200. return $s;
  201. }
  202. /**
  203. * Can include an URL link on each record provided by selector shown on target page.
  204. *
  205. * @param int $id ID
  206. * @param string $sourcetype Source type
  207. * @return string Url link
  208. */
  209. public function url($id, $sourcetype = 'thirdparty')
  210. {
  211. if ($sourcetype == 'thirdparty') {
  212. return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
  213. }
  214. if ($sourcetype == 'member') {
  215. return '<a href="'.DOL_URL_ROOT.'/adherent/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
  216. }
  217. return '';
  218. }
  219. }