dolibarr 24.0.0-beta
partnership.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
22// Load Dolibarr Environment
23include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
24
25
30{
34 public $name = 'PartnershipThirdpartiesOrMembers';
35
39 public $desc = "Thirdparties or members included into a partnership program";
40
44 public $require_admin = 0;
45
50 public $require_module = array('partnership'); // This module allows to select by categories must be also enabled if category module is not activated
51
55 public $picto = 'partnership';
56
60 public $enabled = 'isModEnabled("partnership")';
61
62
68 public function __construct($db)
69 {
70 global $langs;
71 $langs->load('companies');
72
73 $this->db = $db;
74 }
75
76
77 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
84 public function add_to_target($mailing_id)
85 {
86 // phpcs:enable
87 global $conf, $langs;
88
89 $cibles = array();
90 $addDescription = '';
91
92 $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, pt.label as label, 'thirdparty' as source";
93 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
94 $sql .= " WHERE s.email <> ''";
95 $sql .= " AND s.entity IN (".getEntity('societe').")";
96 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing = ".((int) $mailing_id).")";
97 $sql .= " AND p.fk_soc = s.rowid";
98 $sql .= " AND pt.rowid = p.fk_type";
99 if (GETPOSTINT('countryid') > 0) {
100 $sql .= " AND s.fk_pays = ".((int) GETPOSTINT('countryid'));
101 }
102 if (GETPOSTINT('filter') > 0) {
103 $sql .= " AND pt.rowid = ".((int) GETPOSTINT('filter'));
104 }
105 if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
106 $sql .= " AND p.status = ".((int) GETPOSTINT('filter_status_partnership'));
107 }
108 if (empty($this->evenunsubscribe)) {
109 $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).")";
110 }
111
112 $sql .= " UNION ";
113
114 $sql .= "SELECT s.rowid as id, s.email as email, s.lastname as name, null as fk_contact, s.firstname as firstname, pt.label as label, 'member' as source";
115 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
116 $sql .= " WHERE s.email <> ''";
117 $sql .= " AND s.entity IN (".getEntity('member').")";
118 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing = ".((int) $mailing_id).")";
119 $sql .= " AND p.fk_member = s.rowid";
120 $sql .= " AND pt.rowid = p.fk_type";
121 if (GETPOSTINT('countryid') > 0) {
122 $sql .= " AND s.country = ".((int) GETPOSTINT('countryid'));
123 }
124 if (GETPOSTINT('filter') > 0) {
125 $sql .= " AND pt.rowid=".((int) GETPOSTINT('filter'));
126 }
127 if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
128 $sql .= " AND p.status = ".((int) GETPOSTINT('filter_status_partnership'));
129 }
130 if (empty($this->evenunsubscribe)) {
131 $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).")";
132 }
133
134 $sql .= " ORDER BY email";
135
136 // Stock recipients emails into targets table
137 $result = $this->db->query($sql);
138 if ($result) {
139 $num = $this->db->num_rows($result);
140 $i = 0;
141 $j = 0;
142
143 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
144
145 $old = '';
146 while ($i < $num) {
147 $obj = $this->db->fetch_object($result);
148 if ($old != $obj->email) {
149 $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
150 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
151 $otherTxt .= ";";
152 }
153 $otherTxt .= $addDescription;
154 $cibles[$j] = array(
155 'email' => $obj->email,
156 'fk_contact' => (int) $obj->fk_contact,
157 'lastname' => $obj->name, // For a thirdparty, we must use name
158 'firstname' => '', // For a thirdparty, lastname is ''
159 'other' => $otherTxt,
160 'source_url' => $this->url($obj->id, $obj->source),
161 'source_id' => (int) $obj->id,
162 'source_type' => $obj->source
163 );
164 $old = $obj->email;
165 $j++;
166 }
167
168 $i++;
169 }
170 } else {
171 dol_syslog($this->db->error());
172 $this->error = $this->db->error();
173 return -1;
174 }
175
176 return parent::addTargetsToDatabase($mailing_id, $cibles);
177 }
178
179
188 public function getSqlArrayForStats()
189 {
190 // CHANGE THIS: Optional
191
192 //var $statssql=array();
193 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
194 return array();
195 }
196
197
206 public function getNbOfRecipients($sql = '')
207 {
208 global $conf;
209
210 $sql = "SELECT count(distinct(s.email)) as nb";
211 $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
212 $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
213 $sql .= " AND s.entity IN (".getEntity('societe').")";
214 if (empty($this->evenunsubscribe)) {
215 $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).")";
216 }
217
218 $sql .= " UNION ";
219
220 $sql .= "SELECT count(distinct(s.email)) as nb";
221 $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
222 $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
223 $sql .= " AND s.entity IN (".getEntity('member').")";
224 if (empty($this->evenunsubscribe)) {
225 $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).")";
226 }
227
228 //print $sql;
229
230 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
231 return parent::getNbOfRecipients($sql);
232 }
233
240 public function formFilter()
241 {
242 global $conf, $langs, $form;
243
244 $langs->load("companies");
245
246 // Add filter on partnership type
247 $s = '<select id="filter_partnership" name="filter" class="flat">';
248
249 // Get all types of partnership
250 $sql = "SELECT rowid, label, code, active";
251 $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
252 $sql .= " WHERE active = 1";
253 $sql .= " AND entity = ".((int) $conf->entity);
254 $sql .= " ORDER BY label";
255
256 //print $sql;
257 $resql = $this->db->query($sql);
258 if ($resql) {
259 $num = $this->db->num_rows($resql);
260
261 if (!isModEnabled("partnership")) {
262 $num = 0; // Force empty list if module is not enabled
263 }
264
265 if ($num) {
266 $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
267 }
268
269 $i = 0;
270 while ($i < $num) {
271 $obj = $this->db->fetch_object($resql);
272
273 $s .= '<option value="'.$obj->rowid.'"'.($obj->rowid == GETPOST('filter') ? "selected" : "").'>'.dol_escape_htmltag($obj->label);
274 $s .= '</option>';
275 $i++;
276 }
277 $s .= ajax_combobox("filter_partnership");
278 } else {
279 dol_print_error($this->db);
280 }
281
282 $s .= '</select> ';
283
284 // Add filter on thirdparties status
285 include_once DOL_DOCUMENT_ROOT.'/partnership/class/partnership.class.php';
286 $tmppartnership = new Partnership($this->db);
287
288 $dummy = $tmppartnership->getLibStatut(0); // We call this only to have $tmppartnership->labelStatus loaded
289
290 $s .= $form->selectarray('filter_status_partnership', $tmppartnership->labelStatus, GETPOST('filter_status_partnership'), $langs->trans("Status"));
291
292 // Add filter on country
293 $s .= $form->select_country(GETPOST('countryid'), 'countryid', '', 0, 'minwidth150 maxwidth200', '', $langs->trans("Country"), 0, 0, array(), 0, 0);
294
295 return $s;
296 }
297
298
306 public function url($id, $sourcetype = 'thirdparty')
307 {
308 if ($sourcetype == 'thirdparty') {
309 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
310 }
311 if ($sourcetype == 'member') {
312 return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
313 }
314
315 return '';
316 }
317}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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:476
Parent class of emailing target selectors modules.
Class for Partnership.
Class to manage a list of personalised recipients for mailing feature.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
url($id, $sourcetype='thirdparty')
Can include an URL link on each record provided by selector shown on target page.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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...
isModEnabled($module)
Is Dolibarr module enabled.
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...