dolibarr 25.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 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2026 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 $sql = "SELECT count(distinct(c.email)) as nb";
110 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
111 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
112 $sql .= " WHERE c.entity IN (".getEntity('contact').")";
113 $sql .= " AND c.email <> ''"; // Note that null != '' is false
114 $sql .= $this->getSqlToExcludeUnsubscribed('c.email');
115 // exclude unsubscribed users
116 $sql .= " AND c.statut = 1";
117
118 // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
119 return parent::getNbOfRecipients($sql);
120 }
121
122
128 public function formFilter()
129 {
130 global $conf,$langs;
131
132 // Load translation files required by the page
133 $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
134
135 $s = '';
136
137 // Add filter on job position
138 $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
139 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
140 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
141 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
142 $sql .= " AND sp.statut = 1";
143 $sql .= " AND (sp.poste IS NOT NULL AND sp.poste <> '')";
144 $sql .= " GROUP BY sp.poste";
145 $sql .= " ORDER BY sp.poste";
146 $resql = $this->db->query($sql);
147
148 $s .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat maxwidth200" placeholder="'.dol_escape_htmltag($langs->trans("PostOrFunction")).'">';
149 $s .= '<option value="-1">'.$langs->trans("PostOrFunction").'</option>';
150 if ($resql) {
151 $num = $this->db->num_rows($resql);
152 $i = 0;
153 if ($num > 0) {
154 while ($i < $num) {
155 $obj = $this->db->fetch_object($resql);
156 $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
157 $i++;
158 }
159 } else {
160 $s .= '<option disabled="disabled" value="">'.$langs->trans("None").'</option>';
161 }
162 } else {
163 dol_print_error($this->db);
164 }
165 $s .= '</select>';
166 $s .= ajax_combobox("filter_jobposition_contact");
167 $s .= ' ';
168
169 // Filter on contact category
170 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
171 $sql .= " FROM ";
172 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
173 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
174 $sql .= " ".MAIN_DB_PREFIX."categorie_contact as cs";
175 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
176 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
177 $sql .= " AND sp.statut = 1";
178 $sql .= " AND cs.fk_categorie = c.rowid";
179 $sql .= " AND cs.fk_socpeople = sp.rowid";
180 $sql .= " GROUP BY c.label";
181 $sql .= " ORDER BY c.label";
182 $resql = $this->db->query($sql);
183
184 $s .= '<select id="filter_category_contact" name="filter_category" class="flat maxwidth200">';
185 $s .= '<option value="-1">'.$langs->trans("ContactCategoriesShort").'</option>';
186 if ($resql) {
187 $num = $this->db->num_rows($resql);
188 if ($num) {
189 $i = 0;
190 while ($i < $num) {
191 $obj = $this->db->fetch_object($resql);
192 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
193 $i++;
194 }
195 } else {
196 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
197 }
198 } else {
199 dol_print_error($this->db);
200 }
201 $s .= '</select>';
202 $s .= ajax_combobox("filter_category_contact");
203 $s .= '<br>';
204
205 // Add prospect of a particular level
206 $s .= '<select id="filter_contact" name="filter" class="flat maxwidth200">';
207 $sql = "SELECT code, label";
208 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
209 $sql .= " WHERE active > 0";
210 $sql .= " ORDER BY label";
211 $resql = $this->db->query($sql);
212 if ($resql) {
213 $num = $this->db->num_rows($resql);
214 if ($num) {
215 $s .= '<option value="-1">'.$langs->trans("NatureOfThirdParty").'</option>';
216 } else {
217 $s .= '<option value="-1">'.$langs->trans("ContactsAllShort").'</option>';
218 }
219 $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
220
221 $i = 0;
222 while ($i < $num) {
223 $obj = $this->db->fetch_object($resql);
224 $level = $langs->trans($obj->code);
225 if ($level == $obj->code) {
226 $level = $langs->trans($obj->label);
227 }
228 $labeltoshow = $langs->trans("ThirdPartyProspects").' <span class="opacitymedium">('.$langs->trans("ProspectLevelShort").'='.$level.')</span>';
229 $s .= '<option value="prospectslevel'.$obj->code.'" data-html="'.dol_escape_htmltag($labeltoshow).'">'.$labeltoshow.'</option>';
230 $i++;
231 }
232 } else {
233 dol_print_error($this->db);
234 }
235 $s .= '<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
236 //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
237 $s .= '<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
238 $s .= '</select>';
239 $s .= ajax_combobox("filter_contact");
240
241 $s .= ' ';
242
243 // Filter on thirdparty category
244 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
245 $sql .= " FROM ";
246 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
247 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
248 $sql .= " ".MAIN_DB_PREFIX."categorie_societe as cs";
249 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
250 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
251 $sql .= " AND sp.statut = 1";
252 $sql .= " AND cs.fk_categorie = c.rowid";
253 $sql .= " AND cs.fk_soc = sp.fk_soc";
254 $sql .= " GROUP BY c.label";
255 $sql .= " ORDER BY c.label";
256 $resql = $this->db->query($sql);
257
258 $s .= '<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
259 $s .= '<option value="-1">'.$langs->trans("CustomersProspectsCategoriesShort").'</option>';
260 if ($resql) {
261 $num = $this->db->num_rows($resql);
262 if ($num) {
263 $i = 0;
264 while ($i < $num) {
265 $obj = $this->db->fetch_object($resql);
266 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
267 $i++;
268 }
269 } else {
270 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
271 }
272 } else {
273 dol_print_error($this->db);
274 }
275 $s .= '</select>';
276 $s .= ajax_combobox("filter_category_customer_contact");
277
278 $s .= ' ';
279
280 // Filter on thirdparty category
281 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
282 $sql .= " FROM ";
283 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
284 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
285 $sql .= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
286 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
287 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
288 $sql .= " AND sp.statut = 1";
289 $sql .= " AND cs.fk_categorie = c.rowid";
290 $sql .= " AND cs.fk_soc = sp.fk_soc";
291 $sql .= " GROUP BY c.label";
292 $sql .= " ORDER BY c.label";
293 $resql = $this->db->query($sql);
294
295 $s .= '<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
296 $s .= '<option value="-1">'.$langs->trans("SuppliersCategoriesShort").'</option>';
297 if ($resql) {
298 $num = $this->db->num_rows($resql);
299 if ($num) {
300 $i = 0;
301 while ($i < $num) {
302 $obj = $this->db->fetch_object($resql);
303 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
304 $i++;
305 }
306 } else {
307 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
308 }
309 } else {
310 dol_print_error($this->db);
311 }
312 $s .= '</select>';
313
314 $s .= ajax_combobox("filter_category_supplier_contact");
315
316 // Choose language
317 if (getDolGlobalInt('MAIN_MULTILANGS')) {
318 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
319 $formadmin = new FormAdmin($this->db);
320 $s .= '<span class="nowraponall">';
321 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
322 $s .= $formadmin->select_language(GETPOST('filter_lang', 'aZ09'), 'filter_lang', 0, array(), $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, array(), 1);
323 $s .= '</span>';
324 }
325
326 return $s;
327 }
328
329
336 public function url($id)
337 {
338 return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
339 }
340
341
342 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
349 public function add_to_target($mailing_id)
350 {
351 // phpcs:enable
352 global $langs;
353
354 $filter = GETPOST('filter', 'alpha');
355 $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
356 $filter_category = GETPOST('filter_category', 'alpha');
357 $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
358 $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
359 $filter_lang = GETPOST('filter_lang', 'alpha');
360
361 $cibles = array();
362
363 // List prospects levels
364 $prospectlevel = array();
365 $sql = "SELECT code, label";
366 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
367 $sql .= " WHERE active > 0";
368 $sql .= " ORDER BY label";
369 $resql = $this->db->query($sql);
370 if ($resql) {
371 $num = $this->db->num_rows($resql);
372 $i = 0;
373 while ($i < $num) {
374 $obj = $this->db->fetch_object($resql);
375 $prospectlevel[$obj->code] = $obj->label;
376 $i++;
377 }
378 } else {
379 dol_print_error($this->db);
380 }
381
382 // Request must return: id, email, fk_contact, lastname, firstname, other
383 $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,";
384 $sql .= " s.nom as companyname";
385 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
386 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
387 if ($filter_category != 'all' && $filter_category != '-1') {
388 $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
389 $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
390 }
391 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
392 $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
393 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
394 }
395 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
396 $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
397 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
398 }
399 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
400 $sql .= " AND sp.email <> ''";
401
402 $sql .= $this->getSqlToExcludeUnsubscribed('sp.email');
403 // Exclude unsubscribed email addresses
404 $sql .= " AND sp.statut = 1";
405 $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
406
407 // Filter on category
408 if ($filter_category != 'all' && $filter_category != '-1') {
409 $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
410 $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
411 }
412 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
413 $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
414 $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
415 }
416 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
417 $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
418 $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
419 }
420
421 // Filter on language
422 if (!empty($filter_lang) && $filter_lang != '-1') {
423 $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
424 }
425
426 // Filter on nature
427 $key = $filter;
428
429 //print "xx".$key;
430 if ($key == 'prospects') {
431 $sql .= " AND s.client = 2";
432 }
433 foreach ($prospectlevel as $codelevel => $valuelevel) {
434 if ($key == 'prospectslevel'.$codelevel) {
435 $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
436 }
437 }
438 if ($key == 'customers') {
439 $sql .= " AND s.client = 1";
440 }
441 if ($key == 'suppliers') {
442 $sql .= " AND s.fournisseur = 1";
443 }
444
445 // Filter on job position
446 $key = $filter_jobposition;
447 if (!empty($key) && $key != 'all' && $key != '-1') {
448 $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
449 }
450
451 $sql .= " ORDER BY sp.email";
452 //print "wwwwwwx".$sql;
453
454 // Store recipients in target tables
455 $result = $this->db->query($sql);
456 if ($result) {
457 $num = $this->db->num_rows($result);
458 $i = 0;
459 $j = 0;
460
461 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
462
463 $old = '';
464 while ($i < $num) {
465 $obj = $this->db->fetch_object($result);
466 if ($old != $obj->email) {
467 $cibles[$j] = array(
468 'email' => $obj->email,
469 'fk_contact' => (int) $obj->fk_contact,
470 'lastname' => $obj->lastname,
471 'firstname' => $obj->firstname,
472 'other' =>
473 ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
474 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
475 ($langs->transnoentities("PostOrFunction").'='.$obj->jobposition),
476 'source_url' => $this->url($obj->id),
477 'source_id' => (int) $obj->id,
478 'source_type' => 'contact'
479 );
480 $old = $obj->email;
481 $j++;
482 }
483
484 $i++;
485 }
486 } else {
487 dol_syslog($this->db->error());
488 $this->error = $this->db->error();
489 return -1;
490 }
491
492 return parent::addTargetsToDatabase($mailing_id, $cibles);
493 }
494}
$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:476
Class to generate html code for admin pages.
Parent class of emailing target selectors modules.
getSqlToExcludeUnsubscribed($emailfield)
Return the SQL fragment that excludes email addresses which opted out of emailings for the current en...
Class to offer a selector of emailing targets from contacts.
add_to_target($mailing_id)
Add some recipients into target table.
formFilter()
Show filter form that appears in a page to select the recipients of 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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=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.
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)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
Definition html.lib.php:172