diff --git a/application/controllers/system/FAS_UDF.php b/application/controllers/system/FAS_UDF.php index cd78d64d2..febe266c7 100644 --- a/application/controllers/system/FAS_UDF.php +++ b/application/controllers/system/FAS_UDF.php @@ -7,60 +7,34 @@ class FAS_UDF extends Auth_Controller const FAS_UDF_SESSION_NAME = 'fasUdfSessionName'; public function __construct() - { - parent::__construct( + { + parent::__construct( array( 'index' => 'basis/person:r', 'saveUDF' => 'basis/person:rw' ) ); - + $this->load->model('person/Person_model', 'PersonModel'); $this->load->model('crm/Prestudent_model', 'PrestudentModel'); - } + } /** * */ 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); + $data = array(); 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 +44,12 @@ 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/system/infocenter/InfoCenter.php b/application/controllers/system/infocenter/InfoCenter.php index 287dbf515..43de8bb60 100644 --- a/application/controllers/system/infocenter/InfoCenter.php +++ b/application/controllers/system/infocenter/InfoCenter.php @@ -1337,7 +1337,7 @@ class InfoCenter extends Auth_Controller show_error(getError($prestudentWithZgv)); } - $zgvpruefung = $prestudentWithZgv->retval[0]; + $zgvpruefung = getData($prestudentWithZgv); if (isset($zgvpruefung->prestudentstatus)) { @@ -1362,11 +1362,15 @@ class InfoCenter extends Auth_Controller $zgvpruefung->isRtFreigegeben = false; $zgvpruefung->isStgFreigegeben = false; $zgvpruefung->sendStgFreigabeMsg = true;//wether Stgudiengangfreigabemessage can be sent (for "exceptions", Studiengänge with no message sending) - $this->PrestudentstatusModel->addSelect('bestaetigtam, statusgrund_id, tbl_status_grund.bezeichnung_mehrsprachig AS bezeichnung_statusgrund'); - $this->PrestudentstatusModel->addJoin('public.tbl_status_grund', 'statusgrund_id', 'LEFT'); - $isFreigegeben = $this->PrestudentstatusModel->loadWhere(array('studiensemester_kurzbz' => $zgvpruefung->prestudentstatus->studiensemester_kurzbz, - 'tbl_prestudentstatus.status_kurzbz' => self::INTERESSENTSTATUS, 'prestudent_id' => $prestudent->prestudent_id)); + $isFreigegeben = null; + if (isset($zgvpruefung->prestudentstatus->studiensemester_kurzbz)) + { + $this->PrestudentstatusModel->addSelect('bestaetigtam, statusgrund_id, tbl_status_grund.bezeichnung_mehrsprachig AS bezeichnung_statusgrund'); + $this->PrestudentstatusModel->addJoin('public.tbl_status_grund', 'statusgrund_id', 'LEFT'); + $isFreigegeben = $this->PrestudentstatusModel->loadWhere(array('studiensemester_kurzbz' => $zgvpruefung->prestudentstatus->studiensemester_kurzbz, + 'tbl_prestudentstatus.status_kurzbz' => self::INTERESSENTSTATUS, 'prestudent_id' => $prestudent->prestudent_id)); + } if (hasData($isFreigegeben)) { @@ -1537,9 +1541,10 @@ class InfoCenter extends Auth_Controller show_error(getError($prestudent)); } - $person_id = $prestudent->retval[0]->person_id; - $studiengang_kurzbz = $prestudent->retval[0]->studiengang; - $studiengang_bezeichnung = $prestudent->retval[0]->studiengangbezeichnung; + $prestudentdata = getData($prestudent); + $person_id = $prestudentdata->person_id; + $studiengang_kurzbz = $prestudentdata->studiengang; + $studiengang_bezeichnung = $prestudentdata->studiengangbezeichnung; return array('person_id' => $person_id, 'studiengang_kurzbz' => $studiengang_kurzbz, 'studiengang_bezeichnung' => $studiengang_bezeichnung); } @@ -1582,7 +1587,8 @@ class InfoCenter extends Auth_Controller private function _sendFreigabeMail($prestudent_id) { //get data - $prestudent = $this->PrestudentModel->getPrestudentWithZgv($prestudent_id)->retval[0]; + $prestudent = $this->PrestudentModel->getPrestudentWithZgv($prestudent_id); + $prestudent = getData($prestudent); $prestudentstatus = $prestudent->prestudentstatus; $person_id = $prestudent->person_id; $person = $this->PersonModel->getPersonStammdaten($person_id, true)->retval; diff --git a/application/controllers/widgets/Filters.php b/application/controllers/widgets/Filters.php index b5cbc07ea..748272f09 100644 --- a/application/controllers/widgets/Filters.php +++ b/application/controllers/widgets/Filters.php @@ -4,7 +4,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); /** * This controller operates between (interface) the JS (GUI) and the FilterWidgetLib (back-end) - * Provides data to the ajax get calls about the filter + * Provides data to the ajax get calls about the filter widget * Accepts ajax post calls to change the filter data * This controller works with JSON calls on the HTTP GET or POST and the output is always JSON * NOTE: extends the FHC_Controller instead of the Auth_Controller because the FilterWidget has its @@ -12,7 +12,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); */ class Filters extends FHC_Controller { - const FILTER_UNIQUE_ID = 'filterUniqueId'; + const FILTER_UNIQUE_ID = 'filterUniqueId'; // Name of the filter widget unique id /** * Calls the parent's constructor and loads the FilterWidgetLib diff --git a/application/controllers/widgets/Tables.php b/application/controllers/widgets/Tables.php index 21161ff9f..b3ea7d5a5 100644 --- a/application/controllers/widgets/Tables.php +++ b/application/controllers/widgets/Tables.php @@ -4,7 +4,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); /** * This controller operates between (interface) the JS (GUI) and the tablewidgetlib (back-end) - * Provides data to the ajax get calls about the filter + * Provides data to the ajax get calls about the table widget * Accepts ajax post calls to change the filter data * This controller works with JSON calls on the HTTP GET or POST and the output is always JSON * NOTE: extends the FHC_Controller instead of the Auth_Controller because the TableWidget has its @@ -12,7 +12,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); */ class Tables extends FHC_Controller { - const TABLE_UNIQUE_ID = 'tableUniqueId'; + const TABLE_UNIQUE_ID = 'tableUniqueId'; // Name of the table widget unique id /** * Calls the parent's constructor and loads the tablewidgetlib diff --git a/application/controllers/widgets/UDF.php b/application/controllers/widgets/UDF.php new file mode 100644 index 000000000..5b4c45776 --- /dev/null +++ b/application/controllers/widgets/UDF.php @@ -0,0 +1,107 @@ +load->library('AuthLib'); + + // Loads the UDFLib with HTTP GET/POST parameters + $this->_loadUDFLib(); + + // Checks if the caller is allow to use this UDF widget + $this->_isAllowed(); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Save data about the current UDFs and the result 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 UDFWidget + */ + private function _isAllowed() + { + if (!$this->udflib->isAllowed()) + { + $this->terminateWithJsonError('You are not allowed to access to this content'); + } + } + + /** + * Loads the UDFLib 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 UDFLib 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..4b89ae5bf 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 that extends this class + */ + 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 bc8552c25..f9ad7d20d 100644 --- a/application/libraries/UDFLib.php +++ b/application/libraries/UDFLib.php @@ -3,15 +3,20 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); /** - * Library to manage UDF + * Library to manage UDFs */ class UDFLib { - const WIDGET_NAME = 'UDFWidget'; - const SCHEMA_ARG_NAME = 'schema'; - const TABLE_ARG_NAME = 'table'; - const FIELD_ARG_NAME = 'field'; - const UDFS_ARG_NAME = 'udfs'; + const UDF_UNIQUE_ID = 'udfUniqueId'; // Name of the UDF widget unique id + + const SESSION_NAME = 'FHC_UDF_WIDGET'; // Name of the session area used for UDFs + + // Parameters names + const WIDGET_NAME = 'UDFWidget'; // UDFWidget name + const SCHEMA_ARG_NAME = 'schema'; // Schema parameter name + const TABLE_ARG_NAME = 'table'; // Table parameter name + const FIELD_ARG_NAME = 'field'; // Field parameter name + const UDFS_ARG_NAME = 'udfs'; // UDFs parameter name // UDF json schema attributes const NAME = 'name'; // UDF name attribute @@ -22,6 +27,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'; + + // ...to specify the primary key name and value + 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,8 +62,10 @@ class UDFLib private $_ci; // Code igniter instance + private $_udfUniqueId; // Property that contains the UDF widget unique id + /** - * Loads fhc helper + * Gets CI instance */ public function __construct() { @@ -63,8 +80,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 +90,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 +122,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 +139,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 +160,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 +186,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 +199,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 +241,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 +255,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 +290,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 +315,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 +364,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 +383,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 +392,151 @@ class UDFLib return $isUDFColumn; } + /** + * Set the _udfUniqueId property + */ + public function setUDFUniqueId($udfUniqueId) + { + $this->_udfUniqueId = $udfUniqueId; + } + + /** + * Return an unique string that identify this UDF 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 UDF widget + */ + 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 UDF widget + */ + 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 UDF widget + */ + 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 UDF widget + */ + 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 is empty then return an error + if ($session == null) return error('No UDFWidget loaded'); + + // Workaround to load CI + $this->_ci->load->model('system/UDF_model', 'UDFModel'); + + // Initialize a new DB_Model + $dbModel = new DB_Model(); + + // Setup the new dbModel object with... + $dbModel->setup( + $session[self::SCHEMA_ARG_NAME], // ... schema... + $session[self::TABLE_ARG_NAME], // ...table... + $session[self::PRIMARY_KEY_NAME] // ...and primary key name + ); + + // 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]), + (array)$udfs + ); + } + + /** + * Checks if at least one of the permissions given as parameter (requiredPermissions) belongs + * to the authenticated user, if confirmed then is allowed to use this UDFWidget. + * If the parameter requiredPermissions is NOT given or is not present in the session, + * then NO one is allow to use this UDFWidget + * Wrapper method to permissionlib->hasAtLeastOne + */ + public function isAllowed($requiredPermissions = null) + { + $this->_ci->load->library('PermissionLib'); // Load permission library + + // Gets the required permissions from the session if they are not provided as parameter + $rq = $requiredPermissions; + if ($rq == null) $rq = $this->getSessionElement(self::REQUIRED_PERMISSIONS_PARAMETER); + + return $this->_ci->permissionlib->hasAtLeastOne($rq, self::PERMISSION_TABLE_METHOD, self::PERMISSION_TYPE); + } + // ------------------------------------------------------------------------------------------------- // Private methods + /** + * Print the block for UDFs + */ + private function _printStartUDFBlock($widgetData) + { + $startBlock = '