20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/comm/mailing/class/mailing.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/comm/mailing/class/mailing_targets.class.php';
38 public static $FIELDS = array(
47 public static $TARGETFIELDS = array(
60 public $mailing_target;
69 $this->mailing =
new Mailing($this->db);
85 public function get(
$id)
102 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'read')) {
103 throw new RestException(403);
106 $result = $this->mailing->fetch(
$id);
108 throw new RestException(404,
'Mass mailing not found, id='.
$id);
112 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
115 $this->mailing->fetchObjectLinked();
118 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
147 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $fk_projects =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
149 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'read')) {
150 throw new RestException(403);
153 $arrayProjects = explode(
",", $fk_projects);
154 foreach ($arrayProjects as $project => $value) {
156 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
162 $sql =
"SELECT t.rowid";
163 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing AS t";
164 $sql .=
' WHERE t.entity IN ('.getEntity(
'mailing').
')';
166 $sql .=
" AND t.fk_project IN (".$this->db->sanitize($fk_projects).
")";
170 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
172 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
177 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
179 $sql .= $this->db->order($sortfield, $sortorder);
184 $offset = $limit * $page;
186 $sql .= $this->db->plimit($limit + 1, $offset);
190 $result = $this->db->query($sql);
193 $num = $this->db->num_rows($result);
194 $min = min($num, ($limit <= 0 ? $num : $limit));
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) {
202 $mailing_static->fetchObjectLinked();
210 throw new RestException(503,
'Error when retrieve list of mass mailings : '.$this->db->lasterror());
214 if ($pagination_data) {
215 $totalsResult = $this->db->query($sqlTotals);
216 $total = $this->db->fetch_object($totalsResult)->total;
221 $obj_ret[
'data'] = $tmp;
222 $obj_ret[
'pagination'] = [
223 'total' => (int) $total,
225 'page_count' => ceil((
int) $total / $limit),
258 public function indexTargets(
$id, $sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
260 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'read')) {
261 throw new RestException(403);
264 $fetchMailingResult = $this->mailing->fetch(
$id);
265 if ($fetchMailingResult < 0) {
266 throw new RestException(404,
'Mass mailing not found, id='.
$id);
269 $fk_project = $this->mailing->fk_project;
271 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
276 $sql =
"SELECT t.rowid";
277 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_cibles AS t";
278 $sql .=
" WHERE t.fk_mailing = ".((int)
$id);
283 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
285 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
290 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
292 $sql .= $this->db->order($sortfield, $sortorder);
297 $offset = $limit * $page;
299 $sql .= $this->db->plimit($limit + 1, $offset);
302 dol_syslog(
"API Rest request mass mailing target");
303 $result = $this->db->query($sql);
306 $num = $this->db->num_rows($result);
307 $min = min($num, ($limit <= 0 ? $num : $limit));
310 $obj = $this->db->fetch_object($result);
312 if ($mailing_target->fetch($obj->rowid) > 0) {
318 throw new RestException(503,
'Error when retrieve list of mass mailing targetss : '.$this->db->lasterror());
322 if ($pagination_data) {
323 $totalsResult = $this->db->query($sqlTotals);
324 $total = $this->db->fetch_object($totalsResult)->total;
329 $obj_ret[
'data'] = $tmp;
330 $obj_ret[
'pagination'] = [
331 'total' => (int) $total,
333 'page_count' => ceil((
int) $total / $limit),
356 public function clone(
$id, $cloneContent = 1, $cloneRecipients = 1, $notrigger = 0)
358 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
359 throw new RestException(403,
"Insufficient rights");
361 $result = $this->mailing->fetch(
$id);
363 throw new RestException(404,
'Mass mailing to clone not found, id='.
$id);
367 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
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));
375 if ($cloningResult > 0) {
376 return $this->
get($cloningResult);
378 throw new RestException(500, $this->mailing->error);
395 public function post($request_data =
null)
397 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
398 throw new RestException(403,
"Insufficiant rights");
401 $result = $this->
_validate($request_data);
403 foreach ($request_data as $field => $value) {
404 if ($field ===
'caller') {
406 $this->mailing->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
409 if ($field ===
'fk_project') {
411 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
415 $this->mailing->$field = $this->
_checkValForAPI($field, $value, $this->mailing);
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));
422 return ((
int) $this->mailing->id);
440 public function put(
$id, $request_data =
null)
442 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
443 throw new RestException(403);
446 $result = $this->mailing->fetch(
$id);
448 throw new RestException(404,
'Mass mailing not found, id='.
$id);
452 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
456 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
458 foreach ($request_data as $field => $value) {
459 if ($field ==
'id') {
462 if ($field ===
'caller') {
464 $this->mailing->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
467 if ($field ==
'array_options' && is_array($value)) {
468 foreach ($value as $index => $val) {
474 $this->mailing->$field = $this->
_checkValForAPI($field, $value, $this->mailing);
477 if ($this->mailing->update(DolibarrApiAccess::$user) > 0) {
478 return $this->
get(
$id);
480 throw new RestException(500, $this->mailing->error);
498 public function delete(
$id)
500 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'delete')) {
501 throw new RestException(403);
503 $result = $this->mailing->fetch(
$id);
505 throw new RestException(404,
'Mass mailing not found, id='.
$id);
509 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
513 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
516 if (!$this->mailing->delete(DolibarrApiAccess::$user)) {
517 throw new RestException(500,
'Error when delete Mass mailing : '.$this->mailing->error);
523 'message' =>
'Mass mailing with id='.
$id.
' deleted'
548 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
549 throw new RestException(403);
552 $fetchMailingResult = $this->mailing->fetch(
$id);
553 if ($fetchMailingResult < 0) {
554 throw new RestException(404,
'Mass mailing not found, id='.
$id);
556 $result = $this->mailing_target->fetch($targetid);
558 throw new RestException(404,
'Mass mailing target not found, id='.$targetid);
560 if (
$id != $this->mailing_target->fk_mailing) {
561 throw new RestException(404,
'Target id='.$targetid.
' is does not belong to mailing id='.
$id);
565 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
569 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
571 foreach ($request_data as $field => $value) {
572 if ($field ==
'id') {
573 throw new RestException(400,
'Changing id field is forbidden');
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');
578 if ($field ===
'caller') {
580 $this->mailing_target->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
584 $this->mailing_target->$field = $this->
_checkValForAPI($field, $value, $this->mailing_target);
587 if ($this->mailing_target->update(DolibarrApiAccess::$user) > 0) {
590 throw new RestException(500, $this->mailing_target->error);
614 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
615 throw new RestException(403,
"Insufficiant rights");
622 foreach ($request_data as $field => $value) {
623 if ($field ===
'caller') {
625 $this->mailing_target->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
628 if ($field ===
'fk_project') {
630 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
634 if ($field ==
'id') {
635 throw new RestException(400,
'Creating with id field is forbidden');
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));
643 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
645 $fk_mailing_id = ((int) $value);
648 $this->mailing_target->$field = $this->
_checkValForAPI($field, $value, $this->mailing_target);
651 if (0 == $fk_mailing_id) {
652 throw new RestException(404,
'Mass mailing not found, id='.((
int) $fk_mailing_id));
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));
659 return ((
int) $this->mailing_target->id);
696 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'read')) {
697 throw new RestException(403);
700 $fetchMailingResult = $this->mailing->fetch(
$id);
701 if ($fetchMailingResult < 0) {
702 throw new RestException(404,
'Mass mailing not found, id='.
$id);
704 $result = $this->mailing_target->fetch($targetid);
706 throw new RestException(404,
'Mass mailing target not found, id='.$targetid);
708 if (
$id != $this->mailing_target->fk_mailing) {
709 throw new RestException(404,
'Target id='.$targetid.
' is does not belong to mailing id='.
$id);
713 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
717 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
742 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'delete')) {
743 throw new RestException(403);
746 $fetchMailingResult = $this->mailing->fetch(
$id);
747 if ($fetchMailingResult < 0) {
748 throw new RestException(404,
'Mass mailing not found, id='.
$id);
750 $result = $this->mailing_target->fetch($targetid);
752 throw new RestException(404,
'Mass mailing target not found, id='.$targetid);
754 if (
$id != $this->mailing_target->fk_mailing) {
755 throw new RestException(404,
'Target id='.$targetid.
' is does not belong to mailing id='.
$id);
758 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
761 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
764 if (!$this->mailing_target->delete(DolibarrApiAccess::$user)) {
765 throw new RestException(500,
'Error when delete Mass mailing target: '.$this->mailing->error);
771 'message' =>
'Deleting target id='.$targetid.
' belonging to mailing id='.
$id
794 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'delete')) {
795 throw new RestException(403);
797 $result = $this->mailing->fetch(
$id);
799 throw new RestException(404,
'Mass mailing not found, id='.
$id);
803 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
807 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
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);
818 'message' =>
'Target recipients ('.$count.
') of Mass mailing with id='.
$id.
' deleted'
841 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
842 throw new RestException(403);
844 $result = $this->mailing->fetch(
$id);
846 throw new RestException(404,
'Mass mailing not found, id='.
$id);
850 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
854 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
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);
865 'message' =>
'Resetting status of '.$count.
' target recipients in Mass mailing with id='.
$id
887 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
888 throw new RestException(403);
890 $result = $this->mailing->fetch(
$id);
892 throw new RestException(404,
'Mass mailing not found, id='.
$id);
896 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
900 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
903 $result = $this->mailing->setDraft(DolibarrApiAccess::$user);
905 throw new RestException(304,
'Nothing done. May be object is already draft');
908 throw new RestException(500,
'Error : '.$this->mailing->error);
911 $this->mailing->fetchObjectLinked();
939 if (!DolibarrApiAccess::$user->hasRight(
'mailing',
'write')) {
940 throw new RestException(403);
942 $result = $this->mailing->fetch(
$id);
944 throw new RestException(404,
'Mass mailing not found, id='.
$id);
948 throw new RestException(403,
'Access (project) not allowed for login '.DolibarrApiAccess::$user->login);
952 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
955 $result = $this->mailing->valid(DolibarrApiAccess::$user);
957 throw new RestException(304,
'Error nothing done. May be object is already validated');
960 throw new RestException(500,
'Error when validating Mass mailing: '.$this->mailing->error);
963 $this->mailing->fetchObjectLinked();
978 if ($data ===
null) {
982 foreach (Mailings::$FIELDS as $field) {
983 if (!isset($data[$field])) {
984 throw new RestException(400,
"$field field missing");
986 $mailing[$field] = $data[$field];
1001 if ($data ===
null) {
1004 $mailing_target = array();
1005 foreach (Mailings::$TARGETFIELDS as $field) {
1006 if (!isset($data[$field])) {
1007 throw new RestException(400,
"$field field missing");
1009 $mailing_target[$field] = $data[$field];
1011 return $mailing_target;
1026 unset(
$object->TRIGGER_PREFIX);
1029 unset(
$object->actiontypecode);
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);
1038 unset(
$object->civility_code);
1041 unset(
$object->cond_reglement_id);
1042 unset(
$object->cond_reglement_supplier_id);
1045 unset(
$object->contacts_ids_internal);
1052 unset(
$object->date_creation);
1053 unset(
$object->date_validation);
1055 unset(
$object->demand_reason_id);
1056 unset(
$object->deposit_percent);
1057 unset(
$object->element_for_permission);
1067 unset(
$object->fk_delivery_address);
1069 unset(
$object->fk_multicurrency);
1072 unset(
$object->fk_user_creat);
1073 unset(
$object->fk_user_modif);
1075 unset(
$object->isextrafieldmanaged);
1076 unset(
$object->ismultientitymanaged);
1077 unset(
$object->last_main_doc);
1079 unset(
$object->linked_objects);
1080 unset(
$object->linkedObjects);
1081 unset(
$object->linkedObjectsIds);
1082 unset(
$object->mode_reglement_id);
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);
1096 unset(
$object->next_prev_filter);
1103 unset(
$object->origin_object);
1116 unset(
$object->restrictiononfksoc);
1117 unset(
$object->retained_warranty_fk_cond_reglement);
1119 unset(
$object->shipping_method_id);
1120 unset(
$object->shipping_method);
1121 unset(
$object->showphoto_on_popup);
1126 unset(
$object->table_element_line);
1127 unset(
$object->table_element);
1130 unset(
$object->total_localtax1);
1131 unset(
$object->total_localtax2);
1134 unset(
$object->totalpaid_multicurrency);
1137 unset(
$object->transport_mode_id);
1139 unset(
$object->user_creation_id);
1140 unset(
$object->user_validation_id);
1141 unset(
$object->user_closing_id);
1142 unset(
$object->user_modification_id);
1165 unset(
$object->actiontypecode);
1167 unset(
$object->barcode_type_code);
1168 unset(
$object->barcode_type_coder);
1169 unset(
$object->barcode_type_label);
1172 unset(
$object->civility_code);
1175 unset(
$object->cond_reglement_id);
1176 unset(
$object->cond_reglement_supplier_id);
1179 unset(
$object->contacts_ids_internal);
1186 unset(
$object->demand_reason_id);
1187 unset(
$object->deposit_percent);
1188 unset(
$object->element_for_permission);
1197 unset(
$object->fk_delivery_address);
1199 unset(
$object->fk_multicurrency);
1202 unset(
$object->isextrafieldmanaged);
1203 unset(
$object->ismultientitymanaged);
1204 unset(
$object->last_main_doc);
1207 unset(
$object->linked_objects);
1208 unset(
$object->linkedObjects);
1209 unset(
$object->linkedObjectsIds);
1210 unset(
$object->mode_reglement_id);
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);
1224 unset(
$object->next_prev_filter);
1229 unset(
$object->origin_object);
1242 unset(
$object->restrictiononfksoc);
1243 unset(
$object->retained_warranty_fk_cond_reglement);
1245 unset(
$object->shipping_method_id);
1246 unset(
$object->shipping_method);
1247 unset(
$object->showphoto_on_popup);
1252 unset(
$object->table_element_line);
1253 unset(
$object->table_element);
1256 unset(
$object->total_localtax1);
1257 unset(
$object->total_localtax2);
1260 unset(
$object->totalpaid_multicurrency);
1263 unset(
$object->transport_mode_id);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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.
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.
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.