27require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
37 public $element =
'undefined';
42 public $table_element =
'undefined';
47 public $lines = array();
83 $fieldlabel =
'label';
84 if ($this->table_element ==
'c_stcomm') {
85 $fieldlabel =
'libelle';
86 } elseif ($this->table_element ==
'c_type_fees') {
94 if (isset($this->code)) {
95 $this->code = trim($this->code);
97 if (isset($this->label)) {
98 $this->label = trim($this->label);
100 if (isset($this->active)) {
101 $this->active = trim($this->active);
105 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
109 $sql .=
') VALUES (';
110 $sql .=
' '.(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
',';
111 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
112 $sql .=
' '.(!isset($this->active) ?
'NULL' : $this->active);
117 $resql = $this->db->query($sql);
120 $this->errors[] =
'Error '.$this->db->lasterror();
121 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
125 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
140 $this->db->rollback();
159 public function fetch($id, $code =
'', $label =
'')
163 $fieldrowid =
'rowid';
164 $fieldlabel =
'label';
165 if ($this->table_element ==
'c_stcomm') {
167 $fieldlabel =
'libelle';
168 } elseif ($this->table_element ==
'c_type_fees') {
173 $sql .=
" t.".$fieldrowid.
",";
175 $sql .=
" t.".$fieldlabel.
" as label,";
177 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
179 $sql .=
" WHERE t.".$fieldrowid.
" = ".((int) $id);
181 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
183 $sql .=
" WHERE t.label = '".$this->db->escape($label).
"'";
186 $resql = $this->db->query($sql);
188 $numrows = $this->db->num_rows($resql);
190 $obj = $this->db->fetch_object($resql);
192 $this->
id = $obj->$fieldrowid;
194 $this->code = $obj->code;
195 $this->label = $obj->label;
196 $this->active = $obj->active;
205 $this->db->free($resql);
213 $this->errors[] =
'Error '.$this->db->lasterror();
214 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
232 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
236 $fieldrowid =
'rowid';
237 $fieldlabel =
'label';
238 if ($this->table_element ==
'c_stcomm') {
240 $fieldlabel =
'libelle';
241 } elseif ($this->table_element ==
'c_type_fees') {
246 $sql .=
" t.".$fieldrowid.
",";
248 $sql .=
" t.".$fieldlabel.
" as label,";
250 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
254 if (count($filter) > 0) {
255 foreach ($filter as $key => $value) {
256 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
260 if (count($sqlwhere) > 0) {
261 $sql .=
" WHERE ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
263 if (!empty($sortfield)) {
264 $sql .= $this->db->order($sortfield, $sortorder);
266 if (!empty($limit)) {
267 $sql .= $this->db->plimit($limit, $offset);
270 $resql = $this->db->query($sql);
272 $num = $this->db->num_rows($resql);
274 while ($obj = $this->db->fetch_object($resql)) {
275 $line =
new self($this->db);
277 $line->id = $obj->$fieldrowid;
279 $line->code = $obj->code;
280 $line->label = $obj->label;
281 $line->active = $obj->active;
283 $this->db->free($resql);
287 $this->errors[] =
'Error '.$this->db->lasterror();
288 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
308 $fieldrowid =
'rowid';
309 $fieldlabel =
'label';
310 if ($this->table_element ==
'c_stcomm') {
312 $fieldlabel =
'libelle';
313 } elseif ($this->table_element ==
'c_type_fees') {
319 if (isset($this->code)) {
320 $this->code = trim($this->code);
322 if (isset($this->label)) {
323 $this->label = trim($this->label);
325 if (isset($this->active)) {
326 $this->active = trim($this->active);
333 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
334 $sql .=
" code = ".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
',';
335 $sql .=
" ".$fieldlabel.
" = ".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
336 $sql .=
" active = ".(isset($this->active) ? $this->active :
"null");
337 $sql .=
" WHERE ".$fieldrowid.
" = ".((int) $this->
id);
341 $resql = $this->db->query($sql);
344 $this->errors[] =
'Error '.$this->db->lasterror();
345 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
360 $this->db->rollback();
378 public function delete(
User $user, $notrigger =
false)
382 $fieldrowid =
'rowid';
401 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
402 $sql .=
" WHERE ".$fieldrowid.
" = ".((int) $this->
id);
404 $resql = $this->db->query($sql);
407 $this->errors[] =
'Error '.$this->db->lasterror();
408 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
414 $this->db->rollback();
441 $object->fetch($fromid);
449 $object->context[
'createfromclone'] =
'createfromclone';
450 $result = $object->create($user);
455 $this->errors = $object->errors;
456 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
459 unset($object->context[
'createfromclone']);
467 $this->db->rollback();
483 $this->code =
'CODE';
484 $this->label =
'Label';
create(User $user, $notrigger=false)
Create object into database.
update(User $user, $notrigger=false)
Update object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
fetch($id, $code='', $label='')
Load object in memory from the database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
__construct(DoliDB $db)
Constructor.
Parent class of all other dictionary classes.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.