29require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
39 public $element =
'undefined';
44 public $table_element =
'undefined';
49 public $lines = array();
84 $fieldrowid =
'rowid';
85 $fieldlabel =
'label';
86 if ($this->table_element ==
'c_stcomm') {
88 $fieldlabel =
'libelle';
89 } elseif ($this->table_element ==
'c_type_fees') {
97 if (isset($this->code)) {
98 $this->code = trim($this->code);
100 if (isset($this->label)) {
101 $this->label = trim($this->label);
103 if (isset($this->
active)) {
108 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
109 if (!empty($this->
id)) {
110 $sql .= $this->db->sanitize($fieldrowid).
',';
113 $sql .= $this->db->sanitize($fieldlabel).
',';
115 $sql .=
') VALUES (';
116 if (!empty($this->
id)) {
117 $sql .= ((int) $this->
id).
',';
119 $sql .=
' '.(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
',';
120 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
121 $sql .=
' '.(!isset($this->
active) ?
'NULL' : ((int) $this->
active));
126 $resql = $this->db->query($sql);
129 $this->errors[] =
'Error '.$this->db->lasterror();
130 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
133 if (!$error && empty($this->
id)) {
134 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element, $fieldrowid);
150 $this->db->rollback();
169 public function fetch($id, $code =
'', $label =
'')
173 $fieldrowid =
'rowid';
174 $fieldlabel =
'label';
175 if ($this->table_element ==
'c_stcomm') {
177 $fieldlabel =
'libelle';
178 } elseif ($this->table_element ==
'c_type_fees') {
183 $sql .=
" ".$this->db->sanitize(
"t.".$fieldrowid).
",";
185 $sql .=
" ".$this->db->sanitize(
"t.".$fieldlabel).
" as label,";
187 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
189 $sql .=
" WHERE ".$this->db->sanitize(
"t.".$fieldrowid).
" = ".((int) $id);
191 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
193 $sql .=
" WHERE ".$this->db->sanitize(
"t.".$fieldlabel).
" = '".$this->db->escape($label).
"'";
196 $resql = $this->db->query($sql);
198 $numrows = $this->db->num_rows($resql);
200 $obj = $this->db->fetch_object($resql);
202 $this->
id = $obj->$fieldrowid;
204 $this->code = $obj->code;
205 $this->label = $obj->label;
206 $this->
active = $obj->active;
215 $this->db->free($resql);
223 $this->errors[] =
'Error '.$this->db->lasterror();
224 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
241 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
245 $fieldrowid =
'rowid';
246 $fieldlabel =
'label';
247 if ($this->table_element ==
'c_stcomm') {
249 $fieldlabel =
'libelle';
250 } elseif ($this->table_element ==
'c_type_fees') {
255 $sql .=
" t.".$this->db->sanitize($fieldrowid).
",";
257 $sql .=
" t.".$this->db->sanitize($fieldlabel).
" as label,";
259 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
262 if (is_array($filter)) {
264 if (count($filter) > 0) {
265 foreach ($filter as $key => $value) {
266 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
269 if (count($sqlwhere) > 0) {
270 $sql .=
" WHERE ".implode(
' '.$this->db->sanitize($filtermode).
' ', $sqlwhere);
280 $this->errors[] = $errormessage;
281 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
285 if (!empty($sortfield)) {
286 $sql .= $this->db->order($sortfield, $sortorder);
288 if (!empty($limit)) {
289 $sql .= $this->db->plimit($limit, $offset);
292 $resql = $this->db->query($sql);
294 $num = $this->db->num_rows($resql);
296 $this->lines = array();
297 while ($obj = $this->db->fetch_object($resql)) {
298 $line =
new self($this->db);
300 $line->id = $obj->$fieldrowid;
302 $line->code = $obj->code;
303 $line->label = $obj->label;
304 $line->active = $obj->active;
306 $this->lines[] = $line;
308 $this->db->free($resql);
312 $this->errors[] =
'Error '.$this->db->lasterror();
313 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
332 $fieldrowid =
'rowid';
333 $fieldlabel =
'label';
334 if ($this->table_element ==
'c_stcomm') {
336 $fieldlabel =
'libelle';
337 } elseif ($this->table_element ==
'c_type_fees') {
343 if (isset($this->code)) {
344 $this->code = trim($this->code);
346 if (isset($this->label)) {
347 $this->label = trim($this->label);
349 if (isset($this->
active)) {
357 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
358 $sql .=
" code = ".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
',';
359 $sql .=
" ".$this->db->sanitize($fieldlabel).
" = ".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
360 $sql .=
" active = ".(isset($this->
active) ? ((int) $this->
active) :
"null");
361 $sql .=
" WHERE ".$this->db->sanitize($fieldrowid).
" = ".((int) $this->
id);
365 $resql = $this->db->query($sql);
368 $this->errors[] =
'Error '.$this->db->lasterror();
369 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
384 $this->db->rollback();
401 public function delete(
User $user, $notrigger = 0)
405 $fieldrowid =
'rowid';
406 if ($this->table_element ==
'c_stcomm' || $this->table_element ==
'c_type_fees') {
427 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
428 $sql .=
" WHERE ".$this->db->sanitize($fieldrowid).
" = ".((int) $this->
id);
430 $resql = $this->db->query($sql);
433 $this->errors[] =
'Error '.$this->db->lasterror();
434 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
440 $this->db->rollback();
475 $object->context[
'createfromclone'] =
'createfromclone';
476 $result =
$object->create($user);
481 $this->errors =
$object->errors;
482 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
485 unset(
$object->context[
'createfromclone']);
493 $this->db->rollback();
509 $this->code =
'CODE';
510 $this->label =
'Label';
if(! $sortfield) if(! $sortorder) $object
create(User $user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
update(User $user, $notrigger=0)
Update object into database.
fetch($id, $code='', $label='')
Load object in memory from the database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.