dolibarr  20.0.0-alpha
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 
42  public $enabled = 'isModEnabled("partnership")';
43 
44 
50  public function __construct($db)
51  {
52  global $conf, $langs;
53  $langs->load('companies');
54 
55  $this->db = $db;
56  }
57 
58 
59  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
66  public function add_to_target($mailing_id)
67  {
68  // phpcs:enable
69  global $conf, $langs;
70 
71  $cibles = array();
72  $addDescription = '';
73 
74  $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";
75  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
76  $sql .= " WHERE s.email <> ''";
77  $sql .= " AND s.entity IN (".getEntity('societe').")";
78  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
79  $sql .= " AND p.fk_soc = s.rowid";
80  $sql .= " AND pt.rowid = p.fk_type";
81  if (GETPOSTINT('filter') > 0) {
82  $sql .= " AND pt.rowid=".(GETPOSTINT('filter'));
83  }
84  if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
85  $sql .= " AND p.status = ".GETPOSTINT('filter_status_partnership');
86  }
87  if (empty($this->evenunsubscribe)) {
88  $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).")";
89  }
90 
91  $sql .= " UNION ";
92 
93  $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";
94  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
95  $sql .= " WHERE s.email <> ''";
96  $sql .= " AND s.entity IN (".getEntity('member').")";
97  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
98  $sql .= " AND p.fk_member = s.rowid";
99  $sql .= " AND pt.rowid = p.fk_type";
100  if (GETPOSTINT('filter') > 0) {
101  $sql .= " AND pt.rowid=".(GETPOSTINT('filter'));
102  }
103  if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
104  $sql .= " AND p.status = ".GETPOSTINT('filter_status_partnership');
105  }
106  if (empty($this->evenunsubscribe)) {
107  $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).")";
108  }
109 
110  $sql .= " ORDER BY email";
111 
112  // Stock recipients emails into targets table
113  $result = $this->db->query($sql);
114  if ($result) {
115  $num = $this->db->num_rows($result);
116  $i = 0;
117  $j = 0;
118 
119  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
120 
121  $old = '';
122  while ($i < $num) {
123  $obj = $this->db->fetch_object($result);
124  if ($old != $obj->email) {
125  $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
126  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
127  $otherTxt .= ";";
128  }
129  $otherTxt .= $addDescription;
130  $cibles[$j] = array(
131  'email' => $obj->email,
132  'fk_contact' => $obj->fk_contact,
133  'lastname' => $obj->name, // For a thirdparty, we must use name
134  'firstname' => '', // For a thirdparty, lastname is ''
135  'other' => $otherTxt,
136  'source_url' => $this->url($obj->id, $obj->source),
137  'source_id' => $obj->id,
138  'source_type' => $obj->source
139  );
140  $old = $obj->email;
141  $j++;
142  }
143 
144  $i++;
145  }
146  } else {
147  dol_syslog($this->db->error());
148  $this->error = $this->db->error();
149  return -1;
150  }
151 
152  return parent::addTargetsToDatabase($mailing_id, $cibles);
153  }
154 
155 
164  public function getSqlArrayForStats()
165  {
166  // CHANGE THIS: Optional
167 
168  //var $statssql=array();
169  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
170  return array();
171  }
172 
173 
182  public function getNbOfRecipients($sql = '')
183  {
184  global $conf;
185 
186  $sql = "SELECT count(distinct(s.email)) as nb";
187  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
188  $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
189  $sql .= " AND s.entity IN (".getEntity('societe').")";
190  if (empty($this->evenunsubscribe)) {
191  $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).")";
192  }
193 
194  $sql .= " UNION ";
195 
196  $sql .= "SELECT count(distinct(s.email)) as nb";
197  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
198  $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
199  $sql .= " AND s.entity IN (".getEntity('member').")";
200  if (empty($this->evenunsubscribe)) {
201  $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).")";
202  }
203 
204  //print $sql;
205 
206  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
207  return parent::getNbOfRecipients($sql);
208  }
209 
216  public function formFilter()
217  {
218  global $conf, $langs, $form;
219 
220  $langs->load("companies");
221 
222  $s = '<select id="filter_partnership" name="filter" class="flat">';
223 
224  // Show type of partnership
225  $sql = "SELECT rowid, label, code, active";
226  $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
227  $sql .= " WHERE active = 1";
228  $sql .= " AND entity = ".$conf->entity;
229  $sql .= " ORDER BY label";
230 
231  //print $sql;
232  $resql = $this->db->query($sql);
233  if ($resql) {
234  $num = $this->db->num_rows($resql);
235 
236  if (empty($conf->partnership->enabled)) {
237  $num = 0; // Force empty list if category module is not enabled
238  }
239 
240  if ($num) {
241  $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
242  }
243 
244  $i = 0;
245  while ($i < $num) {
246  $obj = $this->db->fetch_object($resql);
247 
248  $s .= '<option value="'.$obj->rowid.'">'.dol_escape_htmltag($obj->label);
249  $s .= '</option>';
250  $i++;
251  }
252  $s .= ajax_combobox("filter_partnership");
253  } else {
254  dol_print_error($this->db);
255  }
256 
257  $s .= '</select> ';
258 
259  // filter_status_thirdparties
260  include_once DOL_DOCUMENT_ROOT.'/partnership/class/partnership.class.php';
261  $tmppartnership = new Partnership($this->db);
262  $dummy = $tmppartnership->getLibStatut(0); // We call this only to have $tmppartnership->labelStatus loaded
263 
264  $s .= $form->selectarray('filter_status_partnership', $tmppartnership->labelStatus, GETPOST('filter_status_partnership'), $langs->trans("Status"));
265 
266  return $s;
267  }
268 
269 
277  public function url($id, $sourcetype = 'thirdparty')
278  {
279  if ($sourcetype == 'thirdparty') {
280  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
281  }
282  if ($sourcetype == 'member') {
283  return '<a href="'.DOL_URL_ROOT.'/adherent/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
284  }
285 
286  return '';
287  }
288 }
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:463
Parent class of emailing target selectors modules.
Class for Partnership.
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.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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...