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 = 1;
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"));
192 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $this->table_element .
" (";
193 $sql .=
" fk_product_attribute, ref, value, entity, position";
196 $sql .=
" " . ((int) $this->fk_product_attribute);
197 $sql .=
", '" . $this->db->escape($this->
ref) .
"'";
198 $sql .=
", '" . $this->db->escape($this->value) .
"'";
199 $sql .=
", " . ((int) $this->entity);
200 $sql .=
", " . ((int) $this->
position);
204 $resql = $this->db->query($sql);
206 $this->errors[] =
"Error " . $this->db->lasterror();
211 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
218 if (!$error && !$notrigger) {
220 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_CREATE', $user);
228 $this->db->rollback();
248 $id = $id > 0 ? $id : 0;
252 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
260 $sql =
"SELECT rowid, fk_product_attribute, ref, value";
261 $sql .=
" FROM " . MAIN_DB_PREFIX . $this->table_element;
262 $sql .=
" WHERE rowid = " . ((int) $id);
263 $sql .=
" AND entity IN (" .
getEntity(
'product') .
")";
266 $resql = $this->db->query($sql);
268 $this->errors[] =
"Error " . $this->db->lasterror();
273 $numrows = $this->db->num_rows($resql);
275 $obj = $this->db->fetch_object($resql);
277 $this->
id = $obj->rowid;
278 $this->fk_product_attribute = $obj->fk_product_attribute;
279 $this->
ref = $obj->ref;
280 $this->value = $obj->value;
283 $this->db->free($resql);
300 $sql =
"SELECT v.fk_product_attribute, v.rowid, v.ref, v.value FROM " . MAIN_DB_PREFIX .
"product_attribute_value v ";
302 $sql .=
"WHERE v.fk_product_attribute = " . ((int) $prodattr_id);
305 $sql .=
" AND EXISTS (SELECT c2v.fk_prod_attr_val ";
306 $sql .=
"FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val c2v ";
307 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product_attribute_combination c ON c.rowid = c2v.fk_prod_combination ";
308 $sql .=
"LEFT JOIN " . MAIN_DB_PREFIX .
"product p ON p.rowid = c.fk_product_child";
309 $sql .=
" WHERE c2v.rowid IS NOT NULL AND p.tosell = 1 AND c2v.fk_prod_attr_val = v.rowid)";
312 $sql .=
" ORDER BY v.position ASC";
314 $query = $this->db->query($sql);
317 while ($result = $this->db->fetch_object($query)) {
318 if (empty($returnonlydata)) {
324 $tmp->fk_product_attribute = $result->fk_product_attribute;
325 $tmp->id = $result->rowid;
326 $tmp->ref = $result->ref;
327 $tmp->value = $result->value;
349 $this->fk_product_attribute = $this->fk_product_attribute > 0 ? $this->fk_product_attribute : 0;
351 $this->value = trim($this->value);
354 if (empty($this->fk_product_attribute)) {
355 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"ProductAttribute"));
358 if (empty($this->
ref)) {
359 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Ref"));
362 if (empty($this->value)) {
363 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Value"));
373 $sql =
"UPDATE " . MAIN_DB_PREFIX . $this->table_element .
" SET";
375 $sql .=
" fk_product_attribute = " . ((int) $this->fk_product_attribute);
376 $sql .=
", ref = '" . $this->db->escape($this->
ref) .
"'";
377 $sql .=
", value = '" . $this->db->escape($this->value) .
"'";
378 $sql .=
", position = " . ((int) $this->
position);
380 $sql .=
" WHERE rowid = " . ((int) $this->
id);
383 $resql = $this->db->query($sql);
385 $this->errors[] =
"Error " . $this->db->lasterror();
389 if (!$error && !$notrigger) {
391 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user);
402 $this->db->rollback();
414 public function delete(
User $user, $notrigger = 0)
420 $this->
id = $this->
id > 0 ? $this->id : 0;
423 if (empty($this->
id)) {
424 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
432 $result = $this->
isUsed();
435 } elseif ($result > 0) {
436 $this->errors[] = $langs->trans(
'ErrorAttributeValueIsUsedIntoProduct');
442 if (!$error && !$notrigger) {
444 $result = $this->
call_trigger(
'PRODUCT_ATTRIBUTE_VALUE_DELETE', $user);
452 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $this->table_element .
" WHERE rowid = " . ((int) $this->
id);
455 $resql = $this->db->query($sql);
457 $this->errors[] =
"Error " . $this->db->lasterror();
471 $this->db->rollback();
485 $sql =
"SELECT max(position) FROM " . MAIN_DB_PREFIX . $this->table_element;
486 $sql .=
" WHERE entity IN (" .
getEntity(
'product') .
")";
487 $sql .=
' AND fk_product_attribute='.(int) $fk_product_attribute;
489 $resql = $this->db->query($sql);
491 $row = $this->db->fetch_row($resql);
508 $this->
id = $this->
id > 0 ? $this->id : 0;
511 if (empty($this->
id)) {
512 $this->errors[] = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"TechnicalID"));
520 $sql =
"SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX .
"product_attribute_combination2val WHERE fk_prod_attr_val = " . ((int) $this->
id);
523 $resql = $this->db->query($sql);
525 $this->errors[] =
"Error " . $this->db->lasterror();
530 if ($obj = $this->db->fetch_object($resql)) {
534 return $used ? 1 : 0;
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
errorsToString()
Method to output saved errors.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
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.
getMaxAttributesValuesPosition($fk_product_attribute)
Get max value used for position of attributes.
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 a 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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...