diff --git a/application/controllers/system/FAS_UDF.php b/application/controllers/system/FAS_UDF.php index febe266c7..ecaa44393 100644 --- a/application/controllers/system/FAS_UDF.php +++ b/application/controllers/system/FAS_UDF.php @@ -31,7 +31,7 @@ class FAS_UDF extends Auth_Controller if (isset($person_id) && is_numeric($person_id)) { - if ($this->PersonModel->hasUDF()) + if ($this->PersonModel->udfsExistAndDefined()) { $personUdfs = $this->PersonModel->getUDFs($person_id); $data['person_id'] = $person_id; @@ -41,7 +41,7 @@ class FAS_UDF extends Auth_Controller if (isset($prestudent_id) && is_numeric($prestudent_id)) { - if ($this->PrestudentModel->hasUDF()) + if ($this->PrestudentModel->udfsExistAndDefined()) { $prestudentUdfs = $this->PrestudentModel->getUDFs($prestudent_id); $data['prestudent_id'] = $prestudent_id; diff --git a/application/controllers/widgets/UDF.php b/application/controllers/widgets/UDF.php index 3ba745a10..26c30293c 100644 --- a/application/controllers/widgets/UDF.php +++ b/application/controllers/widgets/UDF.php @@ -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 { diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 0c43a9baf..4e555be6c 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -670,6 +670,7 @@ class DB_Model extends CI_Model /** * Returns all the UDF contained in this table ($dbTable) * If no UDF are present, an empty array will be returned + * NOTE: only the UDFs that the logged user is allowed to read are loaded by this method */ public function getUDFs($id, $udfName = null) { @@ -700,9 +701,9 @@ class DB_Model extends CI_Model } /** - * Checks if this table has the field udf_values + * Checks if this table has the field udf_values and if there is a UDF definition for this table */ - public function hasUDF() + public function udfsExistAndDefined() { if ($this->fieldExists(UDFLib::COLUMN_NAME)) { @@ -850,11 +851,11 @@ class DB_Model extends CI_Model { $prepareUDFsWrite = success(); - if ($this->hasUDF()) + if ($this->udfsExistAndDefined()) { if ($id != null) { - $prepareUDFsWrite = $this->udflib->prepareUDFsWrite($data, $this->dbTable, $this->getUDFs($id)); + $prepareUDFsWrite = $this->udflib->prepareUDFsWrite($data, $this->dbTable, $this->_getUDFsNoPerms($id)); } else { @@ -989,4 +990,48 @@ class DB_Model extends CI_Model { if ($this->debugMode) $this->loglib->logDebug($this->db->last_query()); } + + /** + * Returns all the UDF contained in this table ($dbTable) + * If no UDF are present, an empty array will be returned + * NOTE: it returns all the UDFs, does _not_ check the permissions + */ + private function _getUDFsNoPerms($id) + { + $udfs = array(); + + $this->db->select(UDFLib::COLUMN_NAME, true); // get only the UDF column + + // Primary key management + $tmpId = $id; + + // Check for composite Primary Key + if (is_array($id)) + { + if (isset($id[0])) + { + $tmpId = $this->_arrayCombine($this->pk, $id); + } + } + elseif ($id != null) + { + $tmpId = array($this->pk => $id); + } + + // Read the record from the table + $result = $this->db->get_where($this->dbTable, $tmpId); + + // If was a success and there are data + if ($result && count($result->result()) == 1) + { + // Get the UDF column and decode it from JSON + $jsonValues = json_decode($result->result()[0]->{UDFLib::COLUMN_NAME}); + + // If the JSON convertion was fine convert the object to an array + if ($jsonValues != null) $udfs = get_object_vars($jsonValues); + } + + return $udfs; + } } + diff --git a/application/libraries/UDFLib.php b/application/libraries/UDFLib.php index 26ae1ec78..827bdc989 100644 --- a/application/libraries/UDFLib.php +++ b/application/libraries/UDFLib.php @@ -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; } diff --git a/application/models/crm/Prestudent_model.php b/application/models/crm/Prestudent_model.php index 2553fd5fa..5589e5049 100644 --- a/application/models/crm/Prestudent_model.php +++ b/application/models/crm/Prestudent_model.php @@ -291,7 +291,7 @@ class Prestudent_model extends DB_Model $prestudentdata->prestudentstatus = $lastStatusData; - if ($this->hasUDF()) + if ($this->udfsExistAndDefined()) { $prestudentdata->prestudentUdfs = $this->getUDFs($prestudent_id); } diff --git a/public/js/UDFWidget.js b/public/js/UDFWidget.js index b9c3a7386..82c29de35 100644 --- a/public/js/UDFWidget.js +++ b/public/js/UDFWidget.js @@ -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:
"; + var errors = FHC_AjaxClient.getError(data); + + for (var i = 0; i < errors.length; i++) + { + var error = errors[i]; + + msgError += FHC_AjaxClient.getError(error)+ "
"; + } + + 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(); }); +