20 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
30 public $module =
'variants';
35 public $element =
'productattributevalue';
40 public $table_element =
'product_attribute_value';
46 public $ismultientitymanaged = 1;
51 public $isextrafieldmanaged = 0;
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,),
89 public $fk_product_attribute;
101 global $conf, $langs;
104 $this->entity = $conf->entity;
106 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
107 $this->fields[
'rowid'][
'visible'] = 0;
109 if (empty($conf->multicompany->enabled) && isset($this->fields[
'entity'])) {
110 $this->fields[
'entity'][
'enabled'] = 0;
114 foreach ($this->fields as $key => $val) {
115 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
116 unset($this->fields[$key]);
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);
145 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
147 $this->value = trim($this->value);
150 if (empty($this->fk_product_attribute)) {
151 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
154 if (empty($this->
ref)) {
155 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
158 if (empty($this->value)) {
159 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
169 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $this->table_element .
" (";
170 $sql .=
" fk_product_attribute, ref, value, entity, position";
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);
183 $this->errors[] =
"Error " . $this->
db->lasterror();
188 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
191 if (!$error && !$notrigger) {
193 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
201 $this->
db->rollback();
221 $id = $id > 0 ? $id : 0;
225 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
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') .
")";
241 $this->errors[] =
"Error " . $this->
db->lasterror();
246 $numrows = $this->
db->num_rows(
$resql);
248 $obj = $this->
db->fetch_object(
$resql);
250 $this->
id = $obj->rowid;
251 $this->fk_product_attribute = $obj->fk_product_attribute;
252 $this->
ref = $obj->ref;
253 $this->value = $obj->value;
277 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX .
"product_attribute_value v ";
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 ";
285 $sql .=
"WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
288 $sql .=
" AND c2v.rowid IS NOT NULL AND p.tosell = 1";
291 $query = $this->
db->query($sql);
293 while ($result = $this->
db->fetch_object($query)) {
295 $tmp->fk_product_attribute = $result->fk_product_attribute;
296 $tmp->id = $result->rowid;
297 $tmp->ref = $result->ref;
298 $tmp->value = $result->value;
319 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
321 $this->value = trim($this->value);
324 if (empty($this->fk_product_attribute)) {
325 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
328 if (empty($this->
ref)) {
329 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
332 if (empty($this->value)) {
333 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
343 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->table_element .
" SET";
345 $sql .=
" fk_product_attribute = " . ((int) $this->fk_product_attribute);
346 $sql .=
", ref = '" . $this->
db->escape($this->
ref) .
"'";
347 $sql .=
", value = '" . $this->
db->escape($this->value) .
"'";
348 $sql .=
", position = " . ((int) $this->position);
350 $sql .=
" WHERE rowid = " . ((int) $this->
id);
355 $this->errors[] =
"Error " . $this->
db->lasterror();
359 if (!$error && !$notrigger) {
361 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
372 $this->
db->rollback();
384 public function delete(
User $user, $notrigger = 0)
390 $this->
id = $this->
id > 0 ? $this->
id : 0;
393 if (empty($this->
id)) {
394 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
402 $result = $this->
isUsed();
405 } elseif ($result > 0) {
406 $this->errors[] = $langs->trans(
'ErrorAttributeValueIsUsedIntoProduct');
412 if (!$error && !$notrigger) {
414 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
422 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $this->table_element .
" WHERE rowid = " . ((int) $this->
id);
427 $this->errors[] =
"Error " . $this->
db->lasterror();
436 $this->
db->rollback();
452 $this->
id = $this->
id > 0 ? $this->
id : 0;
455 if (empty($this->
id)) {
456 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
464 $sql =
"SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->
id);
469 $this->errors[] =
"Error " . $this->
db->lasterror();
474 if ($obj = $this->
db->fetch_object(
$resql)) {
478 return $used ? 1 : 0;
$conf db
API class for accounts.
Class to manage Dolibarr users.
Class to manage Dolibarr database access.
dol_string_nospecial($str, $newstr= '_', $badcharstoreplace= '', $badcharstoremove= '')
Clean a string from all punctuation characters to use it as a ref or login.
isUsed()
Test if used by a product.
Class ProductAttributeValue Used to represent a product attribute value.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
update(User $user, $notrigger=0)
Updates a product attribute value.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
fetch($id)
Gets a product attribute value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
fetchAllByProductAttribute($prodattr_id, $only_used=false)
Returns all product attribute values of a product attribute.
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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
__construct(DoliDB $db)
Constructor.
errorsToString()
Method to output saved errors.
create(User $user, $notrigger=0)
Creates a value for a product attribute.