dolibarr  9.0.0
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  var $name='ThirdPartiesByCategories';
28  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
29  var $desc="Third parties (by categories)";
30  var $require_admin=0;
31 
32  var $require_module=array("societe"); // This module allows to select by categories must be also enabled if category module is not activated
33  var $picto='company';
34 
38  public $db;
39 
40 
46  function __construct($db)
47  {
48  global $conf, $langs;
49  $langs->load("companies");
50 
51  $this->db=$db;
52  }
53 
54 
55  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
63  function add_to_target($mailing_id, $filtersarray=array())
64  {
65  // phpcs:enable
66  global $conf, $langs;
67 
68  $cibles = array();
69 
70  // Select the third parties from category
71  if (empty($_POST['filter']))
72  {
73  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
74  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
75  $sql.= " WHERE s.email <> ''";
76  $sql.= " AND s.entity IN (".getEntity('societe').")";
77  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
78  }
79  else
80  {
81  $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";
82  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
83  $sql.= " WHERE s.email <> ''";
84  $sql.= " AND s.entity IN (".getEntity('societe').")";
85  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
86  $sql.= " AND cs.fk_soc = s.rowid";
87  $sql.= " AND c.rowid = cs.fk_categorie";
88  $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'";
89  $sql.= " UNION ";
90  $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";
91  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
92  $sql.= " WHERE s.email <> ''";
93  $sql.= " AND s.entity IN (".getEntity('societe').")";
94  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
95  $sql.= " AND cs.fk_soc = s.rowid";
96  $sql.= " AND c.rowid = cs.fk_categorie";
97  $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'";
98  }
99 
100  $addDescription= "";
101  if (isset($_POST["filter_client"]) && $_POST["filter_client"] <> '-1')
102  {
103  $sql.= " AND s.client=" . $_POST["filter_client"];
104  $addDescription= $langs->trans('ProspectCustomer')."=";
105  if ($_POST["filter_client"] == 0)
106  {
107  $addDescription.= $langs->trans('NorProspectNorCustomer');
108  }
109  elseif ($_POST["filter_client"] == 1)
110  {
111  $addDescription.= $langs->trans('Customer');
112  }
113  elseif ($_POST["filter_client"] == 2)
114  {
115  $addDescription.= $langs->trans('Prospect');
116  }
117  elseif ($_POST["filter_client"] == 3)
118  {
119  $addDescription.= $langs->trans('ProspectCustomer');
120  }
121  else
122  {
123  $addDescription.= "Unknown status ".$_POST["filter_client"];
124  }
125  }
126  if (isset($_POST["filter_status"]))
127  {
128  if (strlen($addDescription) > 0)
129  {
130  $addDescription.= ";";
131  }
132  $addDescription.= $langs->trans("Status")."=";
133  if ($_POST["filter_status"] == '1')
134  {
135  $sql.= " AND s.status=1";
136  $addDescription.= $langs->trans("Enabled");
137  }
138  else
139  {
140  $sql.= " AND s.status=0";
141  $addDescription.= $langs->trans("Disabled");
142  }
143  }
144  $sql.= " ORDER BY email";
145 
146  // Stock recipients emails into targets table
147  $result=$this->db->query($sql);
148  if ($result)
149  {
150  $num = $this->db->num_rows($result);
151  $i = 0;
152  $j = 0;
153 
154  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
155 
156  $old = '';
157  while ($i < $num)
158  {
159  $obj = $this->db->fetch_object($result);
160  if ($old <> $obj->email)
161  {
162  $otherTxt= ($obj->label?$langs->transnoentities("Category").'='.$obj->label:'');
163  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0)
164  {
165  $otherTxt.= ";";
166  }
167  $otherTxt.= $addDescription;
168  $cibles[$j] = array(
169  'email' => $obj->email,
170  'fk_contact' => $obj->fk_contact,
171  'lastname' => $obj->name, // For a thirdparty, we must use name
172  'firstname' => '', // For a thirdparty, lastname is ''
173  'other' => $otherTxt,
174  'source_url' => $this->url($obj->id),
175  'source_id' => $obj->id,
176  'source_type' => 'thirdparty'
177  );
178  $old = $obj->email;
179  $j++;
180  }
181 
182  $i++;
183  }
184  }
185  else
186  {
187  dol_syslog($this->db->error());
188  $this->error=$this->db->error();
189  return -1;
190  }
191 
192  return parent::add_to_target($mailing_id, $cibles);
193  }
194 
195 
205  {
206  // CHANGE THIS: Optionnal
207 
208  //var $statssql=array();
209  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
210  return array();
211  }
212 
213 
222  function getNbOfRecipients($sql='')
223  {
224  global $conf;
225 
226  $sql = "SELECT count(distinct(s.email)) as nb";
227  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
228  $sql.= " WHERE s.email != ''";
229  $sql.= " AND s.entity IN (".getEntity('societe').")";
230 
231  // La requete doit retourner un champ "nb" pour etre comprise
232  // par parent::getNbOfRecipients
233  return parent::getNbOfRecipients($sql);
234  }
235 
242  function formFilter()
243  {
244  global $conf, $langs;
245 
246  $langs->load("companies");
247 
248  $s=$langs->trans("Categories").': ';
249  $s.='<select name="filter" class="flat">';
250 
251  // Show categories
252  $sql = "SELECT rowid, label, type, visible";
253  $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
254  $sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
255  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
256  $sql.= " AND entity = ".$conf->entity;
257  $sql.= " ORDER BY label";
258 
259  //print $sql;
260  $resql = $this->db->query($sql);
261  if ($resql)
262  {
263  $num = $this->db->num_rows($resql);
264 
265  if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
266 
267  if ($num) $s.='<option value="0">&nbsp;</option>';
268  else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
269 
270  $i = 0;
271  while ($i < $num)
272  {
273  $obj = $this->db->fetch_object($resql);
274 
275  $type='';
276  if ($obj->type == 1) $type=$langs->trans("Supplier");
277  if ($obj->type == 2) $type=$langs->trans("Customer");
278  $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
279  if ($type) $s.=' ('.$type.')';
280  $s.='</option>';
281  $i++;
282  }
283  }
284  else
285  {
286  dol_print_error($this->db);
287  }
288 
289  $s.='</select> ';
290  $s.= $langs->trans('ProspectCustomer');
291  $s.=': <select name="filter_client" class="flat">';
292  $s.= '<option value="-1">&nbsp;</option>';
293  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS))
294  {
295  $s.= '<option value="2">'.$langs->trans('Prospect').'</option>';
296  }
297  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
298  $s.= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
299  }
300  if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
301  $s.= '<option value="1">'.$langs->trans('Customer').'</option>';
302  }
303  $s.= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
304 
305  $s.='</select> ';
306 
307  $s.=$langs->trans("Status");
308  $s.=': <select name="filter_status" class="flat">';
309  $s.='<option value="-1">&nbsp;</option>';
310  $s.='<option value="1" selected>'.$langs->trans("Enabled").'</option>';
311  $s.='<option value="0">'.$langs->trans("Disabled").'</option>';
312  $s.='</select>';
313  return $s;
314  }
315 
316 
323  function url($id)
324  {
325  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('',"company").'</a>';
326  }
327 }
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Class to manage a list of personalised recipients for mailing feature.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
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. ...
add_to_target($mailing_id, $filtersarray=array())
This is the main function that returns the array of emails.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Parent class of emailing target selectors modules.