21require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
31 public $module =
'variants';
36 public $element =
'productattributevalue';
41 public $table_element =
'product_attribute_value';
71 public $fields = array(
72 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'position' => 1,
'notnull' => 1,
'visible' => 0,
'noteditable' => 1,
'index' => 1,
'css' =>
'left',
'comment' =>
"Id"),
73 'fk_product_attribute' => array(
'type' =>
'integer:ProductAttribute:variants/class/ProductAttribute.class.php',
'label' =>
'ProductAttribute',
'enabled' => 1,
'visible' => 0,
'position' => 10,
'notnull' => 1,
'index' => 1,),
74 'ref' => array(
'type' =>
'varchar(255)',
'label' =>
'Ref',
'visible' => 1,
'enabled' => 1,
'position' => 20,
'notnull' => 1,
'index' => 1,
'searchall' => 1,
'comment' =>
"Reference of object",
'css' =>
''),
75 'value' => array(
'type' =>
'varchar(255)',
'label' =>
'Value',
'enabled' => 1,
'position' => 30,
'notnull' => 1,
'visible' => 1,
'searchall' => 1,
'css' =>
'minwidth300',
'help' =>
"",
'showoncombobox' => 1,),
76 'position' => array(
'type' =>
'integer',
'label' =>
'Rank',
'enabled' => 1,
'visible' => 0,
'default' =>
'0',
'position' => 200,
'notnull' => 1,),
89 public $fk_product_attribute;
116 global $conf, $langs;
120 $this->ismultientitymanaged = 1;
121 $this->isextrafieldmanaged = 0;
122 $this->entity = $conf->entity;
125 $this->fields[
'rowid'][
'visible'] = 0;
127 if (!isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
128 $this->fields[
'entity'][
'enabled'] = 0;
132 foreach ($this->fields as $key => $val) {
133 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
134 unset($this->fields[$key]);
139 if (is_object($langs)) {
140 foreach ($this->fields as $key => $val) {
141 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
142 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
143 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
163 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
165 $this->value = trim($this->value);
168 if (empty($this->fk_product_attribute)) {
169 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
172 if (empty($this->
ref)) {
173 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
176 if (empty($this->value)) {
177 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
187 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $this->table_element .
" (";
188 $sql .=
" fk_product_attribute, ref, value, entity, position";
191 $sql .=
" " . ((int) $this->fk_product_attribute);
192 $sql .=
", '" . $this->db->escape($this->
ref) .
"'";
193 $sql .=
", '" . $this->db->escape($this->value) .
"'";
194 $sql .=
", " . ((int) $this->entity);
195 $sql .=
", " . ((int) $this->
position);
199 $resql = $this->db->query($sql);
201 $this->errors[] =
"Error " . $this->db->lasterror();
206 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
209 if (!$error && !$notrigger) {
211 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
219 $this->db->rollback();
239 $id = $id > 0 ? $id : 0;
243 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
251 $sql =
"SELECT rowid, fk_product_attribute, ref, value";
252 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->table_element;
253 $sql .=
" WHERE rowid = " . ((int) $id);
254 $sql .=
" AND entity IN (" .
getEntity(
'product') .
")";
257 $resql = $this->db->query($sql);
259 $this->errors[] =
"Error " . $this->db->lasterror();
264 $numrows = $this->db->num_rows($resql);
266 $obj = $this->db->fetch_object($resql);
268 $this->
id = $obj->rowid;
269 $this->fk_product_attribute = $obj->fk_product_attribute;
270 $this->
ref = $obj->ref;
271 $this->value = $obj->value;
273 $this->db->free($resql);
290 $sql =
"SELECT v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX .
"product_attribute_value v ";
292 $sql .=
"WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
295 $sql .=
" AND EXISTS (SELECT c2v.fk_prod_attr_val ";
296 $sql .=
"FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val c2v ";
297 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product_attribute_combination c ON c.rowid = c2v.fk_prod_combination ";
298 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product p ON p.rowid = c.fk_product_child";
299 $sql .=
" WHERE c2v.rowid IS NOT NULL AND p.tosell = 1 AND c2v.fk_prod_attr_val = v.rowid)";
302 $sql .=
" ORDER BY v.position ASC";
304 $query = $this->db->query($sql);
307 while ($result = $this->db->fetch_object($query)) {
308 if (empty($returnonlydata)) {
314 $tmp->fk_product_attribute = $result->fk_product_attribute;
315 $tmp->id = $result->rowid;
316 $tmp->ref = $result->ref;
317 $tmp->value = $result->value;
339 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
341 $this->value = trim($this->value);
344 if (empty($this->fk_product_attribute)) {
345 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
348 if (empty($this->
ref)) {
349 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
352 if (empty($this->value)) {
353 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
363 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->table_element .
" SET";
365 $sql .=
" fk_product_attribute = " . ((int) $this->fk_product_attribute);
366 $sql .=
", ref = '" . $this->db->escape($this->
ref) .
"'";
367 $sql .=
", value = '" . $this->db->escape($this->value) .
"'";
368 $sql .=
", position = " . ((int) $this->
position);
370 $sql .=
" WHERE rowid = " . ((int) $this->
id);
373 $resql = $this->db->query($sql);
375 $this->errors[] =
"Error " . $this->db->lasterror();
379 if (!$error && !$notrigger) {
381 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
392 $this->db->rollback();
404 public function delete(
User $user, $notrigger = 0)
410 $this->
id = $this->
id > 0 ? $this->id : 0;
413 if (empty($this->
id)) {
414 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
422 $result = $this->
isUsed();
425 } elseif ($result > 0) {
426 $this->errors[] = $langs->trans(
'ErrorAttributeValueIsUsedIntoProduct');
432 if (!$error && !$notrigger) {
434 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
442 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $this->table_element .
" WHERE rowid = " . ((int) $this->
id);
445 $resql = $this->db->query($sql);
447 $this->errors[] =
"Error " . $this->db->lasterror();
456 $this->db->rollback();
472 $this->
id = $this->
id > 0 ? $this->id : 0;
475 if (empty($this->
id)) {
476 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
484 $sql =
"SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->
id);
487 $resql = $this->db->query($sql);
489 $this->errors[] =
"Error " . $this->db->lasterror();
494 if ($obj = $this->db->fetch_object($resql)) {
498 return $used ? 1 : 0;
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.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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.