dolibarr  16.0.5
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 = '$conf->societe->enabled';
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  // Select the third parties from category
77  if (!GETPOST('filter')) {
78  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
79  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
80  $sql .= " WHERE s.email <> ''";
81  $sql .= " AND s.entity IN (".getEntity('societe').")";
82  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
83  if (GETPOST('default_lang', 'alpha')) {
84  $sql .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
85  }
86  } else {
87  $addFilter = "";
88  if (GETPOSTISSET("filter_client") && GETPOST("filter_client") <> '-1') {
89  $addFilter .= " AND s.client=".((int) GETPOST("filter_client", 'int'));
90  $addDescription = $langs->trans('ProspectCustomer')."=";
91  if (GETPOST("filter_client") == 0) {
92  $addDescription .= $langs->trans('NorProspectNorCustomer');
93  } elseif (GETPOST("filter_client") == 1) {
94  $addDescription .= $langs->trans('Customer');
95  } elseif (GETPOST("filter_client") == 2) {
96  $addDescription .= $langs->trans('Prospect');
97  } elseif (GETPOST("filter_client") == 3) {
98  $addDescription .= $langs->trans('ProspectCustomer');
99  } else {
100  $addDescription .= "Unknown status ".GETPOST("filter_client");
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  } else {
112  $addFilter .= " AND s.status=0";
113  $addDescription .= $langs->trans("Disabled");
114  }
115  }
116  if (GETPOST('default_lang', 'alpha')) {
117  $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
118  $addDescription = $langs->trans('DefaultLang')."=";
119  }
120  if (GETPOST('filter_lang_thirdparties', 'alpha')) {
121  $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
122  $addDescription = $langs->trans('DefaultLang')."=";
123  }
124 
125  $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";
126  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
127  $sql .= " WHERE s.email <> ''";
128  $sql .= " AND s.entity IN (".getEntity('societe').")";
129  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
130  $sql .= " AND cs.fk_soc = s.rowid";
131  $sql .= " AND c.rowid = cs.fk_categorie";
132  if (GETPOST('filter', 'int') > 0) {
133  $sql .= " AND c.rowid=".((int) GETPOST('filter', 'int'));
134  }
135  $sql .= $addFilter;
136  $sql .= " UNION ";
137  $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";
138  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
139  $sql .= " WHERE s.email <> ''";
140  $sql .= " AND s.entity IN (".getEntity('societe').")";
141  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
142  $sql .= " AND cs.fk_soc = s.rowid";
143  $sql .= " AND c.rowid = cs.fk_categorie";
144  if (GETPOST('filter', 'int') > 0) {
145  $sql .= " AND c.rowid=".((int) GETPOST('filter', 'int'));
146  }
147  $sql .= $addFilter;
148  }
149  $sql .= " ORDER BY email";
150 
151  // Stock recipients emails into targets table
152  $result = $this->db->query($sql);
153  if ($result) {
154  $num = $this->db->num_rows($result);
155  $i = 0;
156  $j = 0;
157 
158  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
159 
160  $old = '';
161  while ($i < $num) {
162  $obj = $this->db->fetch_object($result);
163  if ($old <> $obj->email) {
164  $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
165  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
166  $otherTxt .= ";";
167  }
168  $otherTxt .= $addDescription;
169  $cibles[$j] = array(
170  'email' => $obj->email,
171  'fk_contact' => $obj->fk_contact,
172  'lastname' => $obj->name, // For a thirdparty, we must use name
173  'firstname' => '', // For a thirdparty, lastname is ''
174  'other' => $otherTxt,
175  'source_url' => $this->url($obj->id),
176  'source_id' => $obj->id,
177  'source_type' => 'thirdparty'
178  );
179  $old = $obj->email;
180  $j++;
181  }
182 
183  $i++;
184  }
185  } else {
186  dol_syslog($this->db->error());
187  $this->error = $this->db->error();
188  return -1;
189  }
190 
191  return parent::addTargetsToDatabase($mailing_id, $cibles);
192  }
193 
194 
203  public function getSqlArrayForStats()
204  {
205  // CHANGE THIS: Optionnal
206 
207  //var $statssql=array();
208  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
209  return array();
210  }
211 
212 
221  public function getNbOfRecipients($sql = '')
222  {
223  global $conf;
224 
225  $sql = "SELECT count(distinct(s.email)) as nb";
226  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
227  $sql .= " WHERE s.email <> ''";
228  $sql .= " AND s.entity IN (".getEntity('societe').")";
229 
230  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
231  return parent::getNbOfRecipients($sql);
232  }
233 
240  public function formFilter()
241  {
242  global $conf, $langs;
243 
244  $langs->load("companies");
245 
246  $s = '<select id="filter_thirdparties" name="filter" class="flat">';
247 
248  // Show categories
249  $sql = "SELECT rowid, label, type, visible";
250  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
251  $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
252  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
253  $sql .= " AND entity = ".$conf->entity;
254  $sql .= " ORDER BY label";
255 
256  //print $sql;
257  $resql = $this->db->query($sql);
258  if ($resql) {
259  $num = $this->db->num_rows($resql);
260 
261  if (empty($conf->categorie->enabled)) {
262  $num = 0; // Force empty list if category module is not enabled
263  }
264 
265  if ($num) {
266  $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
267  } else {
268  $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
269  }
270 
271  $i = 0;
272  while ($i < $num) {
273  $obj = $this->db->fetch_object($resql);
274 
275  $type = '';
276  if ($obj->type == 1) {
277  $type = $langs->trans("Supplier");
278  }
279  if ($obj->type == 2) {
280  $type = $langs->trans("Customer");
281  }
282  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
283  if ($type) {
284  $s .= ' ('.$type.')';
285  }
286  $s .= '</option>';
287  $i++;
288  }
289  $s .= ajax_combobox("filter_thirdparties");
290  } else {
291  dol_print_error($this->db);
292  }
293 
294  $s .= '</select> ';
295 
296  $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat">';
297  $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
298  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
299  $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
300  }
301  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
302  $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
303  }
304  if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
305  $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
306  }
307  $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
308 
309  $s .= '</select> ';
310  $s .= ajax_combobox("filter_client_thirdparties");
311 
312  $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
313  $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
314  $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
315  $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
316  $s .= '</select>';
317  $s .= ajax_combobox("filter_status_thirdparties");
318 
319  // Choose language if multilangue active
320  if (@$conf->global->MAIN_MULTILANGS==1) {
321  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
322  $formadmin = new FormAdmin($this->db);
323  $s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
324  $s .= $formadmin->select_language($langs->getDefaultLang(1), 'filter_lang_thirdparties', 0, null, 1, 0, 0, '', 0, 0, 0, null, 1);
325  }
326  return $s;
327  }
328 
329 
336  public function url($id)
337  {
338  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
339  }
340 }
mailing_thirdparties\formFilter
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
Definition: thirdparties.modules.php:240
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_trunc
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.
Definition: functions.lib.php:3805
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
mailing_thirdparties\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: thirdparties.modules.php:203
FormAdmin
Class to generate html code for admin pages.
Definition: html.formadmin.class.php:30
mailing_thirdparties\__construct
__construct($db)
Constructor.
Definition: thirdparties.modules.php:52
mailing_thirdparties\getNbOfRecipients
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Definition: thirdparties.modules.php:221
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
mailing_thirdparties\add_to_target
add_to_target($mailing_id)
This is the main function that returns the array of emails.
Definition: thirdparties.modules.php:68
mailing_thirdparties\url
url($id)
Can include an URL link on each record provided by selector shown on target page.
Definition: thirdparties.modules.php:336
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
mailing_thirdparties
Class to manage a list of personalised recipients for mailing feature.
Definition: thirdparties.modules.php:25