dolibarr 24.0.1
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 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 global $conf;
86
87 dol_syslog(get_class($this)."::add_to_target_spec socid=".formatLogObject($socid).' contactid='.formatLogObject($contactid));
88
89 $cibles = array();
90
91 if (($type_of_target == 1) || ($type_of_target == 3)) {
92 // Select the third parties from category
93 if (count($socid) > 0) {
94 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
95 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
96 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
97 $sql .= " AND s.rowid IN (".$this->db->sanitize(implode(',', $socid)).")";
98 if (empty($this->evenunsubscribe)) {
99 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
100 }
101 $sql .= " ORDER BY email";
102
103 // Stock recipients emails into targets table
104 $result = $this->db->query($sql);
105 if ($result) {
106 $num = $this->db->num_rows($result);
107 $i = 0;
108
109 dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found", LOG_DEBUG);
110
111 while ($i < $num) {
112 $obj = $this->db->fetch_object($result);
113
114 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
115 if (!array_key_exists($obj->email, $cibles)) {
116 $cibles[$obj->email] = array(
117 'email' => $obj->email,
118 'fk_contact' => (int) $obj->fk_contact,
119 'name' => $obj->name,
120 'firstname' => $obj->firstname,
121 'other' => '',
122 'source_url' => $this->url($obj->id, 'thirdparty'),
123 'source_id' => (int) $obj->id,
124 'source_type' => 'thirdparty'
125 );
126 }
127 }
128
129 $i++;
130 }
131 } else {
132 dol_syslog($this->db->error());
133 $this->error = $this->db->error();
134 return -1;
135 }
136 }
137 }
138
139 if (($type_of_target == 1) || ($type_of_target == 2) || ($type_of_target == 4)) {
140 // Select the third parties from category
141 if (count($socid) > 0 || count($contactid) > 0) {
142 $sql = "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
143 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
144 $sql .= " WHERE socp.entity IN (".getEntity('contact').")";
145 if (count($contactid) > 0) {
146 $sql .= " AND socp.rowid IN (".$this->db->sanitize(implode(',', $contactid)).")";
147 }
148 if (count($socid) > 0) {
149 $sql .= " AND socp.fk_soc IN (".$this->db->sanitize(implode(',', $socid)).")";
150 }
151 if (empty($this->evenunsubscribe)) {
152 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = socp.email and mu.entity = ".((int) $conf->entity).")";
153 }
154 $sql .= " ORDER BY email";
155
156 // Stock recipients emails into targets table
157 $result = $this->db->query($sql);
158 if ($result) {
159 $num = $this->db->num_rows($result);
160 $i = 0;
161
162 dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found");
163
164 while ($i < $num) {
165 $obj = $this->db->fetch_object($result);
166
167 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
168 if (!array_key_exists($obj->email, $cibles)) {
169 $cibles[$obj->email] = array(
170 'email' => $obj->email,
171 'fk_contact' => (int) $obj->id,
172 'lastname' => $obj->lastname,
173 'firstname' => $obj->firstname,
174 'other' => '',
175 'source_url' => $this->url($obj->id, 'contact'),
176 'source_id' => (int) $obj->id,
177 'source_type' => 'contact'
178 );
179 }
180 }
181
182 $i++;
183 }
184 } else {
185 dol_syslog($this->db->error());
186 $this->error = $this->db->error();
187 return -1;
188 }
189 }
190 }
191
192 dol_syslog(get_class($this)."::add_to_target_spec mailing cibles=".formatLogObject($cibles), LOG_DEBUG);
193
194 return parent::addTargetsToDatabase($mailing_id, $cibles);
195 }
196
197
206 public function getSqlArrayForStats()
207 {
208 // CHANGE THIS: Optional
209
210 //var $statssql=array();
211 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
212 return array();
213 }
214
215
224 public function getNbOfRecipients($sql = '')
225 {
226 global $conf;
227
228 $sql = "SELECT count(distinct(s.email)) as nb";
229 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
230 $sql .= " WHERE s.email != ''";
231 $sql .= " AND s.entity IN (".getEntity('societe').")";
232 if (empty($this->evenunsubscribe)) {
233 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
234 }
235
236 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
237 return parent::getNbOfRecipients($sql);
238 }
239
246 public function formFilter()
247 {
248 global $conf, $langs;
249
250 $langs->load("companies");
251
252 $s = '';
253 $s .= '<select name="filter" class="flat">';
254
255 // Show categories
256 $sql = "SELECT rowid, label, type, visible";
257 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
258 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
259 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
260 $sql .= " AND entity = ".((int) $conf->entity);
261 $sql .= " ORDER BY label";
262
263 //print $sql;
264 $resql = $this->db->query($sql);
265 if ($resql) {
266 $num = $this->db->num_rows($resql);
267
268 if (!isModEnabled("category")) {
269 $num = 0; // Force empty list if category module is not enabled
270 }
271
272 if ($num) {
273 $s .= '<option value="0">&nbsp;</option>';
274 } else {
275 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
276 }
277
278 $i = 0;
279 while ($i < $num) {
280 $obj = $this->db->fetch_object($resql);
281
282 $type = '';
283 if ($obj->type == 1) {
284 $type = $langs->trans("Supplier");
285 }
286 if ($obj->type == 2) {
287 $type = $langs->trans("Customer");
288 }
289 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
290 if ($type) {
291 $s .= ' ('.$type.')';
292 }
293 $s .= '</option>';
294 $i++;
295 }
296 } else {
297 dol_print_error($this->db);
298 }
299
300 $s .= '</select>';
301 return $s;
302 }
303
304
312 public function url($id, $type)
313 {
314 $s = "";
315 if ($type == 'thirdparty') {
316 $companystatic = new Societe($this->db);
317 $companystatic->fetch($id);
318 $s = $companystatic->getNomUrl(0, 'nolink', 32, 1, 0);
319 } elseif ($type == 'contact') {
320 $contactstatic = new Contact($this->db);
321 $contactstatic->fetch($id);
322 $s = $contactstatic->getNomUrl(0, 'nolink', 1, '', 0, 32);
323 }
324
325 // When link is too long (for example because of hook in getNomUrl that complete the url), we return empty string.
326 // Link is still possible with new source_id and source_type in db llx_mailing_cibles.
327 if (strlen($s) > 255) {
328 $s = "";
329 }
330
331 return $s;
332 }
333}
$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.
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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.