dolibarr 25.0.0-alpha
advthirdparties.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
6*
7* This file is an example to follow to add your own email selector inside
8* the Dolibarr email tool.
9* Follow instructions given in README file to know what to change to build
10* your own emailing list selector.
11* Code that need to be changed in this file are marked by "CHANGE THIS" tag.
12*/
13
20include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
21include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
22include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
23
24
29{
33 public $name = 'ThirdPartyAdvancedTargeting';
34
38 public $desc = "Third parties";
39
43 public $require_admin = 0;
44
48 public $require_module = array("none"); // This module should not be displayed as Selector in mailing
49
53 public $picto = 'company';
54
58 public $enabled = 'isModEnabled("societe")';
59
60
66 public function __construct($db)
67 {
68 $this->db = $db;
69 }
70
71
72 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
82 public function add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
83 {
84 // phpcs:enable
85 dol_syslog(get_class($this)."::add_to_target_spec socid=".formatLogObject($socid).' contactid='.formatLogObject($contactid));
86
87 $cibles = array();
88
89 if (($type_of_target == 1) || ($type_of_target == 3)) {
90 // Select the third parties from category
91 if (count($socid) > 0) {
92 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
93 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
94 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
95 $sql .= " AND s.rowid IN (".$this->db->sanitize(implode(',', $socid)).")";
96 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
97 $sql .= " ORDER BY email";
98
99 // Stock recipients emails into targets table
100 $result = $this->db->query($sql);
101 if ($result) {
102 $num = $this->db->num_rows($result);
103 $i = 0;
104
105 dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found", LOG_DEBUG);
106
107 while ($i < $num) {
108 $obj = $this->db->fetch_object($result);
109
110 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
111 if (!array_key_exists($obj->email, $cibles)) {
112 $cibles[$obj->email] = array(
113 'email' => $obj->email,
114 'fk_contact' => (int) $obj->fk_contact,
115 'name' => $obj->name,
116 'firstname' => $obj->firstname,
117 'other' => '',
118 'source_url' => $this->url($obj->id, 'thirdparty'),
119 'source_id' => (int) $obj->id,
120 'source_type' => 'thirdparty'
121 );
122 }
123 }
124
125 $i++;
126 }
127 } else {
128 dol_syslog($this->db->error());
129 $this->error = $this->db->error();
130 return -1;
131 }
132 }
133 }
134
135 if (($type_of_target == 1) || ($type_of_target == 2) || ($type_of_target == 4)) {
136 // Select the third parties from category
137 if (count($socid) > 0 || count($contactid) > 0) {
138 $sql = "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
139 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
140 $sql .= " WHERE socp.entity IN (".getEntity('contact').")";
141 if (count($contactid) > 0) {
142 $sql .= " AND socp.rowid IN (".$this->db->sanitize(implode(',', $contactid)).")";
143 }
144 if (count($socid) > 0) {
145 $sql .= " AND socp.fk_soc IN (".$this->db->sanitize(implode(',', $socid)).")";
146 }
147 $sql .= $this->getSqlToExcludeUnsubscribed('socp.email');
148 $sql .= " ORDER BY email";
149
150 // Stock recipients emails into targets table
151 $result = $this->db->query($sql);
152 if ($result) {
153 $num = $this->db->num_rows($result);
154 $i = 0;
155
156 dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found");
157
158 while ($i < $num) {
159 $obj = $this->db->fetch_object($result);
160
161 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
162 if (!array_key_exists($obj->email, $cibles)) {
163 $cibles[$obj->email] = array(
164 'email' => $obj->email,
165 'fk_contact' => (int) $obj->id,
166 'lastname' => $obj->lastname,
167 'firstname' => $obj->firstname,
168 'other' => '',
169 'source_url' => $this->url($obj->id, 'contact'),
170 'source_id' => (int) $obj->id,
171 'source_type' => 'contact'
172 );
173 }
174 }
175
176 $i++;
177 }
178 } else {
179 dol_syslog($this->db->error());
180 $this->error = $this->db->error();
181 return -1;
182 }
183 }
184 }
185
186 dol_syslog(get_class($this)."::add_to_target_spec mailing cibles=".formatLogObject($cibles), LOG_DEBUG);
187
188 return parent::addTargetsToDatabase($mailing_id, $cibles);
189 }
190
191
200 public function getSqlArrayForStats()
201 {
202 // CHANGE THIS: Optional
203
204 //var $statssql=array();
205 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
206 return array();
207 }
208
209
218 public function getNbOfRecipients($sql = '')
219 {
220 $sql = "SELECT count(distinct(s.email)) as nb";
221 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
222 $sql .= " WHERE s.email != ''";
223 $sql .= " AND s.entity IN (".getEntity('societe').")";
224 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
225
226 // The query must return a field "nb" to be understood by parent::getNbOfRecipients
227 return parent::getNbOfRecipients($sql);
228 }
229
236 public function formFilter()
237 {
238 global $conf, $langs;
239
240 $langs->load("companies");
241
242 $s = '';
243 $s .= '<select name="filter" class="flat">';
244
245 // Show categories
246 $sql = "SELECT rowid, label, type, visible";
247 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
248 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
249 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
250 $sql .= " AND entity = ".((int) $conf->entity);
251 $sql .= " ORDER BY label";
252
253 //print $sql;
254 $resql = $this->db->query($sql);
255 if ($resql) {
256 $num = $this->db->num_rows($resql);
257
258 if (!isModEnabled("category")) {
259 $num = 0; // Force empty list if category module is not enabled
260 }
261
262 if ($num) {
263 $s .= '<option value="0">&nbsp;</option>';
264 } else {
265 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
266 }
267
268 $i = 0;
269 while ($i < $num) {
270 $obj = $this->db->fetch_object($resql);
271
272 $type = '';
273 if ($obj->type == 1) {
274 $type = $langs->trans("Supplier");
275 }
276 if ($obj->type == 2) {
277 $type = $langs->trans("Customer");
278 }
279 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
280 if ($type) {
281 $s .= ' ('.$type.')';
282 }
283 $s .= '</option>';
284 $i++;
285 }
286 } else {
287 dol_print_error($this->db);
288 }
289
290 $s .= '</select>';
291 return $s;
292 }
293
294
302 public function url($id, $type)
303 {
304 $s = "";
305 if ($type == 'thirdparty') {
306 $companystatic = new Societe($this->db);
307 $companystatic->fetch($id);
308 $s = $companystatic->getNomUrl(0, 'nolink', 32, 1, 0);
309 } elseif ($type == 'contact') {
310 $contactstatic = new Contact($this->db);
311 $contactstatic->fetch($id);
312 $s = $contactstatic->getNomUrl(0, 'nolink', 1, '', 0, 32);
313 }
314
315 // When link is too long (for example because of hook in getNomUrl that complete the url), we return empty string.
316 // Link is still possible with new source_id and source_type in db llx_mailing_cibles.
317 if (strlen($s) > 255) {
318 $s = "";
319 }
320
321 return $s;
322 }
323}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class to manage contact/addresses.
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 third parties objects (customers, suppliers, prospects...)
Class to manage a list of personalised recipients for mailing feature.
url($id, $type)
Can include an URL link on each record provided by selector shown on target page.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
This is the main function that returns the array of emails.
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:
if(!function_exists( 'utf8_encode')) if(!function_exists('utf8_decode')) if(!function_exists( 'str_starts_with')) if(!function_exists('str_ends_with')) if(!function_exists( 'str_contains')) formatLogObject($data)
Return a string serialized to be output on log with dol_syslog() An option allow to output log in one...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...