dolibarr  x.y.z
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  $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('contact').")";
106  $sql .= " AND c.email <> ''"; // Note that null != '' is false
107  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = c.email) = 0";
108  // exclude unsubscribed users
109  $sql .= " AND c.statut = 1";
110 
111  // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
112  return parent::getNbOfRecipients($sql);
113  }
114 
115 
121  public function formFilter()
122  {
123  global $langs;
124 
125  // Load translation files required by the page
126  $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
127 
128  $s = '';
129 
130  // Add filter on job position
131  $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
132  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
133  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
134  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
135  $sql .= " AND sp.statut = 1";
136  $sql .= " AND (sp.poste IS NOT NULL AND sp.poste <> '')";
137  $sql .= " GROUP BY sp.poste";
138  $sql .= " ORDER BY sp.poste";
139  $resql = $this->db->query($sql);
140 
141  $s .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat" placeholder="'.dol_escape_htmltag($langs->trans("PostOrFunction")).'">';
142  $s .= '<option value="-1">'.$langs->trans("PostOrFunction").'</option>';
143  if ($resql) {
144  $num = $this->db->num_rows($resql);
145  $i = 0;
146  if ($num > 0) {
147  while ($i < $num) {
148  $obj = $this->db->fetch_object($resql);
149  $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
150  $i++;
151  }
152  } else {
153  $s .= '<option disabled="disabled" value="">'.$langs->trans("None").'</option>';
154  }
155  } else {
156  dol_print_error($this->db);
157  }
158  $s .= '</select>';
159  $s .= ajax_combobox("filter_jobposition_contact");
160  $s .= ' ';
161 
162  // Filter on contact category
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.entity IN (".getEntity('contact').")";
169  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
170  $sql .= " AND sp.statut = 1";
171  $sql .= " AND cs.fk_categorie = c.rowid";
172  $sql .= " AND cs.fk_socpeople = sp.rowid";
173  $sql .= " GROUP BY c.label";
174  $sql .= " ORDER BY c.label";
175  $resql = $this->db->query($sql);
176 
177  $s .= '<select id="filter_category_contact" name="filter_category" class="flat">';
178  $s .= '<option value="-1">'.$langs->trans("ContactCategoriesShort").'</option>';
179  if ($resql) {
180  $num = $this->db->num_rows($resql);
181  if ($num) {
182  $i = 0;
183  while ($i < $num) {
184  $obj = $this->db->fetch_object($resql);
185  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
186  $i++;
187  }
188  } else {
189  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
190  }
191  } else {
192  dol_print_error($this->db);
193  }
194  $s .= '</select>';
195  $s .= ajax_combobox("filter_category_contact");
196  $s .= '<br>';
197 
198  // Add prospect of a particular level
199  $s .= '<select id="filter_contact" name="filter" class="flat">';
200  $sql = "SELECT code, label";
201  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
202  $sql .= " WHERE active > 0";
203  $sql .= " ORDER BY label";
204  $resql = $this->db->query($sql);
205  if ($resql) {
206  $num = $this->db->num_rows($resql);
207  if ($num) {
208  $s .= '<option value="-1">'.$langs->trans("NatureOfThirdParty").'</option>';
209  } else {
210  $s .= '<option value="-1">'.$langs->trans("ContactsAllShort").'</option>';
211  }
212  $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
213 
214  $i = 0;
215  while ($i < $num) {
216  $obj = $this->db->fetch_object($resql);
217  $level = $langs->trans($obj->code);
218  if ($level == $obj->code) {
219  $level = $langs->trans($obj->label);
220  }
221  $s .= '<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
222  $i++;
223  }
224  } else {
225  dol_print_error($this->db);
226  }
227  $s .= '<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
228  //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
229  $s .= '<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
230  $s .= '</select>';
231  $s .= ajax_combobox("filter_contact");
232 
233  $s .= ' ';
234 
235  // Filter on thirdparty category
236  $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
237  $sql .= " FROM ";
238  $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
239  $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
240  $sql .= " ".MAIN_DB_PREFIX."categorie_societe as cs";
241  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
242  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
243  $sql .= " AND sp.statut = 1";
244  $sql .= " AND cs.fk_categorie = c.rowid";
245  $sql .= " AND cs.fk_soc = sp.fk_soc";
246  $sql .= " GROUP BY c.label";
247  $sql .= " ORDER BY c.label";
248  $resql = $this->db->query($sql);
249 
250  $s .= '<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
251  $s .= '<option value="-1">'.$langs->trans("CustomersProspectsCategoriesShort").'</option>';
252  if ($resql) {
253  $num = $this->db->num_rows($resql);
254  if ($num) {
255  $i = 0;
256  while ($i < $num) {
257  $obj = $this->db->fetch_object($resql);
258  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
259  $i++;
260  }
261  } else {
262  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
263  }
264  } else {
265  dol_print_error($this->db);
266  }
267  $s .= '</select>';
268  $s .= ajax_combobox("filter_category_customer_contact");
269 
270  $s .= ' ';
271 
272  // Filter on thirdparty category
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.entity IN (".getEntity('contact').")";
279  $sql .= " AND sp.email <> ''"; // Note that null != '' is false
280  $sql .= " AND sp.statut = 1";
281  $sql .= " AND cs.fk_categorie = c.rowid";
282  $sql .= " AND cs.fk_soc = sp.fk_soc";
283  $sql .= " GROUP BY c.label";
284  $sql .= " ORDER BY c.label";
285  $resql = $this->db->query($sql);
286 
287  $s .= '<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
288  $s .= '<option value="-1">'.$langs->trans("SuppliersCategoriesShort").'</option>';
289  if ($resql) {
290  $num = $this->db->num_rows($resql);
291  if ($num) {
292  $i = 0;
293  while ($i < $num) {
294  $obj = $this->db->fetch_object($resql);
295  $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
296  $i++;
297  }
298  } else {
299  $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
300  }
301  } else {
302  dol_print_error($this->db);
303  }
304  $s .= '</select>';
305 
306  $s .= ajax_combobox("filter_category_supplier_contact");
307 
308  // Choose language
309  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
310  $formadmin = new FormAdmin($this->db);
311  $s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
312  $s .= $formadmin->select_language($langs->getDefaultLang(1), 'filter_lang', 0, 0, 1, 0, 0, '', 0, 0, 0, null, 1);
313 
314  return $s;
315  }
316 
317 
324  public function url($id)
325  {
326  return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
327  }
328 
329 
330  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
337  public function add_to_target($mailing_id)
338  {
339  // phpcs:enable
340  global $conf, $langs;
341 
342  $filter = GETPOST('filter', 'alpha');
343  $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
344  $filter_category = GETPOST('filter_category', 'alpha');
345  $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
346  $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
347  $filter_lang = GETPOST('filter_lang', 'alpha');
348 
349  $cibles = array();
350 
351  // List prospects levels
352  $prospectlevel = array();
353  $sql = "SELECT code, label";
354  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
355  $sql .= " WHERE active > 0";
356  $sql .= " ORDER BY label";
357  $resql = $this->db->query($sql);
358  if ($resql) {
359  $num = $this->db->num_rows($resql);
360  $i = 0;
361  while ($i < $num) {
362  $obj = $this->db->fetch_object($resql);
363  $prospectlevel[$obj->code] = $obj->label;
364  $i++;
365  }
366  } else {
367  dol_print_error($this->db);
368  }
369 
370  // Request must return: id, email, fk_contact, lastname, firstname, other
371  $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,";
372  $sql .= " s.nom as companyname";
373  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
374  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
375  if ($filter_category != 'all' && $filter_category != '-1') {
376  $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
377  $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
378  }
379  if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
380  $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
381  $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
382  }
383  if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
384  $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
385  $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
386  }
387  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
388  $sql .= " AND sp.email <> ''";
389 
390  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = sp.email) = 0";
391  // Exclude unsubscribed email adresses
392  $sql .= " AND sp.statut = 1";
393  $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
394 
395  // Filter on category
396  if ($filter_category != 'all' && $filter_category != '-1') {
397  $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
398  $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
399  }
400  if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
401  $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
402  $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
403  }
404  if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
405  $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
406  $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
407  }
408 
409  // Filter on language
410  if (!empty($filter_lang)) {
411  $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
412  }
413 
414  // Filter on nature
415  $key = $filter;
416 
417  //print "xx".$key;
418  if ($key == 'prospects') {
419  $sql .= " AND s.client = 2";
420  }
421  foreach ($prospectlevel as $codelevel => $valuelevel) {
422  if ($key == 'prospectslevel'.$codelevel) {
423  $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
424  }
425  }
426  if ($key == 'customers') {
427  $sql .= " AND s.client = 1";
428  }
429  if ($key == 'suppliers') {
430  $sql .= " AND s.fournisseur = 1";
431  }
432 
433  // Filter on job position
434  $key = $filter_jobposition;
435  if (!empty($key) && $key != 'all' && $key != '-1') {
436  $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
437  }
438 
439  $sql .= " ORDER BY sp.email";
440  // print "wwwwwwx".$sql;
441  // Stocke destinataires dans cibles
442  $result = $this->db->query($sql);
443  if ($result) {
444  $num = $this->db->num_rows($result);
445  $i = 0;
446  $j = 0;
447 
448  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
449 
450  $old = '';
451  while ($i < $num) {
452  $obj = $this->db->fetch_object($result);
453  if ($old <> $obj->email) {
454  $cibles[$j] = array(
455  'email' => $obj->email,
456  'fk_contact' => $obj->fk_contact,
457  'lastname' => $obj->lastname,
458  'firstname' => $obj->firstname,
459  'other' =>
460  ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
461  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
462  ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
463  'source_url' => $this->url($obj->id),
464  'source_id' => $obj->id,
465  'source_type' => 'contact'
466  );
467  $old = $obj->email;
468  $j++;
469  }
470 
471  $i++;
472  }
473  } else {
474  dol_syslog($this->db->error());
475  $this->error = $this->db->error();
476  return -1;
477  }
478 
479  return parent::addTargetsToDatabase($mailing_id, $cibles);
480  }
481 }
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)
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