19 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
29 public $module =
'variants';
34 public $element =
'productattributevalue';
39 public $table_element =
'product_attribute_value';
45 public $ismultientitymanaged = 1;
50 public $isextrafieldmanaged = 0;
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,),
88 public $fk_product_attribute;
100 global $conf, $langs;
103 $this->entity = $conf->entity;
105 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) {
106 $this->fields[
'rowid'][
'visible'] = 0;
108 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
109 $this->fields[
'entity'][
'enabled'] = 0;
113 foreach ($this->fields as $key => $val) {
114 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
115 unset($this->fields[$key]);
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);
144 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
146 $this->value = trim($this->value);
149 if (empty($this->fk_product_attribute)) {
150 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
153 if (empty($this->
ref)) {
154 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
157 if (empty($this->value)) {
158 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
168 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $this->table_element .
" (";
169 $sql .=
" fk_product_attribute, ref, value, entity, position";
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);
180 $resql = $this->db->query(
$sql);
182 $this->errors[] =
"Error " . $this->db->lasterror();
187 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
190 if (!$error && !$notrigger) {
192 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
200 $this->db->rollback();
220 $id = $id > 0 ? $id : 0;
224 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
232 $sql =
"SELECT rowid, fk_product_attribute, ref, value";
233 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->table_element;
234 $sql .=
" WHERE rowid = " . ((int) $id);
238 $resql = $this->db->query(
$sql);
240 $this->errors[] =
"Error " . $this->db->lasterror();
245 $numrows = $this->db->num_rows($resql);
247 $obj = $this->db->fetch_object($resql);
249 $this->
id = $obj->rowid;
250 $this->fk_product_attribute = $obj->fk_product_attribute;
251 $this->
ref = $obj->ref;
252 $this->value = $obj->value;
254 $this->db->free($resql);
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)) {
294 if (empty($returnonlydata)) {
297 $tmp =
new stdClass();
300 $tmp->fk_product_attribute = $result->fk_product_attribute;
301 $tmp->id = $result->rowid;
302 $tmp->ref = $result->ref;
303 $tmp->value = $result->value;
324 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
326 $this->value = trim($this->value);
329 if (empty($this->fk_product_attribute)) {
330 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
333 if (empty($this->
ref)) {
334 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
337 if (empty($this->value)) {
338 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
348 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->table_element .
" SET";
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) .
"'";
355 $sql .=
" WHERE rowid = " . ((int) $this->
id);
358 $resql = $this->db->query(
$sql);
360 $this->errors[] =
"Error " . $this->db->lasterror();
364 if (!$error && !$notrigger) {
366 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
377 $this->db->rollback();
389 public function delete(
User $user, $notrigger = 0)
395 $this->
id = $this->
id > 0 ? $this->id : 0;
398 if (empty($this->
id)) {
399 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
407 $result = $this->
isUsed();
410 } elseif ($result > 0) {
411 $this->errors[] = $langs->trans(
'ErrorAttributeValueIsUsedIntoProduct');
417 if (!$error && !$notrigger) {
419 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
427 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $this->table_element .
" WHERE rowid = " . ((int) $this->
id);
430 $resql = $this->db->query(
$sql);
432 $this->errors[] =
"Error " . $this->db->lasterror();
441 $this->db->rollback();
457 $this->
id = $this->
id > 0 ? $this->id : 0;
460 if (empty($this->
id)) {
461 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
469 $sql =
"SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->
id);
472 $resql = $this->db->query(
$sql);
474 $this->errors[] =
"Error " . $this->db->lasterror();
479 if ($obj = $this->db->fetch_object($resql)) {
483 return $used ? 1 : 0;