dolibarr  16.0.5
ProductAttributeValue.class.php
1 <?php
2 
3 /* Copyright (C) 2016 Marcos GarcĂ­a <marcosgdf@gmail.com>
4  * Copyright (C) 2022 Open-Dsi <support@open-dsi.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 
20 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
26 {
30  public $module = 'variants';
31 
35  public $element = 'productattributevalue';
36 
40  public $table_element = 'product_attribute_value';
41 
46  public $ismultientitymanaged = 1;
47 
51  public $isextrafieldmanaged = 0;
52 
81  public $fields=array(
82  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
83  'fk_product_attribute' => array('type'=>'integer:ProductAttribute:variants/class/ProductAttribute.class.php', 'label'=>'ProductAttribute', 'enabled'=>1, 'visible'=>0, 'position'=>10, 'notnull'=>1, 'index'=>1,),
84  'ref' => array('type'=>'varchar(255)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'css'=>''),
85  'value' => array('type'=>'varchar(255)', 'label'=>'Value', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'help'=>"", 'showoncombobox'=>'1',),
86  'position' => array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'default'=>0, 'position'=>200, 'notnull'=>1,),
87  );
88  public $id;
89  public $fk_product_attribute;
90  public $ref;
91  public $value;
92  public $position;
93 
99  public function __construct(DoliDB $db)
100  {
101  global $conf, $langs;
102 
103  $this->db = $db;
104  $this->entity = $conf->entity;
105 
106  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
107  $this->fields['rowid']['visible'] = 0;
108  }
109  if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
110  $this->fields['entity']['enabled'] = 0;
111  }
112 
113  // Unset fields that are disabled
114  foreach ($this->fields as $key => $val) {
115  if (isset($val['enabled']) && empty($val['enabled'])) {
116  unset($this->fields[$key]);
117  }
118  }
119 
120  // Translate some data of arrayofkeyval
121  if (is_object($langs)) {
122  foreach ($this->fields as $key => $val) {
123  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
124  foreach ($val['arrayofkeyval'] as $key2 => $val2) {
125  $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
126  }
127  }
128  }
129  }
130  }
131 
139  public function create(User $user, $notrigger = 0)
140  {
141  global $langs;
142  $error = 0;
143 
144  // Clean parameters
145  $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
146  $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
147  $this->value = trim($this->value);
148 
149  // Check parameters
150  if (empty($this->fk_product_attribute)) {
151  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductAttribute"));
152  $error++;
153  }
154  if (empty($this->ref)) {
155  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
156  $error++;
157  }
158  if (empty($this->value)) {
159  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value"));
160  $error++;
161  }
162  if ($error) {
163  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
164  return -1;
165  }
166 
167  $this->db->begin();
168 
169  $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (";
170  $sql .= " fk_product_attribute, ref, value, entity, position";
171  $sql .= ")";
172  $sql .= " VALUES (";
173  $sql .= " " . ((int) $this->fk_product_attribute);
174  $sql .= ", '" . $this->db->escape($this->ref) . "'";
175  $sql .= ", '" . $this->db->escape($this->value) . "'";
176  $sql .= ", " . ((int) $this->entity);
177  $sql .= ", " . ((int) $this->position);
178  $sql .= ")";
179 
180  dol_syslog(__METHOD__, LOG_DEBUG);
181  $resql = $this->db->query($sql);
182  if (!$resql) {
183  $this->errors[] = "Error " . $this->db->lasterror();
184  $error++;
185  }
186 
187  if (!$error) {
188  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
189  }
190 
191  if (!$error && !$notrigger) {
192  // Call trigger
193  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
194  if ($result < 0) {
195  $error++;
196  }
197  // End call triggers
198  }
199 
200  if ($error) {
201  $this->db->rollback();
202  return -1 * $error;
203  } else {
204  $this->db->commit();
205  return $this->id;
206  }
207  }
208 
215  public function fetch($id)
216  {
217  global $langs;
218  $error = 0;
219 
220  // Clean parameters
221  $id = $id > 0 ? $id : 0;
222 
223  // Check parameters
224  if (empty($id)) {
225  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
226  $error++;
227  }
228  if ($error) {
229  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
230  return -1;
231  }
232 
233  $sql = "SELECT rowid, fk_product_attribute, ref, value";
234  $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
235  $sql .= " WHERE rowid = " . ((int) $id);
236  $sql .= " AND entity IN (" . getEntity('product') . ")";
237 
238  dol_syslog(__METHOD__, LOG_DEBUG);
239  $resql = $this->db->query($sql);
240  if (!$resql) {
241  $this->errors[] = "Error " . $this->db->lasterror();
242  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
243  return -1;
244  }
245 
246  $numrows = $this->db->num_rows($resql);
247  if ($numrows) {
248  $obj = $this->db->fetch_object($resql);
249 
250  $this->id = $obj->rowid;
251  $this->fk_product_attribute = $obj->fk_product_attribute;
252  $this->ref = $obj->ref;
253  $this->value = $obj->value;
254  }
255  $this->db->free($resql);
256 
257  return $numrows;
258  }
259 
268  public function fetchAllByProductAttribute($prodattr_id, $only_used = false, $returnonlydata = 0)
269  {
270  $return = array();
271 
272  $sql = "SELECT ";
273 
274  if ($only_used) {
275  $sql .= "DISTINCT ";
276  }
277 
278  $sql .= "v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX . "product_attribute_value v ";
279 
280  if ($only_used) {
281  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c2v.fk_prod_attr_val = v.rowid ";
282  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination c ON c.rowid = c2v.fk_prod_combination ";
283  $sql .= "LEFT JOIN " . MAIN_DB_PREFIX . "product p ON p.rowid = c.fk_product_child ";
284  }
285 
286  $sql .= "WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
287 
288  if ($only_used) {
289  $sql .= " AND c2v.rowid IS NOT NULL AND p.tosell = 1";
290  }
291 
292  $query = $this->db->query($sql);
293 
294  while ($result = $this->db->fetch_object($query)) {
295  if (empty($returnonlydata)) {
296  $tmp = new ProductAttributeValue($this->db);
297  } else {
298  $tmp = new stdClass();
299  }
300 
301  $tmp->fk_product_attribute = $result->fk_product_attribute;
302  $tmp->id = $result->rowid;
303  $tmp->ref = $result->ref;
304  $tmp->value = $result->value;
305 
306  $return[] = $tmp;
307  }
308 
309  return $return;
310  }
311 
319  public function update(User $user, $notrigger = 0)
320  {
321  global $langs;
322  $error = 0;
323 
324  // Clean parameters
325  $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
326  $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
327  $this->value = trim($this->value);
328 
329  // Check parameters
330  if (empty($this->fk_product_attribute)) {
331  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductAttribute"));
332  $error++;
333  }
334  if (empty($this->ref)) {
335  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
336  $error++;
337  }
338  if (empty($this->value)) {
339  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value"));
340  $error++;
341  }
342  if ($error) {
343  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
344  return -1;
345  }
346 
347  $this->db->begin();
348 
349  $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET";
350 
351  $sql .= " fk_product_attribute = " . ((int) $this->fk_product_attribute);
352  $sql .= ", ref = '" . $this->db->escape($this->ref) . "'";
353  $sql .= ", value = '" . $this->db->escape($this->value) . "'";
354  $sql .= ", position = " . ((int) $this->position);
355 
356  $sql .= " WHERE rowid = " . ((int) $this->id);
357 
358  dol_syslog(__METHOD__, LOG_DEBUG);
359  $resql = $this->db->query($sql);
360  if (!$resql) {
361  $this->errors[] = "Error " . $this->db->lasterror();
362  $error++;
363  }
364 
365  if (!$error && !$notrigger) {
366  // Call trigger
367  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
368  if ($result < 0) {
369  $error++;
370  }
371  // End call triggers
372  }
373 
374  if (!$error) {
375  $this->db->commit();
376  return 1;
377  } else {
378  $this->db->rollback();
379  return -1 * $error;
380  }
381  }
382 
390  public function delete(User $user, $notrigger = 0)
391  {
392  global $langs;
393  $error = 0;
394 
395  // Clean parameters
396  $this->id = $this->id > 0 ? $this->id : 0;
397 
398  // Check parameters
399  if (empty($this->id)) {
400  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
401  $error++;
402  }
403  if ($error) {
404  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
405  return -1;
406  }
407 
408  $result = $this->isUsed();
409  if ($result < 0) {
410  return -1;
411  } elseif ($result > 0) {
412  $this->errors[] = $langs->trans('ErrorAttributeValueIsUsedIntoProduct');
413  return -1;
414  }
415 
416  $this->db->begin();
417 
418  if (!$error && !$notrigger) {
419  // Call trigger
420  $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
421  if ($result < 0) {
422  $error++;
423  }
424  // End call triggers
425  }
426 
427  if (!$error) {
428  $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element . " WHERE rowid = " . ((int) $this->id);
429 
430  dol_syslog(__METHOD__, LOG_DEBUG);
431  $resql = $this->db->query($sql);
432  if (!$resql) {
433  $this->errors[] = "Error " . $this->db->lasterror();
434  $error++;
435  }
436  }
437 
438  if (!$error) {
439  $this->db->commit();
440  return 1;
441  } else {
442  $this->db->rollback();
443  return -1 * $error;
444  }
445  }
446 
452  public function isUsed()
453  {
454  global $langs;
455  $error = 0;
456 
457  // Clean parameters
458  $this->id = $this->id > 0 ? $this->id : 0;
459 
460  // Check parameters
461  if (empty($this->id)) {
462  $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
463  $error++;
464  }
465  if ($error) {
466  dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
467  return -1;
468  }
469 
470  $sql = "SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->id);
471 
472  dol_syslog(__METHOD__, LOG_DEBUG);
473  $resql = $this->db->query($sql);
474  if (!$resql) {
475  $this->errors[] = "Error " . $this->db->lasterror();
476  return -1;
477  }
478 
479  $used = 0;
480  if ($obj = $this->db->fetch_object($resql)) {
481  $used = $obj->nb;
482  }
483 
484  return $used ? 1 : 0;
485  }
486 }
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ProductAttributeValue
Class ProductAttributeValue Used to represent a product attribute value.
Definition: ProductAttributeValue.class.php:25
ref
$object ref
Definition: info.php:77
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
ProductAttributeValue\isUsed
isUsed()
Test if used by a product.
Definition: ProductAttributeValue.class.php:452
dol_string_nospecial
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='')
Clean a string from all punctuation characters to use it as a ref or login.
Definition: functions.lib.php:1376
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ProductAttributeValue\fetchAllByProductAttribute
fetchAllByProductAttribute($prodattr_id, $only_used=false, $returnonlydata=0)
Returns all product attribute values of a product attribute.
Definition: ProductAttributeValue.class.php:268
ProductAttributeValue\update
update(User $user, $notrigger=0)
Updates a product attribute value.
Definition: ProductAttributeValue.class.php:319
ProductAttributeValue\create
create(User $user, $notrigger=0)
Creates a value for a product attribute.
Definition: ProductAttributeValue.class.php:139
ProductAttributeValue\fetch
fetch($id)
Gets a product attribute value.
Definition: ProductAttributeValue.class.php:215
User
Class to manage Dolibarr users.
Definition: user.class.php:44
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
CommonObject\errorsToString
errorsToString()
Method to output saved errors.
Definition: commonobject.class.php:659
ProductAttributeValue\__construct
__construct(DoliDB $db)
Constructor.
Definition: ProductAttributeValue.class.php:99