dolibarr  13.0.2
contacts1.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  * or see https://www.gnu.org/
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
28 
29 
34 {
35  public $name = 'ContactCompanies'; // Identifiant du module mailing
36  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
37  public $desc = 'Contacts of thirdparties (prospects, customers, suppliers...)';
38  public $require_module = array("societe"); // Module mailing actif si modules require_module actifs
39  public $require_admin = 0; // Module mailing actif pour user admin ou non
40 
44  public $picto = 'contact';
45 
49  public $db;
50 
51 
57  public function __construct($db)
58  {
59  $this->db = $db;
60  }
61 
62 
71  public function getSqlArrayForStats()
72  {
73  global $conf, $langs;
74 
75  $langs->load("commercial");
76 
77  $statssql = array();
78  $statssql[0] = "SELECT '".$this->db->escape($langs->trans("NbOfCompaniesContacts"))."' as label,";
79  $statssql[0] .= " count(distinct(c.email)) as nb";
80  $statssql[0] .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
81  $statssql[0] .= " WHERE c.entity IN (".getEntity('socpeople').")";
82  $statssql[0] .= " AND c.email != ''"; // Note that null != '' is false
83  $statssql[0] .= " AND c.no_email = 0";
84  $statssql[0] .= " AND c.statut = 1";
85 
86  return $statssql;
87  }
88 
89 
98  public function getNbOfRecipients($sql = '')
99  {
100  global $conf;
101 
102  $sql = "SELECT count(distinct(c.email)) as nb";
103  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
104  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
105  $sql .= " WHERE c.entity IN (".getEntity('socpeople').")";
106  $sql .= " AND c.email != ''"; // Note that null != '' is false
107  $sql .= " AND c.no_email = 0";
108  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = c.email) = 0";
109  // exclude unsubscribed users
110  $sql .= " AND c.statut = 1";
111 
112  // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
113  return parent::getNbOfRecipients($sql);
114  }
115 
116 
122  public function formFilter()
123  {
124  global $langs;
125 
126  // Load translation files required by the page
127  $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
128 
129  $s = '';
130 
131  // Add filter on job position
132  $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
133  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
134  $sql .= " WHERE sp.entity IN (".getEntity('socpeople').")";
135  /*$sql.= " AND sp.email != ''"; // Note that null != '' is false
136  $sql.= " AND sp.no_email = 0";
137  $sql.= " AND sp.statut = 1";*/
138  $sql .= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
139  $sql .= " GROUP BY sp.poste";
140  $sql .= " ORDER BY sp.poste";
141  $resql = $this->db->query($sql);
142 
143  $s .= $langs->trans("PostOrFunction").': ';
144  $s .= '<select name="filter_jobposition" class="flat">';
145  $s .= '<option value="all">&nbsp;</option>';
146  if ($resql)
147  {
148  $num = $this->db->num_rows($resql);
149  $i = 0;
150  while ($i < $num)
151  {
152  $obj = $this->db->fetch_object($resql);
153  $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
154  $i++;
155  }
156  } else dol_print_error($this->db);
157  $s .= '</select>';
158 
159  $s .= ' ';
160 
161  // Filter on contact category
162  $s .= $langs->trans("ContactCategoriesShort").': ';
163  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
164  $sql .= " FROM ";
165  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
166  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
167  $sql .= " ".MAIN_DB_PREFIX."categorie_contact as cs";
168  $sql .= " WHERE sp.statut = 1"; // Note that null != '' is false
169  //$sql.= " AND sp.no_email = 0";
170  //$sql.= " AND sp.email != ''";
171  //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
172  $sql .= " AND cs.fk_categorie = c.rowid";
173  $sql .= " AND cs.fk_socpeople = sp.rowid";
174  $sql .= " GROUP BY c.label";
175  $sql .= " ORDER BY c.label";
176  $resql = $this->db->query($sql);
177 
178  $s .= '<select name="filter_category" class="flat">';
179  $s .= '<option value="all">&nbsp;</option>';
180  if ($resql)
181  {
182  $num = $this->db->num_rows($resql);
183  if ($num)
184  {
185  $i = 0;
186  while ($i < $num)
187  {
188  $obj = $this->db->fetch_object($resql);
189  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
190  $i++;
191  }
192  } else {
193  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
194  }
195  } else dol_print_error($this->db);
196  $s .= '</select>';
197 
198  $s .= '<br>';
199 
200  // Add prospect of a particular level
201  $s .= $langs->trans("NatureOfThirdParty").': ';
202  $s .= '<select name="filter" class="flat">';
203  $sql = "SELECT code, label";
204  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
205  $sql .= " WHERE active > 0";
206  $sql .= " ORDER BY label";
207  $resql = $this->db->query($sql);
208  if ($resql)
209  {
210  $num = $this->db->num_rows($resql);
211  if ($num) $s .= '<option value="all">&nbsp;</option>';
212  else $s .= '<option value="all">'.$langs->trans("ContactsAllShort").'</option>';
213  $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
214 
215  $i = 0;
216  while ($i < $num)
217  {
218  $obj = $this->db->fetch_object($resql);
219  $level = $langs->trans($obj->code);
220  if ($level == $obj->code) $level = $langs->trans($obj->label);
221  $s .= '<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
222  $i++;
223  }
224  } else dol_print_error($this->db);
225  $s .= '<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
226  //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
227  $s .= '<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
228  $s .= '</select>';
229 
230  $s .= ' ';
231 
232  // Filter on thirdparty category
233  $s .= $langs->trans("CustomersProspectsCategoriesShort").': ';
234  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
235  $sql .= " FROM ";
236  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
237  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
238  $sql .= " ".MAIN_DB_PREFIX."categorie_societe as cs";
239  $sql .= " WHERE sp.statut = 1"; // Note that null != '' is false
240  //$sql.= " AND sp.no_email = 0";
241  //$sql.= " AND sp.email != ''";
242  //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
243  $sql .= " AND cs.fk_categorie = c.rowid";
244  $sql .= " AND cs.fk_soc = sp.fk_soc";
245  $sql .= " GROUP BY c.label";
246  $sql .= " ORDER BY c.label";
247  $resql = $this->db->query($sql);
248 
249  $s .= '<select name="filter_category_customer" class="flat">';
250  $s .= '<option value="all">&nbsp;</option>';
251  if ($resql)
252  {
253  $num = $this->db->num_rows($resql);
254  if ($num)
255  {
256  $i = 0;
257  while ($i < $num)
258  {
259  $obj = $this->db->fetch_object($resql);
260  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
261  $i++;
262  }
263  } else {
264  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
265  }
266  } else dol_print_error($this->db);
267  $s .= '</select>';
268 
269  $s .= ' ';
270 
271  // Filter on thirdparty category
272  $s .= $langs->trans("SuppliersCategoriesShort").': ';
273  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
274  $sql .= " FROM ";
275  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
276  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
277  $sql .= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
278  $sql .= " WHERE sp.statut = 1"; // Note that null != '' is false
279  //$sql.= " AND sp.no_email = 0";
280  //$sql.= " AND sp.email != ''";
281  //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
282  $sql .= " AND cs.fk_categorie = c.rowid";
283  $sql .= " AND cs.fk_soc = sp.fk_soc";
284  $sql .= " GROUP BY c.label";
285  $sql .= " ORDER BY c.label";
286  $resql = $this->db->query($sql);
287 
288  $s .= '<select name="filter_category_supplier" class="flat">';
289  $s .= '<option value="all">&nbsp;</option>';
290  if ($resql)
291  {
292  $num = $this->db->num_rows($resql);
293  if ($num)
294  {
295  $i = 0;
296  while ($i < $num)
297  {
298  $obj = $this->db->fetch_object($resql);
299  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
300  $i++;
301  }
302  } else {
303  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
304  }
305  } else dol_print_error($this->db);
306  $s .= '</select>';
307 
308  return $s;
309  }
310 
311 
318  public function url($id)
319  {
320  return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
321  }
322 
323 
324  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
331  public function add_to_target($mailing_id)
332  {
333  // phpcs:enable
334  global $conf, $langs;
335 
336  $filter = GETPOST('filter', 'alpha');
337  $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
338  $filter_category = GETPOST('filter_category', 'alpha');
339  $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
340  $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
341 
342  $cibles = array();
343 
344  // List prospects levels
345  $prospectlevel = array();
346  $sql = "SELECT code, label";
347  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
348  $sql .= " WHERE active > 0";
349  $sql .= " ORDER BY label";
350  $resql = $this->db->query($sql);
351  if ($resql)
352  {
353  $num = $this->db->num_rows($resql);
354  $i = 0;
355  while ($i < $num)
356  {
357  $obj = $this->db->fetch_object($resql);
358  $prospectlevel[$obj->code] = $obj->label;
359  $i++;
360  }
361  } else dol_print_error($this->db);
362 
363  // Request must return: id, email, fk_contact, lastname, firstname, other
364  $sql = "SELECT sp.rowid as id, sp.email as email, sp.rowid as fk_contact, sp.lastname, sp.firstname, sp.civility as civility_id, sp.poste as jobposition,";
365  $sql .= " s.nom as companyname";
366  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
367  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
368  if ($filter_category <> 'all') $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
369  if ($filter_category <> 'all') $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
370  if ($filter_category_customer <> 'all') $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
371  if ($filter_category_customer <> 'all') $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
372  if ($filter_category_supplier <> 'all') $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
373  if ($filter_category_supplier <> 'all') $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
374  $sql .= " WHERE sp.entity IN (".getEntity('socpeople').")";
375  $sql .= " AND sp.email <> ''";
376  $sql .= " AND sp.no_email = 0";
377  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = sp.email) = 0";
378  // Exclude unsubscribed email adresses
379  $sql .= " AND sp.statut = 1";
380  $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
381  // Filter on category
382  if ($filter_category <> 'all') $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
383  if ($filter_category <> 'all') $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
384  if ($filter_category_customer <> 'all') $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
385  if ($filter_category_customer <> 'all') $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
386  if ($filter_category_supplier <> 'all') $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
387  if ($filter_category_supplier <> 'all') $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
388  // Filter on nature
389  $key = $filter;
390  {
391  //print "xx".$key;
392  if ($key == 'prospects') $sql .= " AND s.client=2";
393  foreach ($prospectlevel as $codelevel=>$valuelevel) if ($key == 'prospectslevel'.$codelevel) $sql .= " AND s.fk_prospectlevel='".$this->db->escape($codelevel)."'";
394  if ($key == 'customers') $sql .= " AND s.client=1";
395  if ($key == 'suppliers') $sql .= " AND s.fournisseur=1";
396  }
397  // Filter on job position
398  $key = $filter_jobposition;
399  if (!empty($key) && $key != 'all') $sql .= " AND sp.poste ='".$this->db->escape($key)."'";
400  $sql .= " ORDER BY sp.email";
401  //print "wwwwwwx".$sql;
402 
403  // Stocke destinataires dans cibles
404  $result = $this->db->query($sql);
405  if ($result)
406  {
407  $num = $this->db->num_rows($result);
408  $i = 0;
409  $j = 0;
410 
411  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
412 
413  $old = '';
414  while ($i < $num)
415  {
416  $obj = $this->db->fetch_object($result);
417  if ($old <> $obj->email)
418  {
419  $cibles[$j] = array(
420  'email' => $obj->email,
421  'fk_contact' => $obj->fk_contact,
422  'lastname' => $obj->lastname,
423  'firstname' => $obj->firstname,
424  'other' =>
425  ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
426  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
427  ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
428  'source_url' => $this->url($obj->id),
429  'source_id' => $obj->id,
430  'source_type' => 'contact'
431  );
432  $old = $obj->email;
433  $j++;
434  }
435 
436  $i++;
437  }
438  } else {
439  dol_syslog($this->db->error());
440  $this->error = $this->db->error();
441  return -1;
442  }
443 
444  return parent::addTargetsToDatabase($mailing_id, $cibles);
445  }
446 }
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.
getNbOfRecipients($sql= '')
Return here number of distinct emails returned by your selector.
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
$conf db
API class for accounts.
Definition: inc.php:54
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
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)
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...
Class to offer a selector of emailing targets from contacts.
Parent class of emailing target selectors modules.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...