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);