dolibarr 19.0.4
defaultvalues.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2021 Florian HENRY <florian.henry@scopen.fr>
4 * Copyright (C) 2021 Frédéric France <frederic.france@netlogic.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25// Put here all includes required by your class file
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
28//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
29
34{
38 public $element = 'defaultvalues';
39
43 public $table_element = 'default_values';
44
49 public $ismultientitymanaged = 1;
50
54 public $isextrafieldmanaged = 0;
55
59 public $picto = '';
60
87 // BEGIN MODULEBUILDER PROPERTIES
91 public $fields = array(
92 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
93 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>15, 'index'=>1),
94 'type' =>array('type'=>'varchar(10)', 'label'=>'Type', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
95 'user_id' =>array('type'=>'integer', 'label'=>'Userid', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
96 'page' =>array('type'=>'varchar(255)', 'label'=>'RelativeURL', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
97 'param' =>array('type'=>'varchar(255)', 'label'=>'Field', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
98 'value' =>array('type'=>'varchar(128)', 'label'=>'Value', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
99 );
100
104 public $rowid;
105
109 public $entity;
110
114 public $type;
115
119 public $user_id;
120
124 public $page;
125
129 public $param;
130
134 public $value;
135 // END MODULEBUILDER PROPERTIES
136
142 public function __construct(DoliDB $db)
143 {
144 global $conf, $langs;
145
146 $this->db = $db;
147
148 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
149 $this->fields['rowid']['visible'] = 0;
150 }
151 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
152 $this->fields['entity']['enabled'] = 0;
153 }
154
155 // Unset fields that are disabled
156 foreach ($this->fields as $key => $val) {
157 if (isset($val['enabled']) && empty($val['enabled'])) {
158 unset($this->fields[$key]);
159 }
160 }
161
162 // Translate some data of arrayofkeyval
163 if (is_object($langs)) {
164 foreach ($this->fields as $key => $val) {
165 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
166 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
167 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
168 }
169 }
170 }
171 }
172 }
173
181 public function create(User $user, $notrigger = false)
182 {
183 return $this->createCommon($user, $notrigger);
184 }
185
193 public function createFromClone(User $user, $fromid)
194 {
195 global $langs, $extrafields;
196 $error = 0;
197
198 dol_syslog(__METHOD__, LOG_DEBUG);
199
200 $object = new self($this->db);
201
202 $this->db->begin();
203
204 // Load source object
205 $result = $object->fetchCommon($fromid);
206
207 // Reset some properties
208 unset($object->id);
209
210 // Create clone
211 $object->context['createfromclone'] = 'createfromclone';
212 $result = $object->createCommon($user);
213 if ($result < 0) {
214 $error++;
215 $this->error = $object->error;
216 $this->errors = $object->errors;
217 }
218
219 unset($object->context['createfromclone']);
220
221 // End
222 if (!$error) {
223 $this->db->commit();
224 return $object;
225 } else {
226 $this->db->rollback();
227 return -1;
228 }
229 }
230
237 public function fetch($id)
238 {
239 $result = $this->fetchCommon($id, null);
240 return $result;
241 }
242
254 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
255 {
256 global $conf;
257
258 dol_syslog(__METHOD__, LOG_DEBUG);
259
260 $records = array();
261
262 $sql = "SELECT ";
263 $sql .= $this->getFieldList();
264 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
265 $sql .= " WHERE 1 = 1";
266 // Manage filter
267 $sqlwhere = array();
268 if (count($filter) > 0) {
269 foreach ($filter as $key => $value) {
270 if ($key == 't.rowid' || ($key == 't.entity' && !is_array($value)) || ($key == 't.user_id' && !is_array($value))) {
271 $sqlwhere[] = $key." = ".((int) $value);
272 } elseif (array_key_exists($key, $this->fields) && in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
273 $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
274 } elseif ($key == 't.page' || $key == 't.param' || $key == 't.type') {
275 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
276 } elseif ($key == 'customsql') {
277 $sqlwhere[] = $value;
278 } elseif (is_array($value)) {
279 $sqlwhere[] = $key." IN (".$this->db->sanitize(implode(',', $value)).")";
280 } else {
281 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
282 }
283 }
284 }
285 if (count($sqlwhere) > 0) {
286 $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
287 }
288
289 if (!empty($sortfield)) {
290 $sql .= $this->db->order($sortfield, $sortorder);
291 }
292 if (!empty($limit)) {
293 $sql .= $this->db->plimit($limit, $offset);
294 }
295
296 $resql = $this->db->query($sql);
297 if ($resql) {
298 $num = $this->db->num_rows($resql);
299 $i = 0;
300 while ($i < ($limit ? min($limit, $num) : $num)) {
301 $obj = $this->db->fetch_object($resql);
302
303 $record = new self($this->db);
304 $record->setVarsFromFetchObj($obj);
305
306 $records[$record->id] = $record;
307
308 $i++;
309 }
310 $this->db->free($resql);
311
312 return $records;
313 } else {
314 $this->errors[] = 'Error '.$this->db->lasterror();
315 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
316
317 return -1;
318 }
319 }
320
328 public function update(User $user, $notrigger = false)
329 {
330 return $this->updateCommon($user, $notrigger);
331 }
332
340 public function delete(User $user, $notrigger = false)
341 {
342 return $this->deleteCommon($user, $notrigger);
343 }
344
351 public function initAsSpecimen()
352 {
353 $this->initAsSpecimenCommon();
354 }
355}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=false)
Create object into database.
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
getFieldList($alias='', $excludefields=array())
Function to concat keys of fields.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
updateCommon(User $user, $notrigger=false)
Update object into database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
Class for MyObject.
fetch($id)
Load object in memory from the database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
createFromClone(User $user, $fromid)
Clone an object into another one.
update(User $user, $notrigger=false)
Update object into database.
create(User $user, $notrigger=false)
Create object into database.
__construct(DoliDB $db)
Constructor.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.