21 require_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,),
79 public $fk_product_attribute;
95 $this->ismultientitymanaged = 1;
96 $this->isextrafieldmanaged = 0;
97 $this->entity = $conf->entity;
100 $this->fields[
'rowid'][
'visible'] = 0;
102 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
103 $this->fields[
'entity'][
'enabled'] = 0;
107 foreach ($this->fields as $key => $val) {
108 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
109 unset($this->fields[$key]);
114 if (is_object($langs)) {
115 foreach ($this->fields as $key => $val) {
116 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
117 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
118 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
138 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
140 $this->value = trim($this->value);
143 if (empty($this->fk_product_attribute)) {
144 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
147 if (empty($this->
ref)) {
148 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
151 if (empty($this->value)) {
152 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
162 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $this->table_element .
" (";
163 $sql .=
" fk_product_attribute, ref, value, entity, position";
166 $sql .=
" " . ((int) $this->fk_product_attribute);
167 $sql .=
", '" . $this->db->escape($this->
ref) .
"'";
168 $sql .=
", '" . $this->db->escape($this->value) .
"'";
169 $sql .=
", " . ((int) $this->entity);
174 $resql = $this->db->query(
$sql);
176 $this->errors[] =
"Error " . $this->db->lasterror();
181 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
184 if (!$error && !$notrigger) {
186 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
194 $this->db->rollback();
214 $id = $id > 0 ? $id : 0;
218 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
226 $sql =
"SELECT rowid, fk_product_attribute, ref, value";
227 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->table_element;
228 $sql .=
" WHERE rowid = " . ((int) $id);
232 $resql = $this->db->query(
$sql);
234 $this->errors[] =
"Error " . $this->db->lasterror();
239 $numrows = $this->db->num_rows($resql);
241 $obj = $this->db->fetch_object($resql);
243 $this->
id = $obj->rowid;
244 $this->fk_product_attribute = $obj->fk_product_attribute;
245 $this->
ref = $obj->ref;
246 $this->value = $obj->value;
248 $this->db->free($resql);
271 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX .
"product_attribute_value v ";
274 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product_attribute_combination2val c2v ON c2v.fk_prod_attr_val = v.rowid ";
275 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product_attribute_combination c ON c.rowid = c2v.fk_prod_combination ";
276 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product p ON p.rowid = c.fk_product_child ";
279 $sql .=
"WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
282 $sql .=
" AND c2v.rowid IS NOT NULL AND p.tosell = 1";
285 $sql .=
" ORDER BY v.position ASC";
287 $query = $this->db->query(
$sql);
289 while ($result = $this->db->fetch_object($query)) {
290 if (empty($returnonlydata)) {
296 $tmp->fk_product_attribute = $result->fk_product_attribute;
297 $tmp->id = $result->rowid;
298 $tmp->ref = $result->ref;
299 $tmp->value = $result->value;
320 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
322 $this->value = trim($this->value);
325 if (empty($this->fk_product_attribute)) {
326 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
329 if (empty($this->
ref)) {
330 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
333 if (empty($this->value)) {
334 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
344 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->table_element .
" SET";
346 $sql .=
" fk_product_attribute = " . ((int) $this->fk_product_attribute);
347 $sql .=
", ref = '" . $this->db->escape($this->
ref) .
"'";
348 $sql .=
", value = '" . $this->db->escape($this->value) .
"'";
351 $sql .=
" WHERE rowid = " . ((int) $this->
id);
354 $resql = $this->db->query(
$sql);
356 $this->errors[] =
"Error " . $this->db->lasterror();
360 if (!$error && !$notrigger) {
362 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
373 $this->db->rollback();
385 public function delete(
User $user, $notrigger = 0)
391 $this->
id = $this->
id > 0 ? $this->id : 0;
394 if (empty($this->
id)) {
395 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
403 $result = $this->
isUsed();
406 } elseif ($result > 0) {
407 $this->errors[] = $langs->trans(
'ErrorAttributeValueIsUsedIntoProduct');
413 if (!$error && !$notrigger) {
415 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
423 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $this->table_element .
" WHERE rowid = " . ((int) $this->
id);
426 $resql = $this->db->query(
$sql);
428 $this->errors[] =
"Error " . $this->db->lasterror();
437 $this->db->rollback();
453 $this->
id = $this->
id > 0 ? $this->id : 0;
456 if (empty($this->
id)) {
457 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
465 $sql =
"SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->
id);
468 $resql = $this->db->query(
$sql);
470 $this->errors[] =
"Error " . $this->db->lasterror();
475 if ($obj = $this->db->fetch_object($resql)) {
479 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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
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.
isModEnabled($module)
Is Dolibarr module enabled.
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.