dolibarr 21.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 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
28include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
29
30
35{
36 public $name = 'ContactCompanies'; // Identifiant du module mailing
37 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
38 public $desc = 'Contacts of thirdparties (prospects, customers, suppliers...)';
39 public $require_module = array("societe"); // Module mailing actif si modules require_module actifs
40 public $require_admin = 0; // Module mailing actif pour user admin ou non
41
42 public $enabled = 'isModEnabled("societe")';
43
47 public $picto = 'contact';
48
49
55 public function __construct($db)
56 {
57 $this->db = $db;
58 }
59
60
69 public function getSqlArrayForStats()
70 {
71 global $langs;
72
73 $langs->load("commercial");
74
75 $statssql = array();
76 $statssql[0] = "SELECT '".$this->db->escape($langs->trans("NbOfCompaniesContacts"))."' as label,";
77 $statssql[0] .= " count(distinct(c.email)) as nb";
78 $statssql[0] .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
79 $statssql[0] .= " WHERE c.entity IN (".getEntity('contact').")";
80 $statssql[0] .= " AND c.email <> ''"; // Note that null != '' is false
81 $statssql[0] .= " AND c.statut = 1";
82
83 return $statssql;
84 }
85
86
95 public function getNbOfRecipients($sql = '')
96 {
97 global $conf;
98
99 $sql = "SELECT count(distinct(c.email)) as nb";
100 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
101 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
102 $sql .= " WHERE c.entity IN (".getEntity('contact').")";
103 $sql .= " AND c.email <> ''"; // Note that null != '' is false
104 if (empty($this->evenunsubscribe)) {
105 $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).")";
106 }
107 // exclude unsubscribed users
108 $sql .= " AND c.statut = 1";
109
110 // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
111 return parent::getNbOfRecipients($sql);
112 }
113
114
120 public function formFilter()
121 {
122 global $conf,$langs;
123
124 // Load translation files required by the page
125 $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
126
127 $s = '';
128
129 // Add filter on job position
130 $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
131 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
132 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
133 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
134 $sql .= " AND sp.statut = 1";
135 $sql .= " AND (sp.poste IS NOT NULL AND sp.poste <> '')";
136 $sql .= " GROUP BY sp.poste";
137 $sql .= " ORDER BY sp.poste";
138 $resql = $this->db->query($sql);
139
140 $s .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat maxwidth200" placeholder="'.dol_escape_htmltag($langs->trans("PostOrFunction")).'">';
141 $s .= '<option value="-1">'.$langs->trans("PostOrFunction").'</option>';
142 if ($resql) {
143 $num = $this->db->num_rows($resql);
144 $i = 0;
145 if ($num > 0) {
146 while ($i < $num) {
147 $obj = $this->db->fetch_object($resql);
148 $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
149 $i++;
150 }
151 } else {
152 $s .= '<option disabled="disabled" value="">'.$langs->trans("None").'</option>';
153 }
154 } else {
155 dol_print_error($this->db);
156 }
157 $s .= '</select>';
158 $s .= ajax_combobox("filter_jobposition_contact");
159 $s .= ' ';
160
161 // Filter on contact category
162 $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
163 $sql .= " FROM ";
164 $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
165 $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
166 $sql .= " ".MAIN_DB_PREFIX."categorie_contact as cs";
167 $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
168 $sql .= " AND sp.email <> ''"; // Note that null != '' is false
169 $sql .= " AND sp.statut = 1";
170 $sql .= " AND cs.fk_categorie = c.rowid";
171 $sql .= " AND cs.fk_socpeople = sp.rowid";
172 $sql .= " GROUP BY c.label";
173 $sql .= " ORDER BY c.label";
174 $resql = $this->db->query($sql);
175
176 $s .= '<select id="filter_category_contact" name="filter_category" class="flat maxwidth200">';
177 $s .= '<option value="-1">'.$langs->trans("ContactCategoriesShort").'</option>';
178 if ($resql) {
179 $num = $this->db->num_rows($resql);
180 if ($num) {
181 $i = 0;
182 while ($i < $num) {
183 $obj = $this->db->fetch_object($resql);
184 $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
185 $i++;
186 }
187 } else {
188 $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
189 }
190 } else {
191 dol_print_error($this->db);
192 }
193 $s .= '</select>';
194 $s .= ajax_combobox("filter_category_contact");
195 $s .= '<br>';
196
197 // Add prospect of a particular level
198 $s .= '<select id="filter_contact" name="filter" class="flat maxwidth200">';
199 $sql = "SELECT code, label";
200 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
201 $sql .= " WHERE active > 0";
202 $sql .= " ORDER BY label";
203 $resql = $this->db->query($sql);
204 if ($resql) {
205 $num = $this->db->num_rows($resql);
206 if ($num) {
207 $s .= '<option value="-1">'.$langs->trans("NatureOfThirdParty").'</option>';
208 } else {
209 $s .= '<option value="-1">'.$langs->trans("ContactsAllShort").'</option>';
210 }
211 $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
212
213 $i = 0;
214 while ($i < $num) {
215 $obj = $this->db->fetch_object($resql);
216 $level = $langs->trans($obj->code);
217 if ($level == $obj->code) {
218 $level = $langs->trans($obj->label);
219 }
220 $labeltoshow = $langs->trans("ThirdPartyProspects").' <span class="opacitymedium">('.$langs->trans("ProspectLevelShort").'='.$level.')</span>';
221 $s .= '<option value="prospectslevel'.$obj->code.'" data-html="'.dol_escape_htmltag($labeltoshow).'">'.$labeltoshow.'</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 .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
313 $s .= $formadmin->select_language(GETPOST('filter_lang', 'aZ09'), 'filter_lang', 0, null, $langs->trans("DefaultLang"), 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 if (empty($this->evenunsubscribe)) {
393 $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).")";
394 }
395 // Exclude unsubscribed email addresses
396 $sql .= " AND sp.statut = 1";
397 $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
398
399 // Filter on category
400 if ($filter_category != 'all' && $filter_category != '-1') {
401 $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
402 $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
403 }
404 if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
405 $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
406 $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
407 }
408 if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
409 $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
410 $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
411 }
412
413 // Filter on language
414 if (!empty($filter_lang) && $filter_lang != '-1') {
415 $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
416 }
417
418 // Filter on nature
419 $key = $filter;
420
421 //print "xx".$key;
422 if ($key == 'prospects') {
423 $sql .= " AND s.client = 2";
424 }
425 foreach ($prospectlevel as $codelevel => $valuelevel) {
426 if ($key == 'prospectslevel'.$codelevel) {
427 $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
428 }
429 }
430 if ($key == 'customers') {
431 $sql .= " AND s.client = 1";
432 }
433 if ($key == 'suppliers') {
434 $sql .= " AND s.fournisseur = 1";
435 }
436
437 // Filter on job position
438 $key = $filter_jobposition;
439 if (!empty($key) && $key != 'all' && $key != '-1') {
440 $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
441 }
442
443 $sql .= " ORDER BY sp.email";
444 //print "wwwwwwx".$sql;
445
446 // Store recipients in target tables
447 $result = $this->db->query($sql);
448 if ($result) {
449 $num = $this->db->num_rows($result);
450 $i = 0;
451 $j = 0;
452
453 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
454
455 $old = '';
456 while ($i < $num) {
457 $obj = $this->db->fetch_object($result);
458 if ($old != $obj->email) {
459 $cibles[$j] = array(
460 'email' => $obj->email,
461 'fk_contact' => $obj->fk_contact,
462 'lastname' => $obj->lastname,
463 'firstname' => $obj->firstname,
464 'other' =>
465 ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
466 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
467 ($langs->transnoentities("PostOrFunction").'='.$obj->jobposition),
468 'source_url' => $this->url($obj->id),
469 'source_id' => $obj->id,
470 'source_type' => 'contact'
471 );
472 $old = $obj->email;
473 $j++;
474 }
475
476 $i++;
477 }
478 } else {
479 dol_syslog($this->db->error());
480 $this->error = $this->db->error();
481 return -1;
482 }
483
484 return parent::addTargetsToDatabase($mailing_id, $cibles);
485 }
486}
$id
Definition account.php:39
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:457
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_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (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...