dolibarr 23.0.3
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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
29include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
30
31
36{
37 public $name = 'ContactCompanies'; // Identifiant du module mailing
38 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
39 public $desc = 'Contacts of thirdparties (prospects, customers, suppliers...)';
40
44 public $require_module = array("societe"); // Module mailing actif si modules require_module actifs
45
49 public $require_admin = 0; // Module mailing actif pour user admin ou non
50
54 public $enabled = 'isModEnabled("societe")';
55
59 public $picto = 'contact';
60
61
67 public function __construct($db)
68 {
69 $this->db = $db;
70 }
71
72
81 public function getSqlArrayForStats()
82 {
83 global $langs;
84
85 $langs->load("commercial");
86
87 $statssql = array();
88 $statssql[0] = "SELECT '".$this->db->escape($langs->trans("NbOfCompaniesContacts"))."' as label,";
89 $statssql[0] .= " count(distinct(c.email)) as nb";
90 $statssql[0] .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
91 $statssql[0] .= " WHERE c.entity IN (".getEntity('contact').")";
92 $statssql[0] .= " AND c.email <> ''"; // Note that null != '' is false
93 $statssql[0] .= " AND c.statut = 1";
94
95 return $statssql;
96 }
97
98
107 public function getNbOfRecipients($sql = '')
108 {
109 global $conf;
110
111 $sql = "SELECT count(distinct(c.email)) as nb";
112 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
113 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
114 $sql .= " WHERE c.entity IN (".getEntity('contact').")";
115 $sql .= " AND c.email <> ''"; // Note that null != '' is false
116 if (empty($this->evenunsubscribe)) {
117 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = c.email and mu.entity = ".((int) $conf->entity).")";
118 }
119 // exclude unsubscribed users
120 $sql .= " AND c.statut = 1";
121
122 // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
123 return parent::getNbOfRecipients($sql);
124 }
125
126
132 public function formFilter()
133 {
134 global $conf,$langs;
135
136 // Load translation files required by the page
137 $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
138
139 $s = '';
140
141 // Add filter on job position
142 $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
143 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
144 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
145 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
146 $sql .= " AND sp.statut = 1";
147 $sql .= " AND (sp.poste IS NOT NULL AND sp.poste <> '')";
148 $sql .= " GROUP BY sp.poste";
149 $sql .= " ORDER BY sp.poste";
150 $resql = $this->db->query($sql);
151
152 $s .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat maxwidth200" placeholder="'.dol_escape_htmltag($langs->trans("PostOrFunction")).'">';
153 $s .= '<option value="-1">'.$langs->trans("PostOrFunction").'</option>';
154 if ($resql) {
155 $num = $this->db->num_rows($resql);
156 $i = 0;
157 if ($num > 0) {
158 while ($i < $num) {
159 $obj = $this->db->fetch_object($resql);
160 $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
161 $i++;
162 }
163 } else {
164 $s .= '<option disabled="disabled" value="">'.$langs->trans("None").'</option>';
165 }
166 } else {
167 dol_print_error($this->db);
168 }
169 $s .= '</select>';
170 $s .= ajax_combobox("filter_jobposition_contact");
171 $s .= ' ';
172
173 // Filter on contact category
174 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
175 $sql .= " FROM ";
176 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
177 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
178 $sql .= " ".MAIN_DB_PREFIX."categorie_contact as cs";
179 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
180 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
181 $sql .= " AND sp.statut = 1";
182 $sql .= " AND cs.fk_categorie = c.rowid";
183 $sql .= " AND cs.fk_socpeople = sp.rowid";
184 $sql .= " GROUP BY c.label";
185 $sql .= " ORDER BY c.label";
186 $resql = $this->db->query($sql);
187
188 $s .= '<select id="filter_category_contact" name="filter_category" class="flat maxwidth200">';
189 $s .= '<option value="-1">'.$langs->trans("ContactCategoriesShort").'</option>';
190 if ($resql) {
191 $num = $this->db->num_rows($resql);
192 if ($num) {
193 $i = 0;
194 while ($i < $num) {
195 $obj = $this->db->fetch_object($resql);
196 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
197 $i++;
198 }
199 } else {
200 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
201 }
202 } else {
203 dol_print_error($this->db);
204 }
205 $s .= '</select>';
206 $s .= ajax_combobox("filter_category_contact");
207 $s .= '<br>';
208
209 // Add prospect of a particular level
210 $s .= '<select id="filter_contact" name="filter" class="flat maxwidth200">';
211 $sql = "SELECT code, label";
212 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
213 $sql .= " WHERE active > 0";
214 $sql .= " ORDER BY label";
215 $resql = $this->db->query($sql);
216 if ($resql) {
217 $num = $this->db->num_rows($resql);
218 if ($num) {
219 $s .= '<option value="-1">'.$langs->trans("NatureOfThirdParty").'</option>';
220 } else {
221 $s .= '<option value="-1">'.$langs->trans("ContactsAllShort").'</option>';
222 }
223 $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
224
225 $i = 0;
226 while ($i < $num) {
227 $obj = $this->db->fetch_object($resql);
228 $level = $langs->trans($obj->code);
229 if ($level == $obj->code) {
230 $level = $langs->trans($obj->label);
231 }
232 $labeltoshow = $langs->trans("ThirdPartyProspects").' <span class="opacitymedium">('.$langs->trans("ProspectLevelShort").'='.$level.')</span>';
233 $s .= '<option value="prospectslevel'.$obj->code.'" data-html="'.dol_escape_htmltag($labeltoshow).'">'.$labeltoshow.'</option>';
234 $i++;
235 }
236 } else {
237 dol_print_error($this->db);
238 }
239 $s .= '<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
240 //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
241 $s .= '<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
242 $s .= '</select>';
243 $s .= ajax_combobox("filter_contact");
244
245 $s .= ' ';
246
247 // Filter on thirdparty category
248 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
249 $sql .= " FROM ";
250 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
251 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
252 $sql .= " ".MAIN_DB_PREFIX."categorie_societe as cs";
253 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
254 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
255 $sql .= " AND sp.statut = 1";
256 $sql .= " AND cs.fk_categorie = c.rowid";
257 $sql .= " AND cs.fk_soc = sp.fk_soc";
258 $sql .= " GROUP BY c.label";
259 $sql .= " ORDER BY c.label";
260 $resql = $this->db->query($sql);
261
262 $s .= '<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
263 $s .= '<option value="-1">'.$langs->trans("CustomersProspectsCategoriesShort").'</option>';
264 if ($resql) {
265 $num = $this->db->num_rows($resql);
266 if ($num) {
267 $i = 0;
268 while ($i < $num) {
269 $obj = $this->db->fetch_object($resql);
270 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
271 $i++;
272 }
273 } else {
274 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
275 }
276 } else {
277 dol_print_error($this->db);
278 }
279 $s .= '</select>';
280 $s .= ajax_combobox("filter_category_customer_contact");
281
282 $s .= ' ';
283
284 // Filter on thirdparty category
285 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
286 $sql .= " FROM ";
287 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
288 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
289 $sql .= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
290 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
291 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
292 $sql .= " AND sp.statut = 1";
293 $sql .= " AND cs.fk_categorie = c.rowid";
294 $sql .= " AND cs.fk_soc = sp.fk_soc";
295 $sql .= " GROUP BY c.label";
296 $sql .= " ORDER BY c.label";
297 $resql = $this->db->query($sql);
298
299 $s .= '<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
300 $s .= '<option value="-1">'.$langs->trans("SuppliersCategoriesShort").'</option>';
301 if ($resql) {
302 $num = $this->db->num_rows($resql);
303 if ($num) {
304 $i = 0;
305 while ($i < $num) {
306 $obj = $this->db->fetch_object($resql);
307 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
308 $i++;
309 }
310 } else {
311 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
312 }
313 } else {
314 dol_print_error($this->db);
315 }
316 $s .= '</select>';
317
318 $s .= ajax_combobox("filter_category_supplier_contact");
319
320 // Choose language
321 if (getDolGlobalInt('MAIN_MULTILANGS')) {
322 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
323 $formadmin = new FormAdmin($this->db);
324 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
325 $s .= $formadmin->select_language(GETPOST('filter_lang', 'aZ09'), 'filter_lang', 0, array(), $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, array(), 1);
326 }
327
328 return $s;
329 }
330
331
338 public function url($id)
339 {
340 return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
341 }
342
343
344 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
351 public function add_to_target($mailing_id)
352 {
353 // phpcs:enable
354 global $conf, $langs;
355
356 $filter = GETPOST('filter', 'alpha');
357 $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
358 $filter_category = GETPOST('filter_category', 'alpha');
359 $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
360 $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
361 $filter_lang = GETPOST('filter_lang', 'alpha');
362
363 $cibles = array();
364
365 // List prospects levels
366 $prospectlevel = array();
367 $sql = "SELECT code, label";
368 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
369 $sql .= " WHERE active > 0";
370 $sql .= " ORDER BY label";
371 $resql = $this->db->query($sql);
372 if ($resql) {
373 $num = $this->db->num_rows($resql);
374 $i = 0;
375 while ($i < $num) {
376 $obj = $this->db->fetch_object($resql);
377 $prospectlevel[$obj->code] = $obj->label;
378 $i++;
379 }
380 } else {
381 dol_print_error($this->db);
382 }
383
384 // Request must return: id, email, fk_contact, lastname, firstname, other
385 $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,";
386 $sql .= " s.nom as companyname";
387 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
388 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
389 if ($filter_category != 'all' && $filter_category != '-1') {
390 $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
391 $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
392 }
393 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
394 $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
395 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
396 }
397 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
398 $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
399 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
400 }
401 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
402 $sql .= " AND sp.email <> ''";
403
404 if (empty($this->evenunsubscribe)) {
405 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = sp.email and mu.entity = ".((int) $conf->entity).")";
406 }
407 // Exclude unsubscribed email addresses
408 $sql .= " AND sp.statut = 1";
409 $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
410
411 // Filter on category
412 if ($filter_category != 'all' && $filter_category != '-1') {
413 $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
414 $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
415 }
416 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
417 $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
418 $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
419 }
420 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
421 $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
422 $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
423 }
424
425 // Filter on language
426 if (!empty($filter_lang) && $filter_lang != '-1') {
427 $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
428 }
429
430 // Filter on nature
431 $key = $filter;
432
433 //print "xx".$key;
434 if ($key == 'prospects') {
435 $sql .= " AND s.client = 2";
436 }
437 foreach ($prospectlevel as $codelevel => $valuelevel) {
438 if ($key == 'prospectslevel'.$codelevel) {
439 $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
440 }
441 }
442 if ($key == 'customers') {
443 $sql .= " AND s.client = 1";
444 }
445 if ($key == 'suppliers') {
446 $sql .= " AND s.fournisseur = 1";
447 }
448
449 // Filter on job position
450 $key = $filter_jobposition;
451 if (!empty($key) && $key != 'all' && $key != '-1') {
452 $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
453 }
454
455 $sql .= " ORDER BY sp.email";
456 //print "wwwwwwx".$sql;
457
458 // Store recipients in target tables
459 $result = $this->db->query($sql);
460 if ($result) {
461 $num = $this->db->num_rows($result);
462 $i = 0;
463 $j = 0;
464
465 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
466
467 $old = '';
468 while ($i < $num) {
469 $obj = $this->db->fetch_object($result);
470 if ($old != $obj->email) {
471 $cibles[$j] = array(
472 'email' => $obj->email,
473 'fk_contact' => (int) $obj->fk_contact,
474 'lastname' => $obj->lastname,
475 'firstname' => $obj->firstname,
476 'other' =>
477 ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
478 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
479 ($langs->transnoentities("PostOrFunction").'='.$obj->jobposition),
480 'source_url' => $this->url($obj->id),
481 'source_id' => (int) $obj->id,
482 'source_type' => 'contact'
483 );
484 $old = $obj->email;
485 $j++;
486 }
487
488 $i++;
489 }
490 } else {
491 dol_syslog($this->db->error());
492 $this->error = $this->db->error();
493 return -1;
494 }
495
496 return parent::addTargetsToDatabase($mailing_id, $cibles);
497 }
498}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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:475
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)
Add some recipients into target table.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
url($id)
Provide the URL to the car of the source information of the recipient for the 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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a 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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...