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