20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions.lib.php';
24require_once DOL_DOCUMENT_ROOT.
'/core/class/cemailtemplate.class.php';
37 public static $FIELDS = array(
46 public static $INTFIELDS = array(
57 public $email_template;
62 public $table_element =
'c_email_templates';
92 throw new RestException(403,
'denied read access to email templates');
95 $result = $this->email_template->apifetch(
$id,
'');
96 if (!$result ||
$id == 0) {
97 throw new RestException(404,
'Email Template with id '.
$id.
' not found');
100 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
101 throw new RestException(500,
'Error when delete email template : '.$this->email_template->error);
107 'message' =>
'email template deleted'
130 throw new RestException(403,
'denied read access to email templates');
133 $result = $this->email_template->apifetch(0, $label);
135 throw new RestException(404,
"Email Template with label ".$label.
" not found");
138 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
139 throw new RestException(500,
'Error when delete email template : '.$this->email_template->error);
145 'message' =>
'email template deleted'
187 return $this->
_fetch(0, $label);
212 public function index($sortfield =
"e.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $fk_user =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
216 throw new RestException(403,
'denied read access to email templates');
221 $sql =
"SELECT e.rowid";
222 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS e";
223 $sql .=
" WHERE e.entity IN (".getEntity($this->table_element).
")";
224 if (!$fk_user ==
'') {
225 $sql .=
" AND e.fk_user = ".((int) $fk_user);
231 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
233 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
238 $sqlTotals = str_replace(
'SELECT e.rowid',
'SELECT count(e.rowid) as total', $sql);
240 $sql .= $this->db->order($sortfield, $sortorder);
245 $offset = $limit * $page;
247 $sql .= $this->db->plimit($limit + 1, $offset);
250 dol_syslog(get_class($this).
"::index", LOG_DEBUG);
251 $result = $this->db->query($sql);
252 dol_syslog(get_class($this).
"::pindex", LOG_DEBUG);
255 $num = $this->db->num_rows($result);
256 $min = min($num, ($limit <= 0 ? $num : $limit));
259 $obj = $this->db->fetch_object($result);
261 if ($email_template_static->apifetch($obj->rowid,
'') > 0) {
267 throw new RestException(503,
'Error when retrieve email template list : '.$this->db->lasterror());
271 if ($pagination_data) {
272 $totalsResult = $this->db->query($sqlTotals);
273 $total = $this->db->fetch_object($totalsResult)->total;
278 $obj_ret[
'data'] = $tmp;
279 $obj_ret[
'pagination'] = [
280 'total' => (int) $total,
282 'page_count' => ceil((
int) $total / $limit),
308 public function post($request_data =
null)
312 throw new RestException(403,
'denied create access to email templates');
316 $result = $this->
_validate($request_data);
318 foreach ($request_data as $field => $value) {
319 if ($field ===
'caller') {
321 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
324 if ($field ==
'id') {
325 throw new RestException(400,
'Creating with id field is forbidden');
327 if ($field ==
'tms') {
328 throw new RestException(400,
'Creating with tms field is forbidden');
331 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
334 if ($this->email_template->create(DolibarrApiAccess::$user) < 0) {
335 throw new RestException(500,
"Error creating email template", array_merge(array($this->email_template->error), $this->email_template->errors));
338 return ((
int) $this->email_template->id);
365 throw new RestException(403,
'denied update access to email templates');
368 $result = $this->email_template->apifetch(
$id,
'');
369 if (!$result ||
$id == 0) {
370 throw new RestException(404,
'email template with id='.
$id.
' not found');
373 foreach ($request_data as $field => $value) {
374 if ($field ==
'id') {
375 throw new RestException(400,
'Updating with id field is forbidden');
377 if ($field ==
'datec') {
378 throw new RestException(400,
'Updating with datec field is forbidden');
381 if ($field ===
'caller') {
383 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
387 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
390 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
393 throw new RestException(500, $this->email_template->error);
420 throw new RestException(403,
'denied update access to email templates');
423 $result = $this->email_template->apifetch(0, $label);
425 throw new RestException(404,
'email template not found');
429 foreach ($request_data as $field => $value) {
430 if ($field ==
'id') {
431 throw new RestException(400,
'Updating with id field is forbidden');
433 if ($field ==
'datec') {
434 throw new RestException(400,
'Updating with datec field is forbidden');
437 if ($field ==
'label') {
438 $newlabel = $this->
_checkValForAPI($field, $value, $this->email_template);
440 if ($field ===
'caller') {
442 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
446 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
449 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
450 return $this->
_fetch(0, $newlabel);
452 throw new RestException(500, $this->email_template->error);
477 throw new RestException(403,
'denied read access to email templates');
480 $result = $this->email_template->apifetch(
$id, $label);
486 throw new RestException(404,
'Email template with id='.((
string)
$id).
' not found in entity='.(
int)
$conf->entity);
489 throw new RestException(404,
'Email template with label '.$label.
' not found in entity='.(
int)
$conf->entity);
491 throw new RestException(404,
'Email Template not found');
493 if (empty($this->email_template->error)) {
494 throw new RestException(400,
'Unknown error in your request');
496 throw new RestException(400,
'Error: '.$this->email_template->error);
518 dol_syslog(get_class($this).
"::_cleanObjectDatas", LOG_DEBUG);
522 unset(
$object->array_languages);
524 unset(
$object->linkedObjectsIds);
541 unset(
$object->barcode_type_coder);
542 unset(
$object->mode_reglement_id);
543 unset(
$object->cond_reglement_id);
544 unset(
$object->demand_reason_id);
545 unset(
$object->transport_mode_id);
546 unset(
$object->shipping_method_id);
547 unset(
$object->shipping_method);
548 unset(
$object->fk_multicurrency);
549 unset(
$object->multicurrency_code);
550 unset(
$object->multicurrency_tx);
551 unset(
$object->multicurrency_total_ht);
552 unset(
$object->multicurrency_total_tva);
553 unset(
$object->multicurrency_total_ttc);
554 unset(
$object->multicurrency_total_localtax1);
555 unset(
$object->multicurrency_total_localtax2);
562 unset(
$object->total_localtax1);
563 unset(
$object->total_localtax2);
566 unset(
$object->actiontypecode);
573 unset(
$object->user_creation_id);
575 unset(
$object->user_validation);
576 unset(
$object->user_validation_id);
577 unset(
$object->user_closing_id);
578 unset(
$object->user_modification);
579 unset(
$object->user_modification_id);
584 unset(
$object->cond_reglement_supplier_id);
585 unset(
$object->deposit_percent);
586 unset(
$object->retained_warranty_fk_cond_reglement);
592 unset(
$object->date_validation);
593 unset(
$object->date_modification);
611 $email_template = array();
612 foreach (EmailTemplates::$FIELDS as $field) {
613 if (!isset($data[$field])) {
614 throw new RestException(400, $field.
" field missing");
616 $email_template[$field] = $data[$field];
618 return $email_template;
633 $allowaccess =
false;
634 if (
isModEnabled(
"societe") && DolibarrApiAccess::$user->hasRight(
'societe', $accesstype)) {
637 if (
isModEnabled(
'member') && DolibarrApiAccess::$user->hasRight(
'adherent', $accesstype)) {
640 if (
isModEnabled(
"propal") && DolibarrApiAccess::$user->hasRight(
'propal', $accesstype)) {
643 if (
isModEnabled(
'order') && DolibarrApiAccess::$user->hasRight(
'commande', $accesstype)) {
646 if (
isModEnabled(
'invoice') && DolibarrApiAccess::$user->hasRight(
'facture', $accesstype)) {
652 throw new RestException(403,
'denied access to email templates');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Object of table llx_c_email_templates.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_validate($data)
Validate fields before create or update object.
_fetch($id, $label='')
Get properties of an email template.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getById($id)
Get properties of a email template by id.
putbyLabel($label, $request_data=null)
Update an email template.
index($sortfield="e.rowid", $sortorder='ASC', $limit=100, $page=0, $fk_user='', $sqlfilters='', $properties='', $pagination_data=false)
List email templates.
__construct()
Constructor of the class.
post($request_data=null)
Create an email template.
deleteByLAbel($label)
Delete an email template.
deleteById($id)
Delete an email template.
_checkAccessRights($accesstype)
function to check for access rights - should probably have 1.
putById($id, $request_data=null)
Update an email template.
getByLabel($label)
Get properties of an email template by label.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.