83 public function create($user, $notrigger = 0)
86 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
"(";
94 $sql .=
" ".(!isset($this->
id) ?
'NULL' : ((int) $this->
id)).
",";
95 $sql .=
" ".(!isset($this->code) ?
'NULL' : ((int) $this->code)).
",";
96 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape(trim($this->label)).
"'").
",";
98 $sql .=
" ".(!isset($this->percent) ?
'NULL' : (float) $this->percent).
",";
99 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active)).
",";
104 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
105 $resql = $this->db->query($sql);
108 dol_syslog(get_class($this).
"::create ".$this->db->lasterror(), LOG_ERR);
109 $this->error =
"Error ".$this->db->lasterror();
110 $this->db->rollback();
113 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
127 public function fetch($id, $code =
'')
133 $sql .=
" t.position,";
134 $sql .=
" t.percent,";
136 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
137 $sql_where = array();
139 $sql_where[] =
" t.rowid = ".((int) $id);
142 $sql_where[] =
" t.code = ".((int) $code);
144 if (count($sql_where) > 0) {
145 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
148 $resql = $this->db->query($sql);
150 if ($this->db->num_rows($resql)) {
151 $obj = $this->db->fetch_object($resql);
153 $this->
id = $obj->rowid;
154 $this->code = $obj->code;
155 $this->label = $obj->label;
157 $this->percent = $obj->percent;
158 $this->active = $obj->active;
160 $this->db->free($resql);
164 $this->error =
"Error ".$this->db->lasterror();
181 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
189 $sql .=
" t.position,";
190 $sql .=
" t.percent,";
192 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
193 $sql .=
" WHERE 1 = 1";
196 if (is_array($filter)) {
198 if (count($filter) > 0) {
199 foreach ($filter as $key => $value) {
200 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
201 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
202 } elseif (strpos($key,
'date') !==
false) {
203 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
204 } elseif ($key ==
't.label') {
205 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
207 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
211 if (count($sqlwhere) > 0) {
212 $sql .=
" AND ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
222 $this->errors[] = $errormessage;
223 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
227 if (!empty($sortfield)) {
228 $sql .= $this->db->order($sortfield, $sortorder);
230 if (!empty($limit)) {
231 $sql .= $this->db->plimit($limit, $offset);
234 $resql = $this->db->query($sql);
236 $this->records = array();
237 $num = $this->db->num_rows($resql);
239 while ($obj = $this->db->fetch_object($resql)) {
240 $record =
new self($this->db);
242 $record->id = $obj->rowid;
243 $record->code = $obj->code;
244 $record->label = $obj->label;
245 $record->position = $obj->position;
246 $record->percent = $obj->percent;
247 $record->active = $obj->active;
248 $this->records[$record->id] = $record;
251 $this->db->free($resql);
253 return $this->records;
255 $this->errors[] =
'Error '.$this->db->lasterror();
256 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
270 public function update($user =
null, $notrigger = 0)
273 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
274 $sql .=
" code=".(isset($this->code) ? ((int) $this->code) :
"null").
",";
275 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape(trim($this->label)).
"'" :
"null").
",";
276 $sql .=
" position=".(isset($this->
position) ? (int) $this->
position :
"null").
",";
277 $sql .=
" percent=".(isset($this->label) ? (float) $this->percent :
"null").
",";
278 $sql .=
" active=".(isset($this->active) ? ((int) $this->active) :
"null");
279 $sql .=
" WHERE rowid=".(int) $this->
id;
283 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
284 $resql = $this->db->query($sql);
287 dol_syslog(get_class($this).
"::update Error ".$this->db->lasterror(), LOG_ERR);
288 $this->error =
"Error ".$this->db->lasterror();
289 $this->db->rollback();