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);
334 if (!DolibarrApiAccess::$user->admin) {
335 $request_data[
'fk_user'] = (int) DolibarrApiAccess::$user->
id;
338 foreach ($request_data as $field => $value) {
339 if ($field ===
'caller') {
341 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
344 if ($field ==
'id') {
345 throw new RestException(400,
'Creating with id field is forbidden');
347 if ($field ==
'tms') {
348 throw new RestException(400,
'Creating with tms field is forbidden');
350 if ($field ==
'fk_user') {
351 if (!DolibarrApiAccess::$user->admin) {
352 $value = (int) DolibarrApiAccess::$user->
id;
356 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
359 if ($this->email_template->create(DolibarrApiAccess::$user) < 0) {
360 throw new RestException(500,
"Error creating email template", array_merge(array($this->email_template->error), $this->email_template->errors));
363 return ((
int) $this->email_template->id);
390 throw new RestException(403,
'denied update access to email templates');
393 $result = $this->email_template->apiFetch(
$id,
'', DolibarrApiAccess::$user);
394 if ($result <= 0 ||
$id == 0) {
395 throw new RestException(404,
'email template with id='.
$id.
' not found');
398 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
399 throw new RestException(403,
'denied update access to email templates');
402 foreach ($request_data as $field => $value) {
403 if ($field ==
'id') {
404 throw new RestException(400,
'Updating with id field is forbidden');
406 if ($field ==
'datec') {
407 throw new RestException(400,
'Updating with datec field is forbidden');
409 if ($field ==
'fk_user') {
410 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
411 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
415 if ($field ===
'caller') {
417 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
421 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
424 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
427 throw new RestException(500, $this->email_template->errorsToString());
454 throw new RestException(403,
'denied update access to email templates');
457 $result = $this->email_template->apiFetch(0, $label, DolibarrApiAccess::$user);
459 throw new RestException(404,
'email template not found');
462 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
463 throw new RestException(403,
'denied update access to email templates');
467 foreach ($request_data as $field => $value) {
468 if ($field ==
'id') {
469 throw new RestException(400,
'Updating with id field is forbidden');
471 if ($field ==
'datec') {
472 throw new RestException(400,
'Updating with datec field is forbidden');
474 if ($field ==
'fk_user') {
475 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
476 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
480 if ($field ==
'label') {
481 $newlabel = $this->
_checkValForAPI($field, $value, $this->email_template);
483 if ($field ===
'caller') {
485 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
489 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
492 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
493 return $this->
_fetch(0, $newlabel);
495 throw new RestException(500, $this->email_template->errorsToString());
520 throw new RestException(403,
'denied read access to email templates');
523 $result = $this->email_template->apiFetch(
$id, $label, DolibarrApiAccess::$user);
529 throw new RestException(404,
'Email template with id='.((
string)
$id).
' not found in entity='.(
int)
$conf->entity);
532 throw new RestException(404,
'Email template with label '.$label.
' not found in entity='.(
int)
$conf->entity);
534 throw new RestException(404,
'Email Template not found');
536 if (empty($this->email_template->errorsToString())) {
537 throw new RestException(400,
'Unknown error in your request');
539 throw new RestException(400,
'Error: '.$this->email_template->errorsToString());
561 dol_syslog(get_class($this).
"::_cleanObjectDatas", LOG_DEBUG);
565 unset(
$object->array_languages);
567 unset(
$object->linkedObjectsIds);
584 unset(
$object->barcode_type_coder);
585 unset(
$object->mode_reglement_id);
586 unset(
$object->cond_reglement_id);
587 unset(
$object->demand_reason_id);
588 unset(
$object->transport_mode_id);
589 unset(
$object->shipping_method_id);
590 unset(
$object->shipping_method);
591 unset(
$object->fk_multicurrency);
592 unset(
$object->multicurrency_code);
593 unset(
$object->multicurrency_tx);
594 unset(
$object->multicurrency_total_ht);
595 unset(
$object->multicurrency_total_tva);
596 unset(
$object->multicurrency_total_ttc);
597 unset(
$object->multicurrency_total_localtax1);
598 unset(
$object->multicurrency_total_localtax2);
605 unset(
$object->total_localtax1);
606 unset(
$object->total_localtax2);
609 unset(
$object->actiontypecode);
616 unset(
$object->user_creation_id);
618 unset(
$object->user_validation);
619 unset(
$object->user_validation_id);
620 unset(
$object->user_closing_id);
621 unset(
$object->user_modification);
622 unset(
$object->user_modification_id);
627 unset(
$object->cond_reglement_supplier_id);
628 unset(
$object->deposit_percent);
629 unset(
$object->retained_warranty_fk_cond_reglement);
635 unset(
$object->date_validation);
636 unset(
$object->date_modification);
654 $email_template = array();
655 foreach (EmailTemplates::$FIELDS as $field) {
656 if (!isset($data[$field])) {
657 throw new RestException(400, $field.
" field missing");
659 $email_template[$field] = $data[$field];
661 return $email_template;
676 $allowaccess =
false;
677 if (
isModEnabled(
"societe") && DolibarrApiAccess::$user->hasRight(
'societe', $accesstype)) {
680 if (
isModEnabled(
'member') && DolibarrApiAccess::$user->hasRight(
'adherent', $accesstype)) {
683 if (
isModEnabled(
"propal") && DolibarrApiAccess::$user->hasRight(
'propal', $accesstype)) {
686 if (
isModEnabled(
'order') && DolibarrApiAccess::$user->hasRight(
'commande', $accesstype)) {
689 if (
isModEnabled(
'invoice') && DolibarrApiAccess::$user->hasRight(
'facture', $accesstype)) {
695 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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.