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