dolibarr  17.0.4
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
25 {
29  public $module = 'variants';
30 
34  public $element = 'productattributevalue';
35 
39  public $table_element = 'product_attribute_value';
40 
45  public $ismultientitymanaged = 1;
46 
50  public $isextrafieldmanaged = 0;
51 
80  public $fields=array(
81  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
82  'fk_product_attribute' => array('type'=>'integer:ProductAttribute:variants/class/ProductAttribute.class.php', 'label'=>'ProductAttribute', 'enabled'=>1, 'visible'=>0, 'position'=>10, 'notnull'=>1, 'index'=>1,),
83  'ref' => array('type'=>'varchar(255)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'css'=>''),
84  'value' => array('type'=>'varchar(255)', 'label'=>'Value', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'help'=>"", 'showoncombobox'=>'1',),
85  'position' => array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'default'=>0, 'position'=>200, 'notnull'=>1,),
86  );
87  public $id;
88  public $fk_product_attribute;
89  public $ref;
90  public $value;
91  public $position;
92 
98  public function __construct(DoliDB $db)
99  {
100  global $conf, $langs;
101 
102  $this->db = $db;
103  $this->entity = $conf->entity;
104 
105  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
106  $this->fields['rowid']['visible'] = 0;
107  }
108  if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
109  $this->fields['entity']['enabled'] = 0;
110  }
111 
112  // Unset fields that are disabled
113  foreach ($this->fields as $key => $val) {
114  if (isset($val['enabled']) && empty($val['enabled'])) {
115  unset($this->fields[$key]);
116  }
117  }
118 
119  // Translate some data of arrayofkeyval
120  if (is_object($langs)) {
121  foreach ($this->fields as $key => $val) {
122  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
123  foreach ($val['arrayofkeyval'] as $key2 => $val2) {
124  $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
125  }
126  }
127  }
128  }
129  }
130 
138  public function create(User $user, $notrigger = 0)
139  {
140  global $langs;
141  $error = 0;
142 
143  // Clean parameters
144  $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
145  $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
146  $this->value = trim($this->value);
147 
148  // Check parameters
149  if (empty($this->fk_product_attribute)) {
150  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductAttribute"));
151  $error++;
152  }
153  if (empty($this->ref)) {
154  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
155  $error++;
156  }
157  if (empty($this->value)) {
158  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value"));
159  $error++;
160  }
161  if ($error) {
162  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
163  return -1;
164  }
165 
166  $this->db->begin();
167 
168  $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (";
169  $sql .= " fk_product_attribute, ref, value, entity, position";
170  $sql .= ")";
171  $sql .= " VALUES (";
172  $sql .= " " . ((int) $this->fk_product_attribute);
173  $sql .= ", '" . $this->db->escape($this->ref) . "'";
174  $sql .= ", '" . $this->db->escape($this->value) . "'";
175  $sql .= ", " . ((int) $this->entity);
176  $sql .= ", " . ((int) $this->position);
177  $sql .= ")";
178 
179  dol_syslog(__METHOD__, LOG_DEBUG);
180  $resql = $this->db->query($sql);
181  if (!$resql) {
182  $this->errors[] = "Error " . $this->db->lasterror();
183  $error++;
184  }
185 
186  if (!$error) {
187  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
188  }
189 
190  if (!$error && !$notrigger) {
191  // Call trigger
192  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
193  if ($result < 0) {
194  $error++;
195  }
196  // End call triggers
197  }
198 
199  if ($error) {
200  $this->db->rollback();
201  return -1 * $error;
202  } else {
203  $this->db->commit();
204  return $this->id;
205  }
206  }
207 
214  public function fetch($id)
215  {
216  global $langs;
217  $error = 0;
218 
219  // Clean parameters
220  $id = $id > 0 ? $id : 0;
221 
222  // Check parameters
223  if (empty($id)) {
224  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
225  $error++;
226  }
227  if ($error) {
228  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
229  return -1;
230  }
231 
232  $sql = "SELECT rowid, fk_product_attribute, ref, value";
233  $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
234  $sql .= " WHERE rowid = " . ((int) $id);
235  $sql .= " AND entity IN (" . getEntity('product') . ")";
236 
237  dol_syslog(__METHOD__, LOG_DEBUG);
238  $resql = $this->db->query($sql);
239  if (!$resql) {
240  $this->errors[] = "Error " . $this->db->lasterror();
241  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
242  return -1;
243  }
244 
245  $numrows = $this->db->num_rows($resql);
246  if ($numrows) {
247  $obj = $this->db->fetch_object($resql);
248 
249  $this->id = $obj->rowid;
250  $this->fk_product_attribute = $obj->fk_product_attribute;
251  $this->ref = $obj->ref;
252  $this->value = $obj->value;
253  }
254  $this->db->free($resql);
255 
256  return $numrows;
257  }
258 
267  public function fetchAllByProductAttribute($prodattr_id, $only_used = false, $returnonlydata = 0)
268  {
269  $return = array();
270 
271  $sql = "SELECT ";
272 
273  if ($only_used) {
274  $sql .= "DISTINCT ";
275  }
276 
277  $sql .= "v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX . "product_attribute_value v ";
278 
279  if ($only_used) {
280  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c2v.fk_prod_attr_val = v.rowid ";
281  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination c ON c.rowid = c2v.fk_prod_combination ";
282  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product p ON p.rowid = c.fk_product_child ";
283  }
284 
285  $sql .= "WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
286 
287  if ($only_used) {
288  $sql .= " AND c2v.rowid IS NOT NULL AND p.tosell = 1";
289  }
290 
291  $query = $this->db->query($sql);
292 
293  while ($result = $this->db->fetch_object($query)) {
294  if (empty($returnonlydata)) {
295  $tmp = new ProductAttributeValue($this->db);
296  } else {
297  $tmp = new stdClass();
298  }
299 
300  $tmp->fk_product_attribute = $result->fk_product_attribute;
301  $tmp->id = $result->rowid;
302  $tmp->ref = $result->ref;
303  $tmp->value = $result->value;
304 
305  $return[] = $tmp;
306  }
307 
308  return $return;
309  }
310 
318  public function update(User $user, $notrigger = 0)
319  {
320  global $langs;
321  $error = 0;
322 
323  // Clean parameters
324  $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
325  $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
326  $this->value = trim($this->value);
327 
328  // Check parameters
329  if (empty($this->fk_product_attribute)) {
330  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductAttribute"));
331  $error++;
332  }
333  if (empty($this->ref)) {
334  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
335  $error++;
336  }
337  if (empty($this->value)) {
338  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value"));
339  $error++;
340  }
341  if ($error) {
342  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
343  return -1;
344  }
345 
346  $this->db->begin();
347 
348  $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET";
349 
350  $sql .= " fk_product_attribute = " . ((int) $this->fk_product_attribute);
351  $sql .= ", ref = '" . $this->db->escape($this->ref) . "'";
352  $sql .= ", value = '" . $this->db->escape($this->value) . "'";
353  $sql .= ", position = " . ((int) $this->position);
354 
355  $sql .= " WHERE rowid = " . ((int) $this->id);
356 
357  dol_syslog(__METHOD__, LOG_DEBUG);
358  $resql = $this->db->query($sql);
359  if (!$resql) {
360  $this->errors[] = "Error " . $this->db->lasterror();
361  $error++;
362  }
363 
364  if (!$error && !$notrigger) {
365  // Call trigger
366  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
367  if ($result < 0) {
368  $error++;
369  }
370  // End call triggers
371  }
372 
373  if (!$error) {
374  $this->db->commit();
375  return 1;
376  } else {
377  $this->db->rollback();
378  return -1 * $error;
379  }
380  }
381 
389  public function delete(User $user, $notrigger = 0)
390  {
391  global $langs;
392  $error = 0;
393 
394  // Clean parameters
395  $this->id = $this->id > 0 ? $this->id : 0;
396 
397  // Check parameters
398  if (empty($this->id)) {
399  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
400  $error++;
401  }
402  if ($error) {
403  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
404  return -1;
405  }
406 
407  $result = $this->isUsed();
408  if ($result < 0) {
409  return -1;
410  } elseif ($result > 0) {
411  $this->errors[] = $langs->trans('ErrorAttributeValueIsUsedIntoProduct');
412  return -1;
413  }
414 
415  $this->db->begin();
416 
417  if (!$error && !$notrigger) {
418  // Call trigger
419  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
420  if ($result < 0) {
421  $error++;
422  }
423  // End call triggers
424  }
425 
426  if (!$error) {
427  $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element . " WHERE rowid = " . ((int) $this->id);
428 
429  dol_syslog(__METHOD__, LOG_DEBUG);
430  $resql = $this->db->query($sql);
431  if (!$resql) {
432  $this->errors[] = "Error " . $this->db->lasterror();
433  $error++;
434  }
435  }
436 
437  if (!$error) {
438  $this->db->commit();
439  return 1;
440  } else {
441  $this->db->rollback();
442  return -1 * $error;
443  }
444  }
445 
451  public function isUsed()
452  {
453  global $langs;
454  $error = 0;
455 
456  // Clean parameters
457  $this->id = $this->id > 0 ? $this->id : 0;
458 
459  // Check parameters
460  if (empty($this->id)) {
461  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
462  $error++;
463  }
464  if ($error) {
465  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
466  return -1;
467  }
468 
469  $sql = "SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->id);
470 
471  dol_syslog(__METHOD__, LOG_DEBUG);
472  $resql = $this->db->query($sql);
473  if (!$resql) {
474  $this->errors[] = "Error " . $this->db->lasterror();
475  return -1;
476  }
477 
478  $used = 0;
479  if ($obj = $this->db->fetch_object($resql)) {
480  $used = $obj->nb;
481  }
482 
483  return $used ? 1 : 0;
484  }
485 }
$object ref
Definition: info.php:78
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:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='')
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.
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.
$conf db
API class for accounts.
Definition: inc.php:41