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 $table_element =
'c_email_templates';
91 throw new RestException(403,
'denied delete access to email templates');
94 $result = $this->email_template->apifetch(
$id,
'');
95 if (!$result ||
$id == 0) {
96 throw new RestException(404,
'Email Template with id '.
$id.
' not found');
99 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
100 throw new RestException(500,
'Error when delete email template : '.$this->email_template->error);
106 'message' =>
'email template deleted'
129 throw new RestException(403,
'denied delete access to email templates');
132 $result = $this->email_template->apifetch(0, $label);
134 throw new RestException(404,
"Email Template with label ".$label.
" not found");
137 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
138 throw new RestException(500,
'Error when delete email template : '.$this->email_template->error);
144 'message' =>
'email template deleted'
186 return $this->
_fetch(0, $label);
211 public function index($sortfield =
"e.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $fk_user =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
215 throw new RestException(403,
'denied read access to email templates');
220 $sql =
"SELECT e.rowid";
221 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS e";
222 $sql .=
" WHERE e.entity IN (".getEntity($this->table_element).
")";
223 if (!$fk_user ==
'') {
224 $sql .=
" AND e.fk_user = ".((int) $fk_user);
232 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
237 $sqlTotals = str_replace(
'SELECT e.rowid',
'SELECT count(e.rowid) as total', $sql);
239 $sql .= $this->db->order($sortfield, $sortorder);
244 $offset = $limit * $page;
246 $sql .= $this->db->plimit($limit + 1, $offset);
249 dol_syslog(get_class($this).
"::index", LOG_DEBUG);
250 $result = $this->db->query($sql);
251 dol_syslog(get_class($this).
"::pindex", LOG_DEBUG);
254 $num = $this->db->num_rows($result);
255 $min = min($num, ($limit <= 0 ? $num : $limit));
258 $obj = $this->db->fetch_object($result);
260 if ($email_template_static->apifetch($obj->rowid,
'') > 0) {
266 throw new RestException(503,
'Error when retrieve email template list : '.$this->db->lasterror());
270 if ($pagination_data) {
271 $totalsResult = $this->db->query($sqlTotals);
272 $total = $this->db->fetch_object($totalsResult)->total;
277 $obj_ret[
'data'] = $tmp;
278 $obj_ret[
'pagination'] = [
279 'total' => (int) $total,
281 'page_count' => ceil((
int) $total / $limit),
307 public function post($request_data =
null)
311 throw new RestException(403,
'denied create access to email templates');
315 $result = $this->
_validate($request_data);
317 foreach ($request_data as $field => $value) {
318 if ($field ===
'caller') {
320 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
323 if ($field ==
'id') {
324 throw new RestException(400,
'Creating with id field is forbidden');
326 if ($field ==
'tms') {
327 throw new RestException(400,
'Creating with tms field is forbidden');
330 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
333 if ($this->email_template->create(DolibarrApiAccess::$user) < 0) {
334 throw new RestException(500,
"Error creating email template", array_merge(array($this->email_template->error), $this->email_template->errors));
337 return ((
int) $this->email_template->id);
364 throw new RestException(403,
'denied update access to email templates');
367 $result = $this->email_template->apifetch(
$id,
'');
368 if (!$result ||
$id == 0) {
369 throw new RestException(404,
'email template with id='.
$id.
' not found');
372 foreach ($request_data as $field => $value) {
373 if ($field ==
'id') {
374 throw new RestException(400,
'Updating with id field is forbidden');
376 if ($field ==
'datec') {
377 throw new RestException(400,
'Updating with datec field is forbidden');
380 if ($field ===
'caller') {
382 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
386 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
389 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
392 throw new RestException(500, $this->email_template->error);
419 throw new RestException(403,
'denied update access to email templates');
422 $result = $this->email_template->apifetch(0, $label);
424 throw new RestException(404,
'email template not found');
428 foreach ($request_data as $field => $value) {
429 if ($field ==
'id') {
430 throw new RestException(400,
'Updating with id field is forbidden');
432 if ($field ==
'datec') {
433 throw new RestException(400,
'Updating with datec field is forbidden');
436 if ($field ==
'label') {
437 $newlabel = $this->
_checkValForAPI($field, $value, $this->email_template);
439 if ($field ===
'caller') {
441 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
445 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
448 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
449 return $this->
_fetch(0, $newlabel);
451 throw new RestException(500, $this->email_template->error);
476 throw new RestException(403,
'denied read access to email templates');
479 $result = $this->email_template->apifetch(
$id, $label);
485 throw new RestException(404,
'Email template with id='.((
string)
$id).
' not found in entity='.(
int)
$conf->entity);
488 throw new RestException(404,
'Email template with label '.$label.
' not found in entity='.(
int)
$conf->entity);
490 throw new RestException(404,
'Email Template not found');
492 if (empty($this->email_template->error)) {
493 throw new RestException(400,
'Unknown error in your request');
495 throw new RestException(400,
'Error: '.$this->email_template->error);
517 dol_syslog(get_class($this).
"::_cleanObjectDatas", LOG_DEBUG);
521 unset(
$object->array_languages);
523 unset(
$object->linkedObjectsIds);
540 unset(
$object->barcode_type_coder);
541 unset(
$object->mode_reglement_id);
542 unset(
$object->cond_reglement_id);
543 unset(
$object->demand_reason_id);
544 unset(
$object->transport_mode_id);
545 unset(
$object->shipping_method_id);
546 unset(
$object->shipping_method);
547 unset(
$object->fk_multicurrency);
548 unset(
$object->multicurrency_code);
549 unset(
$object->multicurrency_tx);
550 unset(
$object->multicurrency_total_ht);
551 unset(
$object->multicurrency_total_tva);
552 unset(
$object->multicurrency_total_ttc);
553 unset(
$object->multicurrency_total_localtax1);
554 unset(
$object->multicurrency_total_localtax2);
561 unset(
$object->total_localtax1);
562 unset(
$object->total_localtax2);
565 unset(
$object->actiontypecode);
572 unset(
$object->user_creation_id);
574 unset(
$object->user_validation);
575 unset(
$object->user_validation_id);
576 unset(
$object->user_closing_id);
577 unset(
$object->user_modification);
578 unset(
$object->user_modification_id);
583 unset(
$object->cond_reglement_supplier_id);
584 unset(
$object->deposit_percent);
585 unset(
$object->retained_warranty_fk_cond_reglement);
591 unset(
$object->date_validation);
592 unset(
$object->date_modification);
610 $email_template = array();
611 foreach (EmailTemplates::$FIELDS as $field) {
612 if (!isset($data[$field])) {
613 throw new RestException(400, $field.
" field missing");
615 $email_template[$field] = $data[$field];
617 return $email_template;
632 $allowaccess =
false;
633 if (
isModEnabled(
"societe") && DolibarrApiAccess::$user->hasRight(
'societe', $accesstype)) {
636 if (
isModEnabled(
'member') && DolibarrApiAccess::$user->hasRight(
'adherent', $accesstype)) {
639 if (
isModEnabled(
"propal") && DolibarrApiAccess::$user->hasRight(
'propal', $accesstype)) {
642 if (
isModEnabled(
'order') && DolibarrApiAccess::$user->hasRight(
'commande', $accesstype)) {
645 if (
isModEnabled(
'invoice') && DolibarrApiAccess::$user->hasRight(
'facture', $accesstype)) {
651 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.