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;
278 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX .
"product_attribute_value v ";
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 ";
286 $sql .=
"WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
289 $sql .=
" AND c2v.rowid IS NOT NULL AND p.tosell = 1";
292 $query = $this->
db->query($sql);
294 while ($result = $this->
db->fetch_object($query)) {
295 if (empty($returnonlydata)) {
298 $tmp =
new stdClass();
301 $tmp->fk_product_attribute = $result->fk_product_attribute;
302 $tmp->id = $result->rowid;
303 $tmp->ref = $result->ref;
304 $tmp->value = $result->value;
325 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
327 $this->value = trim($this->value);
330 if (empty($this->fk_product_attribute)) {
331 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
334 if (empty($this->
ref)) {
335 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
338 if (empty($this->value)) {
339 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
349 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->table_element .
" SET";
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);
356 $sql .=
" WHERE rowid = " . ((int) $this->
id);
361 $this->errors[] =
"Error " . $this->
db->lasterror();
365 if (!$error && !$notrigger) {
367 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
378 $this->
db->rollback();
390 public function delete(
User $user, $notrigger = 0)
396 $this->
id = $this->
id > 0 ? $this->id : 0;
399 if (empty($this->
id)) {
400 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
408 $result = $this->
isUsed();
411 } elseif ($result > 0) {
412 $this->errors[] = $langs->trans(
'ErrorAttributeValueIsUsedIntoProduct');
418 if (!$error && !$notrigger) {
420 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
428 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $this->table_element .
" WHERE rowid = " . ((int) $this->
id);
433 $this->errors[] =
"Error " . $this->
db->lasterror();
442 $this->
db->rollback();
458 $this->
id = $this->
id > 0 ? $this->id : 0;
461 if (empty($this->
id)) {
462 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
470 $sql =
"SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->
id);
475 $this->errors[] =
"Error " . $this->
db->lasterror();
480 if ($obj = $this->
db->fetch_object(
$resql)) {
484 return $used ? 1 : 0;