-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
fraise.modules.php
267 lines (226 loc) · 10.4 KB
/
fraise.modules.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* or see http://www.gnu.org/
*/
/**
* \file htdocs/core/modules/mailings/fraise.modules.php
* \ingroup mailing
* \brief File of class to generate target according to rule Fraise
*/
include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
/**
* Class to generate target according to rule Fraise
*/
class mailing_fraise extends MailingTargets
{
var $name='FundationMembers'; // Identifiant du module mailing
// This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
var $desc='Foundation members with emails (by status)';
// Set to 1 if selector is available for admin users only
var $require_admin=0;
var $require_module=array('adherent');
var $picto='user';
var $db;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db=$db;
}
/**
* On the main mailing area, there is a box with statistics.
* If you want to add a line in this report you must provide an
* array of SQL request that returns two field:
* One called "label", One called "nb".
*
* @return string[] Array with SQL requests
*/
function getSqlArrayForStats()
{
global $conf, $langs;
$langs->load("members");
// Array for requests for statistics board
$statssql=array();
$statssql[0] ="SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
$statssql[0].=" FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
return $statssql;
}
/**
* Return here number of distinct emails returned by your selector.
* For example if this selector is used to extract 500 different
* emails from a text file, this function must return 500.
*
* @param string $sql Requete sql de comptage
* @return int Nb of recipients
*/
function getNbOfRecipients($sql='')
{
global $conf;
$sql = "SELECT count(distinct(a.email)) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
$sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
// La requete doit retourner un champ "nb" pour etre comprise
// par parent::getNbOfRecipients
return parent::getNbOfRecipients($sql);
}
/**
* Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
*
* @return string Retourne zone select
*/
function formFilter()
{
global $conf, $langs;
$langs->load("members");
$form=new Form($this->db);
$s='';
$s.=$langs->trans("Status").': ';
$s.='<select name="filter" class="flat">';
$s.='<option value="none"> </option>';
$s.='<option value="-1">'.$langs->trans("MemberStatusDraft").'</option>';
$s.='<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
$s.='<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
$s.='<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
$s.='</select> ';
$s.=$langs->trans("Type").': ';
$s.='<select name="filtertype" class="flat">';
$sql = "SELECT rowid, libelle, statut";
$sql.= " FROM ".MAIN_DB_PREFIX."adherent_type";
$sql.= " WHERE entity = ".$conf->entity;
$sql.= " ORDER BY rowid";
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$s.='<option value="0"> </option>';
if (! $num) $s.='<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->libelle,38,'middle');
$s.='</option>';
$i++;
}
}
else
{
dol_print_error($this->db);
}
$s.='</select>';
$s.='<br>';
$s.=$langs->trans("DateEndSubscription").': ';
$s.=$langs->trans("After").' > '.$form->select_date(-1,'subscriptionafter',0,0,1,'fraise',1,0,1,0);
$s.=' ';
$s.=$langs->trans("Before").' < '.$form->select_date(-1,'subscriptionbefore',0,0,1,'fraise',1,0,1,0);
return $s;
}
/**
* Renvoie url lien vers fiche de la source du destinataire du mailing
*
* @param int $id ID
* @return string Url lien
*/
function url($id)
{
return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('',"user").'</a>';
}
/**
* Ajoute destinataires dans table des cibles
*
* @param int $mailing_id Id of emailing
* @param array $filtersarray Param to filter sql request. Deprecated. Should use $_POST instead.
* @return int < 0 si erreur, nb ajout si ok
*/
function add_to_target($mailing_id,$filtersarray=array())
{
// Deprecation warning
if ($filtersarray) {
dol_syslog(__METHOD__ . ": filtersarray parameter is deprecated", LOG_WARNING);
}
global $langs,$_POST;
$langs->load("members");
$langs->load("companies");
$cibles = array();
$now=dol_now();
$dateendsubscriptionafter=dol_mktime($_POST['subscriptionafterhour'],$_POST['subscriptionaftermin'],$_POST['subscriptionaftersec'],$_POST['subscriptionaftermonth'],$_POST['subscriptionafterday'],$_POST['subscriptionafteryear']);
$dateendsubscriptionbefore=dol_mktime($_POST['subscriptionbeforehour'],$_POST['subscriptionbeforemin'],$_POST['subscriptionbeforesec'],$_POST['subscriptionbeforemonth'],$_POST['subscriptionbeforeday'],$_POST['subscriptionbeforeyear']);
// La requete doit retourner: id, email, fk_contact, name, firstname
$sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
$sql.= " a.lastname, a.firstname,";
$sql.= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
$sql.= " FROM ".MAIN_DB_PREFIX."adherent as a, ".MAIN_DB_PREFIX."adherent_type as ta";
$sql.= " WHERE a.email <> ''"; // Note that null != '' is false
$sql.= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
if (isset($_POST["filter"]) && $_POST["filter"] == '-1') $sql.= " AND a.statut=-1";
if (isset($_POST["filter"]) && $_POST["filter"] == '1a') $sql.= " AND a.statut=1 AND a.datefin >= '".$this->db->idate($now)."'";
if (isset($_POST["filter"]) && $_POST["filter"] == '1b') $sql.= " AND a.statut=1 AND (a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."')";
if (isset($_POST["filter"]) && $_POST["filter"] == '0') $sql.= " AND a.statut=0";
if ($dateendsubscriptionafter > 0) $sql.=" AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
if ($dateendsubscriptionbefore > 0) $sql.=" AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
$sql.= " AND a.fk_adherent_type = ta.rowid";
if ($_POST['filtertype']) $sql.= " AND ta.rowid='".$_POST['filtertype']."'";
$sql.= " ORDER BY a.email";
//print $sql;
// Add targets into table
dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
$result=$this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
$j = 0;
dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
$old = '';
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
if ($old <> $obj->email)
{
$cibles[$j] = array(
'email' => $obj->email,
'fk_contact' => $obj->fk_contact,
'lastname' => $obj->lastname,
'firstname' => $obj->firstname,
'other' =>
($langs->transnoentities("Login").'='.$obj->login).';'.
($langs->transnoentities("UserTitle").'='.($obj->civility_id?$langs->transnoentities("Civility".$obj->civility_id):'')).';'.
($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin),'day')).';'.
($langs->transnoentities("Company").'='.$obj->societe),
'source_url' => $this->url($obj->id),
'source_id' => $obj->id,
'source_type' => 'member'
);
$old = $obj->email;
$j++;
}
$i++;
}
}
else
{
dol_syslog($this->db->error());
$this->error=$this->db->error();
return -1;
}
return parent::add_to_target($mailing_id, $cibles);
}
}