dolibarr 19.0.3
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
28
29
34{
38 public $element = 'ctyperesource';
39
43 public $table_element = 'c_type_resource';
44
48 public $lines = array();
49
50
56 public function __construct(DoliDB $db)
57 {
58 $this->db = $db;
59 }
60
69 public function create(User $user, $notrigger = false)
70 {
71 dol_syslog(__METHOD__, LOG_DEBUG);
72
73 $error = 0;
74
75 // Clean parameters
76
77 if (isset($this->code)) {
78 $this->code = trim($this->code);
79 }
80 if (isset($this->label)) {
81 $this->label = trim($this->label);
82 }
83 if (isset($this->active)) {
84 $this->active = trim($this->active);
85 }
86
87 // Insert request
88 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
89 $sql .= 'code,';
90 $sql .= 'label';
91 $sql .= 'active';
92 $sql .= ') VALUES (';
93 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
94 $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
95 $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
96 $sql .= ')';
97
98 $this->db->begin();
99
100 $resql = $this->db->query($sql);
101 if (!$resql) {
102 $error++;
103 $this->errors[] = 'Error '.$this->db->lasterror();
104 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
105 }
106
107 if (!$error) {
108 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
109
110 // Uncomment this and change CTYPERESOURCE to your own tag if you
111 // want this action to call a trigger.
112 //if (!$notrigger) {
113
114 // // Call triggers
115 // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
116 // if ($result < 0) $error++;
117 // // End call triggers
118 //}
119 }
120
121 // Commit or rollback
122 if ($error) {
123 $this->db->rollback();
124
125 return -1 * $error;
126 } else {
127 $this->db->commit();
128
129 return $this->id;
130 }
131 }
132
142 public function fetch($id, $code = '', $label = '')
143 {
144 dol_syslog(__METHOD__, LOG_DEBUG);
145
146 $sql = "SELECT";
147 $sql .= " t.rowid,";
148 $sql .= " t.code,";
149 $sql .= " t.label,";
150 $sql .= " t.active";
151 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
152 if ($id) {
153 $sql .= " WHERE t.id = ".((int) $id);
154 } elseif ($code) {
155 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
156 } elseif ($label) {
157 $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
158 }
159
160 $resql = $this->db->query($sql);
161 if ($resql) {
162 $numrows = $this->db->num_rows($resql);
163 if ($numrows) {
164 $obj = $this->db->fetch_object($resql);
165
166 $this->id = $obj->rowid;
167
168 $this->code = $obj->code;
169 $this->label = $obj->label;
170 $this->active = $obj->active;
171 }
172
173 // Retrieve all extrafields for invoice
174 // fetch optionals attributes and labels
175 // $this->fetch_optionals();
176
177 // $this->fetch_lines();
178
179 $this->db->free($resql);
180
181 if ($numrows) {
182 return 1;
183 } else {
184 return 0;
185 }
186 } else {
187 $this->errors[] = 'Error '.$this->db->lasterror();
188 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
189
190 return -1;
191 }
192 }
193
206 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $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
217 // Manage filter
218 $sqlwhere = array();
219 if (count($filter) > 0) {
220 foreach ($filter as $key => $value) {
221 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
222 }
223 }
224
225 if (count($sqlwhere) > 0) {
226 $sql .= ' WHERE '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
227 }
228 if (!empty($sortfield)) {
229 $sql .= $this->db->order($sortfield, $sortorder);
230 }
231 if (!empty($limit)) {
232 $sql .= $this->db->plimit($limit, $offset);
233 }
234
235 $resql = $this->db->query($sql);
236 if ($resql) {
237 $num = $this->db->num_rows($resql);
238
239 while ($obj = $this->db->fetch_object($resql)) {
240 $line = new self($this->db);
241
242 $line->id = $obj->rowid;
243
244 $line->code = $obj->code;
245 $line->label = $obj->label;
246 $line->active = $obj->active;
247 }
248 $this->db->free($resql);
249
250 return $num;
251 } else {
252 $this->errors[] = 'Error '.$this->db->lasterror();
253 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
254
255 return -1;
256 }
257 }
258
267 public function update(User $user, $notrigger = false)
268 {
269 $error = 0;
270
271 dol_syslog(__METHOD__, LOG_DEBUG);
272
273 // Clean parameters
274
275 if (isset($this->code)) {
276 $this->code = trim($this->code);
277 }
278 if (isset($this->label)) {
279 $this->label = trim($this->label);
280 }
281 if (isset($this->active)) {
282 $this->active = trim($this->active);
283 }
284
285 // Check parameters
286 // Put here code to add a control on parameters values
287
288 // Update request
289 $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
290 $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
291 $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
292 $sql .= ' active = '.(isset($this->active) ? $this->active : "null");
293 $sql .= ' WHERE rowid='.((int) $this->id);
294
295 $this->db->begin();
296
297 $resql = $this->db->query($sql);
298 if (!$resql) {
299 $error++;
300 $this->errors[] = 'Error '.$this->db->lasterror();
301 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
302 }
303
304 // Uncomment this and change CTYPERESOURCE to your own tag if you
305 // want this action calls a trigger.
306 //if (!$error && !$notrigger) {
307
308 // // Call triggers
309 // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
310 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
311 // // End call triggers
312 //}
313
314 // Commit or rollback
315 if ($error) {
316 $this->db->rollback();
317
318 return -1 * $error;
319 } else {
320 $this->db->commit();
321
322 return 1;
323 }
324 }
325
334 public function delete(User $user, $notrigger = false)
335 {
336 dol_syslog(__METHOD__, LOG_DEBUG);
337
338 $error = 0;
339
340 $this->db->begin();
341
342 // Uncomment this and change CTYPERESOURCE to your own tag if you
343 // want this action calls a trigger.
344 //if (!$error && !$notrigger) {
345
346 // // Call triggers
347 // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
348 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
349 // // End call triggers
350 //}
351
352 // If you need to delete child tables to, you can insert them here
353
354 if (!$error) {
355 $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
356 $sql .= ' WHERE rowid='.((int) $this->id);
357
358 $resql = $this->db->query($sql);
359 if (!$resql) {
360 $error++;
361 $this->errors[] = 'Error '.$this->db->lasterror();
362 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
363 }
364 }
365
366 // Commit or rollback
367 if ($error) {
368 $this->db->rollback();
369
370 return -1 * $error;
371 } else {
372 $this->db->commit();
373
374 return 1;
375 }
376 }
377
385 public function createFromClone(User $user, $fromid)
386 {
387 dol_syslog(__METHOD__, LOG_DEBUG);
388
389 $error = 0;
390 $object = new Ctyperesource($this->db);
391
392 $this->db->begin();
393
394 // Load source object
395 $object->fetch($fromid);
396 // Reset object
397 $object->id = 0;
398
399 // Clear fields
400 // ...
401
402 // Create clone
403 $object->context['createfromclone'] = 'createfromclone';
404 $result = $object->create($user);
405
406 // Other options
407 if ($result < 0) {
408 $error++;
409 $this->errors = $object->errors;
410 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
411 }
412
413 unset($object->context['createfromclone']);
414
415 // End
416 if (!$error) {
417 $this->db->commit();
418
419 return $object->id;
420 } else {
421 $this->db->rollback();
422
423 return -1;
424 }
425 }
426
433 public function initAsSpecimen()
434 {
435 $this->id = 0;
436
437 $this->code = '';
438 $this->label = '';
439 $this->active = '';
440 }
441}
442
447{
451 public $id;
452
456 public $code;
457
461 public $label;
462
463 public $active;
464}
Parent class of all other dictionary classes.
Class Ctyperesource.
update(User $user, $notrigger=false)
Update object into database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
fetch($id, $code='', $label='')
Load object in memory from the database.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=false)
Create 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.
Class CtyperesourceLine.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.