dolibarr 25.0.0-alpha
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-2026 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 $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 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
109
110 $sql .= " UNION ";
111
112 $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";
113 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
114 $sql .= " WHERE s.email <> ''";
115 $sql .= " AND s.entity IN (".getEntity('member').")";
116 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing = ".((int) $mailing_id).")";
117 $sql .= " AND p.fk_member = s.rowid";
118 $sql .= " AND pt.rowid = p.fk_type";
119 if (GETPOSTINT('countryid') > 0) {
120 $sql .= " AND s.country = ".((int) GETPOSTINT('countryid'));
121 }
122 if (GETPOSTINT('filter') > 0) {
123 $sql .= " AND pt.rowid=".((int) GETPOSTINT('filter'));
124 }
125 if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
126 $sql .= " AND p.status = ".((int) GETPOSTINT('filter_status_partnership'));
127 }
128 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
129
130 $sql .= " ORDER BY email";
131
132 // Stock recipients emails into targets table
133 $result = $this->db->query($sql);
134 if ($result) {
135 $num = $this->db->num_rows($result);
136 $i = 0;
137 $j = 0;
138
139 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
140
141 $old = '';
142 while ($i < $num) {
143 $obj = $this->db->fetch_object($result);
144 if ($old != $obj->email) {
145 $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
146 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
147 $otherTxt .= ";";
148 }
149 $otherTxt .= $addDescription;
150 $cibles[$j] = array(
151 'email' => $obj->email,
152 'fk_contact' => (int) $obj->fk_contact,
153 'lastname' => $obj->name, // For a thirdparty, we must use name
154 'firstname' => '', // For a thirdparty, lastname is ''
155 'other' => $otherTxt,
156 'source_url' => $this->url($obj->id, $obj->source),
157 'source_id' => (int) $obj->id,
158 'source_type' => $obj->source
159 );
160 $old = $obj->email;
161 $j++;
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 return parent::addTargetsToDatabase($mailing_id, $cibles);
173 }
174
175
184 public function getSqlArrayForStats()
185 {
186 // CHANGE THIS: Optional
187
188 //var $statssql=array();
189 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
190 return array();
191 }
192
193
202 public function getNbOfRecipients($sql = '')
203 {
204 $sql = "SELECT count(distinct(s.email)) as nb";
205 $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
206 $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
207 $sql .= " AND s.entity IN (".getEntity('societe').")";
208 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
209
210 $sql .= " UNION ";
211
212 $sql .= "SELECT count(distinct(s.email)) as nb";
213 $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
214 $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
215 $sql .= " AND s.entity IN (".getEntity('member').")";
216 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
217
218 //print $sql;
219
220 // The query must return a field "nb" to be understood by parent::getNbOfRecipients
221 return parent::getNbOfRecipients($sql);
222 }
223
230 public function formFilter()
231 {
232 global $conf, $langs, $form;
233
234 $langs->load("companies");
235
236 // Add filter on partnership type
237 $s = '<!-- form to filter partnerships -->'."\n";
238 $s .= '<select id="filter_partnership" name="filter" class="flat">';
239
240 // Get all types of partnership
241 $sql = "SELECT rowid, label, code, active";
242 $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
243 $sql .= " WHERE active = 1";
244 $sql .= " AND entity = ".((int) $conf->entity);
245 $sql .= " ORDER BY label";
246
247 //print $sql;
248 $resql = $this->db->query($sql);
249 if ($resql) {
250 $num = $this->db->num_rows($resql);
251
252 if (!isModEnabled("partnership")) {
253 $num = 0; // Force empty list if module is not enabled
254 }
255
256 if ($num) {
257 $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
258 }
259
260 $i = 0;
261 while ($i < $num) {
262 $obj = $this->db->fetch_object($resql);
263
264 $s .= '<option value="'.$obj->rowid.'"'.($obj->rowid == GETPOST('filter') ? "selected" : "").'>';
265 $s .= dolPrintHTML($obj->label);
266 $s .= '</option>';
267 $i++;
268 }
269 $s .= ajax_combobox("filter_partnership");
270 } else {
271 dol_print_error($this->db);
272 }
273
274 $s .= '</select> ';
275
276 // Add filter on thirdparties status
277 include_once DOL_DOCUMENT_ROOT.'/partnership/class/partnership.class.php';
278 $tmppartnership = new Partnership($this->db);
279
280 $tmppartnership->status = 0;
281 $tmppartnership->getLibStatut(0); // We call this only to have $tmppartnership->labelStatus loaded
282
283 $s .= $form->selectarray('filter_status_partnership', $tmppartnership->labelStatus, GETPOST('filter_status_partnership'), $langs->trans("Status"));
284
285 // Add filter on country
286 $s .= $form->select_country(GETPOST('countryid'), 'countryid', '', 0, 'minwidth150 maxwidth200', '', $langs->trans("Country"), 0, 0, array(), 0, 0);
287
288 return $s;
289 }
290
291
299 public function url($id, $sourcetype = 'thirdparty')
300 {
301 if ($sourcetype == 'thirdparty') {
302 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
303 }
304 if ($sourcetype == 'member') {
305 return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
306 }
307
308 return '';
309 }
310}
$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.
getSqlToExcludeUnsubscribed($emailfield)
Return the SQL fragment that excludes email addresses which opted out of emailings for the current en...
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:
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolPrintHTML($s, $allowiframe=0, $moreallowedtags=array())
Return a string (that can be on several lines) ready to be output on a HTML page.
Definition html.lib.php:73
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...