86 public function create($user, $notrigger = 0)
89 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
"(";
97 $sql .=
" ".(!isset($this->
id) ?
'NULL' : ((int) $this->
id)).
",";
98 $sql .=
" ".(!isset($this->code) ?
'NULL' : ((int) $this->code)).
",";
99 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape(trim($this->label)).
"'").
",";
101 $sql .=
" ".(!isset($this->percent) ?
'NULL' : (float) $this->percent).
",";
102 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active)).
",";
107 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
108 $resql = $this->db->query($sql);
111 dol_syslog(get_class($this).
"::create ".$this->db->lasterror(), LOG_ERR);
112 $this->error =
"Error ".$this->db->lasterror();
113 $this->db->rollback();
116 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
130 public function fetch($id, $code =
'')
136 $sql .=
" t.position,";
137 $sql .=
" t.percent,";
139 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
140 $sql_where = array();
142 $sql_where[] =
" t.rowid = ".((int) $id);
145 $sql_where[] =
" t.code = ".((int) $code);
147 if (count($sql_where) > 0) {
148 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
151 $resql = $this->db->query($sql);
153 if ($this->db->num_rows($resql)) {
154 $obj = $this->db->fetch_object($resql);
156 $this->
id = $obj->rowid;
157 $this->code = $obj->code;
158 $this->label = $obj->label;
160 $this->percent = $obj->percent;
161 $this->active = $obj->active;
163 $this->db->free($resql);
167 $this->error =
"Error ".$this->db->lasterror();
184 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
192 $sql .=
" t.position,";
193 $sql .=
" t.percent,";
195 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
196 $sql .=
" WHERE 1 = 1";
199 if (is_array($filter)) {
201 if (count($filter) > 0) {
202 foreach ($filter as $key => $value) {
203 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
204 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
205 } elseif (strpos($key,
'date') !==
false) {
206 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
207 } elseif ($key ==
't.label') {
208 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
210 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
214 if (count($sqlwhere) > 0) {
215 $sql .=
" AND ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
225 $this->errors[] = $errormessage;
226 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
230 if (!empty($sortfield)) {
231 $sql .= $this->db->order($sortfield, $sortorder);
233 if (!empty($limit)) {
234 $sql .= $this->db->plimit($limit, $offset);
237 $resql = $this->db->query($sql);
239 $this->records = array();
240 $num = $this->db->num_rows($resql);
242 while ($obj = $this->db->fetch_object($resql)) {
243 $record =
new self($this->db);
245 $record->id = $obj->rowid;
246 $record->code = $obj->code;
247 $record->label = $obj->label;
248 $record->position = $obj->position;
249 $record->percent = $obj->percent;
250 $record->active = $obj->active;
251 $this->records[$record->id] = $record;
254 $this->db->free($resql);
256 return $this->records;
258 $this->errors[] =
'Error '.$this->db->lasterror();
259 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
273 public function update($user =
null, $notrigger = 0)
276 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
277 $sql .=
" code=".(isset($this->code) ? ((int) $this->code) :
"null").
",";
278 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape(trim($this->label)).
"'" :
"null").
",";
279 $sql .=
" position=".(isset($this->
position) ? (int) $this->
position :
"null").
",";
280 $sql .=
" percent=".(isset($this->label) ? (float) $this->percent :
"null").
",";
281 $sql .=
" active=".(isset($this->active) ? ((int) $this->active) :
"null");
282 $sql .=
" WHERE rowid=".(int) $this->
id;
286 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
287 $resql = $this->db->query($sql);
290 dol_syslog(get_class($this).
"::update Error ".$this->db->lasterror(), LOG_ERR);
291 $this->error =
"Error ".$this->db->lasterror();
292 $this->db->rollback();