29require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
40 public $element =
'ctyperesource';
45 public $table_element =
'c_type_resource';
50 public $lines = array();
78 if (isset($this->code)) {
79 $this->code = trim($this->code);
81 if (isset($this->label)) {
82 $this->label = trim($this->label);
84 if (isset($this->active)) {
85 $this->active = (int) $this->active;
89 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
94 $sql .=
' '.(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
',';
95 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
96 $sql .=
' '.(!isset($this->active) ?
'NULL' : $this->active);
101 $resql = $this->db->query($sql);
104 $this->errors[] =
'Error '.$this->db->lasterror();
105 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
109 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
124 $this->db->rollback();
143 public function fetch($id, $code =
'', $label =
'')
152 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
154 $sql .=
" WHERE t.id = ".((int) $id);
156 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
158 $sql .=
" WHERE t.label = '".$this->db->escape($label).
"'";
161 $resql = $this->db->query($sql);
163 $numrows = $this->db->num_rows($resql);
165 $obj = $this->db->fetch_object($resql);
167 $this->
id = $obj->rowid;
169 $this->code = $obj->code;
170 $this->label = $obj->label;
171 $this->active = $obj->active;
180 $this->db->free($resql);
188 $this->errors[] =
'Error '.$this->db->lasterror();
189 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
206 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
215 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
216 $sql .=
" WHERE 1 = 1";
219 if (is_array($filter)) {
221 if (count($filter) > 0) {
222 foreach ($filter as $key => $value) {
223 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
224 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
225 } elseif (strpos($key,
'date') !==
false) {
226 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
227 } elseif ($key ==
't.label') {
228 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
230 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
234 if (count($sqlwhere) > 0) {
235 $sql .=
" AND ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
245 $this->errors[] = $errormessage;
246 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
250 if (!empty($sortfield)) {
251 $sql .= $this->db->order($sortfield, $sortorder);
253 if (!empty($limit)) {
254 $sql .= $this->db->plimit($limit, $offset);
257 $resql = $this->db->query($sql);
259 $num = $this->db->num_rows($resql);
261 while ($obj = $this->db->fetch_object($resql)) {
262 $line =
new self($this->db);
264 $line->id = $obj->rowid;
266 $line->code = $obj->code;
267 $line->label = $obj->label;
268 $line->active = $obj->active;
270 $this->db->free($resql);
274 $this->errors[] =
'Error '.$this->db->lasterror();
275 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
296 if (isset($this->code)) {
297 $this->code = trim($this->code);
299 if (isset($this->label)) {
300 $this->label = trim($this->label);
302 if (isset($this->active)) {
303 $this->active = (int) $this->active;
310 $sql =
'UPDATE '.$this->db->prefix().$this->table_element.
' SET';
311 $sql .=
' code = '.(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
',';
312 $sql .=
' label = '.(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
313 $sql .=
' active = '.(isset($this->active) ? $this->active :
"null");
314 $sql .=
' WHERE rowid='.((int) $this->
id);
318 $resql = $this->db->query($sql);
321 $this->errors[] =
'Error '.$this->db->lasterror();
322 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
337 $this->db->rollback();
354 public function delete(
User $user, $notrigger = 0)
375 $sql =
'DELETE FROM '.$this->db->prefix().$this->table_element;
376 $sql .=
' WHERE rowid='.((int) $this->
id);
378 $resql = $this->db->query($sql);
381 $this->errors[] =
'Error '.$this->db->lasterror();
382 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
388 $this->db->rollback();
423 $object->context[
'createfromclone'] =
'createfromclone';
424 $result =
$object->create($user);
429 $this->errors =
$object->errors;
430 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
433 unset(
$object->context[
'createfromclone']);
441 $this->db->rollback();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Parent class of all other dictionary classes.
create(User $user, $notrigger=0)
Create object into database.
fetch($id, $code='', $label='')
Load object in memory from the database.
__construct(DoliDB $db)
Constructor.
update(User $user, $notrigger=0)
Update object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load object in memory from the database.
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.