dolibarr  17.0.4
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 
41  public $enabled = 'isModEnabled("societe")';
42 
46  public $picto = 'contact';
47 
51  public $db;
52 
53 
59  public function __construct($db)
60  {
61  $this->db = $db;
62  }
63 
64 
73  public function getSqlArrayForStats()
74  {
75  global $conf, $langs;
76 
77  $langs->load("commercial");
78 
79  $statssql = array();
80  $statssql[0] = "SELECT '".$this->db->escape($langs->trans("NbOfCompaniesContacts"))."' as label,";
81  $statssql[0] .= " count(distinct(c.email)) as nb";
82  $statssql[0] .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
83  $statssql[0] .= " WHERE c.entity IN (".getEntity('contact').")";
84  $statssql[0] .= " AND c.email <> ''"; // Note that null != '' is false
85  $statssql[0] .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = c.email) = 0";
86  $statssql[0] .= " AND c.statut = 1";
87 
88  return $statssql;
89  }
90 
91 
100  public function getNbOfRecipients($sql = '')
101  {
102  global $conf;
103 
104  $sql = "SELECT count(distinct(c.email)) as nb";
105  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
106  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
107  $sql .= " WHERE c.entity IN (".getEntity('contact').")";
108  $sql .= " AND c.email <> ''"; // Note that null != '' is false
109  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = c.email) = 0";
110  // exclude unsubscribed users
111  $sql .= " AND c.statut = 1";
112 
113  // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
114  return parent::getNbOfRecipients($sql);
115  }
116 
117 
123  public function formFilter()
124  {
125  global $langs,$conf;
126 
127  // Load translation files required by the page
128  $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
129 
130  $s = '';
131 
132  // Add filter on job position
133  $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
134  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
135  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
136  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
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 .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat" placeholder="'.dol_escape_htmltag($langs->trans("PostOrFunction")).'">';
144  $s .= '<option value="-1">'.$langs->trans("PostOrFunction").'</option>';
145  if ($resql) {
146  $num = $this->db->num_rows($resql);
147  $i = 0;
148  if ($num > 0) {
149  while ($i < $num) {
150  $obj = $this->db->fetch_object($resql);
151  $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
152  $i++;
153  }
154  } else {
155  $s .= '<option disabled="disabled" value="">'.$langs->trans("None").'</option>';
156  }
157  } else {
158  dol_print_error($this->db);
159  }
160  $s .= '</select>';
161  $s .= ajax_combobox("filter_jobposition_contact");
162  $s .= ' ';
163 
164  // Filter on contact category
165  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
166  $sql .= " FROM ";
167  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
168  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
169  $sql .= " ".MAIN_DB_PREFIX."categorie_contact as cs";
170  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
171  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
172  $sql .= " AND sp.statut = 1";
173  $sql .= " AND cs.fk_categorie = c.rowid";
174  $sql .= " AND cs.fk_socpeople = sp.rowid";
175  $sql .= " GROUP BY c.label";
176  $sql .= " ORDER BY c.label";
177  $resql = $this->db->query($sql);
178 
179  $s .= '<select id="filter_category_contact" name="filter_category" class="flat">';
180  $s .= '<option value="-1">'.$langs->trans("ContactCategoriesShort").'</option>';
181  if ($resql) {
182  $num = $this->db->num_rows($resql);
183  if ($num) {
184  $i = 0;
185  while ($i < $num) {
186  $obj = $this->db->fetch_object($resql);
187  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
188  $i++;
189  }
190  } else {
191  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
192  }
193  } else {
194  dol_print_error($this->db);
195  }
196  $s .= '</select>';
197  $s .= ajax_combobox("filter_category_contact");
198  $s .= '<br>';
199 
200  // Add prospect of a particular level
201  $s .= '<select id="filter_contact" name="filter" class="flat">';
202  $sql = "SELECT code, label";
203  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
204  $sql .= " WHERE active > 0";
205  $sql .= " ORDER BY label";
206  $resql = $this->db->query($sql);
207  if ($resql) {
208  $num = $this->db->num_rows($resql);
209  if ($num) {
210  $s .= '<option value="-1">'.$langs->trans("NatureOfThirdParty").'</option>';
211  } else {
212  $s .= '<option value="-1">'.$langs->trans("ContactsAllShort").'</option>';
213  }
214  $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
215 
216  $i = 0;
217  while ($i < $num) {
218  $obj = $this->db->fetch_object($resql);
219  $level = $langs->trans($obj->code);
220  if ($level == $obj->code) {
221  $level = $langs->trans($obj->label);
222  }
223  $s .= '<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
224  $i++;
225  }
226  } else {
227  dol_print_error($this->db);
228  }
229  $s .= '<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
230  //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
231  $s .= '<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
232  $s .= '</select>';
233  $s .= ajax_combobox("filter_contact");
234 
235  $s .= ' ';
236 
237  // Filter on thirdparty category
238  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
239  $sql .= " FROM ";
240  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
241  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
242  $sql .= " ".MAIN_DB_PREFIX."categorie_societe as cs";
243  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
244  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
245  $sql .= " AND sp.statut = 1";
246  $sql .= " AND cs.fk_categorie = c.rowid";
247  $sql .= " AND cs.fk_soc = sp.fk_soc";
248  $sql .= " GROUP BY c.label";
249  $sql .= " ORDER BY c.label";
250  $resql = $this->db->query($sql);
251 
252  $s .= '<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
253  $s .= '<option value="-1">'.$langs->trans("CustomersProspectsCategoriesShort").'</option>';
254  if ($resql) {
255  $num = $this->db->num_rows($resql);
256  if ($num) {
257  $i = 0;
258  while ($i < $num) {
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 {
267  dol_print_error($this->db);
268  }
269  $s .= '</select>';
270  $s .= ajax_combobox("filter_category_customer_contact");
271 
272  $s .= ' ';
273 
274  // Filter on thirdparty category
275  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
276  $sql .= " FROM ";
277  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
278  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
279  $sql .= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
280  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
281  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
282  $sql .= " AND sp.statut = 1";
283  $sql .= " AND cs.fk_categorie = c.rowid";
284  $sql .= " AND cs.fk_soc = sp.fk_soc";
285  $sql .= " GROUP BY c.label";
286  $sql .= " ORDER BY c.label";
287  $resql = $this->db->query($sql);
288 
289  $s .= '<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
290  $s .= '<option value="-1">'.$langs->trans("SuppliersCategoriesShort").'</option>';
291  if ($resql) {
292  $num = $this->db->num_rows($resql);
293  if ($num) {
294  $i = 0;
295  while ($i < $num) {
296  $obj = $this->db->fetch_object($resql);
297  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
298  $i++;
299  }
300  } else {
301  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
302  }
303  } else {
304  dol_print_error($this->db);
305  }
306  $s .= '</select>';
307 
308  $s .= ajax_combobox("filter_category_supplier_contact");
309 
310  // Choose language
311  if (getDolGlobalInt('MAIN_MULTILANGS')) {
312  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
313  $formadmin = new FormAdmin($this->db);
314  $s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
315  $s .= $formadmin->select_language($langs->getDefaultLang(1), 'filter_lang', 0, 0, 1, 0, 0, '', 0, 0, 0, null, 1);
316  }
317 
318  return $s;
319  }
320 
321 
328  public function url($id)
329  {
330  return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
331  }
332 
333 
334  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
341  public function add_to_target($mailing_id)
342  {
343  // phpcs:enable
344  global $conf, $langs;
345 
346  $filter = GETPOST('filter', 'alpha');
347  $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
348  $filter_category = GETPOST('filter_category', 'alpha');
349  $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
350  $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
351  $filter_lang = GETPOST('filter_lang', 'alpha');
352 
353  $cibles = array();
354 
355  // List prospects levels
356  $prospectlevel = array();
357  $sql = "SELECT code, label";
358  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
359  $sql .= " WHERE active > 0";
360  $sql .= " ORDER BY label";
361  $resql = $this->db->query($sql);
362  if ($resql) {
363  $num = $this->db->num_rows($resql);
364  $i = 0;
365  while ($i < $num) {
366  $obj = $this->db->fetch_object($resql);
367  $prospectlevel[$obj->code] = $obj->label;
368  $i++;
369  }
370  } else {
371  dol_print_error($this->db);
372  }
373 
374  // Request must return: id, email, fk_contact, lastname, firstname, other
375  $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,";
376  $sql .= " s.nom as companyname";
377  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
378  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
379  if ($filter_category != 'all' && $filter_category != '-1') {
380  $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
381  $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
382  }
383  if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
384  $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
385  $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
386  }
387  if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
388  $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
389  $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
390  }
391  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
392  $sql .= " AND sp.email <> ''";
393 
394  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = sp.email) = 0";
395  // Exclude unsubscribed email adresses
396  $sql .= " AND sp.statut = 1";
397  $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
398 
399  // Filter on category
400  if ($filter_category != 'all' && $filter_category != '-1') {
401  $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
402  $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
403  }
404  if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
405  $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
406  $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
407  }
408  if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
409  $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
410  $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
411  }
412 
413  // Filter on language
414  if (!empty($filter_lang)) {
415  $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
416  }
417 
418  // Filter on nature
419  $key = $filter;
420 
421  //print "xx".$key;
422  if ($key == 'prospects') {
423  $sql .= " AND s.client = 2";
424  }
425  foreach ($prospectlevel as $codelevel => $valuelevel) {
426  if ($key == 'prospectslevel'.$codelevel) {
427  $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
428  }
429  }
430  if ($key == 'customers') {
431  $sql .= " AND s.client = 1";
432  }
433  if ($key == 'suppliers') {
434  $sql .= " AND s.fournisseur = 1";
435  }
436 
437  // Filter on job position
438  $key = $filter_jobposition;
439  if (!empty($key) && $key != 'all' && $key != '-1') {
440  $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
441  }
442 
443  $sql .= " ORDER BY sp.email";
444  // print "wwwwwwx".$sql;
445  // Stocke destinataires dans cibles
446  $result = $this->db->query($sql);
447  if ($result) {
448  $num = $this->db->num_rows($result);
449  $i = 0;
450  $j = 0;
451 
452  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
453 
454  $old = '';
455  while ($i < $num) {
456  $obj = $this->db->fetch_object($result);
457  if ($old <> $obj->email) {
458  $cibles[$j] = array(
459  'email' => $obj->email,
460  'fk_contact' => $obj->fk_contact,
461  'lastname' => $obj->lastname,
462  'firstname' => $obj->firstname,
463  'other' =>
464  ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
465  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
466  ($langs->transnoentities("PostOrFunction").'='.$obj->jobposition),
467  'source_url' => $this->url($obj->id),
468  'source_id' => $obj->id,
469  'source_type' => 'contact'
470  );
471  $old = $obj->email;
472  $j++;
473  }
474 
475  $i++;
476  }
477  } else {
478  dol_syslog($this->db->error());
479  $this->error = $this->db->error();
480  return -1;
481  }
482 
483  return parent::addTargetsToDatabase($mailing_id, $cibles);
484  }
485 }
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 offer a selector of emailing targets from contacts.
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
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.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
__construct($db)
Constructor.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
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_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
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_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