dolibarr  13.0.2
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 
37  public $picto = 'company';
38 
42  public $db;
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 
73  $addDescription = "";
74  // Select the third parties from category
75  if (empty($_POST['filter']))
76  {
77  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
78  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
79  $sql .= " WHERE s.email <> ''";
80  $sql .= " AND s.entity IN (".getEntity('societe').")";
81  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
82  } else {
83  $addFilter = "";
84  if (GETPOSTISSET("filter_client") && GETPOST("filter_client") <> '-1') {
85  $addFilter .= " AND s.client=".((int) GETPOST("filter_client", 'int'));
86  $addDescription = $langs->trans('ProspectCustomer')."=";
87  if ($_POST["filter_client"] == 0)
88  {
89  $addDescription .= $langs->trans('NorProspectNorCustomer');
90  } elseif ($_POST["filter_client"] == 1)
91  {
92  $addDescription .= $langs->trans('Customer');
93  } elseif ($_POST["filter_client"] == 2)
94  {
95  $addDescription .= $langs->trans('Prospect');
96  } elseif ($_POST["filter_client"] == 3)
97  {
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  $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";
117  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
118  $sql .= " WHERE s.email <> ''";
119  $sql .= " AND s.entity IN (".getEntity('societe').")";
120  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
121  $sql .= " AND cs.fk_soc = s.rowid";
122  $sql .= " AND c.rowid = cs.fk_categorie";
123  $sql .= " AND c.rowid=".((int) GETPOST('filter', 'int'));
124  $sql .= $addFilter;
125  $sql .= " UNION ";
126  $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";
127  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
128  $sql .= " WHERE s.email <> ''";
129  $sql .= " AND s.entity IN (".getEntity('societe').")";
130  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
131  $sql .= " AND cs.fk_soc = s.rowid";
132  $sql .= " AND c.rowid = cs.fk_categorie";
133  $sql .= " AND c.rowid=".((int) GETPOST('filter', 'int'));
134  $sql .= $addFilter;
135  }
136  $sql .= " ORDER BY email";
137 
138  // Stock recipients emails into targets table
139  $result = $this->db->query($sql);
140  if ($result)
141  {
142  $num = $this->db->num_rows($result);
143  $i = 0;
144  $j = 0;
145 
146  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
147 
148  $old = '';
149  while ($i < $num)
150  {
151  $obj = $this->db->fetch_object($result);
152  if ($old <> $obj->email)
153  {
154  $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
155  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0)
156  {
157  $otherTxt .= ";";
158  }
159  $otherTxt .= $addDescription;
160  $cibles[$j] = array(
161  'email' => $obj->email,
162  'fk_contact' => $obj->fk_contact,
163  'lastname' => $obj->name, // For a thirdparty, we must use name
164  'firstname' => '', // For a thirdparty, lastname is ''
165  'other' => $otherTxt,
166  'source_url' => $this->url($obj->id),
167  'source_id' => $obj->id,
168  'source_type' => 'thirdparty'
169  );
170  $old = $obj->email;
171  $j++;
172  }
173 
174  $i++;
175  }
176  } else {
177  dol_syslog($this->db->error());
178  $this->error = $this->db->error();
179  return -1;
180  }
181 
182  return parent::addTargetsToDatabase($mailing_id, $cibles);
183  }
184 
185 
194  public function getSqlArrayForStats()
195  {
196  // CHANGE THIS: Optionnal
197 
198  //var $statssql=array();
199  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
200  return array();
201  }
202 
203 
212  public function getNbOfRecipients($sql = '')
213  {
214  global $conf;
215 
216  $sql = "SELECT count(distinct(s.email)) as nb";
217  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
218  $sql .= " WHERE s.email != ''";
219  $sql .= " AND s.entity IN (".getEntity('societe').")";
220 
221  // La requete doit retourner un champ "nb" pour etre comprise
222  // par parent::getNbOfRecipients
223  return parent::getNbOfRecipients($sql);
224  }
225 
232  public function formFilter()
233  {
234  global $conf, $langs;
235 
236  $langs->load("companies");
237 
238  $s = $langs->trans("Categories").': ';
239  $s .= '<select name="filter" class="flat">';
240 
241  // Show categories
242  $sql = "SELECT rowid, label, type, visible";
243  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
244  $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
245  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
246  $sql .= " AND entity = ".$conf->entity;
247  $sql .= " ORDER BY label";
248 
249  //print $sql;
250  $resql = $this->db->query($sql);
251  if ($resql)
252  {
253  $num = $this->db->num_rows($resql);
254 
255  if (empty($conf->categorie->enabled)) $num = 0; // Force empty list if category module is not enabled
256 
257  if ($num) $s .= '<option value="0">&nbsp;</option>';
258  else $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
259 
260  $i = 0;
261  while ($i < $num)
262  {
263  $obj = $this->db->fetch_object($resql);
264 
265  $type = '';
266  if ($obj->type == 1) $type = $langs->trans("Supplier");
267  if ($obj->type == 2) $type = $langs->trans("Customer");
268  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
269  if ($type) $s .= ' ('.$type.')';
270  $s .= '</option>';
271  $i++;
272  }
273  } else {
274  dol_print_error($this->db);
275  }
276 
277  $s .= '</select> ';
278  $s .= $langs->trans('ProspectCustomer');
279  $s .= ': <select name="filter_client" class="flat">';
280  $s .= '<option value="-1">&nbsp;</option>';
281  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
282  $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
283  }
284  if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
285  $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
286  }
287  if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
288  $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
289  }
290  $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
291 
292  $s .= '</select> ';
293 
294  $s .= $langs->trans("Status");
295  $s .= ': <select name="filter_status" class="flat">';
296  $s .= '<option value="-1">&nbsp;</option>';
297  $s .= '<option value="1" selected>'.$langs->trans("Enabled").'</option>';
298  $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
299  $s .= '</select>';
300  return $s;
301  }
302 
303 
310  public function url($id)
311  {
312  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
313  }
314 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
__construct($db)
Constructor.
Class to manage a list of personalised recipients for mailing feature.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
$conf db
API class for accounts.
Definition: inc.php:54
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called &quot;...
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
getNbOfRecipients($sql= '')
Return here number of distinct emails returned by your selector.
url($id)
Can include an URL link on each record provided by selector shown on target page. ...
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
Parent class of emailing target selectors modules.