dolibarr 20.0.0
ctyperesource.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2016 Florian Henry <florian.henry@atm-consulting.fr>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28// Put here all includes required by your class file
29require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
30
31
36{
40 public $element = 'ctyperesource';
41
45 public $table_element = 'c_type_resource';
46
50 public $lines = array();
51
52
58 public function __construct(DoliDB $db)
59 {
60 $this->db = $db;
61 }
62
70 public function create(User $user, $notrigger = 0)
71 {
72 dol_syslog(__METHOD__, LOG_DEBUG);
73
74 $error = 0;
75
76 // Clean parameters
77
78 if (isset($this->code)) {
79 $this->code = trim($this->code);
80 }
81 if (isset($this->label)) {
82 $this->label = trim($this->label);
83 }
84 if (isset($this->active)) {
85 $this->active = (int) $this->active;
86 }
87
88 // Insert request
89 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
90 $sql .= 'code,';
91 $sql .= 'label';
92 $sql .= 'active';
93 $sql .= ') VALUES (';
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);
97 $sql .= ')';
98
99 $this->db->begin();
100
101 $resql = $this->db->query($sql);
102 if (!$resql) {
103 $error++;
104 $this->errors[] = 'Error '.$this->db->lasterror();
105 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
106 }
107
108 if (!$error) {
109 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
110
111 // Uncomment this and change CTYPERESOURCE to your own tag if you
112 // want this action to call a trigger.
113 //if (!$notrigger) {
114
115 // // Call triggers
116 // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
117 // if ($result < 0) $error++;
118 // // End call triggers
119 //}
120 }
121
122 // Commit or rollback
123 if ($error) {
124 $this->db->rollback();
125
126 return -1 * $error;
127 } else {
128 $this->db->commit();
129
130 return $this->id;
131 }
132 }
133
143 public function fetch($id, $code = '', $label = '')
144 {
145 dol_syslog(__METHOD__, LOG_DEBUG);
146
147 $sql = "SELECT";
148 $sql .= " t.rowid,";
149 $sql .= " t.code,";
150 $sql .= " t.label,";
151 $sql .= " t.active";
152 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
153 if ($id) {
154 $sql .= " WHERE t.id = ".((int) $id);
155 } elseif ($code) {
156 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
157 } elseif ($label) {
158 $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
159 }
160
161 $resql = $this->db->query($sql);
162 if ($resql) {
163 $numrows = $this->db->num_rows($resql);
164 if ($numrows) {
165 $obj = $this->db->fetch_object($resql);
166
167 $this->id = $obj->rowid;
168
169 $this->code = $obj->code;
170 $this->label = $obj->label;
171 $this->active = $obj->active;
172 }
173
174 // Retrieve all extrafields for invoice
175 // fetch optionals attributes and labels
176 // $this->fetch_optionals();
177
178 // $this->fetch_lines();
179
180 $this->db->free($resql);
181
182 if ($numrows) {
183 return 1;
184 } else {
185 return 0;
186 }
187 } else {
188 $this->errors[] = 'Error '.$this->db->lasterror();
189 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
190
191 return -1;
192 }
193 }
194
206 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
207 {
208 dol_syslog(__METHOD__, LOG_DEBUG);
209
210 $sql = "SELECT";
211 $sql .= " t.rowid,";
212 $sql .= " t.code,";
213 $sql .= " t.label,";
214 $sql .= " t.active";
215 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
216 $sql .= " WHERE 1 = 1";
217
218 // Manage filter
219 if (is_array($filter)) {
220 $sqlwhere = array();
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)."'";
229 } else {
230 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
231 }
232 }
233 }
234 if (count($sqlwhere) > 0) {
235 $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
236 }
237
238 $filter = '';
239 }
240
241 // Manage filter
242 $errormessage = '';
243 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
244 if ($errormessage) {
245 $this->errors[] = $errormessage;
246 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
247 return -1;
248 }
249
250 if (!empty($sortfield)) {
251 $sql .= $this->db->order($sortfield, $sortorder);
252 }
253 if (!empty($limit)) {
254 $sql .= $this->db->plimit($limit, $offset);
255 }
256
257 $resql = $this->db->query($sql);
258 if ($resql) {
259 $num = $this->db->num_rows($resql);
260
261 while ($obj = $this->db->fetch_object($resql)) {
262 $line = new self($this->db);
263
264 $line->id = $obj->rowid;
265
266 $line->code = $obj->code;
267 $line->label = $obj->label;
268 $line->active = $obj->active;
269 }
270 $this->db->free($resql);
271
272 return $num;
273 } else {
274 $this->errors[] = 'Error '.$this->db->lasterror();
275 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
276
277 return -1;
278 }
279 }
280
288 public function update(User $user, $notrigger = 0)
289 {
290 $error = 0;
291
292 dol_syslog(__METHOD__, LOG_DEBUG);
293
294 // Clean parameters
295
296 if (isset($this->code)) {
297 $this->code = trim($this->code);
298 }
299 if (isset($this->label)) {
300 $this->label = trim($this->label);
301 }
302 if (isset($this->active)) {
303 $this->active = (int) $this->active;
304 }
305
306 // Check parameters
307 // Put here code to add a control on parameters values
308
309 // Update request
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);
315
316 $this->db->begin();
317
318 $resql = $this->db->query($sql);
319 if (!$resql) {
320 $error++;
321 $this->errors[] = 'Error '.$this->db->lasterror();
322 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
323 }
324
325 // Uncomment this and change CTYPERESOURCE to your own tag if you
326 // want this action calls a trigger.
327 //if (!$error && !$notrigger) {
328
329 // // Call triggers
330 // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
331 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
332 // // End call triggers
333 //}
334
335 // Commit or rollback
336 if ($error) {
337 $this->db->rollback();
338
339 return -1 * $error;
340 } else {
341 $this->db->commit();
342
343 return 1;
344 }
345 }
346
354 public function delete(User $user, $notrigger = 0)
355 {
356 dol_syslog(__METHOD__, LOG_DEBUG);
357
358 $error = 0;
359
360 $this->db->begin();
361
362 // Uncomment this and change CTYPERESOURCE to your own tag if you
363 // want this action calls a trigger.
364 //if (!$error && !$notrigger) {
365
366 // // Call triggers
367 // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
368 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
369 // // End call triggers
370 //}
371
372 // If you need to delete child tables to, you can insert them here
373
374 if (!$error) {
375 $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
376 $sql .= ' WHERE rowid='.((int) $this->id);
377
378 $resql = $this->db->query($sql);
379 if (!$resql) {
380 $error++;
381 $this->errors[] = 'Error '.$this->db->lasterror();
382 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
383 }
384 }
385
386 // Commit or rollback
387 if ($error) {
388 $this->db->rollback();
389
390 return -1 * $error;
391 } else {
392 $this->db->commit();
393
394 return 1;
395 }
396 }
397
405 public function createFromClone(User $user, $fromid)
406 {
407 dol_syslog(__METHOD__, LOG_DEBUG);
408
409 $error = 0;
410 $object = new Ctyperesource($this->db);
411
412 $this->db->begin();
413
414 // Load source object
415 $object->fetch($fromid);
416 // Reset object
417 $object->id = 0;
418
419 // Clear fields
420 // ...
421
422 // Create clone
423 $object->context['createfromclone'] = 'createfromclone';
424 $result = $object->create($user);
425
426 // Other options
427 if ($result < 0) {
428 $error++;
429 $this->errors = $object->errors;
430 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
431 }
432
433 unset($object->context['createfromclone']);
434
435 // End
436 if (!$error) {
437 $this->db->commit();
438
439 return $object->id;
440 } else {
441 $this->db->rollback();
442
443 return -1;
444 }
445 }
446
453 public function initAsSpecimen()
454 {
455 $this->id = 0;
456
457 $this->code = '';
458 $this->label = '';
459 $this->active = 0;
460
461 return 1;
462 }
463}
464
469{
473 public $id;
474
478 public $code;
479
483 public $label;
484
485 public $active;
486}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class of all other dictionary classes.
Class Ctyperesource.
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 CtyperesourceLine.
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.