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