dolibarr 23.0.3
mailing_targets.class.php
1<?php
2/* Copyright (C) 2025 Cloned from htdocs/comm/mailing/class/mailing.class.php then modified
3 * Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
27
32{
36 public $element = 'mailing_target';
37
41 public $table_element = 'mailing_cibles';
42
46 public $picto = 'contact';
47
51 public $fk_mailing;
52
56 public $fk_contact;
57
61 public $lastname;
62
66 public $firstname;
67
71 public $email;
72
76 public $other;
77
81 public $tag;
82
87 public $statut; // Status 0=Not sent, 1=Sent, 2=Read, 3=Read and unsubscribed, -1=Error
88
92 public $status; // Status 0=Not sent, 1=Sent, 2=Read, 3=Read and unsubscribed, -1=Error
93
97 public $statut_dest = array();
98
102 public $source_url;
103
107 public $source_id;
108
112 public $source_type;
113
117 public $date_envoi;
118
124 public $tms;
125
129 public $error_text;
130
131 const STATUS_NOTSENT = 0;
132 const STATUS_SENT = 1;
133 const STATUS_READ = 2;
134 const STATUS_READANDUNSUBSCRIBED = 3;
135 const STATUS_ERROR = -1;
136
142 public function __construct($db)
143 {
144 $this->db = $db;
145
146 // List of language codes for status
147 $this->labelStatus[0] = 'TargetStatusNotSent';
148 $this->labelStatus[1] = 'TargetStatusSent';
149 $this->labelStatus[2] = 'TargetStatusRead';
150 $this->labelStatus[3] = 'TargetStatusReadAndUnsubscribed';
151 $this->labelStatus[-1] = 'TargetStatusError';
152
153 $this->statut_dest[0] = 'TargetStatusNotSent';
154 $this->statut_dest[1] = 'TargetStatusSent';
155 $this->statut_dest[2] = 'TargetStatusRead';
156 $this->statut_dest[3] = 'TargetStatusReadAndUnsubscribed'; // Read but ask to not be contacted anymore
157 $this->statut_dest[-1] = 'TargetStatusError';
158 }
159
166 public function create($user)
167 {
168 global $conf, $langs;
169
170 if (empty($this->fk_mailing)) {
171 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Mailing"));
172 return -2;
173 // we probably should also check that this number actually exists in ".MAIN_DB_PREFIX."mailing";
174 }
175 if (0 == $this->fk_mailing) {
176 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Mailing"));
177 return -4;
178 }
179 if (empty($this->email)) {
180 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Email"));
181 return -3;
182 }
183 if (empty($this->statut)) {
184 $statut = 0;
185 }
186 if (empty($this->status)) {
187 $status = 0;
188 }
189 if ($this->status !== $this->statut) {
190 $this->error = 'Status='.$this->status.' and Statut='.$this->statut.' field must be identical';
191 return -4;
192 }
193 if (empty($this->fk_contact)) {
194 $fk_contact = 0;
195 }
196
197 $error = 0;
198
199 $this->db->begin();
200
201
202 // 2025-10-09 06:33:26 DEBUG 192.168.127.1 52 33 sql=INSERT INTO llx_mailing_cibles (fk_mailing, fk_contact, email, statut) VALUES ('4', .((int) 0)., 'jon@jonb.dk', .((int) )).
203 //2025-10-09 06:35:13 DEBUG 192.168.127.1 54 33 sql=INSERT INTO llx_mailing_cibles (fk_mailing, fk_contact, email, statut) VALUES (4, .((int) 0)., 'jon@jonb.dk', .((int) )).
204
205 $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
206 $sql .= " (fk_mailing, fk_contact, email, statut)";
207 $sql .= " VALUES (".((int) $this->fk_mailing).", ";
208 $sql .= ((int) $this->fk_contact).", ";
209 $sql .= "'".$this->db->escape($this->email)."', ";
210 $sql .= ((int) $conf->statut)." )";
211
212 dol_syslog(__METHOD__, LOG_DEBUG);
213
214 $resql = $this->db->query($sql);
215 if ($resql) {
216 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing_cibles");
217
218 $result = $this->update($user);
219 if ($result < 0) {
220 $error++;
221 }
222
223 if (!$error) {
224 $this->db->commit();
225 return $this->id;
226 } else {
227 $this->db->rollback();
228 dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
229 return -2;
230 }
231 } else {
232 $this->error = $this->db->lasterror();
233 $this->db->rollback();
234 return -1;
235 }
236 }
237
244 public function delete($user)
245 {
246 $error = 0;
247
248 $this->db->begin();
249
250 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
251 $sql .= " WHERE rowid = " . ((int) $this->id);
252
253 dol_syslog(__METHOD__, LOG_DEBUG);
254 $resql = $this->db->query($sql);
255 if ($resql) {
256 dol_syslog(__METHOD__ . ' success');
257 $this->db->commit();
258 return 1;
259 } else {
260 $this->db->rollback();
261 $this->error = $this->db->lasterror();
262 return -1;
263 }
264 }
265
271 public function setNotSent()
272 {
273 $now = dol_now();
274
275 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
276 $sql .= " SET statut = ".((int) self::STATUS_NOTSENT).", tms = '".$this->db->idate($now)."'";
277 $sql .= " WHERE rowid = ".((int) $this->id);
278
279 dol_syslog("Mailing::valid", LOG_DEBUG);
280 if ($this->db->query($sql)) {
281 return 1;
282 } else {
283 $this->error = $this->db->lasterror();
284 return -1;
285 }
286 }
287
293 public function setSent()
294 {
295 $now = dol_now();
296
297 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
298 $sql .= " SET statut = ".((int) self::STATUS_SENT).", tms = '".$this->db->idate($now)."'";
299 $sql .= " WHERE rowid = ".((int) $this->id);
300
301 dol_syslog("Mailing::valid", LOG_DEBUG);
302 if ($this->db->query($sql)) {
303 return 1;
304 } else {
305 $this->error = $this->db->lasterror();
306 return -1;
307 }
308 }
309
315 public function setRead()
316 {
317 $now = dol_now();
318
319 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
320 $sql .= " SET statut = ".((int) self::STATUS_READ).", tms = '".$this->db->idate($now)."'";
321 $sql .= " WHERE rowid = ".((int) $this->id);
322
323 dol_syslog("Mailing::valid", LOG_DEBUG);
324 if ($this->db->query($sql)) {
325 return 1;
326 } else {
327 $this->error = $this->db->lasterror();
328 return -1;
329 }
330 }
331
337 public function setReadAndUnsubscribed()
338 {
339 $now = dol_now();
340
341 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
342 $sql .= " SET statut = ".((int) self::STATUS_READANDUNSUBSCRIBED).", tms = '".$this->db->idate($now)."'";
343 $sql .= " WHERE rowid = ".((int) $this->id);
344
345 dol_syslog("Mailing::valid", LOG_DEBUG);
346 if ($this->db->query($sql)) {
347 return 1;
348 } else {
349 $this->error = $this->db->lasterror();
350 return -1;
351 }
352 }
353
359 public function setError()
360 {
361 $now = dol_now();
362
363 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
364 $sql .= " SET statut = ".((int) self::STATUS_ERROR).", tms = '".$this->db->idate($now)."'";
365 $sql .= " WHERE rowid = ".((int) $this->id);
366
367 dol_syslog("Mailing::valid", LOG_DEBUG);
368 if ($this->db->query($sql)) {
369 return 1;
370 } else {
371 $this->error = $this->db->lasterror();
372 return -1;
373 }
374 }
375
382 public function update($user)
383 {
384 global $langs;
385
386 if (empty($this->fk_mailing)) {
387 return -2;
388 // we probably should also check that this number actually exists in ".MAIN_DB_PREFIX."mailing";
389 }
390 if (empty($this->email)) {
391 return -3;
392 }
393 if (empty($this->statut)) {
394 $statut = 0;
395 }
396 if (empty($this->status)) {
397 $status = 0;
398 }
399 if ($this->status !== $this->statut) {
400 return -4;
401 }
402 if (empty($this->fk_contact)) {
403 $fk_contact = 0;
404 }
405
406 $now = dol_now();
407 $error = 0;
408 $this->db->begin();
409
410 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
411 $sql .= " SET fk_mailing = '".((int) $this->fk_mailing)."'";
412 $sql .= ", fk_contact = '".((int) $this->fk_contact)."'";
413 $sql .= ", lastname = '".$this->db->escape($this->lastname)."'";
414 $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
415 $sql .= ", email = '".$this->db->escape($this->email)."'";
416 $sql .= ", other = '".$this->db->escape($this->other)."'";
417 $sql .= ", tag = '".$this->db->escape($this->tag)."'";
418 $sql .= ", statut = '".((int) $this->statut)."'";
419 $sql .= ", source_url = '".$this->db->escape($this->source_url)."'";
420 $sql .= ", source_id = '".((int) $this->source_id)."'";
421 $sql .= ", source_type = '".$this->db->escape($this->source_type)."'";
422 if ($this->date_envoi) {
423 $sql .= ", date_envoi = '".$this->db->idate($this->date_envoi)."'";
424 }
425 $sql .= ", error_text = '".($this->error_text ? $this->db->escape($this->error_text) : null)."'";
426 $sql .= " WHERE rowid = ".(int) $this->id;
427
428 dol_syslog(__METHOD__, LOG_DEBUG);
429 $resql = $this->db->query($sql);
430 if ($resql) {
431 dol_syslog(__METHOD__ . ' success');
432 $this->db->commit();
433 return 1;
434 } else {
435 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
436 $this->error = $langs->trans("ErrorRecordAlreadyExists", $this->email);
437 } else {
438 $this->error = $this->db->lasterror();
439 }
440 $this->db->rollback();
441 return -6;
442 }
443 }
444
451 public function fetch($rowid)
452 {
453 $sql = "SELECT t.rowid";
454 $sql .= ", t.fk_mailing";
455 $sql .= ", t.fk_contact";
456 $sql .= ", t.lastname";
457 $sql .= ", t.firstname";
458 $sql .= ", t.email";
459 $sql .= ", t.other";
460 $sql .= ", t.tag";
461 $sql .= ", t.statut as status";
462 $sql .= ", t.source_url";
463 $sql .= ", t.source_id";
464 $sql .= ", t.source_type";
465 $sql .= ", t.date_envoi";
466 $sql .= ", t.tms as date_modification";
467 $sql .= ", t.error_text";
468 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as t";
469 $sql .= " WHERE t.rowid = ".(int) $rowid;
470
471 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
472 $result = $this->db->query($sql);
473 if ($result) {
474 if ($this->db->num_rows($result)) {
475 $obj = $this->db->fetch_object($result);
476
477 $this->id = $obj->rowid;
478 $this->fk_mailing = $obj->fk_mailing;
479 $this->fk_contact = $obj->fk_contact;
480 $this->lastname = $obj->lastname;
481 $this->firstname = $obj->firstname;
482 $this->email = $obj->email;
483 $this->other = $obj->other;
484 $this->tag = $obj->tag;
485 $this->statut = $obj->status; // deprecated
486 $this->status = $obj->status;
487 $this->source_url = $obj->source_url;
488 $this->source_id = $obj->source_id;
489 $this->source_type = $obj->source_type;
490 $this->date_envoi = $this->db->jdate($obj->date_envoi);
491 $this->date_modification = $this->db->jdate($obj->date_modification); // tms
492 $this->tms = $this->db->jdate($obj->date_modification); // tms
493 $this->error_text = $obj->error_text;
494
495 return 1;
496 } else {
497 dol_syslog(get_class($this)."::fetch Error -1");
498 return -1;
499 }
500 } else {
501 dol_syslog(get_class($this)."::fetch Error -2");
502 return -2;
503 }
504 }
505}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage emailings module.
fetch($rowid)
Get object from database.
setRead()
Set read mailing target.
setReadAndUnsubscribed()
Set read and unsubscribed mailing target.
setSent()
Set sent mailing target.
create($user)
Create an Mailing Target.
setError()
Set error mailing target.
update($user)
Update an Mailing Target.
setNotSent()
Set notsent mailing target.
__construct($db)
Constructor.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
dol_now($mode='gmt')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.