partnership.modules.php 8.1 KB

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