dolibarr 21.0.0-beta
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 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, $langs;
86
87 dol_syslog(get_class($this)."::add_to_target_spec socid=".var_export($socid, true).' contactid='.var_export($contactid, true));
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 OUTER 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
193 dol_syslog(get_class($this)."::add_to_target_spec mailing cibles=".var_export($cibles, true), LOG_DEBUG);
194
195 return parent::addTargetsToDatabase($mailing_id, $cibles);
196 }
197
198
207 public function getSqlArrayForStats()
208 {
209 // CHANGE THIS: Optional
210
211 //var $statssql=array();
212 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
213 return array();
214 }
215
216
225 public function getNbOfRecipients($sql = '')
226 {
227 global $conf;
228
229 $sql = "SELECT count(distinct(s.email)) as nb";
230 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
231 $sql .= " WHERE s.email != ''";
232 $sql .= " AND s.entity IN (".getEntity('societe').")";
233 if (empty($this->evenunsubscribe)) {
234 $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).")";
235 }
236
237 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
238 return parent::getNbOfRecipients($sql);
239 }
240
247 public function formFilter()
248 {
249 global $conf, $langs;
250
251 $langs->load("companies");
252
253 $s = '';
254 $s .= '<select name="filter" class="flat">';
255
256 // Show categories
257 $sql = "SELECT rowid, label, type, visible";
258 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
259 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
260 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
261 $sql .= " AND entity = ".$conf->entity;
262 $sql .= " ORDER BY label";
263
264 //print $sql;
265 $resql = $this->db->query($sql);
266 if ($resql) {
267 $num = $this->db->num_rows($resql);
268
269 if (!isModEnabled("category")) {
270 $num = 0; // Force empty list if category module is not enabled
271 }
272
273 if ($num) {
274 $s .= '<option value="0">&nbsp;</option>';
275 } else {
276 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
277 }
278
279 $i = 0;
280 while ($i < $num) {
281 $obj = $this->db->fetch_object($resql);
282
283 $type = '';
284 if ($obj->type == 1) {
285 $type = $langs->trans("Supplier");
286 }
287 if ($obj->type == 2) {
288 $type = $langs->trans("Customer");
289 }
290 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
291 if ($type) {
292 $s .= ' ('.$type.')';
293 }
294 $s .= '</option>';
295 $i++;
296 }
297 } else {
298 dol_print_error($this->db);
299 }
300
301 $s .= '</select>';
302 return $s;
303 }
304
305
313 public function url($id, $type)
314 {
315 if ($type == 'thirdparty') {
316 $companystatic = new Societe($this->db);
317 $companystatic->fetch($id);
318 return $companystatic->getNomUrl(0, '', 0, 1);
319 } elseif ($type == 'contact') {
320 $contactstatic = new Contact($this->db);
321 $contactstatic->fetch($id);
322 return $contactstatic->getNomUrl(0, '', 0, '', -1, 1);
323 }
324 return "";
325 }
326}
$id
Definition account.php:48
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.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79