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);
335 if (!DolibarrApiAccess::$user->admin) {
336 $request_data[
'fk_user'] = (int) DolibarrApiAccess::$user->
id;
339 foreach ($request_data as $field => $value) {
340 if ($field ===
'caller') {
342 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
345 if ($field ==
'id') {
346 throw new RestException(400,
'Creating with id field is forbidden');
348 if ($field ==
'tms') {
349 throw new RestException(400,
'Creating with tms field is forbidden');
351 if ($field ==
'fk_user') {
352 if (!DolibarrApiAccess::$user->admin) {
353 $value = (int) DolibarrApiAccess::$user->
id;
357 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
360 if ($this->email_template->create(DolibarrApiAccess::$user) < 0) {
361 throw new RestException(500,
"Error creating email template", array_merge(array($this->email_template->error), $this->email_template->errors));
364 return ((
int) $this->email_template->id);
391 throw new RestException(403,
'denied update access to email templates');
394 $result = $this->email_template->apiFetch(
$id,
'', DolibarrApiAccess::$user);
395 if ($result <= 0 ||
$id == 0) {
396 throw new RestException(404,
'email template with id='.
$id.
' not found');
399 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
400 throw new RestException(403,
'denied update access to email templates');
403 foreach ($request_data as $field => $value) {
404 if ($field ==
'id') {
405 throw new RestException(400,
'Updating with id field is forbidden');
407 if ($field ==
'datec') {
408 throw new RestException(400,
'Updating with datec field is forbidden');
410 if ($field ==
'fk_user') {
411 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
412 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
416 if ($field ===
'caller') {
418 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
422 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
425 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
428 throw new RestException(500, $this->email_template->error);
455 throw new RestException(403,
'denied update access to email templates');
458 $result = $this->email_template->apiFetch(0, $label, DolibarrApiAccess::$user);
460 throw new RestException(404,
'email template not found');
463 if (!DolibarrApiAccess::$user->admin && (
int) DolibarrApiAccess::$user->
id != $this->email_template->fk_user) {
464 throw new RestException(403,
'denied update access to email templates');
468 foreach ($request_data as $field => $value) {
469 if ($field ==
'id') {
470 throw new RestException(400,
'Updating with id field is forbidden');
472 if ($field ==
'datec') {
473 throw new RestException(400,
'Updating with datec field is forbidden');
475 if ($field ==
'fk_user') {
476 if (!DolibarrApiAccess::$user->admin && (
int) $value != $this->email_template->fk_user) {
477 throw new RestException(400,
'Updating with fk_user that is not yourself is not allowed if you are not admin');
481 if ($field ==
'label') {
482 $newlabel = $this->
_checkValForAPI($field, $value, $this->email_template);
484 if ($field ===
'caller') {
486 $this->email_template->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
490 $this->email_template->$field = $this->
_checkValForAPI($field, $value, $this->email_template);
493 if ($this->email_template->update(DolibarrApiAccess::$user) > 0) {
494 return $this->
_fetch(0, $newlabel);
496 throw new RestException(500, $this->email_template->error);
521 throw new RestException(403,
'denied read access to email templates');
524 $result = $this->email_template->apiFetch(
$id, $label, DolibarrApiAccess::$user);
530 throw new RestException(404,
'Email template with id='.((
string)
$id).
' not found in entity='.(
int) $conf->entity);
533 throw new RestException(404,
'Email template with label '.$label.
' not found in entity='.(
int) $conf->entity);
535 throw new RestException(404,
'Email Template not found');
537 if (empty($this->email_template->error)) {
538 throw new RestException(400,
'Unknown error in your request');
540 throw new RestException(400,
'Error: '.$this->email_template->error);
562 dol_syslog(get_class($this).
"::_cleanObjectDatas", LOG_DEBUG);
566 unset(
$object->array_languages);
568 unset(
$object->linkedObjectsIds);
585 unset(
$object->barcode_type_coder);
586 unset(
$object->mode_reglement_id);
587 unset(
$object->cond_reglement_id);
588 unset(
$object->demand_reason_id);
589 unset(
$object->transport_mode_id);
590 unset(
$object->shipping_method_id);
591 unset(
$object->shipping_method);
592 unset(
$object->fk_multicurrency);
593 unset(
$object->multicurrency_code);
594 unset(
$object->multicurrency_tx);
595 unset(
$object->multicurrency_total_ht);
596 unset(
$object->multicurrency_total_tva);
597 unset(
$object->multicurrency_total_ttc);
598 unset(
$object->multicurrency_total_localtax1);
599 unset(
$object->multicurrency_total_localtax2);
606 unset(
$object->total_localtax1);
607 unset(
$object->total_localtax2);
610 unset(
$object->actiontypecode);
617 unset(
$object->user_creation_id);
619 unset(
$object->user_validation);
620 unset(
$object->user_validation_id);
621 unset(
$object->user_closing_id);
622 unset(
$object->user_modification);
623 unset(
$object->user_modification_id);
628 unset(
$object->cond_reglement_supplier_id);
629 unset(
$object->deposit_percent);
630 unset(
$object->retained_warranty_fk_cond_reglement);
636 unset(
$object->date_validation);
637 unset(
$object->date_modification);
655 $email_template = array();
656 foreach (EmailTemplates::$FIELDS as $field) {
657 if (!isset($data[$field])) {
658 throw new RestException(400, $field.
" field missing");
660 $email_template[$field] = $data[$field];
662 return $email_template;
677 $allowaccess =
false;
678 if (
isModEnabled(
"societe") && DolibarrApiAccess::$user->hasRight(
'societe', $accesstype)) {
681 if (
isModEnabled(
'member') && DolibarrApiAccess::$user->hasRight(
'adherent', $accesstype)) {
684 if (
isModEnabled(
"propal") && DolibarrApiAccess::$user->hasRight(
'propal', $accesstype)) {
687 if (
isModEnabled(
'order') && DolibarrApiAccess::$user->hasRight(
'commande', $accesstype)) {
690 if (
isModEnabled(
'invoice') && DolibarrApiAccess::$user->hasRight(
'facture', $accesstype)) {
696 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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
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.