dolibarr  18.0.0-alpha
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 $conf,$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 maxwidth200" 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 maxwidth200">';
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 maxwidth200">';
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  if (getDolGlobalInt('MAIN_MULTILANGS')) {
310  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
311  $formadmin = new FormAdmin($this->db);
312  $s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
313  $s .= $formadmin->select_language($langs->getDefaultLang(1), 'filter_lang', 0, null, 1, 0, 0, '', 0, 0, 0, null, 1);
314  }
315 
316  return $s;
317  }
318 
319 
326  public function url($id)
327  {
328  return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
329  }
330 
331 
332  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
339  public function add_to_target($mailing_id)
340  {
341  // phpcs:enable
342  global $conf, $langs;
343 
344  $filter = GETPOST('filter', 'alpha');
345  $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
346  $filter_category = GETPOST('filter_category', 'alpha');
347  $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
348  $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
349  $filter_lang = GETPOST('filter_lang', 'alpha');
350 
351  $cibles = array();
352 
353  // List prospects levels
354  $prospectlevel = array();
355  $sql = "SELECT code, label";
356  $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
357  $sql .= " WHERE active > 0";
358  $sql .= " ORDER BY label";
359  $resql = $this->db->query($sql);
360  if ($resql) {
361  $num = $this->db->num_rows($resql);
362  $i = 0;
363  while ($i < $num) {
364  $obj = $this->db->fetch_object($resql);
365  $prospectlevel[$obj->code] = $obj->label;
366  $i++;
367  }
368  } else {
369  dol_print_error($this->db);
370  }
371 
372  // Request must return: id, email, fk_contact, lastname, firstname, other
373  $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,";
374  $sql .= " s.nom as companyname";
375  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
376  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
377  if ($filter_category != 'all' && $filter_category != '-1') {
378  $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
379  $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
380  }
381  if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
382  $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
383  $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
384  }
385  if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
386  $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
387  $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
388  }
389  $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
390  $sql .= " AND sp.email <> ''";
391 
392  $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = sp.email) = 0";
393  // Exclude unsubscribed email adresses
394  $sql .= " AND sp.statut = 1";
395  $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
396 
397  // Filter on category
398  if ($filter_category != 'all' && $filter_category != '-1') {
399  $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
400  $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
401  }
402  if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
403  $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
404  $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
405  }
406  if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
407  $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
408  $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
409  }
410 
411  // Filter on language
412  if (!empty($filter_lang)) {
413  $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
414  }
415 
416  // Filter on nature
417  $key = $filter;
418 
419  //print "xx".$key;
420  if ($key == 'prospects') {
421  $sql .= " AND s.client = 2";
422  }
423  foreach ($prospectlevel as $codelevel => $valuelevel) {
424  if ($key == 'prospectslevel'.$codelevel) {
425  $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
426  }
427  }
428  if ($key == 'customers') {
429  $sql .= " AND s.client = 1";
430  }
431  if ($key == 'suppliers') {
432  $sql .= " AND s.fournisseur = 1";
433  }
434 
435  // Filter on job position
436  $key = $filter_jobposition;
437  if (!empty($key) && $key != 'all' && $key != '-1') {
438  $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
439  }
440 
441  $sql .= " ORDER BY sp.email";
442  // print "wwwwwwx".$sql;
443  // Stocke destinataires dans cibles
444  $result = $this->db->query($sql);
445  if ($result) {
446  $num = $this->db->num_rows($result);
447  $i = 0;
448  $j = 0;
449 
450  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
451 
452  $old = '';
453  while ($i < $num) {
454  $obj = $this->db->fetch_object($result);
455  if ($old <> $obj->email) {
456  $cibles[$j] = array(
457  'email' => $obj->email,
458  'fk_contact' => $obj->fk_contact,
459  'lastname' => $obj->lastname,
460  'firstname' => $obj->firstname,
461  'other' =>
462  ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
463  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
464  ($langs->transnoentities("PostOrFunction").'='.$obj->jobposition),
465  'source_url' => $this->url($obj->id),
466  'source_id' => $obj->id,
467  'source_type' => 'contact'
468  );
469  $old = $obj->email;
470  $j++;
471  }
472 
473  $i++;
474  }
475  } else {
476  dol_syslog($this->db->error());
477  $this->error = $this->db->error();
478  return -1;
479  }
480 
481  return parent::addTargetsToDatabase($mailing_id, $cibles);
482  }
483 }
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
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.
Definition: functions.lib.php:1504
$sql
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)) $sql
Social contributions to pay.
Definition: index.php:745
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:530
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4994
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
mailing_contacts1\formFilter
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
Definition: contacts1.modules.php:121
FormAdmin
Class to generate html code for admin pages.
Definition: html.formadmin.class.php:30
mailing_contacts1\url
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
Definition: contacts1.modules.php:326
mailing_contacts1\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: contacts1.modules.php:73
mailing_contacts1
Class to offer a selector of emailing targets from contacts.
Definition: contacts1.modules.php:33
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1639
mailing_contacts1\add_to_target
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
Definition: contacts1.modules.php:339
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4361
mailing_contacts1\getNbOfRecipients
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Definition: contacts1.modules.php:100
ajax_combobox
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
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:96
mailing_contacts1\__construct
__construct($db)
Constructor.
Definition: contacts1.modules.php:59