dolibarr  19.0.0-dev
thirdparties.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 
19 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
20 
21 
26 {
27  public $name = 'ThirdPartiesByCategories';
28  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
29  public $desc = "Third parties (by categories)";
30  public $require_admin = 0;
31 
32  public $require_module = array("societe"); // This module allows to select by categories must be also enabled if category module is not activated
33 
34  public $enabled = 'isModEnabled("societe")';
35 
39  public $picto = 'company';
40 
44  public $db;
45 
46 
52  public function __construct($db)
53  {
54  global $conf, $langs;
55  $langs->load("companies");
56 
57  $this->db = $db;
58  }
59 
60 
61  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
68  public function add_to_target($mailing_id)
69  {
70  // phpcs:enable
71  global $conf, $langs;
72 
73  $cibles = array();
74 
75  $addDescription = "";
76  $addFilter = "";
77  if (GETPOSTISSET("filter_client_thirdparties") && GETPOST("filter_client_thirdparties") <> '-1') {
78  $addFilter .= " AND s.client=".((int) GETPOST("filter_client_thirdparties", 'int'));
79  $addDescription = $langs->trans('ProspectCustomer')."=";
80  if (GETPOST("filter_client_thirdparties") == 0) {
81  $addDescription .= $langs->trans('NorProspectNorCustomer');
82  } elseif (GETPOST("filter_client_thirdparties") == 1) {
83  $addDescription .= $langs->trans('Customer');
84  } elseif (GETPOST("filter_client_thirdparties") == 2) {
85  $addDescription .= $langs->trans('Prospect');
86  } elseif (GETPOST("filter_client_thirdparties") == 3) {
87  $addDescription .= $langs->trans('ProspectCustomer');
88  } else {
89  $addDescription .= "Unknown status ".GETPOST("filter_client_thirdparties");
90  }
91  }
92  if (GETPOSTISSET("filter_supplier_thirdparties") && GETPOST("filter_supplier_thirdparties") <> '-1') {
93  $addFilter .= " AND s.fournisseur = ".((int) GETPOST("filter_supplier_thirdparties", 'int'));
94  $addDescription = $langs->trans('Supplier')."=";
95  if (GETPOST("filter_supplier_thirdparties") == 0) {
96  $addDescription .= $langs->trans('No');
97  } elseif (GETPOST("filter_supplier_thirdparties") == 1) {
98  $addDescription .= $langs->trans('Yes');
99  } else {
100  $addDescription .= "Unknown status ".GETPOST("filter_supplier_thirdparties");
101  }
102  }
103  if (GETPOSTISSET("filter_status")) {
104  if (strlen($addDescription) > 0) {
105  $addDescription .= ";";
106  }
107  $addDescription .= $langs->trans("Status")."=";
108  if (GETPOST("filter_status") == '1') {
109  $addFilter .= " AND s.status=1";
110  $addDescription .= $langs->trans("Enabled");
111  } elseif (GETPOST("filter_status") == '0') {
112  $addFilter .= " AND s.status=0";
113  $addDescription .= $langs->trans("Disabled");
114  }
115  }
116  if (GETPOSTISSET("filter_status")) {
117  if (strlen($addDescription) > 0) {
118  $addDescription .= ";";
119  }
120  $addDescription .= $langs->trans("Status")."=";
121  if (GETPOST("filter_status") == '1') {
122  $addFilter .= " AND s.status=1";
123  $addDescription .= $langs->trans("Enabled");
124  } elseif (GETPOST("filter_status") == '0') {
125  $addFilter .= " AND s.status=0";
126  $addDescription .= $langs->trans("Disabled");
127  }
128  }
129  if (GETPOST('default_lang', 'alpha') && GETPOST('default_lang', 'alpha') != '-1') {
130  $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
131  $addDescription = $langs->trans('DefaultLang')."=";
132  }
133  if (GETPOST('filter_lang_thirdparties', 'alpha') && GETPOST('filter_lang_thirdparties', 'alpha') != '-1') {
134  $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
135  $addDescription = $langs->trans('DefaultLang')."=";
136  }
137 
138  // Select the third parties from category
139  if (!GETPOST('filter_thirdparties') || GETPOST('filter_thirdparties') == '-1') {
140  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
141  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
142  $sql .= " WHERE s.email <> ''";
143  $sql .= " AND s.entity IN (".getEntity('societe').")";
144  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
145  if (empty($this->evenunsubscribe)) {
146  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
147  }
148  $sql .= $addFilter;
149  } else {
150  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
151  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
152  $sql .= " WHERE s.email <> ''";
153  $sql .= " AND s.entity IN (".getEntity('societe').")";
154  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
155  $sql .= " AND cs.fk_soc = s.rowid";
156  $sql .= " AND c.rowid = cs.fk_categorie";
157  if (GETPOST('filter_thirdparties', 'int') > 0) {
158  $sql .= " AND c.rowid=".((int) GETPOST('filter_thirdparties', 'int'));
159  }
160  if (empty($this->evenunsubscribe)) {
161  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
162  }
163  $sql .= $addFilter;
164  $sql .= " UNION ";
165  $sql .= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
166  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
167  $sql .= " WHERE s.email <> ''";
168  $sql .= " AND s.entity IN (".getEntity('societe').")";
169  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
170  $sql .= " AND cs.fk_soc = s.rowid";
171  $sql .= " AND c.rowid = cs.fk_categorie";
172  if (GETPOST('filter_thirdparties', 'int') > 0) {
173  $sql .= " AND c.rowid=".((int) GETPOST('filter_thirdparties', 'int'));
174  }
175  if (empty($this->evenunsubscribe)) {
176  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
177  }
178  $sql .= $addFilter;
179  }
180  $sql .= " ORDER BY email";
181 
182  //print $sql;exit;
183 
184  // Stock recipients emails into targets table
185  $result = $this->db->query($sql);
186  if ($result) {
187  $num = $this->db->num_rows($result);
188  $i = 0;
189  $j = 0;
190 
191  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
192 
193  $old = '';
194  while ($i < $num) {
195  $obj = $this->db->fetch_object($result);
196  if ($old <> $obj->email) {
197  $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
198  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
199  $otherTxt .= ";";
200  }
201  $otherTxt .= $addDescription;
202  $cibles[$j] = array(
203  'email' => $obj->email,
204  'fk_contact' => $obj->fk_contact,
205  'lastname' => $obj->name, // For a thirdparty, we must use name
206  'firstname' => '', // For a thirdparty, lastname is ''
207  'other' => $otherTxt,
208  'source_url' => $this->url($obj->id),
209  'source_id' => $obj->id,
210  'source_type' => 'thirdparty'
211  );
212  $old = $obj->email;
213  $j++;
214  }
215 
216  $i++;
217  }
218  } else {
219  dol_syslog($this->db->error());
220  $this->error = $this->db->error();
221  return -1;
222  }
223 
224  return parent::addTargetsToDatabase($mailing_id, $cibles);
225  }
226 
227 
236  public function getSqlArrayForStats()
237  {
238  // CHANGE THIS: Optionnal
239 
240  //var $statssql=array();
241  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
242  return array();
243  }
244 
245 
254  public function getNbOfRecipients($sql = '')
255  {
256  global $conf;
257 
258  $sql = "SELECT count(distinct(s.email)) as nb";
259  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
260  $sql .= " WHERE s.email <> ''";
261  $sql .= " AND s.entity IN (".getEntity('societe').")";
262  if (empty($this->evenunsubscribe)) {
263  $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).")";
264  }
265 
266  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
267  return parent::getNbOfRecipients($sql);
268  }
269 
276  public function formFilter()
277  {
278  global $conf, $langs;
279 
280  $langs->load("companies");
281 
282  // filter
283  $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat maxwidth200">';
284 
285  // Show categories
286  $sql = "SELECT rowid, label, type, visible";
287  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
288  $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
289  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
290  $sql .= " AND entity = ".$conf->entity;
291  $sql .= " ORDER BY label";
292 
293  //print $sql;
294  $resql = $this->db->query($sql);
295  if ($resql) {
296  $num = $this->db->num_rows($resql);
297 
298  if (!isModEnabled("categorie")) {
299  $num = 0; // Force empty list if category module is not enabled
300  }
301 
302  if ($num) {
303  $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
304  } else {
305  $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
306  }
307 
308  $i = 0;
309  while ($i < $num) {
310  $obj = $this->db->fetch_object($resql);
311 
312  $type = '';
313  if ($obj->type == 1) {
314  $type = $langs->trans("Supplier");
315  }
316  if ($obj->type == 2) {
317  $type = $langs->trans("Customer");
318  }
319  $labeltoshow = $obj->label;
320  if ($type) {
321  $labeltoshow .= ' <span class="opacitymedium">('.$type.')</span>';
322  }
323  $s .= '<option value="'.$obj->rowid.'" data-html="'.dol_escape_htmltag($labeltoshow).'">';
324  $s .= $labeltoshow;
325  $s .= '</option>';
326  $i++;
327  }
328  $s .= ajax_combobox("filter_thirdparties");
329  } else {
330  dol_print_error($this->db);
331  }
332 
333  $s .= '</select> ';
334 
335  // filter_client_thirdparties
336  $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
337  $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
338  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
339  $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
340  }
341  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
342  $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
343  }
344  if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
345  $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
346  }
347  $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
348 
349  $s .= '</select> ';
350  $s .= ajax_combobox("filter_client_thirdparties");
351 
352  // filter_supplier_thirdparties
353  $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
354  $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
355  $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
356  $s .= '<option value="0">'.$langs->trans("No").'</option>';
357  $s .= '</select>';
358  $s .= ajax_combobox("filter_supplier_thirdparties");
359 
360  // filter_status_thirdparties
361  $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
362  $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
363  $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
364  $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
365  $s .= '</select>';
366  $s .= ajax_combobox("filter_status_thirdparties");
367 
368  // filter_lang_thirdparties
369  if (getDolGlobalInt('MAIN_MULTILANGS')) {
370  // Choose language
371  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
372  $formadmin = new FormAdmin($this->db);
373  $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
374  //$s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
375  $s .= $formadmin->select_language(GETPOST('filter_lang_thirdparties', 'aZ09'), 'filter_lang_thirdparties', 0, null, $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, null, 1);
376  }
377 
378  return $s;
379  }
380 
381 
388  public function url($id)
389  {
390  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
391  }
392 }
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
Class to generate html code for admin pages.
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.
url($id)
Can include an URL link on each record provided by selector shown on target page.
__construct($db)
Constructor.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
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)
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
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...