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 $element =
'email_templates';
67 public $table_element =
'c_email_templates';
97 throw new RestException(403,
'denied delete access to email templates');
100 $result = $this->email_template->apiFetch(
$id,
'', DolibarrApiAccess::$user);
101 if (!$result ||
$id == 0) {
102 throw new RestException(404,
'Email Template with id '.
$id.
' not found');
105 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
106 throw new RestException(403,
'denied delete access to email templates');
109 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
110 throw new RestException(500,
'Error when delete email template : '.$this->email_template->error);
116 'message' =>
'email template deleted'
139 throw new RestException(403,
'denied delete access to email templates');
142 $result = $this->email_template->apiFetch(0, $label, DolibarrApiAccess::$user);
144 throw new RestException(404,
"Email Template with label ".$label.
" not found");
147 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
148 throw new RestException(403,
'denied delete access to email templates');
151 if (!$this->email_template->delete(DolibarrApiAccess::$user)) {
152 throw new RestException(500,
'Error when delete email template : '.$this->email_template->error);
158 'message' =>
'email template deleted'
200 return $this->
_fetch(0, $label);
225 public function index($sortfield =
"e.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $fk_user =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
229 throw new RestException(403,
'denied read access to email templates');
234 $sql =
"SELECT e.rowid";
235 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS e";
236 $sql .=
" WHERE e.entity IN (".getEntity($this->element).
")";
237 if (!$fk_user ==
'') {
238 $sql .=
" AND e.fk_user = ".((int) $fk_user);
240 if (!DolibarrApiAccess::$user->admin) {
241 $sql .=
" AND e.fk_user = ".((int) DolibarrApiAccess::$user->
id);
249 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
254 $sqlTotals = str_replace(
'SELECT e.rowid',
'SELECT count(e.rowid) as total', $sql);
256 $sql .= $this->db->order($sortfield, $sortorder);
261 $offset = $limit * $page;
263 $sql .= $this->db->plimit($limit + 1, $offset);
266 dol_syslog(get_class($this).
"::index", LOG_DEBUG);
267 $result = $this->db->query($sql);
268 dol_syslog(get_class($this).
"::pindex", LOG_DEBUG);
271 $num = $this->db->num_rows($result);
272 $min = min($num, ($limit <= 0 ? $num : $limit));
275 $obj = $this->db->fetch_object($result);
277 if ($email_template_static->apiFetch($obj->rowid,
'', DolibarrApiAccess::$user) > 0) {
283 throw new RestException(503,
'Error when retrieve email template list : '.$this->db->lasterror());
287 if ($pagination_data) {
288 $totalsResult = $this->db->query($sqlTotals);
289 $total = $this->db->fetch_object($totalsResult)->total;
294 $obj_ret[
'data'] = $tmp;
295 $obj_ret[
'pagination'] = [
296 'total' => (int) $total,
298 'page_count' => ceil((
int) $total / $limit),
324 public function post($request_data =
null)
328 throw new RestException(403,
'denied create access to email templates');
332 $result = $this->
_validate($request_data);
334 foreach ($request_data as $field => $value) {
335 if ($field ===
'caller') {
337 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
340 if ($field ==
'id') {
341 throw new RestException(400,
'Creating with id field is forbidden');
343 if ($field ==
'tms') {
344 throw new RestException(400,
'Creating with tms field is forbidden');
346 if ($field ==
'fk_user') {
347 if (!DolibarrApiAccess::$user->admin) {
348 $request_data[$field] = (int) DolibarrApiAccess::$user->
id;
352 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
355 if ($this->email_template->create(DolibarrApiAccess::$user) < 0) {
356 throw new RestException(500,
"Error creating email template", array_merge(array($this->email_template->error), $this->email_template->errors));
359 return ((
int) $this->email_template->id);
386 throw new RestException(403,
'denied update access to email templates');
389 $result = $this->email_template->apiFetch(
$id,
'', DolibarrApiAccess::$user);
390 if (!$result ||
$id == 0) {
391 throw new RestException(404,
'email template with id='.
$id.
' not found');
394 foreach ($request_data as $field => $value) {
395 if ($field ==
'id') {
396 throw new RestException(400,
'Updating with id field is forbidden');
398 if ($field ==
'datec') {
399 throw new RestException(400,
'Updating with datec field is forbidden');
401 if ($field ==
'fk_user') {
402 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
403 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
407 if ($field ===
'caller') {
409 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
413 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
416 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
419 throw new RestException(500, $this->email_template->error);
446 throw new RestException(403,
'denied update access to email templates');
449 $result = $this->email_template->apiFetch(0, $label, DolibarrApiAccess::$user);
451 throw new RestException(404,
'email template not found');
455 foreach ($request_data as $field => $value) {
456 if ($field ==
'id') {
457 throw new RestException(400,
'Updating with id field is forbidden');
459 if ($field ==
'datec') {
460 throw new RestException(400,
'Updating with datec field is forbidden');
462 if ($field ==
'fk_user') {
463 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
464 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
468 if ($field ==
'label') {
469 $newlabel = $this->
_checkValForAPI($field, $value, $this->email_template);
471 if ($field ===
'caller') {
473 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
477 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
480 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
481 return $this->
_fetch(0, $newlabel);
483 throw new RestException(500, $this->email_template->error);
508 throw new RestException(403,
'denied read access to email templates');
511 $result = $this->email_template->apiFetch(
$id, $label, DolibarrApiAccess::$user);
517 throw new RestException(404,
'Email template with id='.((
string)
$id).
' not found in entity='.(
int)
$conf->entity);
520 throw new RestException(404,
'Email template with label '.$label.
' not found in entity='.(
int)
$conf->entity);
522 throw new RestException(404,
'Email Template not found');
524 if (empty($this->email_template->error)) {
525 throw new RestException(400,
'Unknown error in your request');
527 throw new RestException(400,
'Error: '.$this->email_template->error);
549 dol_syslog(get_class($this).
"::_cleanObjectDatas", LOG_DEBUG);
553 unset(
$object->array_languages);
555 unset(
$object->linkedObjectsIds);
572 unset(
$object->barcode_type_coder);
573 unset(
$object->mode_reglement_id);
574 unset(
$object->cond_reglement_id);
575 unset(
$object->demand_reason_id);
576 unset(
$object->transport_mode_id);
577 unset(
$object->shipping_method_id);
578 unset(
$object->shipping_method);
579 unset(
$object->fk_multicurrency);
580 unset(
$object->multicurrency_code);
581 unset(
$object->multicurrency_tx);
582 unset(
$object->multicurrency_total_ht);
583 unset(
$object->multicurrency_total_tva);
584 unset(
$object->multicurrency_total_ttc);
585 unset(
$object->multicurrency_total_localtax1);
586 unset(
$object->multicurrency_total_localtax2);
593 unset(
$object->total_localtax1);
594 unset(
$object->total_localtax2);
597 unset(
$object->actiontypecode);
604 unset(
$object->user_creation_id);
606 unset(
$object->user_validation);
607 unset(
$object->user_validation_id);
608 unset(
$object->user_closing_id);
609 unset(
$object->user_modification);
610 unset(
$object->user_modification_id);
615 unset(
$object->cond_reglement_supplier_id);
616 unset(
$object->deposit_percent);
617 unset(
$object->retained_warranty_fk_cond_reglement);
623 unset(
$object->date_validation);
624 unset(
$object->date_modification);
642 $email_template = array();
643 foreach (EmailTemplates::$FIELDS as $field) {
644 if (!isset($data[$field])) {
645 throw new RestException(400, $field.
" field missing");
647 $email_template[$field] = $data[$field];
649 return $email_template;
664 $allowaccess =
false;
665 if (
isModEnabled(
"societe") && DolibarrApiAccess::$user->hasRight(
'societe', $accesstype)) {
668 if (
isModEnabled(
'member') && DolibarrApiAccess::$user->hasRight(
'adherent', $accesstype)) {
671 if (
isModEnabled(
"propal") && DolibarrApiAccess::$user->hasRight(
'propal', $accesstype)) {
674 if (
isModEnabled(
'order') && DolibarrApiAccess::$user->hasRight(
'commande', $accesstype)) {
677 if (
isModEnabled(
'invoice') && DolibarrApiAccess::$user->hasRight(
'facture', $accesstype)) {
683 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.