dolibarr 25.0.0-alpha
thirdparties.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018-2018 Andre Schild <a.schild@aarboard.ch>
3 * Copyright (C) 2005-2010 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 file is an example to follow to add your own email selector inside
9 * the Dolibarr email tool.
10 * Follow instructions given in README file to know what to change to build
11 * your own emailing list selector.
12 * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
13 */
14
21include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
22
23
28{
32 public $name = 'ThirdPartiesByCategories';
33
37 public $desc = "Third parties (by categories)";
38
42 public $require_admin = 0;
43
47 public $require_module = array("societe");
48
52 public $enabled = 'isModEnabled("societe")';
53
57 public $picto = 'company';
58
59
65 public function __construct($db)
66 {
67 global $langs;
68 $langs->load("companies");
69
70 $this->db = $db;
71 }
72
73
74 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
81 public function add_to_target($mailing_id)
82 {
83 // phpcs:enable
84 global $langs;
85
86 $cibles = array();
87
88 $addDescription = "";
89 $addFilter = "";
90 if (GETPOSTISSET("filter_client_thirdparties") && GETPOST("filter_client_thirdparties") != '-1') {
91 $addFilter .= " AND s.client=".(GETPOSTINT("filter_client_thirdparties"));
92 $addDescription = $langs->trans('ProspectCustomer')."=";
93 if (GETPOST("filter_client_thirdparties") == 0) {
94 $addDescription .= $langs->trans('NorProspectNorCustomer');
95 } elseif (GETPOST("filter_client_thirdparties") == 1) {
96 $addDescription .= $langs->trans('Customer');
97 } elseif (GETPOST("filter_client_thirdparties") == 2) {
98 $addDescription .= $langs->trans('Prospect');
99 } elseif (GETPOST("filter_client_thirdparties") == 3) {
100 $addDescription .= $langs->trans('ProspectCustomer');
101 } else {
102 $addDescription .= "Unknown status ".GETPOST("filter_client_thirdparties");
103 }
104 }
105 if (GETPOSTISSET("filter_supplier_thirdparties") && GETPOST("filter_supplier_thirdparties") != '-1') {
106 $addFilter .= " AND s.fournisseur = ".(GETPOSTINT("filter_supplier_thirdparties"));
107 $addDescription = $langs->trans('Supplier')."=";
108 if (GETPOST("filter_supplier_thirdparties") == 0) {
109 $addDescription .= $langs->trans('No');
110 } elseif (GETPOST("filter_supplier_thirdparties") == 1) {
111 $addDescription .= $langs->trans('Yes');
112 } else {
113 $addDescription .= "Unknown status ".GETPOST("filter_supplier_thirdparties");
114 }
115 }
116 if (GETPOSTISSET("filter_status")) {
117 if (strlen($addDescription) > 0) {
118 $addDescription .= ";";
119 }
120 $addDescription .= $langs->trans("Status")."=";
121 if (GETPOST("filter_status") == '1') {
122 $addFilter .= " AND s.status=1";
123 $addDescription .= $langs->trans("Enabled");
124 } elseif (GETPOST("filter_status") == '0') {
125 $addFilter .= " AND s.status=0";
126 $addDescription .= $langs->trans("Disabled");
127 }
128 }
129 if (GETPOSTISSET("filter_status")) {
130 if (strlen($addDescription) > 0) {
131 $addDescription .= ";";
132 }
133 $addDescription .= $langs->trans("Status")."=";
134 if (GETPOST("filter_status") == '1') {
135 $addFilter .= " AND s.status=1";
136 $addDescription .= $langs->trans("Enabled");
137 } elseif (GETPOST("filter_status") == '0') {
138 $addFilter .= " AND s.status=0";
139 $addDescription .= $langs->trans("Disabled");
140 }
141 }
142 if (GETPOST('default_lang', 'alpha') && GETPOST('default_lang', 'alpha') != '-1') {
143 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
144 $addDescription = $langs->trans('DefaultLang')."=";
145 }
146 if (GETPOST('filter_lang_thirdparties', 'alpha') && GETPOST('filter_lang_thirdparties', 'alpha') != '-1') {
147 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
148 $addDescription = $langs->trans('DefaultLang')."=";
149 }
150
151 // Select the third parties from category
152 if (!GETPOST('filter_thirdparties') || GETPOST('filter_thirdparties') == '-1') {
153 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
154 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
155 $sql .= " WHERE s.email <> ''";
156 $sql .= " AND s.entity IN (".getEntity('societe').")";
157 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
158 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
159 $sql .= $addFilter;
160 } else {
161 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
162 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
163 $sql .= " WHERE s.email <> ''";
164 $sql .= " AND s.entity IN (".getEntity('societe').")";
165 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
166 $sql .= " AND cs.fk_soc = s.rowid";
167 $sql .= " AND c.rowid = cs.fk_categorie";
168 if (GETPOSTINT('filter_thirdparties') > 0) {
169 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
170 }
171 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
172 $sql .= $addFilter;
173 $sql .= " UNION ";
174 $sql .= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
175 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
176 $sql .= " WHERE s.email <> ''";
177 $sql .= " AND s.entity IN (".getEntity('societe').")";
178 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
179 $sql .= " AND cs.fk_soc = s.rowid";
180 $sql .= " AND c.rowid = cs.fk_categorie";
181 if (GETPOSTINT('filter_thirdparties') > 0) {
182 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
183 }
184 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
185 $sql .= $addFilter;
186 }
187 $sql .= " ORDER BY email";
188
189 //print $sql;exit;
190
191 // Stock recipients emails into targets table
192 $result = $this->db->query($sql);
193 if ($result) {
194 $num = $this->db->num_rows($result);
195 $i = 0;
196 $j = 0;
197
198 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
199
200 $old = '';
201 while ($i < $num) {
202 $obj = $this->db->fetch_object($result);
203 if ($old != $obj->email) {
204 $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
205 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
206 $otherTxt .= ";";
207 }
208 $otherTxt .= $addDescription;
209 $cibles[$j] = array(
210 'email' => $obj->email,
211 'fk_contact' => (int) $obj->fk_contact,
212 'lastname' => $obj->name, // For a thirdparty, we must use name
213 'firstname' => '', // For a thirdparty, lastname is ''
214 'other' => $otherTxt,
215 'source_url' => $this->url($obj->id),
216 'source_id' => (int) $obj->id,
217 'source_type' => 'thirdparty'
218 );
219 $old = $obj->email;
220 $j++;
221 }
222
223 $i++;
224 }
225 } else {
226 dol_syslog($this->db->error());
227 $this->error = $this->db->error();
228 return -1;
229 }
230
231 return parent::addTargetsToDatabase($mailing_id, $cibles);
232 }
233
234
243 public function getSqlArrayForStats()
244 {
245 // CHANGE THIS: Optional
246
247 //var $statssql=array();
248 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
249 return array();
250 }
251
252
261 public function getNbOfRecipients($sql = '')
262 {
263 $sql = "SELECT count(distinct(s.email)) as nb";
264 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
265 $sql .= " WHERE s.email <> ''";
266 $sql .= " AND s.entity IN (".getEntity('societe').")";
267 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
268
269 // The query must return a field "nb" to be understood by parent::getNbOfRecipients
270 return parent::getNbOfRecipients($sql);
271 }
272
279 public function formFilter()
280 {
281 global $conf, $langs;
282
283 $langs->load("companies");
284
285 // filter
286 $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat maxwidth200">';
287
288 // Show categories
289 $sql = "SELECT rowid, label, type, visible";
290 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
291 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
292 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
293 $sql .= " AND entity = ".((int) $conf->entity);
294 $sql .= " ORDER BY label";
295
296 //print $sql;
297 $resql = $this->db->query($sql);
298 if ($resql) {
299 $num = $this->db->num_rows($resql);
300
301 if (!isModEnabled("category")) {
302 $num = 0; // Force empty list if category module is not enabled
303 }
304
305 if ($num) {
306 $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
307 } else {
308 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
309 }
310
311 $i = 0;
312 while ($i < $num) {
313 $obj = $this->db->fetch_object($resql);
314
315 $type = '';
316 if ($obj->type == 1) {
317 $type = $langs->trans("Supplier");
318 }
319 if ($obj->type == 2) {
320 $type = $langs->trans("Customer");
321 }
322 $labeltoshow = $obj->label;
323 if ($type) {
324 $labeltoshow .= ' <span class="opacitymedium">('.$type.')</span>';
325 }
326 $s .= '<option value="'.$obj->rowid.'" data-html="'.dol_escape_htmltag($labeltoshow).'">';
327 $s .= $labeltoshow;
328 $s .= '</option>';
329 $i++;
330 }
331 $s .= ajax_combobox("filter_thirdparties");
332 } else {
333 dol_print_error($this->db);
334 }
335
336 $s .= '</select> ';
337
338 // filter_client_thirdparties
339 $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
340 $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
341 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
342 $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
343 }
344 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') && !getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS') && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTSCUSTOMERS')) {
345 $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
346 }
347 if (!getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
348 $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
349 }
350 $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
351
352 $s .= '</select> ';
353 $s .= ajax_combobox("filter_client_thirdparties");
354
355 // filter_supplier_thirdparties
356 $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
357 $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
358 $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
359 $s .= '<option value="0">'.$langs->trans("No").'</option>';
360 $s .= '</select>';
361 $s .= ajax_combobox("filter_supplier_thirdparties");
362
363 // filter_status_thirdparties
364 $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
365 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
366 $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
367 $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
368 $s .= '</select>';
369 $s .= ajax_combobox("filter_status_thirdparties");
370
371 // filter_lang_thirdparties
372 if (getDolGlobalInt('MAIN_MULTILANGS')) {
373 // Choose language
374 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
375 $formadmin = new FormAdmin($this->db);
376 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
377 //$s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
378 $s .= $formadmin->select_language(GETPOST('filter_lang_thirdparties', 'aZ09'), 'filter_lang_thirdparties', 0, array(), $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, array(), 1);
379 }
380
381 return $s;
382 }
383
384
391 public function url($id)
392 {
393 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
394 }
395}
$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 manage a list of personalised recipients for mailing feature.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
url($id)
Can include an URL link on each record provided by selector shown on target page.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
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.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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