dolibarr  20.0.0-beta
ProductAttributeValue.class.php
1 <?php
2 /* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
3  * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  * Copyright (C) 2024 Frédéric France <frederic.france@free.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 
21 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
27 {
31  public $module = 'variants';
32 
36  public $element = 'productattributevalue';
37 
41  public $table_element = 'product_attribute_value';
42 
71  public $fields = array(
72  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id"),
73  'fk_product_attribute' => array('type' => 'integer:ProductAttribute:variants/class/ProductAttribute.class.php', 'label' => 'ProductAttribute', 'enabled' => 1, 'visible' => 0, 'position' => 10, 'notnull' => 1, 'index' => 1,),
74  'ref' => array('type' => 'varchar(255)', 'label' => 'Ref', 'visible' => 1, 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'index' => 1, 'searchall' => 1, 'comment' => "Reference of object", 'css' => ''),
75  'value' => array('type' => 'varchar(255)', 'label' => 'Value', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 1, 'searchall' => 1, 'css' => 'minwidth300', 'help' => "", 'showoncombobox' => 1,),
76  'position' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => 0, 'default' => '0', 'position' => 200, 'notnull' => 1,),
77  );
78  public $id;
79  public $fk_product_attribute;
80  public $ref;
81  public $value;
82  public $position;
83 
89  public function __construct(DoliDB $db)
90  {
91  global $conf, $langs;
92 
93  $this->db = $db;
94 
95  $this->ismultientitymanaged = 1;
96  $this->isextrafieldmanaged = 0;
97  $this->entity = $conf->entity;
98 
99  if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
100  $this->fields['rowid']['visible'] = 0;
101  }
102  if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
103  $this->fields['entity']['enabled'] = 0;
104  }
105 
106  // Unset fields that are disabled
107  foreach ($this->fields as $key => $val) {
108  if (isset($val['enabled']) && empty($val['enabled'])) {
109  unset($this->fields[$key]);
110  }
111  }
112 
113  // Translate some data of arrayofkeyval
114  if (is_object($langs)) {
115  foreach ($this->fields as $key => $val) {
116  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
117  foreach ($val['arrayofkeyval'] as $key2 => $val2) {
118  $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
119  }
120  }
121  }
122  }
123  }
124 
132  public function create(User $user, $notrigger = 0)
133  {
134  global $langs;
135  $error = 0;
136 
137  // Clean parameters
138  $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
139  $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
140  $this->value = trim($this->value);
141 
142  // Check parameters
143  if (empty($this->fk_product_attribute)) {
144  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductAttribute"));
145  $error++;
146  }
147  if (empty($this->ref)) {
148  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
149  $error++;
150  }
151  if (empty($this->value)) {
152  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value"));
153  $error++;
154  }
155  if ($error) {
156  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
157  return -1;
158  }
159 
160  $this->db->begin();
161 
162  $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (";
163  $sql .= " fk_product_attribute, ref, value, entity, position";
164  $sql .= ")";
165  $sql .= " VALUES (";
166  $sql .= " " . ((int) $this->fk_product_attribute);
167  $sql .= ", '" . $this->db->escape($this->ref) . "'";
168  $sql .= ", '" . $this->db->escape($this->value) . "'";
169  $sql .= ", " . ((int) $this->entity);
170  $sql .= ", " . ((int) $this->position);
171  $sql .= ")";
172 
173  dol_syslog(__METHOD__, LOG_DEBUG);
174  $resql = $this->db->query($sql);
175  if (!$resql) {
176  $this->errors[] = "Error " . $this->db->lasterror();
177  $error++;
178  }
179 
180  if (!$error) {
181  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
182  }
183 
184  if (!$error && !$notrigger) {
185  // Call trigger
186  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
187  if ($result < 0) {
188  $error++;
189  }
190  // End call triggers
191  }
192 
193  if ($error) {
194  $this->db->rollback();
195  return -1 * $error;
196  } else {
197  $this->db->commit();
198  return $this->id;
199  }
200  }
201 
208  public function fetch($id)
209  {
210  global $langs;
211  $error = 0;
212 
213  // Clean parameters
214  $id = $id > 0 ? $id : 0;
215 
216  // Check parameters
217  if (empty($id)) {
218  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
219  $error++;
220  }
221  if ($error) {
222  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
223  return -1;
224  }
225 
226  $sql = "SELECT rowid, fk_product_attribute, ref, value";
227  $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
228  $sql .= " WHERE rowid = " . ((int) $id);
229  $sql .= " AND entity IN (" . getEntity('product') . ")";
230 
231  dol_syslog(__METHOD__, LOG_DEBUG);
232  $resql = $this->db->query($sql);
233  if (!$resql) {
234  $this->errors[] = "Error " . $this->db->lasterror();
235  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
236  return -1;
237  }
238 
239  $numrows = $this->db->num_rows($resql);
240  if ($numrows) {
241  $obj = $this->db->fetch_object($resql);
242 
243  $this->id = $obj->rowid;
244  $this->fk_product_attribute = $obj->fk_product_attribute;
245  $this->ref = $obj->ref;
246  $this->value = $obj->value;
247  }
248  $this->db->free($resql);
249 
250  return $numrows;
251  }
252 
261  public function fetchAllByProductAttribute($prodattr_id, $only_used = false, $returnonlydata = 0)
262  {
263  $return = array();
264 
265  $sql = "SELECT ";
266 
267  if ($only_used) {
268  $sql .= "DISTINCT ";
269  }
270 
271  $sql .= "v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX . "product_attribute_value v ";
272 
273  if ($only_used) {
274  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c2v.fk_prod_attr_val = v.rowid ";
275  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination c ON c.rowid = c2v.fk_prod_combination ";
276  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product p ON p.rowid = c.fk_product_child ";
277  }
278 
279  $sql .= "WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
280 
281  if ($only_used) {
282  $sql .= " AND c2v.rowid IS NOT NULL AND p.tosell = 1";
283  }
284 
285  $sql .= " ORDER BY v.position ASC";
286 
287  $query = $this->db->query($sql);
288 
289  while ($result = $this->db->fetch_object($query)) {
290  if (empty($returnonlydata)) {
291  $tmp = new ProductAttributeValue($this->db);
292  } else {
293  $tmp = new stdClass();
294  }
295 
296  $tmp->fk_product_attribute = $result->fk_product_attribute;
297  $tmp->id = $result->rowid;
298  $tmp->ref = $result->ref;
299  $tmp->value = $result->value;
300 
301  $return[] = $tmp;
302  }
303 
304  return $return;
305  }
306 
314  public function update(User $user, $notrigger = 0)
315  {
316  global $langs;
317  $error = 0;
318 
319  // Clean parameters
320  $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
321  $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
322  $this->value = trim($this->value);
323 
324  // Check parameters
325  if (empty($this->fk_product_attribute)) {
326  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductAttribute"));
327  $error++;
328  }
329  if (empty($this->ref)) {
330  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
331  $error++;
332  }
333  if (empty($this->value)) {
334  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value"));
335  $error++;
336  }
337  if ($error) {
338  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
339  return -1;
340  }
341 
342  $this->db->begin();
343 
344  $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET";
345 
346  $sql .= " fk_product_attribute = " . ((int) $this->fk_product_attribute);
347  $sql .= ", ref = '" . $this->db->escape($this->ref) . "'";
348  $sql .= ", value = '" . $this->db->escape($this->value) . "'";
349  $sql .= ", position = " . ((int) $this->position);
350 
351  $sql .= " WHERE rowid = " . ((int) $this->id);
352 
353  dol_syslog(__METHOD__, LOG_DEBUG);
354  $resql = $this->db->query($sql);
355  if (!$resql) {
356  $this->errors[] = "Error " . $this->db->lasterror();
357  $error++;
358  }
359 
360  if (!$error && !$notrigger) {
361  // Call trigger
362  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
363  if ($result < 0) {
364  $error++;
365  }
366  // End call triggers
367  }
368 
369  if (!$error) {
370  $this->db->commit();
371  return 1;
372  } else {
373  $this->db->rollback();
374  return -1 * $error;
375  }
376  }
377 
385  public function delete(User $user, $notrigger = 0)
386  {
387  global $langs;
388  $error = 0;
389 
390  // Clean parameters
391  $this->id = $this->id > 0 ? $this->id : 0;
392 
393  // Check parameters
394  if (empty($this->id)) {
395  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
396  $error++;
397  }
398  if ($error) {
399  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
400  return -1;
401  }
402 
403  $result = $this->isUsed();
404  if ($result < 0) {
405  return -1;
406  } elseif ($result > 0) {
407  $this->errors[] = $langs->trans('ErrorAttributeValueIsUsedIntoProduct');
408  return -1;
409  }
410 
411  $this->db->begin();
412 
413  if (!$error && !$notrigger) {
414  // Call trigger
415  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
416  if ($result < 0) {
417  $error++;
418  }
419  // End call triggers
420  }
421 
422  if (!$error) {
423  $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element . " WHERE rowid = " . ((int) $this->id);
424 
425  dol_syslog(__METHOD__, LOG_DEBUG);
426  $resql = $this->db->query($sql);
427  if (!$resql) {
428  $this->errors[] = "Error " . $this->db->lasterror();
429  $error++;
430  }
431  }
432 
433  if (!$error) {
434  $this->db->commit();
435  return 1;
436  } else {
437  $this->db->rollback();
438  return -1 * $error;
439  }
440  }
441 
447  public function isUsed()
448  {
449  global $langs;
450  $error = 0;
451 
452  // Clean parameters
453  $this->id = $this->id > 0 ? $this->id : 0;
454 
455  // Check parameters
456  if (empty($this->id)) {
457  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
458  $error++;
459  }
460  if ($error) {
461  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
462  return -1;
463  }
464 
465  $sql = "SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->id);
466 
467  dol_syslog(__METHOD__, LOG_DEBUG);
468  $resql = $this->db->query($sql);
469  if (!$resql) {
470  $this->errors[] = "Error " . $this->db->lasterror();
471  return -1;
472  }
473 
474  $used = 0;
475  if ($obj = $this->db->fetch_object($resql)) {
476  $used = $obj->nb;
477  }
478 
479  return $used ? 1 : 0;
480  }
481 }
print $object position
Definition: edit.php:195
$object ref
Definition: info.php:79
errorsToString()
Method to output saved errors.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage Dolibarr database access.
Class ProductAttributeValue Used to represent a product attribute value.
create(User $user, $notrigger=0)
Creates a value for a product attribute.
fetch($id)
Gets a product attribute value.
update(User $user, $notrigger=0)
Updates a product attribute value.
isUsed()
Test if used by a product.
__construct(DoliDB $db)
Constructor.
fetchAllByProductAttribute($prodattr_id, $only_used=false, $returnonlydata=0)
Returns all product attribute values of a product attribute.
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
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.