dolibarr 23.0.3
api_mailings.class.php
1<?php
2/* Copyright (C) 2025 Cloned from htdocs/comm/propal/class/api_proposals.class.php then modified
3 * Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
4 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
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
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
23require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing_targets.class.php';
24
33class Mailings extends DolibarrApi
34{
38 public static $FIELDS = array(
39 'title',
40 'sujet',
41 'body'
42 );
43
47 public static $TARGETFIELDS = array(
48 'fk_mailing',
49 'email'
50 );
51
55 public $mailing;
56
60 public $mailing_target;
61
65 public function __construct()
66 {
67 global $db;
68 $this->db = $db;
69 $this->mailing = new Mailing($this->db);
70 $this->mailing_target = new MailingTarget($this->db);
71 }
72
85 public function get($id)
86 {
87 return $this->_fetch($id);
88 }
89
100 private function _fetch($id)
101 {
102 if (!DolibarrApiAccess::$user->hasRight('mailing', 'read')) {
103 throw new RestException(403);
104 }
105
106 $result = $this->mailing->fetch($id);
107 if ($result < 0) {
108 throw new RestException(404, 'Mass mailing not found, id='.$id);
109 }
110
111 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
112 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
113 }
114
115 $this->mailing->fetchObjectLinked();
116
117 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
118 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
119 }
120
121 return $this->_cleanObjectDatas($this->mailing);
122 }
123
147 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $fk_projects = '', $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
148 {
149 if (!DolibarrApiAccess::$user->hasRight('mailing', 'read')) {
150 throw new RestException(403);
151 }
152
153 $arrayProjects = explode(",", $fk_projects);
154 foreach ($arrayProjects as $project => $value) {
155 if (!DolibarrApi::_checkAccessToResource('project', ((int) $value))) {
156 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
157 }
158 }
159
160 $obj_ret = array();
161
162 $sql = "SELECT t.rowid";
163 $sql .= " FROM ".MAIN_DB_PREFIX."mailing AS t";
164 $sql .= ' WHERE t.entity IN ('.getEntity('mailing').')';
165 if ($fk_projects) {
166 $sql .= " AND t.fk_project IN (".$this->db->sanitize($fk_projects).")";
167 } // Add sql filters
168 if ($sqlfilters) {
169 $errormessage = '';
170 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
171 if ($errormessage) {
172 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
173 }
174 }
175
176 //this query will return total mass mailings with the filters given
177 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
178
179 $sql .= $this->db->order($sortfield, $sortorder);
180 if ($limit) {
181 if ($page < 0) {
182 $page = 0;
183 }
184 $offset = $limit * $page;
185
186 $sql .= $this->db->plimit($limit + 1, $offset);
187 }
188
189 dol_syslog("API Rest request mass mailing");
190 $result = $this->db->query($sql);
191
192 if ($result) {
193 $num = $this->db->num_rows($result);
194 $min = min($num, ($limit <= 0 ? $num : $limit));
195 $i = 0;
196 while ($i < $min) {
197 $obj = $this->db->fetch_object($result);
198 $mailing_static = new Mailing($this->db);
199 if ($mailing_static->fetch($obj->rowid) > 0) {
200 if ($loadlinkedobjects) {
201 // retrieve linked objects
202 $mailing_static->fetchObjectLinked();
203 }
204
205 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($mailing_static), $properties);
206 }
207 $i++;
208 }
209 } else {
210 throw new RestException(503, 'Error when retrieve list of mass mailings : '.$this->db->lasterror());
211 }
212
213 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
214 if ($pagination_data) {
215 $totalsResult = $this->db->query($sqlTotals);
216 $total = $this->db->fetch_object($totalsResult)->total;
217
218 $tmp = $obj_ret;
219 $obj_ret = [];
220
221 $obj_ret['data'] = $tmp;
222 $obj_ret['pagination'] = [
223 'total' => (int) $total,
224 'page' => $page, //count starts from 0
225 'page_count' => ceil((int) $total / $limit),
226 'limit' => $limit
227 ];
228 }
229
230 return $obj_ret;
231 }
232
258 public function indexTargets($id, $sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
259 {
260 if (!DolibarrApiAccess::$user->hasRight('mailing', 'read')) {
261 throw new RestException(403);
262 }
263
264 $fetchMailingResult = $this->mailing->fetch($id);
265 if ($fetchMailingResult < 0) {
266 throw new RestException(404, 'Mass mailing not found, id='.$id);
267 }
268
269 $fk_project = $this->mailing->fk_project;
270 if (!DolibarrApi::_checkAccessToResource('project', ((int) $fk_project))) {
271 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
272 }
273
274 $obj_ret = array();
275
276 $sql = "SELECT t.rowid";
277 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles AS t";
278 $sql .= " WHERE t.fk_mailing = ".((int) $id);
279
280 // Add sql filters
281 if ($sqlfilters) {
282 $errormessage = '';
283 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
284 if ($errormessage) {
285 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
286 }
287 }
288
289 //this query will return total mass mailing targets with the filters given
290 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
291
292 $sql .= $this->db->order($sortfield, $sortorder);
293 if ($limit) {
294 if ($page < 0) {
295 $page = 0;
296 }
297 $offset = $limit * $page;
298
299 $sql .= $this->db->plimit($limit + 1, $offset);
300 }
301
302 dol_syslog("API Rest request mass mailing target");
303 $result = $this->db->query($sql);
304
305 if ($result) {
306 $num = $this->db->num_rows($result);
307 $min = min($num, ($limit <= 0 ? $num : $limit));
308 $i = 0;
309 while ($i < $min) {
310 $obj = $this->db->fetch_object($result);
311 $mailing_target = new MailingTarget($this->db);
312 if ($mailing_target->fetch($obj->rowid) > 0) {
313 $obj_ret[] = $this->_filterObjectProperties($this->_cleanTargetDatas($mailing_target), $properties);
314 }
315 $i++;
316 }
317 } else {
318 throw new RestException(503, 'Error when retrieve list of mass mailing targetss : '.$this->db->lasterror());
319 }
320
321 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
322 if ($pagination_data) {
323 $totalsResult = $this->db->query($sqlTotals);
324 $total = $this->db->fetch_object($totalsResult)->total;
325
326 $tmp = $obj_ret;
327 $obj_ret = [];
328
329 $obj_ret['data'] = $tmp;
330 $obj_ret['pagination'] = [
331 'total' => (int) $total,
332 'page' => $page, //count starts from 0
333 'page_count' => ceil((int) $total / $limit),
334 'limit' => $limit
335 ];
336 }
337
338 return $obj_ret;
339 }
340
356 public function clone($id, $cloneContent = 1, $cloneRecipients = 1, $notrigger = 0)
357 {
358 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
359 throw new RestException(403, "Insufficient rights");
360 }
361 $result = $this->mailing->fetch($id);
362 if ($result < 0) {
363 throw new RestException(404, 'Mass mailing to clone not found, id='.$id);
364 }
365
366 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
367 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
368 }
369
370 $cloningResult = $this->mailing->createFromClone(DolibarrApiAccess::$user, ((int) $id), ((int) $cloneContent), ((int) $cloneRecipients), ((int) $notrigger));
371 if ($cloningResult < 0) {
372 throw new RestException(500, "Error cloning mass mailing", array_merge(array($this->mailing->error), $this->mailing->errors));
373 }
374
375 if ($cloningResult > 0) {
376 return $this->get($cloningResult);
377 } else {
378 throw new RestException(500, $this->mailing->error);
379 }
380 }
381
395 public function post($request_data = null)
396 {
397 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
398 throw new RestException(403, "Insufficiant rights");
399 }
400 // Check mandatory fields
401 $result = $this->_validate($request_data);
402
403 foreach ($request_data as $field => $value) {
404 if ($field === 'caller') {
405 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
406 $this->mailing->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
407 continue;
408 }
409 if ($field === 'fk_project') {
410 if (!DolibarrApi::_checkAccessToResource('project', ((int) $value))) {
411 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
412 }
413 }
414
415 $this->mailing->$field = $this->_checkValForAPI($field, $value, $this->mailing);
416 }
417
418 if ($this->mailing->create(DolibarrApiAccess::$user) < 0) {
419 throw new RestException(500, "Error creating mass mailing", array_merge(array($this->mailing->error), $this->mailing->errors));
420 }
421
422 return ((int) $this->mailing->id);
423 }
424
440 public function put($id, $request_data = null)
441 {
442 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
443 throw new RestException(403);
444 }
445
446 $result = $this->mailing->fetch($id);
447 if ($result < 0) {
448 throw new RestException(404, 'Mass mailing not found, id='.$id);
449 }
450
451 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
452 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
453 }
454
455 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
456 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
457 }
458 foreach ($request_data as $field => $value) {
459 if ($field == 'id') {
460 continue;
461 }
462 if ($field === 'caller') {
463 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
464 $this->mailing->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
465 continue;
466 }
467 if ($field == 'array_options' && is_array($value)) {
468 foreach ($value as $index => $val) {
469 $this->mailing->array_options[$index] = $this->_checkValForAPI($field, $val, $this->mailing);
470 }
471 continue;
472 }
473
474 $this->mailing->$field = $this->_checkValForAPI($field, $value, $this->mailing);
475 }
476
477 if ($this->mailing->update(DolibarrApiAccess::$user) > 0) {
478 return $this->get($id);
479 } else {
480 throw new RestException(500, $this->mailing->error);
481 }
482 }
483
498 public function delete($id)
499 {
500 if (!DolibarrApiAccess::$user->hasRight('mailing', 'delete')) {
501 throw new RestException(403);
502 }
503 $result = $this->mailing->fetch($id);
504 if ($result < 0) {
505 throw new RestException(404, 'Mass mailing not found, id='.$id);
506 }
507
508 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
509 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
510 }
511
512 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
513 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
514 }
515
516 if (!$this->mailing->delete(DolibarrApiAccess::$user)) {
517 throw new RestException(500, 'Error when delete Mass mailing : '.$this->mailing->error);
518 }
519
520 return array(
521 'success' => array(
522 'code' => 200,
523 'message' => 'Mass mailing with id='.$id.' deleted'
524 )
525 );
526 }
527
546 public function updateTarget($id, $targetid, $request_data = null)
547 {
548 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
549 throw new RestException(403);
550 }
551
552 $fetchMailingResult = $this->mailing->fetch($id);
553 if ($fetchMailingResult < 0) {
554 throw new RestException(404, 'Mass mailing not found, id='.$id);
555 }
556 $result = $this->mailing_target->fetch($targetid);
557 if ($result < 0) {
558 throw new RestException(404, 'Mass mailing target not found, id='.$targetid);
559 }
560 if ($id != $this->mailing_target->fk_mailing) {
561 throw new RestException(404, 'Target id='.$targetid.' is does not belong to mailing id='.$id);
562 }
563
564 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
565 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
566 }
567
568 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
569 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
570 }
571 foreach ($request_data as $field => $value) {
572 if ($field == 'id') {
573 throw new RestException(400, 'Changing id field is forbidden');
574 }
575 if ($field == 'fk_mailing') {
576 throw new RestException(400, 'Changing fk_mailing field is forbidden to protect inserting a wrong fk_mailing number. Use a POST to create a new mailing target with the correct mailing id, then an PUT to update the new target in the right mailing id, and finally a delete to remove the old target');
577 }
578 if ($field === 'caller') {
579 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
580 $this->mailing_target->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
581 continue;
582 }
583
584 $this->mailing_target->$field = $this->_checkValForAPI($field, $value, $this->mailing_target);
585 }
586
587 if ($this->mailing_target->update(DolibarrApiAccess::$user) > 0) {
588 return $this->getTarget($id, $targetid);
589 } else {
590 throw new RestException(500, $this->mailing_target->error);
591 }
592 }
593
612 public function postTarget($id, $request_data = null)
613 {
614 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
615 throw new RestException(403, "Insufficiant rights");
616 }
617 // Check mandatory fields
618 $result = $this->_validateTarget($request_data);
619
620 $fk_mailing_id = 0;
621
622 foreach ($request_data as $field => $value) {
623 if ($field === 'caller') {
624 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
625 $this->mailing_target->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
626 continue;
627 }
628 if ($field === 'fk_project') {
629 if (!DolibarrApi::_checkAccessToResource('project', ((int) $value))) {
630 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
631 }
632 }
633
634 if ($field == 'id') {
635 throw new RestException(400, 'Creating with id field is forbidden');
636 }
637 if ($field == 'fk_mailing') {
638 $fetchMailingResult = $this->mailing->fetch((int) $value);
639 if ($fetchMailingResult < 0) {
640 throw new RestException(404, 'Mass mailing not found, id='.((int) $value));
641 }
642 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
643 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
644 }
645 $fk_mailing_id = ((int) $value);
646 }
647
648 $this->mailing_target->$field = $this->_checkValForAPI($field, $value, $this->mailing_target);
649 }
650
651 if (0 == $fk_mailing_id) {
652 throw new RestException(404, 'Mass mailing not found, id='.((int) $fk_mailing_id));
653 }
654
655 if ($this->mailing_target->create(DolibarrApiAccess::$user) < 0) {
656 throw new RestException(500, "Error creating mass mailing target", array_merge(array($this->mailing->error), $this->mailing->errors));
657 }
658
659 return ((int) $this->mailing_target->id);
660 }
661
677 public function getTarget($id, $targetid)
678 {
679 return $this->_fetchTarget($id, $targetid);
680 }
681
694 private function _fetchTarget($id, $targetid)
695 {
696 if (!DolibarrApiAccess::$user->hasRight('mailing', 'read')) {
697 throw new RestException(403);
698 }
699
700 $fetchMailingResult = $this->mailing->fetch($id);
701 if ($fetchMailingResult < 0) {
702 throw new RestException(404, 'Mass mailing not found, id='.$id);
703 }
704 $result = $this->mailing_target->fetch($targetid);
705 if ($result < 0) {
706 throw new RestException(404, 'Mass mailing target not found, id='.$targetid);
707 }
708 if ($id != $this->mailing_target->fk_mailing) {
709 throw new RestException(404, 'Target id='.$targetid.' is does not belong to mailing id='.$id);
710 }
711
712 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
713 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
714 }
715
716 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
717 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
718 }
719
720 return $this->_cleanTargetDatas($this->mailing_target);
721 }
722
740 public function deleteTarget($id, $targetid)
741 {
742 if (!DolibarrApiAccess::$user->hasRight('mailing', 'delete')) {
743 throw new RestException(403);
744 }
745
746 $fetchMailingResult = $this->mailing->fetch($id);
747 if ($fetchMailingResult < 0) {
748 throw new RestException(404, 'Mass mailing not found, id='.$id);
749 }
750 $result = $this->mailing_target->fetch($targetid);
751 if ($result < 0) {
752 throw new RestException(404, 'Mass mailing target not found, id='.$targetid);
753 }
754 if ($id != $this->mailing_target->fk_mailing) {
755 throw new RestException(404, 'Target id='.$targetid.' is does not belong to mailing id='.$id);
756 }
757 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
758 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
759 }
760 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
761 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
762 }
763
764 if (!$this->mailing_target->delete(DolibarrApiAccess::$user)) {
765 throw new RestException(500, 'Error when delete Mass mailing target: '.$this->mailing->error);
766 }
767
768 return array(
769 'success' => array(
770 'code' => 200,
771 'message' => 'Deleting target id='.$targetid.' belonging to mailing id='.$id
772 )
773 );
774 }
775
792 public function deleteTargets($id)
793 {
794 if (!DolibarrApiAccess::$user->hasRight('mailing', 'delete')) {
795 throw new RestException(403);
796 }
797 $result = $this->mailing->fetch($id);
798 if ($result < 0) {
799 throw new RestException(404, 'Mass mailing not found, id='.$id);
800 }
801
802 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
803 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
804 }
805
806 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
807 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
808 }
809
810 $count = $this->mailing->countNbOfTargets('all');
811 if (!$this->mailing->delete_targets()) {
812 throw new RestException(500, 'Error when delete targets of Mass mailing : '.$this->mailing->error);
813 }
814
815 return array(
816 'success' => array(
817 'code' => 200,
818 'message' => 'Target recipients ('.$count.') of Mass mailing with id='.$id.' deleted'
819 )
820 );
821 }
822
839 public function resetTargetsStatus($id)
840 {
841 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
842 throw new RestException(403);
843 }
844 $result = $this->mailing->fetch($id);
845 if ($result < 0) {
846 throw new RestException(404, 'Mass mailing not found, id='.$id);
847 }
848
849 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
850 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
851 }
852
853 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
854 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
855 }
856
857 $count = $this->mailing->countNbOfTargets('all');
858 if (!$this->mailing->reset_targets_status(DolibarrApiAccess::$user)) {
859 throw new RestException(500, 'Error when reset targets status of Mass mailing : '.$this->mailing->error);
860 }
861
862 return array(
863 'success' => array(
864 'code' => 200,
865 'message' => 'Resetting status of '.$count.' target recipients in Mass mailing with id='.$id
866 )
867 );
868 }
869
885 public function settodraft($id)
886 {
887 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
888 throw new RestException(403);
889 }
890 $result = $this->mailing->fetch($id);
891 if ($result < 0) {
892 throw new RestException(404, 'Mass mailing not found, id='.$id);
893 }
894
895 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
896 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
897 }
898
899 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
900 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
901 }
902
903 $result = $this->mailing->setDraft(DolibarrApiAccess::$user);
904 if ($result == 0) {
905 throw new RestException(304, 'Nothing done. May be object is already draft');
906 }
907 if ($result < 0) {
908 throw new RestException(500, 'Error : '.$this->mailing->error);
909 }
910
911 $this->mailing->fetchObjectLinked();
912
913 return $this->_cleanObjectDatas($this->mailing);
914 }
915
916
937 public function validate($id)
938 {
939 if (!DolibarrApiAccess::$user->hasRight('mailing', 'write')) {
940 throw new RestException(403);
941 }
942 $result = $this->mailing->fetch($id);
943 if ($result < 0) {
944 throw new RestException(404, 'Mass mailing not found, id='.$id);
945 }
946
947 if (!DolibarrApi::_checkAccessToResource('project', ((int) $this->mailing->fk_project))) {
948 throw new RestException(403, 'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
949 }
950
951 if (!DolibarrApi::_checkAccessToResource('mailing', $this->mailing->id)) {
952 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
953 }
954
955 $result = $this->mailing->valid(DolibarrApiAccess::$user);
956 if ($result == 0) {
957 throw new RestException(304, 'Error nothing done. May be object is already validated');
958 }
959 if ($result < 0) {
960 throw new RestException(500, 'Error when validating Mass mailing: '.$this->mailing->error);
961 }
962
963 $this->mailing->fetchObjectLinked();
964
965 return $this->_cleanObjectDatas($this->mailing);
966 }
967
976 private function _validate($data)
977 {
978 if ($data === null) {
979 $data = array();
980 }
981 $mailing = array();
982 foreach (Mailings::$FIELDS as $field) {
983 if (!isset($data[$field])) {
984 throw new RestException(400, "$field field missing");
985 }
986 $mailing[$field] = $data[$field];
987 }
988 return $mailing;
989 }
990
999 private function _validateTarget($data)
1000 {
1001 if ($data === null) {
1002 $data = array();
1003 }
1004 $mailing_target = array();
1005 foreach (Mailings::$TARGETFIELDS as $field) {
1006 if (!isset($data[$field])) {
1007 throw new RestException(400, "$field field missing");
1008 }
1009 $mailing_target[$field] = $data[$field];
1010 }
1011 return $mailing_target;
1012 }
1013
1014 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1021 protected function _cleanTargetDatas($object)
1022 {
1023 // phpcs:enable
1024 $object = parent::_cleanObjectDatas($object);
1025
1026 unset($object->TRIGGER_PREFIX);
1027 unset($object->actionmsg);
1028 unset($object->actionmsg2);
1029 unset($object->actiontypecode);
1030 unset($object->alreadypaid);
1031 unset($object->array_options);
1032 unset($object->array_languages);
1033 unset($object->barcode_type_code);
1034 unset($object->barcode_type_coder);
1035 unset($object->barcode_type_label);
1036 unset($object->barcode_type);
1037 unset($object->canvas);
1038 unset($object->civility_code);
1039 unset($object->civility_id);
1040 unset($object->comments);
1041 unset($object->cond_reglement_id);
1042 unset($object->cond_reglement_supplier_id);
1043 unset($object->contact_id);
1044 unset($object->contact);
1045 unset($object->contacts_ids_internal);
1046 unset($object->contacts_ids);
1047 unset($object->context);
1048 unset($object->country_code);
1049 unset($object->country_id);
1050 unset($object->country);
1051 unset($object->date_cloture);
1052 unset($object->date_creation);
1053 unset($object->date_validation);
1054 unset($object->db);
1055 unset($object->demand_reason_id);
1056 unset($object->deposit_percent);
1057 unset($object->element_for_permission);
1058 unset($object->element);
1059 unset($object->entity);
1060 unset($object->error);
1061 unset($object->errorhidden);
1062 unset($object->errors);
1063 unset($object->extraparams);
1064 unset($object->fields);
1065 unset($object->fk_account);
1066 unset($object->fk_bank);
1067 unset($object->fk_delivery_address);
1068 unset($object->fk_element);
1069 unset($object->fk_multicurrency);
1070 unset($object->fk_projet);
1071 unset($object->fk_project);
1072 unset($object->fk_user_creat);
1073 unset($object->fk_user_modif);
1074 unset($object->import_key);
1075 unset($object->isextrafieldmanaged);
1076 unset($object->ismultientitymanaged);
1077 unset($object->last_main_doc);
1078 unset($object->lines);
1079 unset($object->linked_objects);
1080 unset($object->linkedObjects);
1081 unset($object->linkedObjectsIds);
1082 unset($object->mode_reglement_id);
1083 unset($object->model_pdf);
1084 unset($object->module);
1085 unset($object->multicurrency_code);
1086 unset($object->multicurrency_total_ht);
1087 unset($object->multicurrency_total_localtax1);
1088 unset($object->multicurrency_total_localtax2);
1089 unset($object->multicurrency_total_ttc);
1090 unset($object->multicurrency_total_tva);
1091 unset($object->multicurrency_tx);
1092 unset($object->name);
1093 unset($object->nb);
1094 unset($object->nbphoto);
1095 unset($object->newref);
1096 unset($object->next_prev_filter);
1097 unset($object->note);
1098 unset($object->note_public);
1099 unset($object->note_private);
1100 unset($object->oldcopy);
1101 unset($object->oldref);
1102 unset($object->origin_id);
1103 unset($object->origin_object);
1104 unset($object->origin_type);
1105 unset($object->origin);
1106 unset($object->output);
1107 unset($object->product);
1108 unset($object->project);
1109 unset($object->ref_ext);
1110 unset($object->ref_next);
1111 unset($object->ref_previous);
1112 unset($object->ref);
1113 unset($object->region_code);
1114 unset($object->region_id);
1115 unset($object->region);
1116 unset($object->restrictiononfksoc);
1117 unset($object->retained_warranty_fk_cond_reglement);
1118 unset($object->sendtoid);
1119 unset($object->shipping_method_id);
1120 unset($object->shipping_method);
1121 unset($object->showphoto_on_popup);
1122 unset($object->specimen);
1123 unset($object->state_code);
1124 unset($object->state_id);
1125 unset($object->state);
1126 unset($object->table_element_line);
1127 unset($object->table_element);
1128 unset($object->thirdparty);
1129 unset($object->total_ht);
1130 unset($object->total_localtax1);
1131 unset($object->total_localtax2);
1132 unset($object->total_ttc);
1133 unset($object->total_tva);
1134 unset($object->totalpaid_multicurrency);
1135 unset($object->totalpaid);
1136 unset($object->tpl);
1137 unset($object->transport_mode_id);
1138 unset($object->user);
1139 unset($object->user_creation_id);
1140 unset($object->user_validation_id);
1141 unset($object->user_closing_id);
1142 unset($object->user_modification_id);
1143 unset($object->warehouse_id);
1144
1145 return $object;
1146 }
1147
1148 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1158 protected function _cleanObjectDatas($object)
1159 {
1160 // phpcs:enable
1161 $object = parent::_cleanObjectDatas($object);
1162
1163 unset($object->actionmsg);
1164 unset($object->actionmsg2);
1165 unset($object->actiontypecode);
1166 unset($object->alreadypaid);
1167 unset($object->barcode_type_code);
1168 unset($object->barcode_type_coder);
1169 unset($object->barcode_type_label);
1170 unset($object->barcode_type);
1171 unset($object->canvas);
1172 unset($object->civility_code);
1173 unset($object->civility_id);
1174 unset($object->comments);
1175 unset($object->cond_reglement_id);
1176 unset($object->cond_reglement_supplier_id);
1177 unset($object->contact_id);
1178 unset($object->contact);
1179 unset($object->contacts_ids_internal);
1180 unset($object->contacts_ids);
1181 unset($object->context);
1182 unset($object->country_code);
1183 unset($object->country_id);
1184 unset($object->country);
1185 unset($object->db);
1186 unset($object->demand_reason_id);
1187 unset($object->deposit_percent);
1188 unset($object->element_for_permission);
1189 unset($object->element);
1190 unset($object->error);
1191 unset($object->errorhidden);
1192 unset($object->errors);
1193 unset($object->fields);
1194 unset($object->firstname);
1195 unset($object->fk_account);
1196 unset($object->fk_bank);
1197 unset($object->fk_delivery_address);
1198 unset($object->fk_element);
1199 unset($object->fk_multicurrency);
1200 unset($object->fk_projet);
1201 unset($object->import_key);
1202 unset($object->isextrafieldmanaged);
1203 unset($object->ismultientitymanaged);
1204 unset($object->last_main_doc);
1205 unset($object->lastname);
1206 unset($object->lines);
1207 unset($object->linked_objects);
1208 unset($object->linkedObjects);
1209 unset($object->linkedObjectsIds);
1210 unset($object->mode_reglement_id);
1211 unset($object->model_pdf);
1212 unset($object->module);
1213 unset($object->multicurrency_code);
1214 unset($object->multicurrency_total_ht);
1215 unset($object->multicurrency_total_localtax1);
1216 unset($object->multicurrency_total_localtax2);
1217 unset($object->multicurrency_total_ttc);
1218 unset($object->multicurrency_total_tva);
1219 unset($object->multicurrency_tx);
1220 unset($object->name);
1221 unset($object->nb);
1222 unset($object->nbphoto);
1223 unset($object->newref);
1224 unset($object->next_prev_filter);
1225 unset($object->note);
1226 unset($object->oldcopy);
1227 unset($object->oldref);
1228 unset($object->origin_id);
1229 unset($object->origin_object);
1230 unset($object->origin_type);
1231 unset($object->origin);
1232 unset($object->output);
1233 unset($object->product);
1234 unset($object->project);
1235 unset($object->ref_ext);
1236 unset($object->ref_next);
1237 unset($object->ref_previous);
1238 unset($object->ref);
1239 unset($object->region_code);
1240 unset($object->region_id);
1241 unset($object->region);
1242 unset($object->restrictiononfksoc);
1243 unset($object->retained_warranty_fk_cond_reglement);
1244 unset($object->sendtoid);
1245 unset($object->shipping_method_id);
1246 unset($object->shipping_method);
1247 unset($object->showphoto_on_popup);
1248 unset($object->specimen);
1249 unset($object->state_code);
1250 unset($object->state_id);
1251 unset($object->state);
1252 unset($object->table_element_line);
1253 unset($object->table_element);
1254 unset($object->thirdparty);
1255 unset($object->total_ht);
1256 unset($object->total_localtax1);
1257 unset($object->total_localtax2);
1258 unset($object->total_ttc);
1259 unset($object->total_tva);
1260 unset($object->totalpaid_multicurrency);
1261 unset($object->totalpaid);
1262 unset($object->tpl);
1263 unset($object->transport_mode_id);
1264 unset($object->user);
1265 unset($object->warehouse_id);
1266
1267 return $object;
1268 }
1269}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
Class to manage emailings module.
Class to manage emailings module.
_validateTarget($data)
Validate fields before create or update object.
clone($id, $cloneContent=1, $cloneRecipients=1, $notrigger=0)
Clone a mass mailing.
getTarget($id, $targetid)
Get a target in a mass mailing.
post($request_data=null)
Create a mass mailing.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
_fetch($id)
Get properties of an mailing object.
_validate($data)
Validate fields before create or update object.
deleteTargets($id)
Delete targets of a mass mailing.
indexTargets($id, $sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List mass mailing targets.
validate($id)
Validate a mass mailing.
deleteTarget($id, $targetid)
Delete a mass mailing general fields (won't change lines of mass mailing)
_fetchTarget($id, $targetid)
Get properties of an mailing object.
resetTargetsStatus($id)
reset target status of a mass mailing
put($id, $request_data=null)
Update a mass mailing general fields (won't change lines of mass mailing)
_cleanTargetDatas($object)
Clean sensible object (mailing target) datas.
postTarget($id, $request_data=null)
Create a mass mailing.
__construct()
Constructor.
updateTarget($id, $targetid, $request_data=null)
Update a mass mailing general fields (won't change lines of mass mailing)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $fk_projects='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List mass mailings.
settodraft($id)
Set a mass mailing to draft.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.