188 $this->ismultientitymanaged = 1;
189 $this->isextrafieldmanaged = 1;
192 if (!
getDolGlobalInt(
'MAIN_SHOW_TECHNICAL_ID') && isset($this->fields[
'rowid']) && !empty($this->fields[
'ref'])) {
193 $this->fields[
'rowid'][
'visible'] = 0;
195 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
196 $this->fields[
'entity'][
'enabled'] = 0;
206 foreach ($this->fields as $key => $val) {
207 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
208 unset($this->fields[$key]);
213 if (is_object($langs)) {
214 foreach ($this->fields as $key => $val) {
215 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
216 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
217 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
232 public function create($user, $notrigger = 0)
237 dol_syslog(get_class($this).
"::create user=".$user->id);
240 if (!empty($this->label)) {
243 $this->error =
'ErrorLabelAlreadyExists';
244 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
245 $this->db->rollback();
254 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
" (entity,";
255 $sql .=
" module, type_template, lang, private, fk_user, datec, label,";
256 $sql .=
" position, defaultfortype, enabled, active, email_from, email_to,";
257 $sql .=
" email_tocc, email_tobcc, topic, joinfiles, content, content_lines)";
259 $sql .=
" ".((int) $conf->entity).
",";
260 if (is_null($this->module)) {
263 $sql .=
" '".$this->db->escape($this->module).
"',";
265 $sql .=
" '".$this->db->escape($this->type_template).
"',";
266 if (is_null($this->lang)) {
269 $sql .=
" '".$this->db->escape($this->lang).
"',";
271 $sql .=
" ".((int) $this->
private).
",";
272 if (is_null($this->fk_user)) {
275 $sql .=
" '".((int) $this->fk_user).
"',";
277 if (is_null($this->datec)) {
278 $sql .=
" '".$this->db->idate($now).
"',";
280 $sql .=
" '".$this->db->idate($this->datec).
"',";
282 $sql .=
" '".$this->db->escape($this->label).
"',";
283 $sql .=
" ".((int) $this->
position).
", ".((int) $this->defaultfortype).
",";
284 if (is_null($this->enabled)) {
287 $sql .=
" '".((int) $this->enabled).
"',";
289 if (is_null($this->
active)) {
292 $sql .=
" '".((int) $this->
active).
"',";
294 if (is_null($this->email_from)) {
297 $sql .=
" '".$this->db->escape($this->email_from).
"',";
299 if (is_null($this->email_to)) {
302 $sql .=
" '".$this->db->escape($this->email_to).
"',";
304 if (is_null($this->email_tocc)) {
307 $sql .=
" '".$this->db->escape($this->email_tocc).
"',";
309 if (is_null($this->email_tobcc)) {
312 $sql .=
" '".$this->db->escape($this->email_tobcc).
"',";
314 $sql .=
" '".$this->db->escape($this->topic).
"',";
315 $sql .=
" ".((int) $this->joinfiles).
",";
316 if (is_null($this->content)) {
319 $sql .=
" '".((string) $this->db->escape($this->content)).
"',";
321 if (is_null($this->content_lines)) {
324 $sql .=
" '".((string) $this->db->escape($this->content_lines)).
"'";
329 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
330 $resql = $this->db->query($sql);
332 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
336 $result = $this->call_trigger(self::TRIGGER_PREFIX.
'_CREATE', $user);
347 $this->db->rollback();
351 $this->error = $this->db->lasterror();
352 $this->db->rollback();
369 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
370 $sql .=
" module=".($this->module ?
"'".$this->db->escape($this->module).
"', " :
'NULL, ');
371 $sql .=
" type_template=".($this->type_template ?
"'".$this->db->escape($this->type_template).
"', " :
'NULL, ');
372 $sql .=
" lang=".($this->lang ?
"'".$this->db->escape($this->lang).
"', " :
'NULL, ');
373 $sql .=
" private=".((int) $this->
private).
",";
374 $sql .=
" fk_user=".((int) $this->fk_user).
",";
375 $sql .=
" label=".($this->label ?
"'".$this->db->escape($this->label).
"', " :
'NULL, ');
376 $sql .=
" position=".((int) $this->
position).
",";
377 $sql .=
" defaultfortype=".((int) $this->defaultfortype).
",";
378 $sql .=
" enabled=".($this->enabled ?
"'".$this->db->escape($this->enabled).
"', " :
'NULL, ');
379 $sql .=
" active=".((int) $this->
active).
",";
380 $sql .=
" email_from=".($this->email_from ?
"'".$this->db->escape($this->email_from).
"', " :
'NULL, ');
381 $sql .=
" email_to=".($this->email_to ?
"'".$this->db->escape($this->email_to).
"', " :
'NULL, ');
382 $sql .=
" email_tocc=".($this->email_tocc ?
"'".$this->db->escape($this->email_tocc).
"', " :
'NULL, ');
383 $sql .=
" email_tobcc=".($this->email_tobcc ?
"'".$this->db->escape($this->email_tobcc).
"', " :
'NULL, ');
384 $sql .=
" topic=".($this->topic ?
"'".$this->db->escape($this->topic).
"', " :
'NULL, ');
385 $sql .=
" joinfiles=".((int) $this->joinfiles).
",";
386 $sql .=
" content=".($this->content ?
"'".$this->db->escape($this->content).
"', " :
'NULL, ');
387 $sql .=
" content_lines=".($this->content_lines ?
"'".$this->db->escape($this->content_lines).
"'" :
'NULL');
388 $sql .=
" WHERE rowid=".((int) $this->
id);
392 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
393 $resql = $this->db->query($sql);
396 $this->errors[] =
"Error ".$this->db->lasterror();
399 if (!$error && !$notrigger) {
401 $result = $this->call_trigger(self::TRIGGER_PREFIX.
'_MODIFY', $user);
410 foreach ($this->errors as $errmsg) {
411 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
412 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
414 $this->db->rollback();
429 public function delete($user, $notrigger = 0)
433 dol_syslog(get_class($this).
"::delete ".$this->
id, LOG_DEBUG);
439 $result = $this->call_trigger(self::TRIGGER_PREFIX.
'_DELETE', $user);
448 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
449 $res = $this->db->query($sql);
452 $this->error = $this->db->lasterror();
453 $this->errors[] = $this->error;
454 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
459 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
463 $this->db->rollback();
477 public function fetch($id, $label =
null, $noextrafields = 0, $nolines = 0)
481 $result = $this->
fetchCommon($id,
'',
" AND t.label = '".$this->db->escape($label).
"'", $noextrafields);
483 if ($result > 0 && !empty($this->table_element_line) && empty($nolines)) {
484 $this->fetchLines($noextrafields);
499 if (($id == 0 || empty($id)) && empty($label)) {
500 dol_syslog(get_class($this).
"::apifetch id and label are empty", LOG_DEBUG);
501 $this->error =
'id='.$id.
' and label are empty';
505 $sql =
"SELECT e.rowid, e.entity, e.module, e.type_template, e.lang,";
506 $sql .=
" e.private, e.fk_user, e.datec, e.tms, e.label, e.position,";
507 $sql .=
" e.defaultfortype, e.enabled, e.active, e.email_from, e.email_to,";
508 $sql .=
" e.email_tocc, e.email_tobcc, e.topic, e.joinfiles, e.content,";
509 $sql .=
" e.content_lines FROM ".$this->db->prefix().$this->table_element.
" as e";
511 $sql .=
" WHERE e.rowid = ".((int) $id);
513 $sql .=
" WHERE e.entity IN (".getEntity($this->table_element).
")";
515 $sql .=
" AND e.label = '".$this->db->escape($label).
"'";
519 dol_syslog(get_class($this).
"::apifetch", LOG_DEBUG);
520 $result = $this->db->query($sql);
522 $obj = $this->db->fetch_object($result);
524 $this->
id = (int) $obj->rowid;
525 $this->entity = (int) $obj->entity;
527 $this->
active = (int) $obj->active;
528 $this->content = (string) $obj->content;
529 $this->content_lines = (string) $obj->content_lines;
530 $this->datec = $this->db->jdate($obj->datec);
531 $this->defaultfortype = (int) $obj->defaultfortype;
532 $this->email_from = (string) $obj->email_from;
533 $this->email_to = (string) $obj->email_to;
534 $this->email_tobcc = (string) $obj->email_tobcc;
535 $this->email_tocc = (string) $obj->email_tocc;
536 $this->enabled = (string) $obj->enabled;
537 $this->fk_user = (int) $obj->fk_user;
538 $this->joinfiles = (int) $obj->joinfiles;
539 $this->label = (string) $obj->label;
540 $this->lang = (string) $obj->lang;
541 $this->module = (string) $obj->module;
542 $this->
position = (int) $obj->position;
543 $this->
private = (int) $obj->private;
544 $this->tms = $this->db->jdate($obj->tms);
545 $this->topic = (string) $obj->topic;
546 $this->type_template = (string) $obj->type_template;
549 $this->date_creation = $this->db->jdate($obj->datec);
554 $this->error =
'Email template with id '.((string) $id).
' not found sql='.$sql;
556 $this->error =
'Email template with label '.$label.
' not found sql='.$sql;
561 $this->error = $this->db->error();