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