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 (GETPOST('default_lang', 'alpha') && GETPOST('default_lang', 'alpha') != '-1') {
130 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
131 $addDescription = $langs->trans('DefaultLang')."=";
132 }
133 if (GETPOST('filter_lang_thirdparties', 'alpha') && GETPOST('filter_lang_thirdparties', 'alpha') != '-1') {
134 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
135 $addDescription = $langs->trans('DefaultLang')."=";
136 }
137
138 // Select the third parties from category
139 if (!GETPOST('filter_thirdparties') || GETPOST('filter_thirdparties') == '-1') {
140 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
141 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
142 $sql .= " WHERE s.email <> ''";
143 $sql .= " AND s.entity IN (".getEntity('societe').")";
144 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
145 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
146 $sql .= $addFilter;
147 } else {
148 $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";
149 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
150 $sql .= " WHERE s.email <> ''";
151 $sql .= " AND s.entity IN (".getEntity('societe').")";
152 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
153 $sql .= " AND cs.fk_soc = s.rowid";
154 $sql .= " AND c.rowid = cs.fk_categorie";
155 if (GETPOSTINT('filter_thirdparties') > 0) {
156 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
157 }
158 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
159 $sql .= $addFilter;
160 $sql .= " UNION ";
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_fournisseur 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 }
174 $sql .= " ORDER BY email";
175
176 //print $sql;exit;
177
178 // Stock recipients emails into targets table
179 $result = $this->db->query($sql);
180 if ($result) {
181 $num = $this->db->num_rows($result);
182 $i = 0;
183 $j = 0;
184
185 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
186
187 $old = '';
188 while ($i < $num) {
189 $obj = $this->db->fetch_object($result);
190 if ($old != $obj->email) {
191 $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
192 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
193 $otherTxt .= ";";
194 }
195 $otherTxt .= $addDescription;
196 $cibles[$j] = array(
197 'email' => $obj->email,
198 'fk_contact' => (int) $obj->fk_contact,
199 'lastname' => $obj->name, // For a thirdparty, we must use name
200 'firstname' => '', // For a thirdparty, lastname is ''
201 'other' => $otherTxt,
202 'source_url' => $this->url($obj->id),
203 'source_id' => (int) $obj->id,
204 'source_type' => 'thirdparty'
205 );
206 $old = $obj->email;
207 $j++;
208 }
209
210 $i++;
211 }
212 } else {
213 dol_syslog($this->db->error());
214 $this->error = $this->db->error();
215 return -1;
216 }
217
218 return parent::addTargetsToDatabase($mailing_id, $cibles);
219 }
220
221
230 public function getSqlArrayForStats()
231 {
232 // CHANGE THIS: Optional
233
234 //var $statssql=array();
235 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
236 return array();
237 }
238
239
248 public function getNbOfRecipients($sql = '')
249 {
250 $sql = "SELECT count(distinct(s.email)) as nb";
251 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
252 $sql .= " WHERE s.email <> ''";
253 $sql .= " AND s.entity IN (".getEntity('societe').")";
254 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
255
256 // The query must return a field "nb" to be understood by parent::getNbOfRecipients
257 return parent::getNbOfRecipients($sql);
258 }
259
266 public function formFilter()
267 {
268 global $conf, $langs;
269
270 $langs->load("companies");
271
272 // filter
273 $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat maxwidth200">';
274
275 // Show categories
276 $sql = "SELECT rowid, label, type, visible";
277 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
278 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
279 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
280 $sql .= " AND entity = ".((int) $conf->entity);
281 $sql .= " ORDER BY label";
282
283 //print $sql;
284 $resql = $this->db->query($sql);
285 if ($resql) {
286 $num = $this->db->num_rows($resql);
287
288 if (!isModEnabled("category")) {
289 $num = 0; // Force empty list if category module is not enabled
290 }
291
292 if ($num) {
293 $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
294 } else {
295 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
296 }
297
298 $i = 0;
299 while ($i < $num) {
300 $obj = $this->db->fetch_object($resql);
301
302 $type = '';
303 if ($obj->type == 1) {
304 $type = $langs->trans("Supplier");
305 }
306 if ($obj->type == 2) {
307 $type = $langs->trans("Customer");
308 }
309 $labeltoshow = $obj->label;
310 if ($type) {
311 $labeltoshow .= ' <span class="opacitymedium">('.$type.')</span>';
312 }
313 $s .= '<option value="'.$obj->rowid.'" data-html="'.dol_escape_htmltag($labeltoshow).'">';
314 $s .= $labeltoshow;
315 $s .= '</option>';
316 $i++;
317 }
318 $s .= ajax_combobox("filter_thirdparties");
319 } else {
320 dol_print_error($this->db);
321 }
322
323 $s .= '</select> ';
324
325 // filter_client_thirdparties
326 $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
327 $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
328 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
329 $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
330 }
331 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') && !getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS') && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTSCUSTOMERS')) {
332 $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
333 }
334 if (!getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
335 $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
336 }
337 $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
338
339 $s .= '</select> ';
340 $s .= ajax_combobox("filter_client_thirdparties");
341
342 // filter_supplier_thirdparties
343 $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
344 $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
345 $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
346 $s .= '<option value="0">'.$langs->trans("No").'</option>';
347 $s .= '</select>';
348 $s .= ajax_combobox("filter_supplier_thirdparties");
349
350 // filter_status_thirdparties
351 $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
352 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
353 $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
354 $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
355 $s .= '</select>';
356 $s .= ajax_combobox("filter_status_thirdparties");
357
358 // filter_lang_thirdparties
359 if (getDolGlobalInt('MAIN_MULTILANGS')) {
360 // Choose language
361 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
362 $formadmin = new FormAdmin($this->db);
363 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
364 //$s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
365 $s .= $formadmin->select_language(GETPOST('filter_lang_thirdparties', 'aZ09'), 'filter_lang_thirdparties', 0, array(), $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, array(), 1);
366 }
367
368 return $s;
369 }
370
371
378 public function url($id)
379 {
380 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
381 }
382}
$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='', $placeholder='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:477
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181