29require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
39 public $element =
'undefined';
44 public $table_element =
'undefined';
49 public $lines = array();
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)) {
102 $this->active = (int) $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 .=
" t.".$fieldrowid.
",";
176 $sql .=
" t.".$fieldlabel.
" as label,";
178 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
180 $sql .=
" WHERE 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);
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)) {
338 $this->active = (int) $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 .=
" ".$fieldlabel.
" = ".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
348 $sql .=
" active = ".(isset($this->active) ? $this->active :
"null");
349 $sql .=
" WHERE ".$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 ".$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();
460 $object->context[
'createfromclone'] =
'createfromclone';
461 $result =
$object->create($user);
466 $this->errors =
$object->errors;
467 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
470 unset(
$object->context[
'createfromclone']);
478 $this->db->rollback();
494 $this->code =
'CODE';
495 $this->label =
'Label';
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.