FIX putAttributes, ADD putAttributeValue

This commit is contained in:
Cédric 2019-11-21 17:41:09 +01:00 committed by GitHub
parent 0074589b6f
commit 416a2f7f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -919,9 +919,8 @@ class Products extends DolibarrApi
* Update attributes by id. * Update attributes by id.
* *
* @param int $id ID of Attribute * @param int $id ID of Attribute
* @param string $ref Reference of Attribute * @param array $request_data Datas
* @param string $label Label of Attribute * @return array
* @return int
* *
* @throws RestException * @throws RestException
* @throws 401 * @throws 401
@ -929,7 +928,7 @@ class Products extends DolibarrApi
* *
* @url PUT attributes/{id} * @url PUT attributes/{id}
*/ */
public function putAttributes($id, $ref, $label) public function putAttributes($id, $request_data = null)
{ {
if(! DolibarrApiAccess::$user->rights->produit->creer) { if(! DolibarrApiAccess::$user->rights->produit->creer) {
throw new RestException(401); throw new RestException(401);
@ -944,12 +943,21 @@ class Products extends DolibarrApi
throw new RestException(500, "Error fetching attribute"); throw new RestException(500, "Error fetching attribute");
} }
foreach($request_data as $field => $value) {
$prodattr->label = $label; if ($field == 'rowid') { continue;
$prodattr->ref = $ref; }
$prodattr->$field = $value;
}
if ($prodattr->update(DolibarrApiAccess::$user) > 0) { if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
return 1; $result = $prodattr->fetch((int) $id);
if ($result == 0) {
throw new RestException(404, 'Attribute not found');
} elseif ($result < 0) {
throw new RestException(500, "Error fetching attribute");
} else {
return $prodattr;
}
} }
throw new RestException(500, "Error updating attribute"); throw new RestException(500, "Error updating attribute");
} }
@ -1183,6 +1191,52 @@ class Products extends DolibarrApi
throw new RestException(500, "Error creating new attribute value"); throw new RestException(500, "Error creating new attribute value");
} }
/**
* Update attribute value.
*
* @param int $id ID of Attribute
* @param array $request_data Datas
* @return array
*
* @throws RestException
* @throws 401
*
* @url PUT attributes/values/{id}
*/
public function putAttributeValue($id, $request_data)
{
if(! DolibarrApiAccess::$user->rights->produit->creer) {
throw new RestException(401);
}
$objectval = new ProductAttributeValue($this->db);
$result = $objectval->fetch((int) $id);
if ($result == 0) {
throw new RestException(404, 'Attribute value not found');
} elseif ($result < 0) {
throw new RestException(500, "Error fetching attribute value");
}
foreach($request_data as $field => $value) {
if ($field == 'rowid') { continue;
}
$objectval->$field = $value;
}
if ($objectval->update(DolibarrApiAccess::$user) > 0) {
$result = $objectval->fetch((int) $id);
if ($result == 0) {
throw new RestException(404, 'Attribute not found');
} elseif ($result < 0) {
throw new RestException(500, "Error fetching attribute");
} else {
return $objectval;
}
}
throw new RestException(500, "Error updating attribute");
}
/** /**
* Delete attribute value by id. * Delete attribute value by id.
* *