dolibarr 24.0.0-beta
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 .= '<span class="nowraponall">';
325 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
326 $s .= $formadmin->select_language(GETPOST('filter_lang', 'aZ09'), 'filter_lang', 0, array(), $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, array(), 1);
327 $s .= '</span>';
328 }
329
330 return $s;
331 }
332
333
340 public function url($id)
341 {
342 return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
343 }
344
345
346 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
353 public function add_to_target($mailing_id)
354 {
355 // phpcs:enable
356 global $conf, $langs;
357
358 $filter = GETPOST('filter', 'alpha');
359 $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
360 $filter_category = GETPOST('filter_category', 'alpha');
361 $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
362 $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
363 $filter_lang = GETPOST('filter_lang', 'alpha');
364
365 $cibles = array();
366
367 // List prospects levels
368 $prospectlevel = array();
369 $sql = "SELECT code, label";
370 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
371 $sql .= " WHERE active > 0";
372 $sql .= " ORDER BY label";
373 $resql = $this->db->query($sql);
374 if ($resql) {
375 $num = $this->db->num_rows($resql);
376 $i = 0;
377 while ($i < $num) {
378 $obj = $this->db->fetch_object($resql);
379 $prospectlevel[$obj->code] = $obj->label;
380 $i++;
381 }
382 } else {
383 dol_print_error($this->db);
384 }
385
386 // Request must return: id, email, fk_contact, lastname, firstname, other
387 $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,";
388 $sql .= " s.nom as companyname";
389 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
390 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
391 if ($filter_category != 'all' && $filter_category != '-1') {
392 $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
393 $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
394 }
395 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
396 $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
397 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
398 }
399 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
400 $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
401 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
402 }
403 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
404 $sql .= " AND sp.email <> ''";
405
406 if (empty($this->evenunsubscribe)) {
407 $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).")";
408 }
409 // Exclude unsubscribed email addresses
410 $sql .= " AND sp.statut = 1";
411 $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
412
413 // Filter on category
414 if ($filter_category != 'all' && $filter_category != '-1') {
415 $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
416 $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
417 }
418 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
419 $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
420 $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
421 }
422 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
423 $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
424 $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
425 }
426
427 // Filter on language
428 if (!empty($filter_lang) && $filter_lang != '-1') {
429 $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
430 }
431
432 // Filter on nature
433 $key = $filter;
434
435 //print "xx".$key;
436 if ($key == 'prospects') {
437 $sql .= " AND s.client = 2";
438 }
439 foreach ($prospectlevel as $codelevel => $valuelevel) {
440 if ($key == 'prospectslevel'.$codelevel) {
441 $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
442 }
443 }
444 if ($key == 'customers') {
445 $sql .= " AND s.client = 1";
446 }
447 if ($key == 'suppliers') {
448 $sql .= " AND s.fournisseur = 1";
449 }
450
451 // Filter on job position
452 $key = $filter_jobposition;
453 if (!empty($key) && $key != 'all' && $key != '-1') {
454 $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
455 }
456
457 $sql .= " ORDER BY sp.email";
458 //print "wwwwwwx".$sql;
459
460 // Store recipients in target tables
461 $result = $this->db->query($sql);
462 if ($result) {
463 $num = $this->db->num_rows($result);
464 $i = 0;
465 $j = 0;
466
467 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
468
469 $old = '';
470 while ($i < $num) {
471 $obj = $this->db->fetch_object($result);
472 if ($old != $obj->email) {
473 $cibles[$j] = array(
474 'email' => $obj->email,
475 'fk_contact' => (int) $obj->fk_contact,
476 'lastname' => $obj->lastname,
477 'firstname' => $obj->firstname,
478 'other' =>
479 ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
480 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
481 ($langs->transnoentities("PostOrFunction").'='.$obj->jobposition),
482 'source_url' => $this->url($obj->id),
483 'source_id' => (int) $obj->id,
484 'source_type' => 'contact'
485 );
486 $old = $obj->email;
487 $j++;
488 }
489
490 $i++;
491 }
492 } else {
493 dol_syslog($this->db->error());
494 $this->error = $this->db->error();
495 return -1;
496 }
497
498 return parent::addTargetsToDatabase($mailing_id, $cibles);
499 }
500}
$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.
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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...