diff --git a/application/controllers/system/FAS_UDF.php b/application/controllers/system/FAS_UDF.php index cd78d64d2..7f39543c7 100644 --- a/application/controllers/system/FAS_UDF.php +++ b/application/controllers/system/FAS_UDF.php @@ -24,43 +24,15 @@ class FAS_UDF extends Auth_Controller */ public function index() { - $fasUdfSession = getSession(self::FAS_UDF_SESSION_NAME); - $person_id = $this->input->get('person_id'); - if (isset($fasUdfSession['person_id'])) - { - if (!isset($person_id)) - { - $person_id = $fasUdfSession['person_id']; - } - unset($fasUdfSession['person_id']); - } - $prestudent_id = $this->input->get('prestudent_id'); - if (isset($fasUdfSession['prestudent_id'])) - { - if (!isset($prestudent_id)) - { - $prestudent_id = $fasUdfSession['prestudent_id']; - } - unset($fasUdfSession['prestudent_id']); - } - - $result = null; - if (isset($fasUdfSession['result'])) - { - $result = clone $fasUdfSession['result']; - setSessionElement(self::FAS_UDF_SESSION_NAME, 'result', null); - } - - $data = array('result' => $result); if (isset($person_id) && is_numeric($person_id)) { if ($this->PersonModel->hasUDF()) { $personUdfs = $this->PersonModel->getUDFs($person_id); - $personUdfs['person_id'] = $person_id; + $data['person_id'] = $person_id; $data['personUdfs'] = $personUdfs; } } @@ -70,61 +42,11 @@ class FAS_UDF extends Auth_Controller if ($this->PrestudentModel->hasUDF()) { $prestudentUdfs = $this->PrestudentModel->getUDFs($prestudent_id); - $prestudentUdfs['prestudent_id'] = $prestudent_id; + $data['prestudent_id'] = $prestudent_id; $data['prestudentUdfs'] = $prestudentUdfs; } } $this->load->view('system/fas_udf', $data); } - - /** - * - */ - public function saveUDF() - { - $udfs = $this->input->post(); - $validation = $this->_validate($udfs); - - $userdata = array( - 'person_id' => $this->input->post('person_id'), - 'prestudent_id' => $this->input->post('prestudent_id') - ); - - if (isSuccess($validation)) - { - // Load model UDF_model - $this->load->model('system/FAS_UDF_model', 'FASUDFModel'); - - $result = $this->FASUDFModel->saveUDFs($udfs); - - $userdata['result'] = $result; - } - else - { - $userdata['result'] = $validation; - } - - setSessionElement(self::FAS_UDF_SESSION_NAME, 'person_id', $userdata['person_id']); - setSessionElement(self::FAS_UDF_SESSION_NAME, 'prestudent_id', $userdata['prestudent_id']); - setSessionElement(self::FAS_UDF_SESSION_NAME, 'result', $userdata['result']); - - redirect('system/FAS_UDF'); - } - - /** - * - */ - private function _validate($udfs) - { - $validation = error('person_id or prestudent_id is missing'); - - if((isset($udfs['person_id']) && !(is_null($udfs['person_id'])) && ($udfs['person_id'] != '')) - || (isset($udfs['prestudent_id']) && !(is_null($udfs['prestudent_id'])) && ($udfs['prestudent_id'] != ''))) - { - $validation = success(true); - } - - return $validation; - } } diff --git a/application/controllers/widgets/UDF.php b/application/controllers/widgets/UDF.php new file mode 100644 index 000000000..c71eba73a --- /dev/null +++ b/application/controllers/widgets/UDF.php @@ -0,0 +1,102 @@ +load->library('AuthLib'); + + // Loads the UDFLib with HTTP GET/POST parameters + $this->_loadUDFLib(); + + // Checks if the caller is allow to read this data + $this->_isAllowed(); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Retrieves data about the current filter from the session and will be written on the output in JSON format + */ + public function saveUDFs() + { + $udfUniqueId = $this->input->post(self::UDF_UNIQUE_ID); + $udfs = $this->input->post(UDFLib::UDFS_ARG_NAME); + + if (!isEmptyString($udfs)) + { + $jsonDecodedUDF = json_decode($udfs); + if ($jsonDecodedUDF != null) + { + $this->outputJson($this->udflib->saveUDFs($udfUniqueId, $jsonDecodedUDF)); + } + else + { + $this->outputJsonError('No valid JSON format for UDF values'); + } + } + else + { + $this->outputJsonError('UDFUniqueId, schema, table name, primary key name and primary key value are mandatory paramenters'); + } + } + + //------------------------------------------------------------------------------------------------------------------ + // Private methods + + /** + * Checks if the user is allowed to use this filter + */ + private function _isAllowed() + { + if (!$this->udflib->isAllowed()) + { + $this->terminateWithJsonError('You are not allowed to access to this content'); + } + } + + /** + * Loads the tablewidgetlib with the UDF_UNIQUE_ID parameter + * If the parameter UDF_UNIQUE_ID is not given then the execution of the controller is terminated and + * an error message is printed + */ + private function _loadUDFLib() + { + // If the parameter UDF_UNIQUE_ID is present in the HTTP GET or POST + if (isset($_GET[self::UDF_UNIQUE_ID]) || isset($_POST[self::UDF_UNIQUE_ID])) + { + // If it is present in the HTTP GET + if (isset($_GET[self::UDF_UNIQUE_ID])) + { + $udfUniqueId = $this->input->get(self::UDF_UNIQUE_ID); // is retrieved from the HTTP GET + } + elseif (isset($_POST[self::UDF_UNIQUE_ID])) // Else if it is present in the HTTP POST + { + $udfUniqueId = $this->input->post(self::UDF_UNIQUE_ID); // is retrieved from the HTTP POST + } + + // Loads the tablewidgetlib that contains all the used logic + $this->load->library('UDFLib'); + + $this->udflib->setUDFUniqueId($udfUniqueId); + } + else // Otherwise an error will be written in the output + { + $this->terminateWithJsonError('Parameter "'.self::UDF_UNIQUE_ID.'" not provided!'); + } + } +} diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index ca760c662..667ff00ca 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -60,6 +60,20 @@ class DB_Model extends CI_Model // ------------------------------------------------------------------------------------------ // Public methods + /** + * This method provides a way to setup a database model without declaring one + */ + public function setup($schema, $table, $primaryKey, $hasSequence = true) + { + // + if (!isEmptyString($schema) && !isEmptyString($table) && !isEmptyString($primaryKey) && is_bool($hasSequence)) + { + $this->dbTable = $schema.'.'.$table; + $this->pk = $primaryKey; + $this->hasSequence = $hasSequence; + } + } + /** * Insert Data into DB-Table * @@ -690,7 +704,7 @@ class DB_Model extends CI_Model */ public function hasUDF() { - if($this->fieldExists(UDFLib::COLUMN_NAME)) + if ($this->fieldExists(UDFLib::COLUMN_NAME)) { $resultUDFsDefinitions = $this->UDFModel->getUDFsDefinitions($this->dbTable); if (hasData($resultUDFsDefinitions)) @@ -727,8 +741,8 @@ class DB_Model extends CI_Model $cleanedQuery = trim(preg_replace('/\t|\n|\r|;/', '', $query)); // // - if (stripos($cleanedQuery, 'SELECT') == 0 - && (stripos($cleanedQuery, 'INSERT') > 0 || stripos($cleanedQuery, 'INSERT') == false) + if ( + (stripos($cleanedQuery, 'INSERT') > 0 || stripos($cleanedQuery, 'INSERT') == false) && (stripos($cleanedQuery, 'UPDATE') > 0 || stripos($cleanedQuery, 'UPDATE') == false) && (stripos($cleanedQuery, 'CREATE') > 0 || stripos($cleanedQuery, 'CREATE') == false) && (stripos($cleanedQuery, 'DELETE') > 0 || stripos($cleanedQuery, 'DELETE') == false) diff --git a/application/libraries/UDFLib.php b/application/libraries/UDFLib.php index 6166b44a9..edd11fd99 100644 --- a/application/libraries/UDFLib.php +++ b/application/libraries/UDFLib.php @@ -7,6 +7,10 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); */ class UDFLib { + const UDF_UNIQUE_ID = 'udfUniqueId'; + + const SESSION_NAME = 'FHC_UDF_WIDGET'; + const WIDGET_NAME = 'UDFWidget'; const SCHEMA_ARG_NAME = 'schema'; const TABLE_ARG_NAME = 'table'; @@ -22,6 +26,16 @@ class UDFLib const FE_REGEX_LANGUAGE = 'js'; // UDF javascript regex language attribute (front end) const BE_REGEX_LANGUAGE = 'php'; // UDF php regex language attribute (back end) + // ...to specify permissions that are needed to use this TableWidget + const REQUIRED_PERMISSIONS_PARAMETER = 'requiredPermissions'; + + // ... + const PRIMARY_KEY_NAME = 'primaryKeyName'; + const PRIMARY_KEY_VALUE = 'primaryKeyValue'; + + const PERMISSION_TABLE_METHOD = 'UDFWidget'; // Name for fake method to be checked by the PermissionLib + const PERMISSION_TYPE = 'rw'; + // HTML components const LABEL = 'title'; const TITLE = 'description'; @@ -47,6 +61,8 @@ class UDFLib private $_ci; // Code igniter instance + private $_udfUniqueId; // + /** * Loads fhc helper */ @@ -63,8 +79,8 @@ class UDFLib */ public function UDFWidget($args, $htmlArgs = array()) { - if ((isset($args[UDFLib::SCHEMA_ARG_NAME]) && !isEmptyString($args[UDFLib::SCHEMA_ARG_NAME])) - && (isset($args[UDFLib::TABLE_ARG_NAME]) && !isEmptyString($args[UDFLib::TABLE_ARG_NAME]))) + if ((isset($args[self::SCHEMA_ARG_NAME]) && !isEmptyString($args[self::SCHEMA_ARG_NAME])) + && (isset($args[self::TABLE_ARG_NAME]) && !isEmptyString($args[self::TABLE_ARG_NAME]))) { // Loads the widget library $this->_ci->load->library('WidgetLib'); @@ -73,26 +89,26 @@ class UDFLib loadResource(APPPATH.'widgets/udf'); // Default external block is true - if (!isset($args[UDFLib::FIELD_ARG_NAME]) && !isset($htmlArgs[HTMLWidget::EXTERNAL_BLOCK])) + if (!isset($args[self::FIELD_ARG_NAME]) && !isset($htmlArgs[HTMLWidget::EXTERNAL_BLOCK])) { $htmlArgs[HTMLWidget::EXTERNAL_BLOCK] = true; } return $this->_ci->widgetlib->widget( - UDFLib::WIDGET_NAME, + self::WIDGET_NAME, $args, $htmlArgs ); } else { - if (!isset($args[UDFLib::SCHEMA_ARG_NAME]) || isEmptyString($args[UDFLib::SCHEMA_ARG_NAME])) + if (!isset($args[self::SCHEMA_ARG_NAME]) || isEmptyString($args[self::SCHEMA_ARG_NAME])) { - show_error(UDFLib::SCHEMA_ARG_NAME.' parameter is missing!'); + show_error(self::SCHEMA_ARG_NAME.' parameter is missing!'); } - if (!isset($args[UDFLib::TABLE_ARG_NAME]) || isEmptyString($args[UDFLib::TABLE_ARG_NAME])) + if (!isset($args[self::TABLE_ARG_NAME]) || isEmptyString($args[self::TABLE_ARG_NAME])) { - show_error(UDFLib::TABLE_ARG_NAME.' parameter is missing!'); + show_error(self::TABLE_ARG_NAME.' parameter is missing!'); } } } @@ -105,12 +121,12 @@ class UDFLib */ public function displayUDFWidget(&$widgetData) { - $schema = $widgetData[UDFLib::SCHEMA_ARG_NAME]; // schema attribute - $table = $widgetData[UDFLib::TABLE_ARG_NAME]; // table attribute + $schema = $widgetData[self::SCHEMA_ARG_NAME]; // schema attribute + $table = $widgetData[self::TABLE_ARG_NAME]; // table attribute - if (isset($widgetData[UDFLib::FIELD_ARG_NAME])) + if (isset($widgetData[self::FIELD_ARG_NAME])) { - $field = $widgetData[UDFLib::FIELD_ARG_NAME]; // UDF name + $field = $widgetData[self::FIELD_ARG_NAME]; // UDF name } $udfResults = $this->_loadUDF($schema, $table); // loads UDF definition @@ -122,6 +138,9 @@ class UDFLib $jsonSchemas = json_decode($udf->jsons); // decode the json schema if (is_object($jsonSchemas) || is_array($jsonSchemas)) { + // + $this->_printStartUDFBlock($widgetData); + // If the schema is an object then convert it into an array if (is_object($jsonSchemas)) { @@ -140,18 +159,18 @@ class UDFLib foreach ($jsonSchemasArray as $jsonSchema) { // If the type property is not present then show an error - if (!isset($jsonSchema->{UDFLib::TYPE})) + if (!isset($jsonSchema->{self::TYPE})) { show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); } // If the name property is not present then show an error - if (!isset($jsonSchema->{UDFLib::NAME})) + if (!isset($jsonSchema->{self::NAME})) { show_error(sprintf('%s.%s: Attribute "name" not present in the json schema', $schema, $table)); } // If a UDF is specified and is present in the json schemas list or no UDF is specified - if ((isset($field) && $field == $jsonSchema->{UDFLib::NAME}) || !isset($field)) + if ((isset($field) && $field == $jsonSchema->{self::NAME}) || !isset($field)) { // Set attributes using phrases $this->_setAttributesWithPhrases($jsonSchema, $widgetData[HTMLWidget::HTML_ARG_NAME]); @@ -166,7 +185,7 @@ class UDFLib $this->_render($jsonSchema, $widgetData); // If a UDf is specified and it was found then stop looking through this list - if (isset($field) && $field == $jsonSchema->{UDFLib::NAME}) + if (isset($field) && $field == $jsonSchema->{self::NAME}) { $found = true; break; @@ -179,6 +198,9 @@ class UDFLib { show_error(sprintf('%s.%s: No schema present for field: %s', $schema, $table, $field)); } + + // + $this->_printEndUDFBlock(); } else // not a valid schema { @@ -218,7 +240,7 @@ class UDFLib // Decodes json that define the UDFs for this table $decodedUDFDefinitions = json_decode( - $resultUDFsDefinitions->retval[0]->{UDFLib::COLUMN_JSON_DESCRIPTION} + $resultUDFsDefinitions->retval[0]->{self::COLUMN_JSON_DESCRIPTION} ); // Loops through the UDFs definitions @@ -232,28 +254,28 @@ class UDFLib $tmpValidate = success(true); // temporary variable used to store the returned value from _validateUDFs // If this is the definition of this UDF - if ($decodedUDFDefinition->{UDFLib::NAME} == $key) + if ($decodedUDFDefinition->{self::NAME} == $key) { - if (isset($decodedUDFDefinition->{UDFLib::VALIDATION})) // If validation rules are present for this UDF + if (isset($decodedUDFDefinition->{self::VALIDATION})) // If validation rules are present for this UDF { // Checks if the given UDF is required and the result will be stored in $chkRequiredPassed // If $chkRequiredPassed == true => required check passed // If $chkRequiredPassed == false => required check NOT passed $chkRequiredPassed = true; // If required property is present in the UDF description and it is true - if (isset($decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED}) - && $decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED} === true) + if (isset($decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED}) + && $decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED} === true) { // If this UDF is a checkbox and the given value is false // OR - // if this UDF is NOT a checkbox and the given value is null - if (($decodedUDFDefinition->{UDFLib::TYPE} == UDFLib::CHKBOX_TYPE && $val === false) - || ($decodedUDFDefinition->{UDFLib::TYPE} != UDFLib::CHKBOX_TYPE && $val == null)) + // if this UD7F is NOT a checkbox and the given value is null + if (($decodedUDFDefinition->{self::TYPE} == self::CHKBOX_TYPE && $val === false) + || ($decodedUDFDefinition->{self::TYPE} != self::CHKBOX_TYPE && $val == null)) { $chkRequiredPassed = false; // not passed // A new error is generated and added to array $requiredUDFsArray - $requiredUDFsArray[$decodedUDFDefinition->{UDFLib::NAME}] = error( - $decodedUDFDefinition->{UDFLib::NAME}, + $requiredUDFsArray[$decodedUDFDefinition->{self::NAME}] = error( + $decodedUDFDefinition->{self::NAME}, EXIT_VALIDATION_UDF_REQUIRED ); } @@ -267,22 +289,22 @@ class UDFLib // If $toBeValidated == false => validation is NOT performed $toBeValidated = false; // If this UDF is NOT a checkbox - if ($decodedUDFDefinition->{UDFLib::TYPE} != UDFLib::CHKBOX_TYPE) + if ($decodedUDFDefinition->{self::TYPE} != self::CHKBOX_TYPE) { // If required property is NOT present in the UDF description - if (!isset($decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED})) + if (!isset($decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED})) { $toBeValidated = true; } // If required property is present in the UDF description and it is true - if (isset($decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED}) - && $decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED} === true) + if (isset($decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED}) + && $decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED} === true) { $toBeValidated = true; } // If required property is present in the UDF description and it is true and the given value is null - if (isset($decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED}) - && $decodedUDFDefinition->{UDFLib::VALIDATION}->{UDFLib::REQUIRED} === false + if (isset($decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED}) + && $decodedUDFDefinition->{self::VALIDATION}->{self::REQUIRED} === false && $val != null) { $toBeValidated = true; @@ -292,8 +314,8 @@ class UDFLib if ($toBeValidated === true) // Checks if validation should be performed { $tmpValidate = $this->_validateUDFs( - $decodedUDFDefinition->{UDFLib::VALIDATION}, - $decodedUDFDefinition->{UDFLib::NAME}, + $decodedUDFDefinition->{self::VALIDATION}, + $decodedUDFDefinition->{self::NAME}, $val ); } @@ -341,7 +363,7 @@ class UDFLib if ($encodedToBeStoredUDFs !== false) // if encode was ok { // Save the supplied UDFs values - $data[UDFLib::COLUMN_NAME] = $encodedToBeStoredUDFs; + $data[self::COLUMN_NAME] = $encodedToBeStoredUDFs; } } else // otherwise the returning value will be the list of UDFs validation errors @@ -360,8 +382,8 @@ class UDFLib { $isUDFColumn = false; - if (substr($columnName, 0, strlen(UDFLib::COLUMN_PREFIX)) == UDFLib::COLUMN_PREFIX - && $columnType == UDFLib::COLUMN_TYPE) + if (substr($columnName, 0, strlen(self::COLUMN_PREFIX)) == self::COLUMN_PREFIX + && $columnType == self::COLUMN_TYPE) { $isUDFColumn = true; } @@ -369,9 +391,148 @@ class UDFLib return $isUDFColumn; } + /** + * Set the _udfUniqueId property + */ + public function setUDFUniqueId($udfUniqueId) + { + $this->_udfUniqueId = $udfUniqueId; + } + + /** + * Return an unique string that identify this filter widget + * NOTE: The default value is the URI where the FilterWidget is called + * If the fhc_controller_id is present then is also used + */ + public function setUDFUniqueIdByParams($params) + { + if ($params != null + && is_array($params) + && isset($params[self::UDF_UNIQUE_ID]) + && !isEmptyString($params[self::UDF_UNIQUE_ID])) + { + $udfUniqueId = $this->_ci->router->directory.$this->_ci->router->class.'/'. + $this->_ci->router->method.'/'. + $params[self::UDF_UNIQUE_ID]; + + $this->setUDFUniqueId($udfUniqueId); + } + } + + /** + * Wrapper method to the session helper funtions to retrieve the whole session for this filter + */ + public function getSession() + { + return getSessionElement(self::SESSION_NAME, $this->_udfUniqueId); + } + + /** + * Wrapper method to the session helper funtions to retrieve one element from the session of this filter + */ + public function getSessionElement($name) + { + $session = getSessionElement(self::SESSION_NAME, $this->_udfUniqueId); + + if (isset($session[$name])) + { + return $session[$name]; + } + + return null; + } + + /** + * Wrapper method to the session helper funtions to set the whole session for this filter + */ + public function setSession($data) + { + setSessionElement(self::SESSION_NAME, $this->_udfUniqueId, $data); + } + + /** + * Wrapper method to the session helper funtions to set one element in the session for this filter + */ + public function setSessionElement($name, $value) + { + $session = getSessionElement(self::SESSION_NAME, $this->_udfUniqueId); + + $session[$name] = $value; + + setSessionElement(self::SESSION_NAME, $this->_udfUniqueId, $session); // stores the single value + } + + /** + * Save UDFs + */ + public function saveUDFs($udfUniqueId, $udfs) + { + // Read the all session for this udf widget + $session = $this->getSession(); + + if ($session == null) return error('No UDFWidget loaded'); + + // Workaround + $this->_ci->load->model('system/UDF_model', 'UDFModel'); + + // + $dbModel = new DB_Model(); + + $dbModel->setup( + $session[self::SCHEMA_ARG_NAME], // + $session[self::TABLE_ARG_NAME], // + $session[self::PRIMARY_KEY_NAME] // + ); + + return $dbModel->update( + array($session[self::PRIMARY_KEY_NAME] => $session[self::PRIMARY_KEY_VALUE]), + (array)$udfs + ); + } + // ------------------------------------------------------------------------------------------------- // Private methods + /** + * + */ + private function _printStartUDFBlock($widgetData) + { + $startBlock = '