29require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
39 public $element =
'undefined';
44 public $table_element =
'undefined';
49 public $lines = array();
90 $fieldlabel =
'label';
91 if ($this->table_element ==
'c_stcomm') {
92 $fieldlabel =
'libelle';
93 } elseif ($this->table_element ==
'c_type_fees') {
101 if (isset($this->code)) {
102 $this->code = trim($this->code);
104 if (isset($this->label)) {
105 $this->label = trim($this->label);
107 if (isset($this->active)) {
108 $this->active = (int) $this->active;
112 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
116 $sql .=
') VALUES (';
117 $sql .=
' '.(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
',';
118 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
119 $sql .=
' '.(!isset($this->active) ?
'NULL' : $this->active);
124 $resql = $this->db->query($sql);
127 $this->errors[] =
'Error '.$this->db->lasterror();
128 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
132 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
147 $this->db->rollback();
166 public function fetch($id, $code =
'', $label =
'')
170 $fieldrowid =
'rowid';
171 $fieldlabel =
'label';
172 if ($this->table_element ==
'c_stcomm') {
174 $fieldlabel =
'libelle';
175 } elseif ($this->table_element ==
'c_type_fees') {
180 $sql .=
" t.".$fieldrowid.
",";
182 $sql .=
" t.".$fieldlabel.
" as label,";
184 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
186 $sql .=
" WHERE t.".$fieldrowid.
" = ".((int) $id);
188 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
190 $sql .=
" WHERE t.label = '".$this->db->escape($label).
"'";
193 $resql = $this->db->query($sql);
195 $numrows = $this->db->num_rows($resql);
197 $obj = $this->db->fetch_object($resql);
199 $this->
id = $obj->$fieldrowid;
201 $this->code = $obj->code;
202 $this->label = $obj->label;
203 $this->active = $obj->active;
212 $this->db->free($resql);
220 $this->errors[] =
'Error '.$this->db->lasterror();
221 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
238 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
242 $fieldrowid =
'rowid';
243 $fieldlabel =
'label';
244 if ($this->table_element ==
'c_stcomm') {
246 $fieldlabel =
'libelle';
247 } elseif ($this->table_element ==
'c_type_fees') {
252 $sql .=
" t.".$this->db->sanitize($fieldrowid).
",";
254 $sql .=
" t.".$this->db->sanitize($fieldlabel).
" as label,";
256 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
259 if (is_array($filter)) {
261 if (count($filter) > 0) {
262 foreach ($filter as $key => $value) {
263 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
266 if (count($sqlwhere) > 0) {
267 $sql .=
" WHERE ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
277 $this->errors[] = $errormessage;
278 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
282 if (!empty($sortfield)) {
283 $sql .= $this->db->order($sortfield, $sortorder);
285 if (!empty($limit)) {
286 $sql .= $this->db->plimit($limit, $offset);
289 $resql = $this->db->query($sql);
291 $num = $this->db->num_rows($resql);
293 while ($obj = $this->db->fetch_object($resql)) {
294 $line =
new self($this->db);
296 $line->id = $obj->$fieldrowid;
298 $line->code = $obj->code;
299 $line->label = $obj->label;
300 $line->active = $obj->active;
302 $this->db->free($resql);
306 $this->errors[] =
'Error '.$this->db->lasterror();
307 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
326 $fieldrowid =
'rowid';
327 $fieldlabel =
'label';
328 if ($this->table_element ==
'c_stcomm') {
330 $fieldlabel =
'libelle';
331 } elseif ($this->table_element ==
'c_type_fees') {
337 if (isset($this->code)) {
338 $this->code = trim($this->code);
340 if (isset($this->label)) {
341 $this->label = trim($this->label);
343 if (isset($this->active)) {
344 $this->active = (int) $this->active;
351 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
352 $sql .=
" code = ".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
',';
353 $sql .=
" ".$this->db->sanitize($fieldlabel).
" = ".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
354 $sql .=
" active = ".(isset($this->active) ? $this->active :
"null");
355 $sql .=
" WHERE ".$this->db->sanitize($fieldrowid).
" = ".((int) $this->
id);
359 $resql = $this->db->query($sql);
362 $this->errors[] =
'Error '.$this->db->lasterror();
363 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
378 $this->db->rollback();
395 public function delete(
User $user, $notrigger = 0)
399 $fieldrowid =
'rowid';
418 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
419 $sql .=
" WHERE ".$fieldrowid.
" = ".((int) $this->
id);
421 $resql = $this->db->query($sql);
424 $this->errors[] =
'Error '.$this->db->lasterror();
425 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
431 $this->db->rollback();
466 $object->context[
'createfromclone'] =
'createfromclone';
467 $result =
$object->create($user);
472 $this->errors =
$object->errors;
473 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
476 unset(
$object->context[
'createfromclone']);
484 $this->db->rollback();
500 $this->code =
'CODE';
501 $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.