dolibarr 21.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 MDW <mdeweerd@users.noreply.github.com>
5*
6* This file is an example to follow to add your own email selector inside
7* the Dolibarr email tool.
8* Follow instructions given in README file to know what to change to build
9* your own emailing list selector.
10* Code that need to be changed in this file are marked by "CHANGE THIS" tag.
11*/
12
19include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
20include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
21include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
22
23
28{
29 public $name = 'ThirdPartyAdvancedTargeting';
30 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
31 public $desc = "Third parties";
32 public $require_admin = 0;
33
34 public $require_module = array("none"); // This module should not be displayed as Selector in mailing
35
39 public $picto = 'company';
40
41 public $enabled = 'isModEnabled("societe")';
42
43
49 public function __construct($db)
50 {
51 $this->db = $db;
52 }
53
54
55 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
65 public function add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
66 {
67 // phpcs:enable
68 global $conf, $langs;
69
70 dol_syslog(get_class($this)."::add_to_target_spec socid=".var_export($socid, true).' contactid='.var_export($contactid, true));
71
72 $cibles = array();
73
74 if (($type_of_target == 1) || ($type_of_target == 3)) {
75 // Select the third parties from category
76 if (count($socid) > 0) {
77 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
78 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
79 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
80 $sql .= " AND s.rowid IN (".$this->db->sanitize(implode(',', $socid)).")";
81 if (empty($this->evenunsubscribe)) {
82 $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).")";
83 }
84 $sql .= " ORDER BY email";
85
86 // Stock recipients emails into targets table
87 $result = $this->db->query($sql);
88 if ($result) {
89 $num = $this->db->num_rows($result);
90 $i = 0;
91
92 dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found", LOG_DEBUG);
93
94 while ($i < $num) {
95 $obj = $this->db->fetch_object($result);
96
97 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
98 if (!array_key_exists($obj->email, $cibles)) {
99 $cibles[$obj->email] = array(
100 'email' => $obj->email,
101 'fk_contact' => $obj->fk_contact,
102 'name' => $obj->name,
103 'firstname' => $obj->firstname,
104 'other' => '',
105 'source_url' => $this->url($obj->id, 'thirdparty'),
106 'source_id' => $obj->id,
107 'source_type' => 'thirdparty'
108 );
109 }
110 }
111
112 $i++;
113 }
114 } else {
115 dol_syslog($this->db->error());
116 $this->error = $this->db->error();
117 return -1;
118 }
119 }
120 }
121
122 if (($type_of_target == 1) || ($type_of_target == 2) || ($type_of_target == 4)) {
123 // Select the third parties from category
124 if (count($socid) > 0 || count($contactid) > 0) {
125 $sql = "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
126 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
127 $sql .= " WHERE socp.entity IN (".getEntity('contact').")";
128 if (count($contactid) > 0) {
129 $sql .= " AND socp.rowid IN (".$this->db->sanitize(implode(',', $contactid)).")";
130 }
131 if (count($socid) > 0) {
132 $sql .= " AND socp.fk_soc IN (".$this->db->sanitize(implode(',', $socid)).")";
133 }
134 if (empty($this->evenunsubscribe)) {
135 $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).")";
136 }
137 $sql .= " ORDER BY email";
138
139 // Stock recipients emails into targets table
140 $result = $this->db->query($sql);
141 if ($result) {
142 $num = $this->db->num_rows($result);
143 $i = 0;
144
145 dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found");
146
147 while ($i < $num) {
148 $obj = $this->db->fetch_object($result);
149
150 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
151 if (!array_key_exists($obj->email, $cibles)) {
152 $cibles[$obj->email] = array(
153 'email' => $obj->email,
154 'fk_contact' => $obj->id,
155 'lastname' => $obj->lastname,
156 'firstname' => $obj->firstname,
157 'other' => '',
158 'source_url' => $this->url($obj->id, 'contact'),
159 'source_id' => $obj->id,
160 'source_type' => 'contact'
161 );
162 }
163 }
164
165 $i++;
166 }
167 } else {
168 dol_syslog($this->db->error());
169 $this->error = $this->db->error();
170 return -1;
171 }
172 }
173 }
174
175
176 dol_syslog(get_class($this)."::add_to_target_spec mailing cibles=".var_export($cibles, true), LOG_DEBUG);
177
178 return parent::addTargetsToDatabase($mailing_id, $cibles);
179 }
180
181
190 public function getSqlArrayForStats()
191 {
192 // CHANGE THIS: Optional
193
194 //var $statssql=array();
195 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
196 return array();
197 }
198
199
208 public function getNbOfRecipients($sql = '')
209 {
210 global $conf;
211
212 $sql = "SELECT count(distinct(s.email)) as nb";
213 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
214 $sql .= " WHERE s.email != ''";
215 $sql .= " AND s.entity IN (".getEntity('societe').")";
216 if (empty($this->evenunsubscribe)) {
217 $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).")";
218 }
219
220 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
221 return parent::getNbOfRecipients($sql);
222 }
223
230 public function formFilter()
231 {
232 global $conf, $langs;
233
234 $langs->load("companies");
235
236 $s = '';
237 $s .= '<select name="filter" class="flat">';
238
239 // Show categories
240 $sql = "SELECT rowid, label, type, visible";
241 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
242 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
243 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
244 $sql .= " AND entity = ".$conf->entity;
245 $sql .= " ORDER BY label";
246
247 //print $sql;
248 $resql = $this->db->query($sql);
249 if ($resql) {
250 $num = $this->db->num_rows($resql);
251
252 if (!isModEnabled("category")) {
253 $num = 0; // Force empty list if category module is not enabled
254 }
255
256 if ($num) {
257 $s .= '<option value="0">&nbsp;</option>';
258 } else {
259 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
260 }
261
262 $i = 0;
263 while ($i < $num) {
264 $obj = $this->db->fetch_object($resql);
265
266 $type = '';
267 if ($obj->type == 1) {
268 $type = $langs->trans("Supplier");
269 }
270 if ($obj->type == 2) {
271 $type = $langs->trans("Customer");
272 }
273 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
274 if ($type) {
275 $s .= ' ('.$type.')';
276 }
277 $s .= '</option>';
278 $i++;
279 }
280 } else {
281 dol_print_error($this->db);
282 }
283
284 $s .= '</select>';
285 return $s;
286 }
287
288
296 public function url($id, $type)
297 {
298 if ($type == 'thirdparty') {
299 $companystatic = new Societe($this->db);
300 $companystatic->fetch($id);
301 return $companystatic->getNomUrl(0, '', 0, 1);
302 } elseif ($type == 'contact') {
303 $contactstatic = new Contact($this->db);
304 $contactstatic->fetch($id);
305 return $contactstatic->getNomUrl(0, '', 0, '', -1, 1);
306 }
307 return "";
308 }
309}
$id
Definition account.php:39
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.