- Adapted controllers/widgets/UDF->saveUDFs to call UDFLib->saveUDFs without the udfUniqueId parameter

- UDFLib->saveUDFs changed, the udfUniqueId parameter is retrieved from the session
- UDFLib->saveUDFs changed, the check of the permission is performed by
  the method prepare prepareUDFsWrite called by the DB_Model
- Changed UDFLib->_validateUDFs and public/js/UDFWidget.js to have a
  better error handling and better message errors
This commit is contained in:
Paolo
2021-10-11 10:08:58 +02:00
parent c325046e8e
commit 1ebae59292
3 changed files with 22 additions and 35 deletions
+1 -2
View File
@@ -36,7 +36,6 @@ class UDF extends FHC_Controller
*/
public function saveUDFs()
{
$udfUniqueId = $this->input->post(self::UDF_UNIQUE_ID);
$udfs = $this->input->post(UDFLib::UDFS_ARG_NAME);
if (!isEmptyString($udfs))
@@ -44,7 +43,7 @@ class UDF extends FHC_Controller
$jsonDecodedUDF = json_decode($udfs);
if ($jsonDecodedUDF != null)
{
$this->outputJson($this->udflib->saveUDFs($udfUniqueId, $jsonDecodedUDF));
$this->outputJson($this->udflib->saveUDFs($jsonDecodedUDF));
}
else
{
+7 -30
View File
@@ -371,7 +371,7 @@ class UDFLib
// Loops through the UDFs values that should be stored
foreach ($udfsParameters as $key => $val)
{
$tmpValidate = success(true); // temporary variable used to store the returned value from _validateUDFs
$tmpValidateArray = array(); // temporary variable used to store the returned value from _validateUDFs
// If this is the definition of this UDF
if ($decodedUDFDefinition->{self::NAME} == $key)
@@ -433,7 +433,7 @@ class UDFLib
if ($toBeValidated === true) // Checks if validation should be performed
{
$tmpValidate = $this->_validateUDFs(
$tmpValidateArray = $this->_validateUDFs(
$decodedUDFDefinition->{self::VALIDATION},
$decodedUDFDefinition->{self::NAME},
$val
@@ -443,13 +443,13 @@ class UDFLib
}
// If validation is ok copy the value that is to be stored into $toBeStoredUDFsArray
if (isSuccess($tmpValidate))
if (isEmptyArray($tmpValidateArray))
{
$toBeStoredUDFsArray[$key] = $val;
}
else // otherwise store the validation error in $notValidUDFsArray
else // otherwise store the validation errors in $notValidUDFsArray
{
$notValidUDFsArray[] = $tmpValidate;
$notValidUDFsArray = array_merge($notValidUDFsArray, $tmpValidateArray);
}
}
}
@@ -585,34 +585,14 @@ class UDFLib
/**
* Save UDFs
*/
public function saveUDFs($udfUniqueId, $udfs)
public function saveUDFs($udfs)
{
$udfToBewritten = array(); // UDFs to be written into database
// Read the all session for this udf widget
$session = $this->getSession();
// If session is empty then return an error
if ($session == null) return error('No UDFWidget loaded');
// Get the required permission from the session
$requiredPermissions = $session[self::REQUIRED_PERMISSIONS_PARAMETER];
// For each UDF that is trying to save
foreach ($udfs as $udfName => $udfValue)
{
// If the UDFs exists in the requiredPermissions array
if (array_key_exists($udfName, $requiredPermissions))
{
// Then check if the user has the permissions to write such UDF
if ($this->_writeAllowed($requiredPermissions[$udfName]))
{
// If allowed then save the UDF name and value to be stored later into the database
$udfToBewritten[$udfName] = $udfValue;
}
}
}
// Workaround to load CI
$this->_ci->load->model('system/UDF_model', 'UDFModel');
@@ -629,7 +609,7 @@ class UDFLib
// Returns the result of the database update operation to save UDFs
return $dbModel->update(
array($session[self::PRIMARY_KEY_NAME] => $session[self::PRIMARY_KEY_VALUE]),
$udfToBewritten
get_object_vars($udfs)
);
}
@@ -834,9 +814,6 @@ class UDFLib
}
}
// If no UDF validation errors were raised, it's a success!!
if (isEmptyArray($returnArrayValidation)) $returnArrayValidation = success(true);
return $returnArrayValidation;
}
+14 -3
View File
@@ -55,17 +55,27 @@ var FHC_UDFWidget = {
if (FHC_AjaxClient.hasData(data))
{
FHC_DialogLib.alertSuccess('Done!');
FHC_DialogLib.alertSuccess("Successfully saved");
}
else
{
console.log(FHC_AjaxClient.getError(data));
var msgError = "An error occurred while saving these fields:<br>";
var errors = FHC_AjaxClient.getError(data);
for (var i = 0; i < errors.length; i++)
{
var error = errors[i];
msgError += FHC_AjaxClient.getError(error)+ "<br>";
}
FHC_DialogLib.alertError(msgError);
}
}
},
{
errorCallback: function(data, textStatus, jqXHR) {
console.log('Contact the administrator');
FHC_DialogLib.alertError("A generic error occurred, please contact the support");
}
}
);
@@ -98,3 +108,4 @@ $(document).ready(function() {
FHC_UDFWidget.display();
});