dolibarr 21.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 *
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
21// Load Dolibarr Environment
22include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
23
24
29{
30 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
31 public $name = 'PartnershipThirdpartiesOrMembers';
32 public $desc = "Thirdparties or members included into a partnership program";
33
34 public $require_admin = 0;
35
36 public $require_module = array('partnership'); // This module allows to select by categories must be also enabled if category module is not activated
37
41 public $picto = 'partnership';
42
43 public $enabled = 'isModEnabled("partnership")';
44
45
51 public function __construct($db)
52 {
53 global $conf, $langs;
54 $langs->load('companies');
55
56 $this->db = $db;
57 }
58
59
60 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
67 public function add_to_target($mailing_id)
68 {
69 // phpcs:enable
70 global $conf, $langs;
71
72 $cibles = array();
73 $addDescription = '';
74
75 $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";
76 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
77 $sql .= " WHERE s.email <> ''";
78 $sql .= " AND s.entity IN (".getEntity('societe').")";
79 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
80 $sql .= " AND p.fk_soc = s.rowid";
81 $sql .= " AND pt.rowid = p.fk_type";
82 if (GETPOSTINT('filter') > 0) {
83 $sql .= " AND pt.rowid=".(GETPOSTINT('filter'));
84 }
85 if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
86 $sql .= " AND p.status = ".GETPOSTINT('filter_status_partnership');
87 }
88 if (empty($this->evenunsubscribe)) {
89 $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).")";
90 }
91
92 $sql .= " UNION ";
93
94 $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";
95 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
96 $sql .= " WHERE s.email <> ''";
97 $sql .= " AND s.entity IN (".getEntity('member').")";
98 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
99 $sql .= " AND p.fk_member = s.rowid";
100 $sql .= " AND pt.rowid = p.fk_type";
101 if (GETPOSTINT('filter') > 0) {
102 $sql .= " AND pt.rowid=".(GETPOSTINT('filter'));
103 }
104 if (GETPOSTISSET('filter_status_partnership') && GETPOSTINT('filter_status_partnership') >= 0) {
105 $sql .= " AND p.status = ".GETPOSTINT('filter_status_partnership');
106 }
107 if (empty($this->evenunsubscribe)) {
108 $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).")";
109 }
110
111 $sql .= " ORDER BY email";
112
113 // Stock recipients emails into targets table
114 $result = $this->db->query($sql);
115 if ($result) {
116 $num = $this->db->num_rows($result);
117 $i = 0;
118 $j = 0;
119
120 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
121
122 $old = '';
123 while ($i < $num) {
124 $obj = $this->db->fetch_object($result);
125 if ($old != $obj->email) {
126 $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
127 if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
128 $otherTxt .= ";";
129 }
130 $otherTxt .= $addDescription;
131 $cibles[$j] = array(
132 'email' => $obj->email,
133 'fk_contact' => $obj->fk_contact,
134 'lastname' => $obj->name, // For a thirdparty, we must use name
135 'firstname' => '', // For a thirdparty, lastname is ''
136 'other' => $otherTxt,
137 'source_url' => $this->url($obj->id, $obj->source),
138 'source_id' => $obj->id,
139 'source_type' => $obj->source
140 );
141 $old = $obj->email;
142 $j++;
143 }
144
145 $i++;
146 }
147 } else {
148 dol_syslog($this->db->error());
149 $this->error = $this->db->error();
150 return -1;
151 }
152
153 return parent::addTargetsToDatabase($mailing_id, $cibles);
154 }
155
156
165 public function getSqlArrayForStats()
166 {
167 // CHANGE THIS: Optional
168
169 //var $statssql=array();
170 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
171 return array();
172 }
173
174
183 public function getNbOfRecipients($sql = '')
184 {
185 global $conf;
186
187 $sql = "SELECT count(distinct(s.email)) as nb";
188 $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
189 $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
190 $sql .= " AND s.entity IN (".getEntity('societe').")";
191 if (empty($this->evenunsubscribe)) {
192 $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).")";
193 }
194
195 $sql .= " UNION ";
196
197 $sql .= "SELECT count(distinct(s.email)) as nb";
198 $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
199 $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
200 $sql .= " AND s.entity IN (".getEntity('member').")";
201 if (empty($this->evenunsubscribe)) {
202 $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).")";
203 }
204
205 //print $sql;
206
207 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
208 return parent::getNbOfRecipients($sql);
209 }
210
217 public function formFilter()
218 {
219 global $conf, $langs, $form;
220
221 $langs->load("companies");
222
223 $s = '<select id="filter_partnership" name="filter" class="flat">';
224
225 // Show type of partnership
226 $sql = "SELECT rowid, label, code, active";
227 $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
228 $sql .= " WHERE active = 1";
229 $sql .= " AND entity = ".$conf->entity;
230 $sql .= " ORDER BY label";
231
232 //print $sql;
233 $resql = $this->db->query($sql);
234 if ($resql) {
235 $num = $this->db->num_rows($resql);
236
237 if (empty($conf->partnership->enabled)) {
238 $num = 0; // Force empty list if category module is not enabled
239 }
240
241 if ($num) {
242 $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
243 }
244
245 $i = 0;
246 while ($i < $num) {
247 $obj = $this->db->fetch_object($resql);
248
249 $s .= '<option value="'.$obj->rowid.'">'.dol_escape_htmltag($obj->label);
250 $s .= '</option>';
251 $i++;
252 }
253 $s .= ajax_combobox("filter_partnership");
254 } else {
255 dol_print_error($this->db);
256 }
257
258 $s .= '</select> ';
259
260 // filter_status_thirdparties
261 include_once DOL_DOCUMENT_ROOT.'/partnership/class/partnership.class.php';
262 $tmppartnership = new Partnership($this->db);
263 $dummy = $tmppartnership->getLibStatut(0); // We call this only to have $tmppartnership->labelStatus loaded
264
265 $s .= $form->selectarray('filter_status_partnership', $tmppartnership->labelStatus, GETPOST('filter_status_partnership'), $langs->trans("Status"));
266
267 return $s;
268 }
269
270
278 public function url($id, $sourcetype = 'thirdparty')
279 {
280 if ($sourcetype == 'thirdparty') {
281 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
282 }
283 if ($sourcetype == 'member') {
284 return '<a href="'.DOL_URL_ROOT.'/adherent/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
285 }
286
287 return '';
288 }
289}
$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
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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...
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...