dolibarr  20.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-2024 Frédéric France <frederic.france@free.fr>
5  * Copyright (C) 2024 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
27 require_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++;
208  $this->error = $object->error;
209  $this->errors = $object->errors;
210  }
211 
212  unset($object->context['createfromclone']);
213 
214  // End
215  if (!$error) {
216  $this->db->commit();
217  return $object;
218  } else {
219  $this->db->rollback();
220  return -1;
221  }
222  }
223 
230  public function fetch($id)
231  {
232  $result = $this->fetchCommon($id, null);
233  return $result;
234  }
235 
248  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
249  {
250  dol_syslog(__METHOD__, LOG_DEBUG);
251 
252  $records = array();
253 
254  $sql = "SELECT ";
255  $sql .= $this->getFieldList();
256  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
257  $sql .= " WHERE 1 = 1";
258 
259  // Deprecated. For compatibility.
260  if (is_array($filter)) {
261  $sqlwhere = array();
262  if (count($filter) > 0) {
263  foreach ($filter as $key => $value) {
264  if ($key == 't.rowid' || ($key == 't.entity' && !is_array($value)) || ($key == 't.user_id' && !is_array($value))) {
265  $sqlwhere[] = $key." = ".((int) $value);
266  } elseif (array_key_exists($key, $this->fields) && in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
267  $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
268  } elseif ($key == 't.page' || $key == 't.param' || $key == 't.type') {
269  $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
270  } elseif (is_array($value)) {
271  $sqlwhere[] = $key." IN (".$this->db->sanitize(implode(',', $value)).")";
272  } else {
273  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
274  }
275  }
276  }
277  if (count($sqlwhere) > 0) {
278  $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
279  }
280 
281  $filter = '';
282  }
283 
284  // Manage filter
285  $errormessage = '';
286  $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
287  if ($errormessage) {
288  $this->errors[] = $errormessage;
289  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
290  return -1;
291  }
292 
293  if (!empty($sortfield)) {
294  $sql .= $this->db->order($sortfield, $sortorder);
295  }
296  if (!empty($limit)) {
297  $sql .= $this->db->plimit($limit, $offset);
298  }
299 
300  $resql = $this->db->query($sql);
301  if ($resql) {
302  $num = $this->db->num_rows($resql);
303  $i = 0;
304  while ($i < ($limit ? min($limit, $num) : $num)) {
305  $obj = $this->db->fetch_object($resql);
306 
307  $record = new self($this->db);
308  $record->setVarsFromFetchObj($obj);
309 
310  $records[$record->id] = $record;
311 
312  $i++;
313  }
314  $this->db->free($resql);
315 
316  return $records;
317  } else {
318  $this->errors[] = 'Error '.$this->db->lasterror();
319  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
320 
321  return -1;
322  }
323  }
324 
332  public function update(User $user, $notrigger = 0)
333  {
334  return $this->updateCommon($user, $notrigger);
335  }
336 
344  public function delete(User $user, $notrigger = 0)
345  {
346  return $this->deleteCommon($user, $notrigger);
347  }
348 
355  public function initAsSpecimen()
356  {
357  return $this->initAsSpecimenCommon();
358  }
359 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return 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.