From fcabb3d37fcd0dafb577727e83db33b498e2e718 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Mon, 8 Apr 2024 11:02:53 +0200 Subject: [PATCH 1/2] insert fields order (#29272) --- htdocs/product/class/product.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index c1313fcbd49..f4982331cd5 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -856,10 +856,10 @@ class Product extends CommonObject $sql .= ", ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null"); $sql .= ", ".((int) $user->id); $sql .= ", ".((int) $this->type); - $sql .= ", ".(!empty($this->price_label) ? "'".$this->db->escape($this->price_label)."'" : "null"); $sql .= ", ".price2num($price_ht, 'MT'); $sql .= ", ".price2num($price_ttc, 'MT'); $sql .= ", '".$this->db->escape($this->price_base_type)."'"; + $sql .= ", ".(!empty($this->price_label) ? "'".$this->db->escape($this->price_label)."'" : "null"); $sql .= ", ".((int) $this->status); $sql .= ", ".((int) $this->status_buy); if (!getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) { From 1207ce21cc5dad5474fab21008ac2eb29ed216e0 Mon Sep 17 00:00:00 2001 From: Jon Bendtsen Date: Mon, 8 Apr 2024 11:11:53 +0200 Subject: [PATCH 2/2] NEW: API setup delete extrafields from name and elementtype v2 (#29269) * NEW: API delete extrafields from name and elementtype * 401 came back, but was 403'ed again * Fix sql injection --------- Co-authored-by: Jon Bendtsen Co-authored-by: Laurent Destailleur --- htdocs/api/class/api_setup.class.php | 34 +++++++++++++++++++++++++ htdocs/core/class/extrafields.class.php | 7 +++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 9d32fc142ca..42a398ee3b5 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -1197,6 +1197,40 @@ class Setup extends DolibarrApi return $list; } + /** + * Delete extrafield + * + * @param string $attrname extrafield attrname + * @param string $elementtype extrafield elementtype + * @return array + * + * @url DELETE extrafields/{elementtype}/{attrname} + * + */ + public function deleteExtrafieldsFromNames($attrname, $elementtype) + { + if (!DolibarrApiAccess::$user->admin) { + throw new RestException(403, 'Only an admin user can delete an extrafield by attrname and elementtype'); + } + + $extrafields = new ExtraFields($this->db); + + $result = $extrafields->fetch_name_optionals_label($elementtype, false, $attrname); + if (!$result) { + throw new RestException(404, 'Extrafield not found from attrname and elementtype'); + } + + if (!$extrafields->delete($attrname, $elementtype)) { + throw new RestException(500, 'Error when delete extrafield : '.$extrafields->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Extrafield deleted from attrname and elementtype' + ) + ); + } /** * Get the list of towns. diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index a8ea6513f52..32147ed9122 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -824,16 +824,16 @@ class ExtraFields } } - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Load the array of extrafields definition $this->attributes * * @param string $elementtype Type of element ('all' = all or $object->table_element like 'adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...). * @param boolean $forceload Force load of extra fields whatever is status of cache. + * @param string $attrname The name of the attribute. * @return array{}|array{label:array,type:array,size:array,default:array,computed:array,unique:array,required:array,param:array,perms:array,list:array|array,pos:array,totalizable:array,help:array,printable:array,enabled:array,langfile:array,css:array,csslist:array,hidden:array,mandatoryfieldsofotherentities?:array,loaded?:int,count:int} Array of attributes keys+label for all extra fields. Note: count set as present to avoid static analysis notices */ - public function fetch_name_optionals_label($elementtype, $forceload = false) + public function fetch_name_optionals_label($elementtype, $forceload = false, $attrname = '') { // phpcs:enable global $conf; @@ -865,6 +865,9 @@ class ExtraFields if ($elementtype && $elementtype != 'all') { $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'"; // Filed with object->table_element } + if ($attrname && $elementtype && $elementtype != 'all') { + $sql .= " AND name = '".$this->db->escape($attrname)."'"; + } $sql .= " ORDER BY pos"; $resql = $this->db->query($sql);