dolibarr  19.0.0-dev
partnership.modules.php
Go to the documentation of this file.
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 
20 // Load Dolibarr Environment
21 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
22 
23 
28 {
29  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
30  public $name = 'PartnershipThirdpartiesOrMembers';
31  public $desc = "Thirdparties or members included into a partnership program";
32 
33  public $require_admin = 0;
34 
35  public $require_module = array('partnership'); // This module allows to select by categories must be also enabled if category module is not activated
36 
40  public $picto = 'partnership';
41 
45  public $db;
46 
47  public $enabled = 'isModEnabled("partnership")';
48 
49 
55  public function __construct($db)
56  {
57  global $conf, $langs;
58  $langs->load('companies');
59 
60  $this->db = $db;
61  }
62 
63 
64  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
71  public function add_to_target($mailing_id)
72  {
73  // phpcs:enable
74  global $conf, $langs;
75 
76  $cibles = array();
77  $addDescription = '';
78 
79  $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";
80  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
81  $sql .= " WHERE s.email <> ''";
82  $sql .= " AND s.entity IN (".getEntity('societe').")";
83  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
84  $sql .= " AND p.fk_soc = s.rowid";
85  $sql .= " AND pt.rowid = p.fk_type";
86  if (GETPOST('filter', 'int') > 0) {
87  $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
88  }
89  if (empty($this->evenunsubscribe)) {
90  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
91  }
92 
93  $sql .= " UNION ";
94 
95  $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";
96  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
97  $sql .= " WHERE s.email <> ''";
98  $sql .= " AND s.entity IN (".getEntity('member').")";
99  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
100  $sql .= " AND p.fk_member = s.rowid";
101  $sql .= " AND pt.rowid = p.fk_type";
102  if (GETPOST('filter', 'int') > 0) {
103  $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
104  }
105  if (empty($this->evenunsubscribe)) {
106  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
107  }
108 
109  $sql .= " ORDER BY email";
110 
111  // Stock recipients emails into targets table
112  $result = $this->db->query($sql);
113  if ($result) {
114  $num = $this->db->num_rows($result);
115  $i = 0;
116  $j = 0;
117 
118  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
119 
120  $old = '';
121  while ($i < $num) {
122  $obj = $this->db->fetch_object($result);
123  if ($old <> $obj->email) {
124  $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
125  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
126  $otherTxt .= ";";
127  }
128  $otherTxt .= $addDescription;
129  $cibles[$j] = array(
130  'email' => $obj->email,
131  'fk_contact' => $obj->fk_contact,
132  'lastname' => $obj->name, // For a thirdparty, we must use name
133  'firstname' => '', // For a thirdparty, lastname is ''
134  'other' => $otherTxt,
135  'source_url' => $this->url($obj->id, $obj->source),
136  'source_id' => $obj->id,
137  'source_type' => $obj->source
138  );
139  $old = $obj->email;
140  $j++;
141  }
142 
143  $i++;
144  }
145  } else {
146  dol_syslog($this->db->error());
147  $this->error = $this->db->error();
148  return -1;
149  }
150 
151  return parent::addTargetsToDatabase($mailing_id, $cibles);
152  }
153 
154 
163  public function getSqlArrayForStats()
164  {
165  // CHANGE THIS: Optionnal
166 
167  //var $statssql=array();
168  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
169  return array();
170  }
171 
172 
181  public function getNbOfRecipients($sql = '')
182  {
183  global $conf;
184 
185  $sql = "SELECT count(distinct(s.email)) as nb";
186  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
187  $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
188  $sql .= " AND s.entity IN (".getEntity('societe').")";
189  if (empty($this->evenunsubscribe)) {
190  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
191  }
192 
193  $sql .= " UNION ";
194 
195  $sql .= "SELECT count(distinct(s.email)) as nb";
196  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
197  $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
198  $sql .= " AND s.entity IN (".getEntity('member').")";
199  if (empty($this->evenunsubscribe)) {
200  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
201  }
202 
203  //print $sql;
204 
205  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
206  return parent::getNbOfRecipients($sql);
207  }
208 
215  public function formFilter()
216  {
217  global $conf, $langs;
218 
219  $langs->load("companies");
220 
221  $s = '<select id="filter_partnership" name="filter" class="flat">';
222 
223  // Show categories
224  $sql = "SELECT rowid, label, code, active";
225  $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
226  $sql .= " WHERE active = 1";
227  $sql .= " AND entity = ".$conf->entity;
228  $sql .= " ORDER BY label";
229 
230  //print $sql;
231  $resql = $this->db->query($sql);
232  if ($resql) {
233  $num = $this->db->num_rows($resql);
234 
235  if (empty($conf->partnership->enabled)) {
236  $num = 0; // Force empty list if category module is not enabled
237  }
238 
239  if ($num) {
240  $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
241  }
242 
243  $i = 0;
244  while ($i < $num) {
245  $obj = $this->db->fetch_object($resql);
246 
247  $s .= '<option value="'.$obj->rowid.'">'.dol_escape_htmltag($obj->label);
248  $s .= '</option>';
249  $i++;
250  }
251  $s .= ajax_combobox("filter_partnership");
252  } else {
253  dol_print_error($this->db);
254  }
255 
256  $s .= '</select> ';
257 
258  return $s;
259  }
260 
261 
269  public function url($id, $sourcetype = 'thirdparty')
270  {
271  if ($sourcetype == 'thirdparty') {
272  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
273  }
274  if ($sourcetype == 'member') {
275  return '<a href="'.DOL_URL_ROOT.'/adherent/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
276  }
277 
278  return '';
279  }
280 }
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:449
Parent class of emailing target selectors modules.
Class to manage a list of personalised recipients for mailing feature.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
url($id, $sourcetype='thirdparty')
Can include an URL link on each record provided by selector shown on target page.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...