dolibarr 21.0.0-alpha
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 *
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';
21
22
27{
28 public $name = 'ThirdPartiesByCategories';
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 (by categories)";
31 public $require_admin = 0;
32
33 public $require_module = array("societe"); // This module allows to select by categories must be also enabled if category module is not activated
34
35 public $enabled = 'isModEnabled("societe")';
36
40 public $picto = 'company';
41
42
48 public function __construct($db)
49 {
50 global $conf, $langs;
51 $langs->load("companies");
52
53 $this->db = $db;
54 }
55
56
57 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
64 public function add_to_target($mailing_id)
65 {
66 // phpcs:enable
67 global $conf, $langs;
68
69 $cibles = array();
70
71 $addDescription = "";
72 $addFilter = "";
73 if (GETPOSTISSET("filter_client_thirdparties") && GETPOST("filter_client_thirdparties") != '-1') {
74 $addFilter .= " AND s.client=".(GETPOSTINT("filter_client_thirdparties"));
75 $addDescription = $langs->trans('ProspectCustomer')."=";
76 if (GETPOST("filter_client_thirdparties") == 0) {
77 $addDescription .= $langs->trans('NorProspectNorCustomer');
78 } elseif (GETPOST("filter_client_thirdparties") == 1) {
79 $addDescription .= $langs->trans('Customer');
80 } elseif (GETPOST("filter_client_thirdparties") == 2) {
81 $addDescription .= $langs->trans('Prospect');
82 } elseif (GETPOST("filter_client_thirdparties") == 3) {
83 $addDescription .= $langs->trans('ProspectCustomer');
84 } else {
85 $addDescription .= "Unknown status ".GETPOST("filter_client_thirdparties");
86 }
87 }
88 if (GETPOSTISSET("filter_supplier_thirdparties") && GETPOST("filter_supplier_thirdparties") != '-1') {
89 $addFilter .= " AND s.fournisseur = ".(GETPOSTINT("filter_supplier_thirdparties"));
90 $addDescription = $langs->trans('Supplier')."=";
91 if (GETPOST("filter_supplier_thirdparties") == 0) {
92 $addDescription .= $langs->trans('No');
93 } elseif (GETPOST("filter_supplier_thirdparties") == 1) {
94 $addDescription .= $langs->trans('Yes');
95 } else {
96 $addDescription .= "Unknown status ".GETPOST("filter_supplier_thirdparties");
97 }
98 }
99 if (GETPOSTISSET("filter_status")) {
100 if (strlen($addDescription) > 0) {
101 $addDescription .= ";";
102 }
103 $addDescription .= $langs->trans("Status")."=";
104 if (GETPOST("filter_status") == '1') {
105 $addFilter .= " AND s.status=1";
106 $addDescription .= $langs->trans("Enabled");
107 } elseif (GETPOST("filter_status") == '0') {
108 $addFilter .= " AND s.status=0";
109 $addDescription .= $langs->trans("Disabled");
110 }
111 }
112 if (GETPOSTISSET("filter_status")) {
113 if (strlen($addDescription) > 0) {
114 $addDescription .= ";";
115 }
116 $addDescription .= $langs->trans("Status")."=";
117 if (GETPOST("filter_status") == '1') {
118 $addFilter .= " AND s.status=1";
119 $addDescription .= $langs->trans("Enabled");
120 } elseif (GETPOST("filter_status") == '0') {
121 $addFilter .= " AND s.status=0";
122 $addDescription .= $langs->trans("Disabled");
123 }
124 }
125 if (GETPOST('default_lang', 'alpha') && GETPOST('default_lang', 'alpha') != '-1') {
126 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
127 $addDescription = $langs->trans('DefaultLang')."=";
128 }
129 if (GETPOST('filter_lang_thirdparties', 'alpha') && GETPOST('filter_lang_thirdparties', 'alpha') != '-1') {
130 $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
131 $addDescription = $langs->trans('DefaultLang')."=";
132 }
133
134 // Select the third parties from category
135 if (!GETPOST('filter_thirdparties') || GETPOST('filter_thirdparties') == '-1') {
136 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
137 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
138 $sql .= " WHERE s.email <> ''";
139 $sql .= " AND s.entity IN (".getEntity('societe').")";
140 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
141 if (empty($this->evenunsubscribe)) {
142 $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
143 }
144 $sql .= $addFilter;
145 } else {
146 $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";
147 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
148 $sql .= " WHERE s.email <> ''";
149 $sql .= " AND s.entity IN (".getEntity('societe').")";
150 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
151 $sql .= " AND cs.fk_soc = s.rowid";
152 $sql .= " AND c.rowid = cs.fk_categorie";
153 if (GETPOSTINT('filter_thirdparties') > 0) {
154 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
155 }
156 if (empty($this->evenunsubscribe)) {
157 $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
158 }
159 $sql .= $addFilter;
160 $sql .= " UNION ";
161 $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";
162 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
163 $sql .= " WHERE s.email <> ''";
164 $sql .= " AND s.entity IN (".getEntity('societe').")";
165 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
166 $sql .= " AND cs.fk_soc = s.rowid";
167 $sql .= " AND c.rowid = cs.fk_categorie";
168 if (GETPOSTINT('filter_thirdparties') > 0) {
169 $sql .= " AND c.rowid=".(GETPOSTINT('filter_thirdparties'));
170 }
171 if (empty($this->evenunsubscribe)) {
172 $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
173 }
174 $sql .= $addFilter;
175 }
176 $sql .= " ORDER BY email";
177
178 //print $sql;exit;
179
180 // Stock recipients emails into targets table
181 $result = $this->db->query($sql);
182 if ($result) {
183 $num = $this->db->num_rows($result);
184 $i = 0;
185 $j = 0;
186
187 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
188
189 $old = '';
190 while ($i < $num) {
191 $obj = $this->db->fetch_object($result);
192 if ($old != $obj->email) {
193 $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
194 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
195 $otherTxt .= ";";
196 }
197 $otherTxt .= $addDescription;
198 $cibles[$j] = array(
199 'email' => $obj->email,
200 'fk_contact' => $obj->fk_contact,
201 'lastname' => $obj->name, // For a thirdparty, we must use name
202 'firstname' => '', // For a thirdparty, lastname is ''
203 'other' => $otherTxt,
204 'source_url' => $this->url($obj->id),
205 'source_id' => $obj->id,
206 'source_type' => 'thirdparty'
207 );
208 $old = $obj->email;
209 $j++;
210 }
211
212 $i++;
213 }
214 } else {
215 dol_syslog($this->db->error());
216 $this->error = $this->db->error();
217 return -1;
218 }
219
220 return parent::addTargetsToDatabase($mailing_id, $cibles);
221 }
222
223
232 public function getSqlArrayForStats()
233 {
234 // CHANGE THIS: Optional
235
236 //var $statssql=array();
237 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
238 return array();
239 }
240
241
250 public function getNbOfRecipients($sql = '')
251 {
252 global $conf;
253
254 $sql = "SELECT count(distinct(s.email)) as nb";
255 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
256 $sql .= " WHERE s.email <> ''";
257 $sql .= " AND s.entity IN (".getEntity('societe').")";
258 if (empty($this->evenunsubscribe)) {
259 $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).")";
260 }
261
262 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
263 return parent::getNbOfRecipients($sql);
264 }
265
272 public function formFilter()
273 {
274 global $conf, $langs;
275
276 $langs->load("companies");
277
278 // filter
279 $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat maxwidth200">';
280
281 // Show categories
282 $sql = "SELECT rowid, label, type, visible";
283 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
284 $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
285 // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
286 $sql .= " AND entity = ".$conf->entity;
287 $sql .= " ORDER BY label";
288
289 //print $sql;
290 $resql = $this->db->query($sql);
291 if ($resql) {
292 $num = $this->db->num_rows($resql);
293
294 if (!isModEnabled("category")) {
295 $num = 0; // Force empty list if category module is not enabled
296 }
297
298 if ($num) {
299 $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
300 } else {
301 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
302 }
303
304 $i = 0;
305 while ($i < $num) {
306 $obj = $this->db->fetch_object($resql);
307
308 $type = '';
309 if ($obj->type == 1) {
310 $type = $langs->trans("Supplier");
311 }
312 if ($obj->type == 2) {
313 $type = $langs->trans("Customer");
314 }
315 $labeltoshow = $obj->label;
316 if ($type) {
317 $labeltoshow .= ' <span class="opacitymedium">('.$type.')</span>';
318 }
319 $s .= '<option value="'.$obj->rowid.'" data-html="'.dol_escape_htmltag($labeltoshow).'">';
320 $s .= $labeltoshow;
321 $s .= '</option>';
322 $i++;
323 }
324 $s .= ajax_combobox("filter_thirdparties");
325 } else {
326 dol_print_error($this->db);
327 }
328
329 $s .= '</select> ';
330
331 // filter_client_thirdparties
332 $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
333 $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
334 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
335 $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
336 }
337 if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') && !getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS') && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTSCUSTOMERS')) {
338 $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
339 }
340 if (!getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
341 $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
342 }
343 $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
344
345 $s .= '</select> ';
346 $s .= ajax_combobox("filter_client_thirdparties");
347
348 // filter_supplier_thirdparties
349 $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
350 $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
351 $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
352 $s .= '<option value="0">'.$langs->trans("No").'</option>';
353 $s .= '</select>';
354 $s .= ajax_combobox("filter_supplier_thirdparties");
355
356 // filter_status_thirdparties
357 $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
358 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
359 $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
360 $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
361 $s .= '</select>';
362 $s .= ajax_combobox("filter_status_thirdparties");
363
364 // filter_lang_thirdparties
365 if (getDolGlobalInt('MAIN_MULTILANGS')) {
366 // Choose language
367 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
368 $formadmin = new FormAdmin($this->db);
369 $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
370 //$s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
371 $s .= $formadmin->select_language(GETPOST('filter_lang_thirdparties', 'aZ09'), 'filter_lang_thirdparties', 0, null, $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, null, 1);
372 }
373
374 return $s;
375 }
376
377
384 public function url($id)
385 {
386 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
387 }
388}
$id
Definition account.php:39
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:457
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...