dolibarr  17.0.4
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')) {
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')) {
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  $sql .= $addFilter;
146  } else {
147  $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";
148  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
149  $sql .= " WHERE s.email <> ''";
150  $sql .= " AND s.entity IN (".getEntity('societe').")";
151  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
152  $sql .= " AND cs.fk_soc = s.rowid";
153  $sql .= " AND c.rowid = cs.fk_categorie";
154  if (GETPOST('filter_thirdparties', 'int') > 0) {
155  $sql .= " AND c.rowid=".((int) GETPOST('filter_thirdparties', 'int'));
156  }
157  $sql .= $addFilter;
158  $sql .= " UNION ";
159  $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";
160  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
161  $sql .= " WHERE s.email <> ''";
162  $sql .= " AND s.entity IN (".getEntity('societe').")";
163  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
164  $sql .= " AND cs.fk_soc = s.rowid";
165  $sql .= " AND c.rowid = cs.fk_categorie";
166  if (GETPOST('filter_thirdparties', 'int') > 0) {
167  $sql .= " AND c.rowid=".((int) GETPOST('filter_thirdparties', 'int'));
168  }
169  $sql .= $addFilter;
170  }
171  $sql .= " ORDER BY email";
172 
173  //print $sql;exit;
174 
175  // Stock recipients emails into targets table
176  $result = $this->db->query($sql);
177  if ($result) {
178  $num = $this->db->num_rows($result);
179  $i = 0;
180  $j = 0;
181 
182  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
183 
184  $old = '';
185  while ($i < $num) {
186  $obj = $this->db->fetch_object($result);
187  if ($old <> $obj->email) {
188  $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
189  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
190  $otherTxt .= ";";
191  }
192  $otherTxt .= $addDescription;
193  $cibles[$j] = array(
194  'email' => $obj->email,
195  'fk_contact' => $obj->fk_contact,
196  'lastname' => $obj->name, // For a thirdparty, we must use name
197  'firstname' => '', // For a thirdparty, lastname is ''
198  'other' => $otherTxt,
199  'source_url' => $this->url($obj->id),
200  'source_id' => $obj->id,
201  'source_type' => 'thirdparty'
202  );
203  $old = $obj->email;
204  $j++;
205  }
206 
207  $i++;
208  }
209  } else {
210  dol_syslog($this->db->error());
211  $this->error = $this->db->error();
212  return -1;
213  }
214 
215  return parent::addTargetsToDatabase($mailing_id, $cibles);
216  }
217 
218 
227  public function getSqlArrayForStats()
228  {
229  // CHANGE THIS: Optionnal
230 
231  //var $statssql=array();
232  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
233  return array();
234  }
235 
236 
245  public function getNbOfRecipients($sql = '')
246  {
247  global $conf;
248 
249  $sql = "SELECT count(distinct(s.email)) as nb";
250  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
251  $sql .= " WHERE s.email <> ''";
252  $sql .= " AND s.entity IN (".getEntity('societe').")";
253  $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).")";
254  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
255  return parent::getNbOfRecipients($sql);
256  }
257 
264  public function formFilter()
265  {
266  global $conf, $langs;
267 
268  $langs->load("companies");
269 
270  // filter
271  $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat">';
272 
273  // Show categories
274  $sql = "SELECT rowid, label, type, visible";
275  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
276  $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
277  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
278  $sql .= " AND entity = ".$conf->entity;
279  $sql .= " ORDER BY label";
280 
281  //print $sql;
282  $resql = $this->db->query($sql);
283  if ($resql) {
284  $num = $this->db->num_rows($resql);
285 
286  if (empty($conf->categorie->enabled)) {
287  $num = 0; // Force empty list if category module is not enabled
288  }
289 
290  if ($num) {
291  $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
292  } else {
293  $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
294  }
295 
296  $i = 0;
297  while ($i < $num) {
298  $obj = $this->db->fetch_object($resql);
299 
300  $type = '';
301  if ($obj->type == 1) {
302  $type = $langs->trans("Supplier");
303  }
304  if ($obj->type == 2) {
305  $type = $langs->trans("Customer");
306  }
307  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
308  if ($type) {
309  $s .= ' ('.$type.')';
310  }
311  $s .= '</option>';
312  $i++;
313  }
314  $s .= ajax_combobox("filter_thirdparties");
315  } else {
316  dol_print_error($this->db);
317  }
318 
319  $s .= '</select> ';
320 
321  // filter_client_thirdparties
322  $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
323  $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
324  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
325  $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
326  }
327  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
328  $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
329  }
330  if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
331  $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
332  }
333  $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
334 
335  $s .= '</select> ';
336  $s .= ajax_combobox("filter_client_thirdparties");
337 
338  // filter_supplier_thirdparties
339  $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
340  $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
341  $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
342  $s .= '<option value="0">'.$langs->trans("No").'</option>';
343  $s .= '</select>';
344  $s .= ajax_combobox("filter_supplier_thirdparties");
345 
346  // filter_status_thirdparties
347  $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
348  $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
349  $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
350  $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
351  $s .= '</select>';
352  $s .= ajax_combobox("filter_status_thirdparties");
353 
354  // filter_lang_thirdparties
355  if (getDolGlobalInt('MAIN_MULTILANGS')) {
356  // Choose language
357  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
358  $formadmin = new FormAdmin($this->db);
359  $s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
360  $s .= $formadmin->select_language($langs->getDefaultLang(1), 'filter_lang_thirdparties', 0, null, 1, 0, 0, '', 0, 0, 0, null, 1);
361  }
362 
363  return $s;
364  }
365 
366 
373  public function url($id)
374  {
375  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
376  }
377 }
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
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.
$conf db
API class for accounts.
Definition: inc.php:41