84 $fieldlabel =
'label';
85 if ($this->table_element ==
'c_stcomm') {
86 $fieldlabel =
'libelle';
87 } elseif ($this->table_element ==
'c_type_fees') {
95 if (isset($this->code)) {
96 $this->code = trim($this->code);
98 if (isset($this->label)) {
99 $this->label = trim($this->label);
101 if (isset($this->
active)) {
106 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
110 $sql .=
') VALUES (';
111 $sql .=
' '.(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
',';
112 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
113 $sql .=
' '.(!isset($this->
active) ?
'NULL' : $this->active);
118 $resql = $this->db->query($sql);
121 $this->errors[] =
'Error '.$this->db->lasterror();
122 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
126 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
141 $this->db->rollback();
160 public function fetch($id, $code =
'', $label =
'')
164 $fieldrowid =
'rowid';
165 $fieldlabel =
'label';
166 if ($this->table_element ==
'c_stcomm') {
168 $fieldlabel =
'libelle';
169 } elseif ($this->table_element ==
'c_type_fees') {
174 $sql .=
" ".$this->db->sanitize(
"t.".$fieldrowid).
",";
176 $sql .=
" ".$this->db->sanitize(
"t.".$fieldlabel).
" as label,";
178 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
180 $sql .=
" WHERE ".$this->db->sanitize(
"t.".$fieldrowid).
" = ".((int) $id);
182 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
184 $sql .=
" WHERE t.label = '".$this->db->escape($label).
"'";
187 $resql = $this->db->query($sql);
189 $numrows = $this->db->num_rows($resql);
191 $obj = $this->db->fetch_object($resql);
193 $this->
id = $obj->$fieldrowid;
195 $this->code = $obj->code;
196 $this->label = $obj->label;
197 $this->
active = $obj->active;
206 $this->db->free($resql);
214 $this->errors[] =
'Error '.$this->db->lasterror();
215 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
232 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $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.".$this->db->sanitize($fieldrowid).
",";
248 $sql .=
" t.".$this->db->sanitize($fieldlabel).
" as label,";
250 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
253 if (is_array($filter)) {
255 if (count($filter) > 0) {
256 foreach ($filter as $key => $value) {
257 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
260 if (count($sqlwhere) > 0) {
261 $sql .=
" WHERE ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
269 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
271 $this->errors[] = $errormessage;
272 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
276 if (!empty($sortfield)) {
277 $sql .= $this->db->order($sortfield, $sortorder);
279 if (!empty($limit)) {
280 $sql .= $this->db->plimit($limit, $offset);
283 $resql = $this->db->query($sql);
285 $num = $this->db->num_rows($resql);
287 while ($obj = $this->db->fetch_object($resql)) {
288 $line =
new self($this->db);
290 $line->id = $obj->$fieldrowid;
292 $line->code = $obj->code;
293 $line->label = $obj->label;
294 $line->active = $obj->active;
296 $this->db->free($resql);
300 $this->errors[] =
'Error '.$this->db->lasterror();
301 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
320 $fieldrowid =
'rowid';
321 $fieldlabel =
'label';
322 if ($this->table_element ==
'c_stcomm') {
324 $fieldlabel =
'libelle';
325 } elseif ($this->table_element ==
'c_type_fees') {
331 if (isset($this->code)) {
332 $this->code = trim($this->code);
334 if (isset($this->label)) {
335 $this->label = trim($this->label);
337 if (isset($this->
active)) {
345 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
346 $sql .=
" code = ".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
',';
347 $sql .=
" ".$this->db->sanitize($fieldlabel).
" = ".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
348 $sql .=
" active = ".(isset($this->
active) ? $this->
active :
"null");
349 $sql .=
" WHERE ".$this->db->sanitize($fieldrowid).
" = ".((int) $this->
id);
353 $resql = $this->db->query($sql);
356 $this->errors[] =
'Error '.$this->db->lasterror();
357 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
372 $this->db->rollback();
389 public function delete(
User $user, $notrigger = 0)
393 $fieldrowid =
'rowid';
412 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
413 $sql .=
" WHERE ".$this->db->sanitize($fieldrowid).
" = ".((int) $this->
id);
415 $resql = $this->db->query($sql);
418 $this->errors[] =
'Error '.$this->db->lasterror();
419 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
425 $this->db->rollback();