dolibarr 25.0.0-alpha
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 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
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
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
28
33{
37 public $element = 'mailing_target';
38
42 public $table_element = 'mailing_cibles';
43
47 public $picto = 'contact';
48
52 public $fk_mailing;
53
57 public $fk_contact;
58
62 public $lastname;
63
67 public $firstname;
68
72 public $email;
73
77 public $other;
78
82 public $tag;
83
88 public $statut; // Status 0=Not sent, 1=Sent, 2=Read, 3=Read and unsubscribed, -1=Error
89
93 public $status; // Status 0=Not sent, 1=Sent, 2=Read, 3=Read and unsubscribed, -1=Error
94
98 public $statut_dest = array();
99
103 public $source_url;
104
108 public $source_id;
109
113 public $source_type;
114
118 public $date_envoi;
119
125 public $tms;
126
130 public $error_text;
131
132 const STATUS_NOTSENT = 0;
133 const STATUS_SENT = 1;
134 const STATUS_READ = 2;
135 const STATUS_READANDUNSUBSCRIBED = 3;
136 const STATUS_ERROR = -1;
137
143 public function __construct($db)
144 {
145 $this->db = $db;
146
147 // List of language codes for status
148 $this->labelStatus[0] = 'TargetStatusNotSent';
149 $this->labelStatus[1] = 'TargetStatusSent';
150 $this->labelStatus[2] = 'TargetStatusRead';
151 $this->labelStatus[3] = 'TargetStatusReadAndUnsubscribed';
152 $this->labelStatus[-1] = 'TargetStatusError';
153
154 $this->statut_dest[0] = 'TargetStatusNotSent';
155 $this->statut_dest[1] = 'TargetStatusSent';
156 $this->statut_dest[2] = 'TargetStatusRead';
157 $this->statut_dest[3] = 'TargetStatusReadAndUnsubscribed'; // Read but ask to not be contacted anymore
158 $this->statut_dest[-1] = 'TargetStatusError';
159 }
160
167 public function create($user)
168 {
169 global $conf, $langs;
170
171 if (empty($this->fk_mailing)) {
172 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Mailing"));
173 return -2;
174 // we probably should also check that this number actually exists in ".MAIN_DB_PREFIX."mailing";
175 }
176 if (0 == $this->fk_mailing) {
177 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Mailing"));
178 return -4;
179 }
180 if (empty($this->email)) {
181 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Email"));
182 return -3;
183 }
184 if (empty($this->statut)) {
185 $statut = 0;
186 }
187 if (empty($this->status)) {
188 $status = 0;
189 }
190 if ($this->status !== $this->statut) {
191 $this->error = 'Status='.$this->status.' and Statut='.$this->statut.' field must be identical';
192 return -4;
193 }
194 if (empty($this->fk_contact)) {
195 $fk_contact = 0;
196 }
197
198 $error = 0;
199
200 $this->db->begin();
201
202
203 // 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) )).
204 //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) )).
205
206 $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
207 $sql .= " (fk_mailing, fk_contact, email, statut)";
208 $sql .= " VALUES (".((int) $this->fk_mailing).", ";
209 $sql .= ((int) $this->fk_contact).", ";
210 $sql .= "'".$this->db->escape($this->email)."', ";
211 $sql .= ((int) $conf->statut)." )";
212
213 dol_syslog(__METHOD__, LOG_DEBUG);
214
215 $resql = $this->db->query($sql);
216 if ($resql) {
217 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing_cibles");
218
219 $result = $this->update($user);
220 if ($result < 0) {
221 $error++;
222 }
223
224 if (!$error) {
225 $this->db->commit();
226 return $this->id;
227 } else {
228 $this->db->rollback();
229 dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
230 return -2;
231 }
232 } else {
233 $this->error = $this->db->lasterror();
234 $this->db->rollback();
235 return -1;
236 }
237 }
238
245 public function delete($user)
246 {
247 $error = 0;
248
249 $this->db->begin();
250
251 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
252 $sql .= " WHERE rowid = " . ((int) $this->id);
253
254 dol_syslog(__METHOD__, LOG_DEBUG);
255 $resql = $this->db->query($sql);
256 if ($resql) {
257 dol_syslog(__METHOD__ . ' success');
258 $this->db->commit();
259 return 1;
260 } else {
261 $this->db->rollback();
262 $this->error = $this->db->lasterror();
263 return -1;
264 }
265 }
266
272 public function setNotSent()
273 {
274 $now = dol_now();
275
276 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
277 $sql .= " SET statut = ".((int) self::STATUS_NOTSENT).", tms = '".$this->db->idate($now)."'";
278 $sql .= " WHERE rowid = ".((int) $this->id);
279
280 dol_syslog("Mailing::valid", LOG_DEBUG);
281 if ($this->db->query($sql)) {
282 return 1;
283 } else {
284 $this->error = $this->db->lasterror();
285 return -1;
286 }
287 }
288
294 public function setSent()
295 {
296 $now = dol_now();
297
298 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
299 $sql .= " SET statut = ".((int) self::STATUS_SENT).", tms = '".$this->db->idate($now)."'";
300 $sql .= " WHERE rowid = ".((int) $this->id);
301
302 dol_syslog("Mailing::valid", LOG_DEBUG);
303 if ($this->db->query($sql)) {
304 return 1;
305 } else {
306 $this->error = $this->db->lasterror();
307 return -1;
308 }
309 }
310
316 public function setRead()
317 {
318 $now = dol_now();
319
320 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
321 $sql .= " SET statut = ".((int) self::STATUS_READ).", tms = '".$this->db->idate($now)."'";
322 $sql .= " WHERE rowid = ".((int) $this->id);
323
324 dol_syslog("Mailing::valid", LOG_DEBUG);
325 if ($this->db->query($sql)) {
326 return 1;
327 } else {
328 $this->error = $this->db->lasterror();
329 return -1;
330 }
331 }
332
338 public function setReadAndUnsubscribed()
339 {
340 $now = dol_now();
341
342 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
343 $sql .= " SET statut = ".((int) self::STATUS_READANDUNSUBSCRIBED).", tms = '".$this->db->idate($now)."'";
344 $sql .= " WHERE rowid = ".((int) $this->id);
345
346 dol_syslog("Mailing::valid", LOG_DEBUG);
347 if ($this->db->query($sql)) {
348 return 1;
349 } else {
350 $this->error = $this->db->lasterror();
351 return -1;
352 }
353 }
354
360 public function setError()
361 {
362 $now = dol_now();
363
364 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles ";
365 $sql .= " SET statut = ".((int) self::STATUS_ERROR).", tms = '".$this->db->idate($now)."'";
366 $sql .= " WHERE rowid = ".((int) $this->id);
367
368 dol_syslog("Mailing::valid", LOG_DEBUG);
369 if ($this->db->query($sql)) {
370 return 1;
371 } else {
372 $this->error = $this->db->lasterror();
373 return -1;
374 }
375 }
376
383 public function update($user)
384 {
385 global $langs;
386
387 if (empty($this->fk_mailing)) {
388 return -2;
389 // we probably should also check that this number actually exists in ".MAIN_DB_PREFIX."mailing";
390 }
391 if (empty($this->email)) {
392 return -3;
393 }
394 if (empty($this->statut)) {
395 $statut = 0;
396 }
397 if (empty($this->status)) {
398 $status = 0;
399 }
400 if ($this->status !== $this->statut) {
401 return -4;
402 }
403 if (empty($this->fk_contact)) {
404 $fk_contact = 0;
405 }
406
407 $now = dol_now();
408 $error = 0;
409 $this->db->begin();
410
411 $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
412 $sql .= " SET fk_mailing = ".((int) $this->fk_mailing);
413 $sql .= ", fk_contact = ".((int) $this->fk_contact);
414 $sql .= ", lastname = '".$this->db->escape($this->lastname)."'";
415 $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
416 $sql .= ", email = '".$this->db->escape($this->email)."'";
417 $sql .= ", other = '".$this->db->escape($this->other)."'";
418 $sql .= ", tag = '".$this->db->escape($this->tag)."'";
419 $sql .= ", statut = ".((int) $this->statut);
420 $sql .= ", source_url = '".$this->db->escape($this->source_url)."'";
421 $sql .= ", source_id = ".((int) $this->source_id);
422 $sql .= ", source_type = '".$this->db->escape($this->source_type)."'";
423 if ($this->date_envoi) {
424 $sql .= ", date_envoi = '".$this->db->idate($this->date_envoi)."'";
425 }
426 $sql .= ", error_text = '".($this->error_text ? $this->db->escape($this->error_text) : null)."'";
427 $sql .= " WHERE rowid = ".(int) $this->id;
428
429 dol_syslog(__METHOD__, LOG_DEBUG);
430 $resql = $this->db->query($sql);
431 if ($resql) {
432 dol_syslog(__METHOD__ . ' success');
433 $this->db->commit();
434 return 1;
435 } else {
436 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
437 $this->error = $langs->trans("ErrorRecordAlreadyExists", $this->email);
438 } else {
439 $this->error = $this->db->lasterror();
440 }
441 $this->db->rollback();
442 return -6;
443 }
444 }
445
452 public function fetch($rowid)
453 {
454 $sql = "SELECT t.rowid";
455 $sql .= ", t.fk_mailing";
456 $sql .= ", t.fk_contact";
457 $sql .= ", t.lastname";
458 $sql .= ", t.firstname";
459 $sql .= ", t.email";
460 $sql .= ", t.other";
461 $sql .= ", t.tag";
462 $sql .= ", t.statut as status";
463 $sql .= ", t.source_url";
464 $sql .= ", t.source_id";
465 $sql .= ", t.source_type";
466 $sql .= ", t.date_envoi";
467 $sql .= ", t.tms as date_modification";
468 $sql .= ", t.error_text";
469 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as t";
470 $sql .= " WHERE t.rowid = ".(int) $rowid;
471
472 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
473 $result = $this->db->query($sql);
474 if ($result) {
475 if ($this->db->num_rows($result)) {
476 $obj = $this->db->fetch_object($result);
477
478 $this->id = $obj->rowid;
479 $this->fk_mailing = $obj->fk_mailing;
480 $this->fk_contact = $obj->fk_contact;
481 $this->lastname = $obj->lastname;
482 $this->firstname = $obj->firstname;
483 $this->email = $obj->email;
484 $this->other = $obj->other;
485 $this->tag = $obj->tag;
486 $this->statut = $obj->status; // deprecated
487 $this->status = $obj->status;
488 $this->source_url = $obj->source_url;
489 $this->source_id = $obj->source_id;
490 $this->source_type = $obj->source_type;
491 $this->date_envoi = $this->db->jdate($obj->date_envoi);
492 $this->date_modification = $this->db->jdate($obj->date_modification); // tms
493 $this->tms = $this->db->jdate($obj->date_modification); // tms
494 $this->error_text = $obj->error_text;
495
496 return 1;
497 } else {
498 dol_syslog(get_class($this)."::fetch Error -1");
499 return -1;
500 }
501 } else {
502 dol_syslog(get_class($this)."::fetch Error -2");
503 return -2;
504 }
505 }
506}
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:169
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").
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.