dolibarr 21.0.0-beta
thirdparties.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018-2018 Andre Schild <a.schild@aarboard.ch>
3 * Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This file is an example to follow to add your own email selector inside
9 * the Dolibarr email tool.
10 * Follow instructions given in README file to know what to change to build
11 * your own emailing list selector.
12 * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
13 */
14
21include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
22
23
28{
32 public $name = 'ThirdPartiesByCategories';
33
37 public $desc = "Third parties (by categories)";
38
42 public $require_admin = 0;
43
47 public $require_module = array("societe");
48
52 public $enabled = 'isModEnabled("societe")';
53
57 public $picto = 'company';
58
59
65 public function __construct($db)
66 {
67 global $langs;
68 $langs->load("companies");
69
70 $this->db = $db;
71 }
72
73
74 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
81 public function add_to_target($mailing_id)
82 {
83 // phpcs:enable
84 global $conf, $langs;
85
86 $cibles = array();
87
88 $addDescription = "";
89 $addFilter = "";
90 if (GETPOSTISSET("filter_client_thirdparties") && GETPOST("filter_client_thirdparties") != '-1') {
91 $addFilter .= " AND s.client=".(GETPOSTINT("filter_client_thirdparties"));
92 $addDescription = $langs->trans('ProspectCustomer')."=";
93 if (GETPOST("filter_client_thirdparties") == 0) {
94 $addDescription .= $langs->trans('NorProspectNorCustomer');
95 } elseif (GETPOST("filter_client_thirdparties") == 1) {
96 $addDescription .= $langs->trans('Customer');
97 } elseif (GETPOST("filter_client_thirdparties") == 2) {
98 $addDescription .= $langs->trans('Prospect');
99 } elseif (GETPOST("filter_client_thirdparties") == 3) {
100 $addDescription .= $langs->trans('ProspectCustomer');
101 } else {
102 $addDescription .= "Unknown status ".GETPOST("filter_client_thirdparties");
103 }
104 }
105 if (GETPOSTISSET("filter_supplier_thirdparties") && GETPOST("filter_supplier_thirdparties") != '-1') {
106 $addFilter .= " AND s.fournisseur = ".(GETPOSTINT("filter_supplier_thirdparties"));
107 $addDescription = $langs->trans('Supplier')."=";
108 if (GETPOST("filter_supplier_thirdparties") == 0) {
109 $addDescription .= $langs->trans('No');
110 } elseif (GETPOST("filter_supplier_thirdparties") == 1) {
111 $addDescription .= $langs->trans('Yes');
112 } else {
113 $addDescription .= "Unknown status ".GETPOST("filter_supplier_thirdparties");
114 }
115 }
116 if (GETPOSTISSET("filter_status")) {
117 if (strlen($addDescription) > 0) {
118 $addDescription .= ";";
119 }
120 $addDescription .= $langs->trans("Status")."=";
121 if (GETPOST("filter_status") == '1') {
122 $addFilter .= " AND s.status=1";
123 $addDescription .= $langs->trans("Enabled");
124 } elseif (GETPOST("filter_status") == '0') {
125 $addFilter .= " AND s.status=0";
126 $addDescription .= $langs->trans("Disabled");
127 }
128 }
129 if (GETPOSTISSET("filter_status")) {
130 if (strlen($addDescription) > 0) {
131 $addDescription .= ";";
132 }
133 $addDescription .= $langs->trans("Status")."=";
134 if (GETPOST("filter_status") == '1') {
135 $addFilter .= " AND s.status=1";
136 $addDescription .= $langs->trans("Enabled");
137 } elseif (GETPOST("filter_status") == '0') {
138 $addFilter .= " AND s.status=0";
139 $addDescription .= $langs->trans("Disabled");
140 }
141 }
142 if (GETPOST('default_lang', 'alpha') && GETPOST('default_lang', 'alpha') != '-1') {
143 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
144 $addDescription = $langs->trans('DefaultLang')."=";
145 }
146 if (GETPOST('filter_lang_thirdparties', 'alpha') && GETPOST('filter_lang_thirdparties', 'alpha') != '-1') {
147 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
148 $addDescription = $langs->trans('DefaultLang')."=";
149 }
150
151 // Select the third parties from category
152 if (!GETPOST('filter_thirdparties') || GETPOST('filter_thirdparties') == '-1') {
153 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
154 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
155 $sql .= " WHERE s.email <> ''";
156 $sql .= " AND s.entity IN (".getEntity('societe').")";
157 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
158 if (empty($this->evenunsubscribe)) {
159 $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
160 }
161 $sql .= $addFilter;
162 } else {
163 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
164 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
165 $sql .= " WHERE s.email <> ''";
166 $sql .= " AND s.entity IN (".getEntity('societe').")";
167 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
168 $sql .= " AND cs.fk_soc = s.rowid";
169 $sql .= " AND c.rowid = cs.fk_categorie";
170 if (GETPOSTINT('filter_thirdparties') > 0) {
171 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
172 }
173 if (empty($this->evenunsubscribe)) {
174 $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
175 }
176 $sql .= $addFilter;
177 $sql .= " UNION ";
178 $sql .= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
179 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
180 $sql .= " WHERE s.email <> ''";
181 $sql .= " AND s.entity IN (".getEntity('societe').")";
182 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
183 $sql .= " AND cs.fk_soc = s.rowid";
184 $sql .= " AND c.rowid = cs.fk_categorie";
185 if (GETPOSTINT('filter_thirdparties') > 0) {
186 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
187 }
188 if (empty($this->evenunsubscribe)) {
189 $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
190 }
191 $sql .= $addFilter;
192 }
193 $sql .= " ORDER BY email";
194
195 //print $sql;exit;
196
197 // Stock recipients emails into targets table
198 $result = $this->db->query($sql);
199 if ($result) {
200 $num = $this->db->num_rows($result);
201 $i = 0;
202 $j = 0;
203
204 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
205
206 $old = '';
207 while ($i < $num) {
208 $obj = $this->db->fetch_object($result);
209 if ($old != $obj->email) {
210 $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
211 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
212 $otherTxt .= ";";
213 }
214 $otherTxt .= $addDescription;
215 $cibles[$j] = array(
216 'email' => $obj->email,
217 'fk_contact' => (int) $obj->fk_contact,
218 'lastname' => $obj->name, // For a thirdparty, we must use name
219 'firstname' => '', // For a thirdparty, lastname is ''
220 'other' => $otherTxt,
221 'source_url' => $this->url($obj->id),
222 'source_id' => (int) $obj->id,
223 'source_type' => 'thirdparty'
224 );
225 $old = $obj->email;
226 $j++;
227 }
228
229 $i++;
230 }
231 } else {
232 dol_syslog($this->db->error());
233 $this->error = $this->db->error();
234 return -1;
235 }
236
237 return parent::addTargetsToDatabase($mailing_id, $cibles);
238 }
239
240
249 public function getSqlArrayForStats()
250 {
251 // CHANGE THIS: Optional
252
253 //var $statssql=array();
254 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
255 return array();
256 }
257
258
267 public function getNbOfRecipients($sql = '')
268 {
269 global $conf;
270
271 $sql = "SELECT count(distinct(s.email)) as nb";
272 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
273 $sql .= " WHERE s.email <> ''";
274 $sql .= " AND s.entity IN (".getEntity('societe').")";
275 if (empty($this->evenunsubscribe)) {
276 $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).")";
277 }
278
279 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
280 return parent::getNbOfRecipients($sql);
281 }
282
289 public function formFilter()
290 {
291 global $conf, $langs;
292
293 $langs->load("companies");
294
295 // filter
296 $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat maxwidth200">';
297
298 // Show categories
299 $sql = "SELECT rowid, label, type, visible";
300 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
301 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
302 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
303 $sql .= " AND entity = ".$conf->entity;
304 $sql .= " ORDER BY label";
305
306 //print $sql;
307 $resql = $this->db->query($sql);
308 if ($resql) {
309 $num = $this->db->num_rows($resql);
310
311 if (!isModEnabled("category")) {
312 $num = 0; // Force empty list if category module is not enabled
313 }
314
315 if ($num) {
316 $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
317 } else {
318 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
319 }
320
321 $i = 0;
322 while ($i < $num) {
323 $obj = $this->db->fetch_object($resql);
324
325 $type = '';
326 if ($obj->type == 1) {
327 $type = $langs->trans("Supplier");
328 }
329 if ($obj->type == 2) {
330 $type = $langs->trans("Customer");
331 }
332 $labeltoshow = $obj->label;
333 if ($type) {
334 $labeltoshow .= ' <span class="opacitymedium">('.$type.')</span>';
335 }
336 $s .= '<option value="'.$obj->rowid.'" data-html="'.dol_escape_htmltag($labeltoshow).'">';
337 $s .= $labeltoshow;
338 $s .= '</option>';
339 $i++;
340 }
341 $s .= ajax_combobox("filter_thirdparties");
342 } else {
343 dol_print_error($this->db);
344 }
345
346 $s .= '</select> ';
347
348 // filter_client_thirdparties
349 $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
350 $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
351 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
352 $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
353 }
354 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') && !getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS') && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTSCUSTOMERS')) {
355 $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
356 }
357 if (!getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
358 $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
359 }
360 $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
361
362 $s .= '</select> ';
363 $s .= ajax_combobox("filter_client_thirdparties");
364
365 // filter_supplier_thirdparties
366 $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
367 $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
368 $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
369 $s .= '<option value="0">'.$langs->trans("No").'</option>';
370 $s .= '</select>';
371 $s .= ajax_combobox("filter_supplier_thirdparties");
372
373 // filter_status_thirdparties
374 $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
375 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
376 $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
377 $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
378 $s .= '</select>';
379 $s .= ajax_combobox("filter_status_thirdparties");
380
381 // filter_lang_thirdparties
382 if (getDolGlobalInt('MAIN_MULTILANGS')) {
383 // Choose language
384 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
385 $formadmin = new FormAdmin($this->db);
386 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
387 //$s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
388 $s .= $formadmin->select_language(GETPOST('filter_lang_thirdparties', 'aZ09'), 'filter_lang_thirdparties', 0, null, $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, null, 1);
389 }
390
391 return $s;
392 }
393
394
401 public function url($id)
402 {
403 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
404 }
405}
$id
Definition account.php:48
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:459
Class to generate html code for admin pages.
Parent class of emailing target selectors modules.
Class to manage a list of personalised recipients for mailing feature.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
url($id)
Can include an URL link on each record provided by selector shown on target page.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79