|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2005 Rodolphe Quiedeville <[email protected]> |
|
3
|
|
|
* Copyright (C) 2005-2016 Laurent Destailleur <[email protected]> |
|
4
|
|
|
* Copyright (C) 2005-2009 Regis Houssin <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* \file htdocs/comm/mailing/class/mailing.class.php |
|
22
|
|
|
* \ingroup mailing |
|
23
|
|
|
* \brief File of class to manage emailings module |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class to manage emailings module |
|
31
|
|
|
*/ |
|
32
|
|
|
class Mailing extends CommonObject |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string ID to identify managed object |
|
36
|
|
|
*/ |
|
37
|
|
|
public $element = 'mailing'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string Name of table without prefix where object is stored |
|
41
|
|
|
*/ |
|
42
|
|
|
public $table_element = 'mailing'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
|
46
|
|
|
*/ |
|
47
|
|
|
public $picto = 'email'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string title |
|
51
|
|
|
*/ |
|
52
|
|
|
public $title; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string subject |
|
56
|
|
|
*/ |
|
57
|
|
|
public $sujet; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string body |
|
61
|
|
|
*/ |
|
62
|
|
|
public $body; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var int number of email |
|
66
|
|
|
*/ |
|
67
|
|
|
public $nbemail; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string background color |
|
71
|
|
|
*/ |
|
72
|
|
|
public $bgcolor; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string background image |
|
76
|
|
|
*/ |
|
77
|
|
|
public $bgimage; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var int status |
|
81
|
|
|
*/ |
|
82
|
|
|
public $statut; // Status 0=Draft, 1=Validated, 2=Sent partially, 3=Sent completely |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var string email from |
|
86
|
|
|
*/ |
|
87
|
|
|
public $email_from; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var string email reply to |
|
91
|
|
|
*/ |
|
92
|
|
|
public $email_replyto; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var string email errors to |
|
96
|
|
|
*/ |
|
97
|
|
|
public $email_errorsto; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var string first joined file |
|
101
|
|
|
*/ |
|
102
|
|
|
public $joined_file1; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @var string second joined file |
|
106
|
|
|
*/ |
|
107
|
|
|
public $joined_file2; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @var string third joined file |
|
111
|
|
|
*/ |
|
112
|
|
|
public $joined_file3; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @var string fourth joined file |
|
116
|
|
|
*/ |
|
117
|
|
|
public $joined_file4; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @var int id of user create |
|
121
|
|
|
*/ |
|
122
|
|
|
public $user_creation; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @var int id of user create |
|
126
|
|
|
* @deprecated |
|
127
|
|
|
*/ |
|
128
|
|
|
public $user_creat; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var int id of user validate |
|
132
|
|
|
*/ |
|
133
|
|
|
public $user_validation; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @var int id of user validate |
|
137
|
|
|
* @deprecated |
|
138
|
|
|
*/ |
|
139
|
|
|
public $user_valid; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @var integer|string date_creation |
|
143
|
|
|
* @deprecated |
|
144
|
|
|
*/ |
|
145
|
|
|
public $date_creat; |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @var integer|string date_creation |
|
149
|
|
|
*/ |
|
150
|
|
|
public $date_creation; |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @var int date validate |
|
154
|
|
|
* @deprecated |
|
155
|
|
|
*/ |
|
156
|
|
|
public $date_valid; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @var int date validate |
|
160
|
|
|
*/ |
|
161
|
|
|
public $date_validation; |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @var array extraparams |
|
165
|
|
|
*/ |
|
166
|
|
|
public $extraparams = array(); |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @var array statut dest |
|
170
|
|
|
*/ |
|
171
|
|
|
public $statut_dest = array(); |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @var array statuts |
|
175
|
|
|
*/ |
|
176
|
|
|
public $statuts = array(); |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Constructor |
|
181
|
|
|
* |
|
182
|
|
|
* @param DoliDb $db Database handler |
|
183
|
|
|
*/ |
|
184
|
|
|
public function __construct($db) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->db = $db; |
|
187
|
|
|
|
|
188
|
|
|
// List of language codes for status |
|
189
|
|
|
$this->statuts[0] = 'MailingStatusDraft'; |
|
190
|
|
|
$this->statuts[1] = 'MailingStatusValidated'; |
|
191
|
|
|
$this->statuts[2] = 'MailingStatusSentPartialy'; |
|
192
|
|
|
$this->statuts[3] = 'MailingStatusSentCompletely'; |
|
193
|
|
|
|
|
194
|
|
|
$this->statut_dest[-1] = 'MailingStatusError'; |
|
195
|
|
|
$this->statut_dest[0] = 'MailingStatusNotSent'; |
|
196
|
|
|
$this->statut_dest[1] = 'MailingStatusSent'; |
|
197
|
|
|
$this->statut_dest[2] = 'MailingStatusRead'; |
|
198
|
|
|
$this->statut_dest[3] = 'MailingStatusReadAndUnsubscribe'; // Read but ask to not be contacted anymore |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Create an EMailing |
|
203
|
|
|
* |
|
204
|
|
|
* @param User $user Object of user making creation |
|
205
|
|
|
* @return int -1 if error, Id of created object if OK |
|
206
|
|
|
*/ |
|
207
|
|
|
public function create($user) |
|
208
|
|
|
{ |
|
209
|
|
|
global $conf, $langs; |
|
210
|
|
|
|
|
211
|
|
|
$this->db->begin(); |
|
212
|
|
|
|
|
213
|
|
|
$this->title = trim($this->title); |
|
214
|
|
|
$this->email_from = trim($this->email_from); |
|
215
|
|
|
|
|
216
|
|
|
if (!$this->email_from) { |
|
217
|
|
|
$this->error = $langs->trans("ErrorMailFromRequired"); |
|
218
|
|
|
return -1; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$now = dol_now(); |
|
222
|
|
|
|
|
223
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing"; |
|
224
|
|
|
$sql .= " (date_creat, fk_user_creat, entity)"; |
|
225
|
|
|
$sql .= " VALUES ('".$this->db->idate($now)."', ".$user->id.", ".$conf->entity.")"; |
|
226
|
|
|
|
|
227
|
|
|
if (!$this->title) { |
|
228
|
|
|
$this->title = $langs->trans("NoTitle"); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
dol_syslog("Mailing::Create", LOG_DEBUG); |
|
232
|
|
|
$result = $this->db->query($sql); |
|
233
|
|
|
if ($result) { |
|
234
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing"); |
|
235
|
|
|
|
|
236
|
|
|
if ($this->update($user) > 0) { |
|
237
|
|
|
$this->db->commit(); |
|
238
|
|
|
} else { |
|
239
|
|
|
$this->error = $this->db->lasterror(); |
|
240
|
|
|
$this->db->rollback(); |
|
241
|
|
|
return -1; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $this->id; |
|
245
|
|
|
} else { |
|
246
|
|
|
$this->error = $this->db->lasterror(); |
|
247
|
|
|
$this->db->rollback(); |
|
248
|
|
|
return -1; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Update emailing record |
|
254
|
|
|
* |
|
255
|
|
|
* @param User $user Object of user making change |
|
256
|
|
|
* @return int < 0 if KO, > 0 if OK |
|
257
|
|
|
*/ |
|
258
|
|
|
public function update($user) |
|
259
|
|
|
{ |
|
260
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing "; |
|
261
|
|
|
$sql .= " SET titre = '".$this->db->escape($this->title)."'"; |
|
262
|
|
|
$sql .= ", sujet = '".$this->db->escape($this->sujet)."'"; |
|
263
|
|
|
$sql .= ", body = '".$this->db->escape($this->body)."'"; |
|
264
|
|
|
$sql .= ", email_from = '".$this->db->escape($this->email_from)."'"; |
|
265
|
|
|
$sql .= ", email_replyto = '".$this->db->escape($this->email_replyto)."'"; |
|
266
|
|
|
$sql .= ", email_errorsto = '".$this->db->escape($this->email_errorsto)."'"; |
|
267
|
|
|
$sql .= ", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) : null)."'"; |
|
268
|
|
|
$sql .= ", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) : null)."'"; |
|
269
|
|
|
$sql .= " WHERE rowid = ".(int) $this->id; |
|
270
|
|
|
|
|
271
|
|
|
dol_syslog("Mailing::Update", LOG_DEBUG); |
|
272
|
|
|
$result = $this->db->query($sql); |
|
273
|
|
|
if ($result) { |
|
274
|
|
|
return 1; |
|
275
|
|
|
} else { |
|
276
|
|
|
$this->error = $this->db->lasterror(); |
|
277
|
|
|
return -1; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Get object from database |
|
283
|
|
|
* |
|
284
|
|
|
* @param int $rowid Id of emailing |
|
285
|
|
|
* @return int <0 if KO, >0 if OK |
|
286
|
|
|
*/ |
|
287
|
|
|
public function fetch($rowid) |
|
288
|
|
|
{ |
|
289
|
|
|
global $conf; |
|
290
|
|
|
|
|
291
|
|
|
$sql = "SELECT m.rowid, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage"; |
|
292
|
|
|
$sql .= ", m.email_from, m.email_replyto, m.email_errorsto"; |
|
293
|
|
|
$sql .= ", m.statut, m.nbemail"; |
|
294
|
|
|
$sql .= ", m.fk_user_creat, m.fk_user_valid"; |
|
295
|
|
|
$sql .= ", m.date_creat"; |
|
296
|
|
|
$sql .= ", m.date_valid"; |
|
297
|
|
|
$sql .= ", m.date_envoi"; |
|
298
|
|
|
$sql .= ", m.extraparams"; |
|
299
|
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."mailing as m"; |
|
300
|
|
|
$sql .= " WHERE m.rowid = ".(int) $rowid; |
|
301
|
|
|
|
|
302
|
|
|
dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
|
303
|
|
|
$result = $this->db->query($sql); |
|
304
|
|
|
if ($result) { |
|
305
|
|
|
if ($this->db->num_rows($result)) { |
|
306
|
|
|
$obj = $this->db->fetch_object($result); |
|
307
|
|
|
|
|
308
|
|
|
$this->id = $obj->rowid; |
|
309
|
|
|
$this->ref = $obj->rowid; |
|
310
|
|
|
$this->statut = $obj->statut; |
|
311
|
|
|
$this->nbemail = $obj->nbemail; |
|
312
|
|
|
$this->title = $obj->title; |
|
313
|
|
|
|
|
314
|
|
|
$this->sujet = $obj->sujet; |
|
315
|
|
|
if (!empty($conf->global->FCKEDITOR_ENABLE_MAILING) && dol_textishtml(dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML5))) { |
|
316
|
|
|
$this->body = dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML5); |
|
317
|
|
|
} else { |
|
318
|
|
|
$this->body = $obj->body; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$this->bgcolor = $obj->bgcolor; |
|
322
|
|
|
$this->bgimage = $obj->bgimage; |
|
323
|
|
|
|
|
324
|
|
|
$this->email_from = $obj->email_from; |
|
325
|
|
|
$this->email_replyto = $obj->email_replyto; |
|
326
|
|
|
$this->email_errorsto = $obj->email_errorsto; |
|
327
|
|
|
|
|
328
|
|
|
$this->user_creat = $obj->fk_user_creat; |
|
329
|
|
|
$this->user_creation = $obj->fk_user_creat; |
|
330
|
|
|
$this->user_valid = $obj->fk_user_valid; |
|
331
|
|
|
$this->user_validation = $obj->fk_user_valid; |
|
332
|
|
|
|
|
333
|
|
|
$this->date_creat = $this->db->jdate($obj->date_creat); |
|
334
|
|
|
$this->date_creation = $this->db->jdate($obj->date_creat); |
|
335
|
|
|
$this->date_valid = $this->db->jdate($obj->date_valid); |
|
336
|
|
|
$this->date_validation = $this->db->jdate($obj->date_valid); |
|
337
|
|
|
$this->date_envoi = $this->db->jdate($obj->date_envoi); |
|
338
|
|
|
|
|
339
|
|
|
$this->extraparams = (array) json_decode($obj->extraparams, true); |
|
340
|
|
|
|
|
341
|
|
|
return 1; |
|
342
|
|
|
} else { |
|
343
|
|
|
dol_syslog(get_class($this)."::fetch Erreur -1"); |
|
344
|
|
|
return -1; |
|
345
|
|
|
} |
|
346
|
|
|
} else { |
|
347
|
|
|
dol_syslog(get_class($this)."::fetch Erreur -2"); |
|
348
|
|
|
return -2; |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Load an object from its id and create a new one in database |
|
355
|
|
|
* |
|
356
|
|
|
* @param User $user User making the clone |
|
357
|
|
|
* @param int $fromid Id of object to clone |
|
358
|
|
|
* @param int $option1 1=Clone content, 0=Forget content |
|
359
|
|
|
* @param int $option2 1=Clone recipients |
|
360
|
|
|
* @return int New id of clone |
|
361
|
|
|
*/ |
|
362
|
|
|
public function createFromClone(User $user, $fromid, $option1, $option2) |
|
363
|
|
|
{ |
|
364
|
|
|
global $langs; |
|
365
|
|
|
|
|
366
|
|
|
$error = 0; |
|
367
|
|
|
|
|
368
|
|
|
$object = new Mailing($this->db); |
|
369
|
|
|
|
|
370
|
|
|
$this->db->begin(); |
|
371
|
|
|
|
|
372
|
|
|
// Load source object |
|
373
|
|
|
$object->fetch($fromid); |
|
374
|
|
|
$object->id = 0; |
|
375
|
|
|
$object->statut = 0; |
|
376
|
|
|
|
|
377
|
|
|
// Clear fields |
|
378
|
|
|
$object->title = $langs->trans("CopyOf").' '.$object->title.' '.dol_print_date(dol_now()); |
|
379
|
|
|
|
|
380
|
|
|
// If no option copy content |
|
381
|
|
|
if (empty($option1)) { |
|
382
|
|
|
// Clear values |
|
383
|
|
|
$object->nbemail = 0; |
|
384
|
|
|
$object->sujet = ''; |
|
385
|
|
|
$object->body = ''; |
|
386
|
|
|
$object->bgcolor = ''; |
|
387
|
|
|
$object->bgimage = ''; |
|
388
|
|
|
|
|
389
|
|
|
//$object->email_from = ''; // We do not reset from email because it is a mandatory value |
|
390
|
|
|
$object->email_replyto = ''; |
|
391
|
|
|
$object->email_errorsto = ''; |
|
392
|
|
|
|
|
393
|
|
|
$object->user_creat = $user->id; |
|
|
|
|
|
|
394
|
|
|
$object->user_valid = ''; |
|
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
$object->date_creat = ''; |
|
|
|
|
|
|
397
|
|
|
$object->date_valid = ''; |
|
|
|
|
|
|
398
|
|
|
$object->date_envoi = ''; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
// Create clone |
|
402
|
|
|
$object->context['createfromclone'] = 'createfromclone'; |
|
403
|
|
|
$result = $object->create($user); |
|
404
|
|
|
|
|
405
|
|
|
// Other options |
|
406
|
|
|
if ($result < 0) { |
|
407
|
|
|
$this->error = $object->error; |
|
408
|
|
|
$this->errors = array_merge($this->errors, $object->errors); |
|
409
|
|
|
$error++; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
if (!$error) { |
|
413
|
|
|
// Clone recipient targets |
|
414
|
|
|
if (!empty($option2)) { |
|
415
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php'; |
|
416
|
|
|
|
|
417
|
|
|
$mailing_target = new MailingTargets($this->db); |
|
418
|
|
|
|
|
419
|
|
|
$target_array = array(); |
|
420
|
|
|
|
|
421
|
|
|
$sql = "SELECT fk_contact,"; |
|
422
|
|
|
$sql .= " lastname,"; |
|
423
|
|
|
$sql .= " firstname,"; |
|
424
|
|
|
$sql .= " email,"; |
|
425
|
|
|
$sql .= " other,"; |
|
426
|
|
|
$sql .= " source_url,"; |
|
427
|
|
|
$sql .= " source_id ,"; |
|
428
|
|
|
$sql .= " source_type"; |
|
429
|
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles"; |
|
430
|
|
|
$sql .= " WHERE fk_mailing = ".$fromid; |
|
431
|
|
|
|
|
432
|
|
|
$result = $this->db->query($sql); |
|
433
|
|
|
if ($result) { |
|
434
|
|
|
if ($this->db->num_rows($result)) { |
|
435
|
|
|
while ($obj = $this->db->fetch_object($result)) { |
|
436
|
|
|
$target_array[] = array( |
|
437
|
|
|
'fk_contact'=>$obj->fk_contact, |
|
438
|
|
|
'lastname'=>$obj->lastname, |
|
439
|
|
|
'firstname'=>$obj->firstname, |
|
440
|
|
|
'email'=>$obj->email, |
|
441
|
|
|
'other'=>$obj->other, |
|
442
|
|
|
'source_url'=>$obj->source_url, |
|
443
|
|
|
'source_id'=>$obj->source_id, |
|
444
|
|
|
'source_type'=>$obj->source_type |
|
445
|
|
|
); |
|
446
|
|
|
} |
|
447
|
|
|
} |
|
448
|
|
|
} else { |
|
449
|
|
|
$this->error = $this->db->lasterror(); |
|
450
|
|
|
return -1; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
$mailing_target->addTargetsToDatabase($object->id, $target_array); |
|
454
|
|
|
} |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
unset($object->context['createfromclone']); |
|
458
|
|
|
|
|
459
|
|
|
// End |
|
460
|
|
|
if (!$error) { |
|
461
|
|
|
$this->db->commit(); |
|
462
|
|
|
return $object->id; |
|
463
|
|
|
} else { |
|
464
|
|
|
$this->db->rollback(); |
|
465
|
|
|
return -1; |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Validate emailing |
|
471
|
|
|
* |
|
472
|
|
|
* @param User $user Objet user qui valide |
|
473
|
|
|
* @return int <0 if KO, >0 if OK |
|
474
|
|
|
*/ |
|
475
|
|
|
public function valid($user) |
|
476
|
|
|
{ |
|
477
|
|
|
$now = dol_now(); |
|
478
|
|
|
|
|
479
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing "; |
|
480
|
|
|
$sql .= " SET statut = 1, date_valid = '".$this->db->idate($now)."', fk_user_valid=".$user->id; |
|
481
|
|
|
$sql .= " WHERE rowid = ".$this->id; |
|
482
|
|
|
|
|
483
|
|
|
dol_syslog("Mailing::valid", LOG_DEBUG); |
|
484
|
|
|
if ($this->db->query($sql)) { |
|
485
|
|
|
return 1; |
|
486
|
|
|
} else { |
|
487
|
|
|
$this->error = $this->db->lasterror(); |
|
488
|
|
|
return -1; |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Delete emailing |
|
495
|
|
|
* |
|
496
|
|
|
* @param int $rowid id du mailing a supprimer |
|
497
|
|
|
* @return int 1 en cas de succes |
|
498
|
|
|
*/ |
|
499
|
|
|
public function delete($rowid) |
|
500
|
|
|
{ |
|
501
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing"; |
|
502
|
|
|
$sql .= " WHERE rowid = ".$rowid; |
|
503
|
|
|
|
|
504
|
|
|
dol_syslog("Mailing::delete", LOG_DEBUG); |
|
505
|
|
|
$resql = $this->db->query($sql); |
|
506
|
|
|
if ($resql) { |
|
507
|
|
|
return $this->delete_targets(); |
|
508
|
|
|
} else { |
|
509
|
|
|
$this->error = $this->db->lasterror(); |
|
510
|
|
|
return -1; |
|
511
|
|
|
} |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
515
|
|
|
/** |
|
516
|
|
|
* Delete targets emailing |
|
517
|
|
|
* |
|
518
|
|
|
* @return int 1 if OK, 0 if error |
|
519
|
|
|
*/ |
|
520
|
|
|
public function delete_targets() |
|
521
|
|
|
{ |
|
522
|
|
|
// phpcs:enable |
|
523
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles"; |
|
524
|
|
|
$sql .= " WHERE fk_mailing = ".$this->id; |
|
525
|
|
|
|
|
526
|
|
|
dol_syslog("Mailing::delete_targets", LOG_DEBUG); |
|
527
|
|
|
$resql = $this->db->query($sql); |
|
528
|
|
|
if ($resql) { |
|
529
|
|
|
$this->refreshNbOfTargets(); |
|
530
|
|
|
|
|
531
|
|
|
return 1; |
|
532
|
|
|
} else { |
|
533
|
|
|
$this->error = $this->db->lasterror(); |
|
534
|
|
|
return 0; |
|
535
|
|
|
} |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
|
|
539
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
540
|
|
|
/** |
|
541
|
|
|
* Change status of each recipient |
|
542
|
|
|
* |
|
543
|
|
|
* @param User $user Objet user qui valide |
|
544
|
|
|
* @return int <0 if KO, >0 if OK |
|
545
|
|
|
*/ |
|
546
|
|
|
public function reset_targets_status($user) |
|
547
|
|
|
{ |
|
548
|
|
|
// phpcs:enable |
|
549
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; |
|
550
|
|
|
$sql .= " SET statut = 0"; |
|
551
|
|
|
$sql .= " WHERE fk_mailing = ".$this->id; |
|
552
|
|
|
|
|
553
|
|
|
dol_syslog("Mailing::reset_targets_status", LOG_DEBUG); |
|
554
|
|
|
$resql = $this->db->query($sql); |
|
555
|
|
|
if ($resql) { |
|
556
|
|
|
return 1; |
|
557
|
|
|
} else { |
|
558
|
|
|
$this->error = $this->db->lasterror(); |
|
559
|
|
|
return -1; |
|
560
|
|
|
} |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
|
|
564
|
|
|
/** |
|
565
|
|
|
* Count number of target with status |
|
566
|
|
|
* |
|
567
|
|
|
* @param string $mode Mode ('alreadysent' = Sent success or error, 'alreadysentok' = Sent success, 'alreadysentko' = Sent error) |
|
568
|
|
|
* @return int Nb of target with status |
|
569
|
|
|
*/ |
|
570
|
|
|
public function countNbOfTargets($mode) |
|
571
|
|
|
{ |
|
572
|
|
|
$sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles"; |
|
573
|
|
|
$sql .= " WHERE fk_mailing = ".$this->id; |
|
574
|
|
|
if ($mode == 'alreadysent') { |
|
575
|
|
|
$sql .= " AND statut <> 0"; |
|
576
|
|
|
} elseif ($mode == 'alreadysentok') { |
|
577
|
|
|
$sql .= " AND statut > 0"; |
|
578
|
|
|
} elseif ($mode == 'alreadysentko') { |
|
579
|
|
|
$sql .= " AND statut = -1"; |
|
580
|
|
|
} else { |
|
581
|
|
|
$this->error = 'BadValueForParameterMode'; |
|
582
|
|
|
return -2; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
$resql = $this->db->query($sql); |
|
586
|
|
|
if ($resql) { |
|
587
|
|
|
$obj = $this->db->fetch_object($resql); |
|
588
|
|
|
if ($obj) { |
|
589
|
|
|
return $obj->nb; |
|
590
|
|
|
} |
|
591
|
|
|
} else { |
|
592
|
|
|
$this->error = $this->db->lasterror(); |
|
593
|
|
|
return -1; |
|
594
|
|
|
} |
|
595
|
|
|
return 0; |
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
/** |
|
599
|
|
|
* Refresh denormalized value ->nbemail into emailing record |
|
600
|
|
|
* Note: There is also the method update_nb into modules_mailings that is used for this. |
|
601
|
|
|
* |
|
602
|
|
|
* @return int <0 if KO, >0 if OK |
|
603
|
|
|
*/ |
|
604
|
|
|
public function refreshNbOfTargets() |
|
605
|
|
|
{ |
|
606
|
|
|
$sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles"; |
|
607
|
|
|
$sql .= " WHERE fk_mailing = ".$this->id; |
|
608
|
|
|
|
|
609
|
|
|
$resql = $this->db->query($sql); |
|
610
|
|
|
if ($resql) { |
|
611
|
|
|
$obj = $this->db->fetch_object($resql); |
|
612
|
|
|
if ($obj) { |
|
613
|
|
|
$nbforupdate = $obj->nb; |
|
614
|
|
|
|
|
615
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.'mailing SET nbemail = '.((int) $nbforupdate); |
|
616
|
|
|
$sql .= ' WHERE rowid = '.$this->id; |
|
617
|
|
|
|
|
618
|
|
|
$resqlupdate = $this->db->query($sql); |
|
619
|
|
|
if (! $resqlupdate) { |
|
620
|
|
|
$this->error = $this->db->lasterror(); |
|
621
|
|
|
return -1; |
|
622
|
|
|
} |
|
623
|
|
|
} |
|
624
|
|
|
} else { |
|
625
|
|
|
$this->error = $this->db->lasterror(); |
|
626
|
|
|
return -1; |
|
627
|
|
|
} |
|
628
|
|
|
return 1; |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
/** |
|
632
|
|
|
* Return a link to the object card (with optionally the picto) |
|
633
|
|
|
* |
|
634
|
|
|
* @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
|
635
|
|
|
* @param string $option On what the link point to ('nolink', ...) |
|
636
|
|
|
* @param int $notooltip 1=Disable tooltip |
|
637
|
|
|
* @param string $morecss Add more css on link |
|
638
|
|
|
* @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
|
639
|
|
|
* @return string String with URL |
|
640
|
|
|
*/ |
|
641
|
|
|
public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
|
642
|
|
|
{ |
|
643
|
|
|
global $db, $conf, $langs, $hookmanager; |
|
644
|
|
|
global $dolibarr_main_authentication, $dolibarr_main_demo; |
|
645
|
|
|
global $menumanager; |
|
646
|
|
|
|
|
647
|
|
|
if (!empty($conf->dol_no_mouse_hover)) { |
|
648
|
|
|
$notooltip = 1; // Force disable tooltips |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
$result = ''; |
|
652
|
|
|
$companylink = ''; |
|
653
|
|
|
|
|
654
|
|
|
$label = '<u>'.$langs->trans("ShowEMailing").'</u>'; |
|
655
|
|
|
$label .= '<br>'; |
|
656
|
|
|
$label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref; |
|
657
|
|
|
|
|
658
|
|
|
$url = DOL_URL_ROOT.'/comm/mailing/card.php?id='.$this->id; |
|
659
|
|
|
|
|
660
|
|
|
if ($option != 'nolink') { |
|
661
|
|
|
// Add param to save lastsearch_values or not |
|
662
|
|
|
$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
|
663
|
|
|
if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
|
664
|
|
|
$add_save_lastsearch_values = 1; |
|
665
|
|
|
} |
|
666
|
|
|
if ($add_save_lastsearch_values) { |
|
667
|
|
|
$url .= '&save_lastsearch_values=1'; |
|
668
|
|
|
} |
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
$linkclose = ''; |
|
672
|
|
|
if (empty($notooltip)) { |
|
673
|
|
|
if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) { |
|
674
|
|
|
$label = $langs->trans("ShowEMailing"); |
|
675
|
|
|
$linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"'; |
|
676
|
|
|
} |
|
677
|
|
|
$linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"'; |
|
678
|
|
|
$linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"'; |
|
679
|
|
|
|
|
680
|
|
|
/* |
|
681
|
|
|
$hookmanager->initHooks(array('myobjectdao')); |
|
682
|
|
|
$parameters=array('id'=>$this->id); |
|
683
|
|
|
$reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
|
684
|
|
|
if ($reshook > 0) $linkclose = $hookmanager->resPrint; |
|
685
|
|
|
*/ |
|
686
|
|
|
} else { |
|
687
|
|
|
$linkclose = ($morecss ? ' class="'.$morecss.'"' : ''); |
|
688
|
|
|
} |
|
689
|
|
|
|
|
690
|
|
|
$linkstart = '<a href="'.$url.'"'; |
|
691
|
|
|
$linkstart .= $linkclose.'>'; |
|
692
|
|
|
$linkend = '</a>'; |
|
693
|
|
|
|
|
694
|
|
|
$result .= $linkstart; |
|
695
|
|
|
if ($withpicto) { |
|
696
|
|
|
$result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1); |
|
697
|
|
|
} |
|
698
|
|
|
if ($withpicto != 2) { |
|
699
|
|
|
$result .= $this->ref; |
|
700
|
|
|
} |
|
701
|
|
|
$result .= $linkend; |
|
702
|
|
|
//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
|
703
|
|
|
|
|
704
|
|
|
global $action; |
|
705
|
|
|
$hookmanager->initHooks(array('emailingdao')); |
|
706
|
|
|
$parameters = array('id'=>$this->id, 'getnomurl'=>$result); |
|
707
|
|
|
$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
|
708
|
|
|
if ($reshook > 0) { |
|
709
|
|
|
$result = $hookmanager->resPrint; |
|
710
|
|
|
} else { |
|
711
|
|
|
$result .= $hookmanager->resPrint; |
|
712
|
|
|
} |
|
713
|
|
|
|
|
714
|
|
|
return $result; |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
/** |
|
718
|
|
|
* Return label of status of emailing (draft, validated, ...) |
|
719
|
|
|
* |
|
720
|
|
|
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long |
|
721
|
|
|
* @return string Label |
|
722
|
|
|
*/ |
|
723
|
|
|
public function getLibStatut($mode = 0) |
|
724
|
|
|
{ |
|
725
|
|
|
return $this->LibStatut($this->statut, $mode); |
|
726
|
|
|
} |
|
727
|
|
|
|
|
728
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
729
|
|
|
/** |
|
730
|
|
|
* Renvoi le libelle d'un statut donne |
|
731
|
|
|
* |
|
732
|
|
|
* @param int $status Id status |
|
733
|
|
|
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
|
734
|
|
|
* @return string Label |
|
735
|
|
|
*/ |
|
736
|
|
|
public function LibStatut($status, $mode = 0) |
|
737
|
|
|
{ |
|
738
|
|
|
// phpcs:enable |
|
739
|
|
|
global $langs; |
|
740
|
|
|
$langs->load("mailing"); |
|
741
|
|
|
|
|
742
|
|
|
$labelStatus = $langs->trans($this->statuts[$status]); |
|
743
|
|
|
$labelStatusShort = $langs->trans($this->statuts[$status]); |
|
744
|
|
|
|
|
745
|
|
|
$statusType = 'status'.$status; |
|
746
|
|
|
if ($status == 2) { |
|
747
|
|
|
$statusType = 'status3'; |
|
748
|
|
|
} |
|
749
|
|
|
if ($status == 3) { |
|
750
|
|
|
$statusType = 'status6'; |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode); |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
|
|
757
|
|
|
/** |
|
758
|
|
|
* Renvoi le libelle d'un statut donne |
|
759
|
|
|
* TODO Add class mailin_target.class.php |
|
760
|
|
|
* |
|
761
|
|
|
* @param int $status Id status |
|
762
|
|
|
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
|
763
|
|
|
* @param string $desc Desc error |
|
764
|
|
|
* @return string Label |
|
765
|
|
|
*/ |
|
766
|
|
|
public static function libStatutDest($status, $mode = 0, $desc = '') |
|
767
|
|
|
{ |
|
768
|
|
|
global $langs; |
|
769
|
|
|
$langs->load("mails"); |
|
770
|
|
|
|
|
771
|
|
|
$labelStatus = array(); |
|
772
|
|
|
$labelStatusShort = array(); |
|
773
|
|
|
|
|
774
|
|
|
$labelStatus[-1] = $langs->trans('MailingStatusError'); |
|
775
|
|
|
$labelStatus[0] = $langs->trans('MailingStatusNotSent'); |
|
776
|
|
|
$labelStatus[1] = $langs->trans('MailingStatusSent'); |
|
777
|
|
|
$labelStatus[2] = $langs->trans('MailingStatusRead'); |
|
778
|
|
|
$labelStatus[3] = $langs->trans('MailingStatusNotContact'); |
|
779
|
|
|
$labelStatusShort[-1] = $langs->trans('MailingStatusError'); |
|
780
|
|
|
$labelStatusShort[0] = $langs->trans('MailingStatusNotSent'); |
|
781
|
|
|
$labelStatusShort[1] = $langs->trans('MailingStatusSent'); |
|
782
|
|
|
$labelStatusShort[2] = $langs->trans('MailingStatusRead'); |
|
783
|
|
|
$labelStatusShort[3] = $langs->trans('MailingStatusNotContact'); |
|
784
|
|
|
|
|
785
|
|
|
$statusType = 'status'.$status; |
|
786
|
|
|
if ($status == -1) { |
|
787
|
|
|
$statusType = 'status8'; |
|
788
|
|
|
} |
|
789
|
|
|
if ($status == 1) { |
|
790
|
|
|
$statusType = 'status6'; |
|
791
|
|
|
} |
|
792
|
|
|
if ($status == 2) { |
|
793
|
|
|
$statusType = 'status4'; |
|
794
|
|
|
} |
|
795
|
|
|
|
|
796
|
|
|
$param = array(); |
|
797
|
|
|
if ($status == - 1) { |
|
798
|
|
|
$param = array('badgeParams'=>array('attr'=>array('title'=>$desc))); |
|
799
|
|
|
} |
|
800
|
|
|
return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode, '', $param); |
|
801
|
|
|
} |
|
802
|
|
|
} |
|
803
|
|
|
|