20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/core/class/cemailtemplate.class.php';
36 public static $FIELDS = array(
45 public static $INTFIELDS = array(
56 public $email_template;
61 public $element =
'email_templates';
66 public $table_element =
'c_email_templates';
96 throw new RestException(403,
'denied delete access to email templates');
99 $result = $this->email_template->apiFetch(
$id,
'', DolibarrApiAccess::$user);
100 if (!$result ||
$id == 0) {
101 throw new RestException(404,
'Email Template with id '.
$id.
' not found');
104 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
105 throw new RestException(403,
'denied delete access to email templates');
108 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
109 throw new RestException(500,
'Error when delete email template : '.$this->email_template->errorsToString());
115 'message' =>
'email template deleted'
138 throw new RestException(403,
'denied delete access to email templates');
141 $result = $this->email_template->apiFetch(0, $label, DolibarrApiAccess::$user);
143 throw new RestException(404,
"Email Template with label ".$label.
" not found");
146 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
147 throw new RestException(403,
'denied delete access to email templates');
150 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
151 throw new RestException(500,
'Error when delete email template : '.$this->email_template->errorsToString());
157 'message' =>
'email template deleted'
199 return $this->
_fetch(0, $label);
224 public function index($sortfield =
"e.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $fk_user =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
228 throw new RestException(403,
'denied read access to email templates');
233 $sql =
"SELECT e.rowid";
234 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS e";
235 $sql .=
" WHERE e.entity IN (".getEntity($this->element).
")";
236 if (!$fk_user ==
'') {
237 $sql .=
" AND e.fk_user = ".((int) $fk_user);
239 if (!DolibarrApiAccess::$user->admin) {
240 $sql .=
" AND e.fk_user = ".((int) DolibarrApiAccess::$user->
id);
248 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
253 $sqlTotals = str_replace(
'SELECT e.rowid',
'SELECT count(e.rowid) as total', $sql);
255 $sql .= $this->db->order($sortfield, $sortorder);
260 $offset = $limit * $page;
262 $sql .= $this->db->plimit($limit + 1, $offset);
265 dol_syslog(get_class($this).
"::index", LOG_DEBUG);
266 $result = $this->db->query($sql);
267 dol_syslog(get_class($this).
"::pindex", LOG_DEBUG);
270 $num = $this->db->num_rows($result);
271 $min = min($num, ($limit <= 0 ? $num : $limit));
274 $obj = $this->db->fetch_object($result);
276 if ($email_template_static->apiFetch($obj->rowid,
'', DolibarrApiAccess::$user) > 0) {
282 throw new RestException(503,
'Error when retrieve email template list : '.$this->db->lasterror());
286 if ($pagination_data) {
287 $totalsResult = $this->db->query($sqlTotals);
288 $total = $this->db->fetch_object($totalsResult)->total;
293 $obj_ret[
'data'] = $tmp;
294 $obj_ret[
'pagination'] = [
295 'total' => (int) $total,
297 'page_count' => ceil((
int) $total / $limit),
323 public function post($request_data =
null)
327 throw new RestException(403,
'denied create access to email templates');
331 $result = $this->
_validate($request_data);
333 foreach ($request_data as $field => $value) {
334 if ($field ===
'caller') {
336 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
339 if ($field ==
'id') {
340 throw new RestException(400,
'Creating with id field is forbidden');
342 if ($field ==
'tms') {
343 throw new RestException(400,
'Creating with tms field is forbidden');
345 if ($field ==
'fk_user') {
346 if (!DolibarrApiAccess::$user->admin) {
347 $request_data[$field] = (int) DolibarrApiAccess::$user->
id;
351 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
354 if ($this->email_template->create(DolibarrApiAccess::$user) < 0) {
355 throw new RestException(500,
"Error creating email template", array_merge(array($this->email_template->error), $this->email_template->errors));
358 return ((
int) $this->email_template->id);
385 throw new RestException(403,
'denied update access to email templates');
388 $result = $this->email_template->apiFetch(
$id,
'', DolibarrApiAccess::$user);
389 if (!$result ||
$id == 0) {
390 throw new RestException(404,
'email template with id='.
$id.
' not found');
393 foreach ($request_data as $field => $value) {
394 if ($field ==
'id') {
395 throw new RestException(400,
'Updating with id field is forbidden');
397 if ($field ==
'datec') {
398 throw new RestException(400,
'Updating with datec field is forbidden');
400 if ($field ==
'fk_user') {
401 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
402 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
406 if ($field ===
'caller') {
408 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
412 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
415 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
418 throw new RestException(500, $this->email_template->errorsToString());
445 throw new RestException(403,
'denied update access to email templates');
448 $result = $this->email_template->apiFetch(0, $label, DolibarrApiAccess::$user);
450 throw new RestException(404,
'email template not found');
454 foreach ($request_data as $field => $value) {
455 if ($field ==
'id') {
456 throw new RestException(400,
'Updating with id field is forbidden');
458 if ($field ==
'datec') {
459 throw new RestException(400,
'Updating with datec field is forbidden');
461 if ($field ==
'fk_user') {
462 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
463 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
467 if ($field ==
'label') {
468 $newlabel = $this->
_checkValForAPI($field, $value, $this->email_template);
470 if ($field ===
'caller') {
472 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
476 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
479 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
480 return $this->
_fetch(0, $newlabel);
482 throw new RestException(500, $this->email_template->errorsToString());
507 throw new RestException(403,
'denied read access to email templates');
510 $result = $this->email_template->apiFetch(
$id, $label, DolibarrApiAccess::$user);
516 throw new RestException(404,
'Email template with id='.((
string)
$id).
' not found in entity='.(
int)
$conf->entity);
519 throw new RestException(404,
'Email template with label '.$label.
' not found in entity='.(
int)
$conf->entity);
521 throw new RestException(404,
'Email Template not found');
523 if (empty($this->email_template->errorsToString())) {
524 throw new RestException(400,
'Unknown error in your request');
526 throw new RestException(400,
'Error: '.$this->email_template->errorsToString());
548 dol_syslog(get_class($this).
"::_cleanObjectDatas", LOG_DEBUG);
552 unset(
$object->array_languages);
554 unset(
$object->linkedObjectsIds);
571 unset(
$object->barcode_type_coder);
572 unset(
$object->mode_reglement_id);
573 unset(
$object->cond_reglement_id);
574 unset(
$object->demand_reason_id);
575 unset(
$object->transport_mode_id);
576 unset(
$object->shipping_method_id);
577 unset(
$object->shipping_method);
578 unset(
$object->fk_multicurrency);
579 unset(
$object->multicurrency_code);
580 unset(
$object->multicurrency_tx);
581 unset(
$object->multicurrency_total_ht);
582 unset(
$object->multicurrency_total_tva);
583 unset(
$object->multicurrency_total_ttc);
584 unset(
$object->multicurrency_total_localtax1);
585 unset(
$object->multicurrency_total_localtax2);
592 unset(
$object->total_localtax1);
593 unset(
$object->total_localtax2);
596 unset(
$object->actiontypecode);
603 unset(
$object->user_creation_id);
605 unset(
$object->user_validation);
606 unset(
$object->user_validation_id);
607 unset(
$object->user_closing_id);
608 unset(
$object->user_modification);
609 unset(
$object->user_modification_id);
614 unset(
$object->cond_reglement_supplier_id);
615 unset(
$object->deposit_percent);
616 unset(
$object->retained_warranty_fk_cond_reglement);
622 unset(
$object->date_validation);
623 unset(
$object->date_modification);
641 $email_template = array();
642 foreach (EmailTemplates::$FIELDS as $field) {
643 if (!isset($data[$field])) {
644 throw new RestException(400, $field.
" field missing");
646 $email_template[$field] = $data[$field];
648 return $email_template;
663 $allowaccess =
false;
664 if (
isModEnabled(
"societe") && DolibarrApiAccess::$user->hasRight(
'societe', $accesstype)) {
667 if (
isModEnabled(
'member') && DolibarrApiAccess::$user->hasRight(
'adherent', $accesstype)) {
670 if (
isModEnabled(
"propal") && DolibarrApiAccess::$user->hasRight(
'propal', $accesstype)) {
673 if (
isModEnabled(
'order') && DolibarrApiAccess::$user->hasRight(
'commande', $accesstype)) {
676 if (
isModEnabled(
'invoice') && DolibarrApiAccess::$user->hasRight(
'facture', $accesstype)) {
682 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.
deleteByLabel($label)
Delete an email template.
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.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.