dolibarr 24.0.0-beta
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-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
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/commonobject.class.php';
28//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
29//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
30
35{
39 public $element = 'defaultvalues';
40
44 public $table_element = 'default_values';
45
49 public $picto = '';
50
77 // BEGIN MODULEBUILDER PROPERTIES
81 public $fields = array(
82 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
83 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 15, 'index' => 1),
84 'type' => array('type' => 'varchar(10)', 'label' => 'Type', 'enabled' => 1, 'visible' => -1, 'position' => 20),
85 'user_id' => array('type' => 'integer', 'label' => 'Userid', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 25),
86 'page' => array('type' => 'varchar(255)', 'label' => 'RelativeURL', 'enabled' => 1, 'visible' => -1, 'position' => 30),
87 'param' => array('type' => 'varchar(255)', 'label' => 'Field', 'enabled' => 1, 'visible' => -1, 'position' => 35),
88 'value' => array('type' => 'varchar(128)', 'label' => 'Value', 'enabled' => 1, 'visible' => -1, 'position' => 40),
89 );
90
94 public $rowid;
95
99 public $entity;
100
104 public $type;
105
109 public $user_id;
110
114 public $page;
115
119 public $param;
120
124 public $value;
125 // END MODULEBUILDER PROPERTIES
126
132 public function __construct(DoliDB $db)
133 {
134 global $conf, $langs;
135
136 $this->db = $db;
137
138 $this->ismultientitymanaged = 1;
139 $this->isextrafieldmanaged = 0;
140
141 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
142 $this->fields['rowid']['visible'] = 0;
143 }
144 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
145 $this->fields['entity']['enabled'] = 0;
146 }
147
148 // Unset fields that are disabled
149 foreach ($this->fields as $key => $val) {
150 if (isset($val['enabled']) && empty($val['enabled'])) {
151 unset($this->fields[$key]);
152 }
153 }
154
155 // Translate some data of arrayofkeyval
156 if (is_object($langs)) {
157 foreach ($this->fields as $key => $val) {
158 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
159 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
160 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
161 }
162 }
163 }
164 }
165 }
166
174 public function create(User $user, $notrigger = 0)
175 {
176 return $this->createCommon($user, $notrigger);
177 }
178
186 public function createFromClone(User $user, $fromid)
187 {
188 global $langs, $extrafields;
189 $error = 0;
190
191 dol_syslog(__METHOD__, LOG_DEBUG);
192
193 $object = new self($this->db);
194
195 $this->db->begin();
196
197 // Load source object
198 $result = $object->fetchCommon($fromid);
199
200 // Reset some properties
201 unset($object->id);
202
203 // Create clone
204 $object->context['createfromclone'] = 'createfromclone';
205 $result = $object->createCommon($user);
206 if ($result < 0) {
207 $error++;
209 }
210
211 unset($object->context['createfromclone']);
212
213 // End
214 if (!$error) {
215 $this->db->commit();
216 return $object;
217 } else {
218 $this->db->rollback();
219 return -1;
220 }
221 }
222
229 public function fetch($id)
230 {
231 $result = $this->fetchCommon($id, null);
232 return $result;
233 }
234
247 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
248 {
249 dol_syslog(__METHOD__, LOG_DEBUG);
250
251 $records = array();
252
253 $sql = "SELECT ";
254 $sql .= $this->getFieldList();
255 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
256 $sql .= " WHERE 1 = 1";
257
258 // Deprecated. For compatibility.
259 if (is_array($filter)) {
260 $sqlwhere = array();
261 if (count($filter) > 0) {
262 foreach ($filter as $key => $value) {
263 if ($key == 't.rowid' || ($key == 't.entity' && !is_array($value)) || ($key == 't.user_id' && !is_array($value))) {
264 $sqlwhere[] = $key." = ".((int) $value);
265 } elseif (array_key_exists($key, $this->fields) && in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
266 $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
267 } elseif ($key == 't.page' || $key == 't.param' || $key == 't.type') {
268 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
269 } elseif (is_array($value)) {
270 $sqlwhere[] = $key." IN (".$this->db->sanitize(implode(',', $value)).")";
271 } else {
272 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
273 }
274 }
275 }
276 if (count($sqlwhere) > 0) {
277 $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
278 }
279
280 $filter = '';
281 }
282
283 // Manage filter
284 $errormessage = '';
285 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
286 if ($errormessage) {
287 $this->errors[] = $errormessage;
288 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
289 return -1;
290 }
291
292 if (!empty($sortfield)) {
293 $sql .= $this->db->order($sortfield, $sortorder);
294 }
295 if (!empty($limit)) {
296 $sql .= $this->db->plimit($limit, $offset);
297 }
298
299 $resql = $this->db->query($sql);
300 if ($resql) {
301 $num = $this->db->num_rows($resql);
302 $i = 0;
303 while ($i < ($limit ? min($limit, $num) : $num)) {
304 $obj = $this->db->fetch_object($resql);
305
306 $record = new self($this->db);
307 $record->setVarsFromFetchObj($obj);
308
309 $records[$record->id] = $record;
310
311 $i++;
312 }
313 $this->db->free($resql);
314
315 return $records;
316 } else {
317 $this->errors[] = 'Error '.$this->db->lasterror();
318 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
319
320 return -1;
321 }
322 }
323
331 public function update(User $user, $notrigger = 0)
332 {
333 return $this->updateCommon($user, $notrigger);
334 }
335
343 public function delete(User $user, $notrigger = 0)
344 {
345 return $this->deleteCommon($user, $notrigger);
346 }
347
354 public function initAsSpecimen()
355 {
356 return $this->initAsSpecimenCommon();
357 }
358}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class of all other business classes (invoices, contracts, proposals, orders,...
setErrorsFromObject($object)
setErrorsFromObject
createCommon(User $user, $notrigger=0)
Create object in the database.
getFieldList($alias='', $excludefields=array())
Function to concat keys of fields.
updateCommon(User $user, $notrigger=0)
Update object into database.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class for MyObject.
fetch($id)
Load object in memory from the database.
createFromClone(User $user, $fromid)
Clone an object into another one.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
create(User $user, $notrigger=0)
Create object into database.
update(User $user, $notrigger=0)
Update 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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.