Merge branch 'udf'

This commit is contained in:
Paolo
2017-08-07 16:44:03 +02:00
32 changed files with 4310 additions and 1986 deletions
+11 -2
View File
@@ -101,5 +101,14 @@ define('EXIT_UNKNOWN_CLASS', 5); // unknown class
define('EXIT_UNKNOWN_METHOD', 6); // unknown class member
define('EXIT_USER_INPUT', 7); // invalid user input
define('EXIT_DATABASE', 8); // database error
define('EXIT__AUTO_MIN', 9); // lowest automatically-assigned error code
define('EXIT__AUTO_MAX', 125); // highest automatically-assigned error code
define('EXIT_VALIDATION_UDF', 10); // UDF validation has been failed
define('EXIT_VALIDATION_UDF_MIN_VALUE', 11); // UDF validation has been failed -> MIN VALUE
define('EXIT_VALIDATION_UDF_MAX_VALUE', 12); // UDF validation has been failed -> MAX VALUE
define('EXIT_VALIDATION_UDF_MIN_LENGTH', 13); // UDF validation has been failed -> MIN LENGTH
define('EXIT_VALIDATION_UDF_MAX_LENGTH', 14); // UDF validation has been failed -> MAX LENGTH
define('EXIT_VALIDATION_UDF_REGEX', 15); // UDF validation has been failed -> REGEX
define('EXIT_VALIDATION_UDF_REQUIRED', 16); // UDF validation has been failed -> REQUIRED
define('EXIT_VALIDATION_UDF_NOT_VALID_VAL', 17); // UDF validation has been failed -> Not valid value, object or array
define('EXIT_AUTO_MIN', 1000); // lowest automatically-assigned error code
define('EXIT_AUTO_MAX', 2000); // highest automatically-assigned error code
+1
View File
@@ -207,6 +207,7 @@ $config['fhc_acl'] = array
'system.tbl_webservicelog' => 'basis/webservicelog',
'system.tbl_webservicerecht' => 'basis/webservicerecht',
'system.tbl_webservicetyp' => 'basis/webservicetyp',
'system.tbl_udf' => 'system/udf',
'testtool.tbl_ablauf' => 'basis/ablauf',
'testtool.tbl_antwort' => 'basis/antwort',
'testtool.tbl_frage' => 'basis/frage',
+71
View File
@@ -0,0 +1,71 @@
{
"title": "UDF",
"type": "object",
"tableswhitelist": {
"type": "string",
"enum": ["tbl_person", "tbl_prestudent", "tbl_mitarbeiter", "tbl_lehrveranstaltung", "tbl_lehreinheit"]
},
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "array",
},
"placeholder": {
"type": "array",
},
"title": {
"type": "array",
},
"type": {
"type": "string",
"enum": ["checkbox", "textfield", "textarea", "date", "dropdown", "multipledropdown"]
},
"sort": {
"type": "integer"
},
"defaultValue": {
"type": "string"
},
"validation": {
"type": "object",
"properties": {
"required": {
"type": "boolean"
},
"min-length": {
"type": "integer"
},
"max-length": {
"type": "integer"
},
"min-value": {
"type": "integer"
},
"min-value": {
"type": "integer"
},
"regex": {
"type": "object",
"properties": {
"language": "string",
"expression": "string"
}
}
}
},
"listValues": {
"type": "object",
"properties": {
"sql": {
"type": "string"
},
"enum": {
"type": "array"
}
}
}
},
"required": ["type", "name"]
}
@@ -0,0 +1,133 @@
<?php
/**
* FH-Complete
*
* @package FHC-API
* @author FHC-Team
* @copyright Copyright (c) 2016, fhcomplete.org
* @license GPLv3
* @link http://fhcomplete.org
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
if (!defined('BASEPATH')) exit('No direct script access allowed');
class UDF extends APIv1_Controller
{
/**
* UDF API constructor.
*/
public function __construct()
{
parent::__construct();
// Load model UDF_model
$this->load->model('system/UDF_model', 'UDFModel');
}
/**
* @return void
*/
public function getUDF()
{
$decode = $this->get('decode');
$schema = $this->get('schema');
$table = $this->get('table');
$result = error();
if (isset($schema) || isset($table))
{
$result = $this->UDFModel->loadWhere(
array(
'schema' => $schema,
'table' => $table
)
);
}
else
{
$result = $this->UDFModel->load();
}
if ($decode)
{
$this->_jsonDecodeResult($result);
}
$this->response($result, REST_Controller::HTTP_OK);
}
/**
*
*/
public function postUDF()
{
$udfs = $this->post();
$validation = $this->_validate($udfs);
if (isSuccess($validation))
{
$caller = null;
if (isset($udfs['caller']))
{
$caller = $udfs['caller'];
unset($udfs['caller']);
}
$result = $this->UDFModel->saveUDFs($udfs);
if ($caller != null)
{
$res = 'ERR';
if (isSuccess($result))
{
$res = 'OK';
}
redirect($caller.'&res='.$res);
}
else
{
$this->response($result, REST_Controller::HTTP_OK);
}
}
else
{
$this->response($validation, REST_Controller::HTTP_OK);
}
}
/**
*
*/
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;
}
/**
* Decode to json the column jsons for every result set
*/
private function _jsonDecodeResult(&$result)
{
if (hasData($result))
{
for($i = 0; $i < count($result->retval); $i++)
{
$obj = $result->retval[$i];
$obj->jsons = json_decode($obj->jsons);
}
}
}
}
+127
View File
@@ -0,0 +1,127 @@
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class UDF extends VileSci_Controller
{
public function __construct()
{
parent::__construct();
// Load session library
$this->load->library('session');
// Loads the widget library
$this->load->library('WidgetLib');
//
$this->load->model('person/Person_model', 'PersonModel');
$this->load->model('crm/Prestudent_model', 'PrestudentModel');
}
/**
*
*/
public function index()
{
$person_id = $this->input->get('person_id');
if (isset($this->session->person_id))
{
if (!isset($person_id))
{
$person_id = $this->session->person_id;
}
unset($this->session->person_id);
}
$prestudent_id = $this->input->get('prestudent_id');
if (isset($this->session->prestudent_id))
{
if (!isset($prestudent_id))
{
$prestudent_id = $this->session->prestudent_id;
}
unset($this->session->prestudent_id);
}
$result = null;
if (isset($this->session->result))
{
$result = clone $this->session->result;
$this->session->set_userdata('result', null);
}
$data = array('result' => $result);
if (isset($person_id) && is_numeric($person_id))
{
if ($this->PersonModel->hasUDF())
{
$person = $this->PersonModel->load($person_id);
$personUdfs = $this->PersonModel->getUDFs();
$personUdfs['person_id'] = $person_id;
$data['personUdfs'] = $personUdfs;
}
}
if (isset($prestudent_id) && is_numeric($prestudent_id))
{
if ($this->PrestudentModel->hasUDF())
{
$prestudent = $this->PrestudentModel->load($prestudent_id);
$prestudentUdfs = $this->PrestudentModel->getUDFs();
$prestudentUdfs['prestudent_id'] = $prestudent_id;
$data['prestudentUdfs'] = $prestudentUdfs;
}
}
$this->load->view('system/udf', $data);
}
/**
*
*/
public function saveUDF()
{
$udfs = $this->input->post();
$validation = $this->_validate($udfs);
$userdata = array(
'person_id' => $udfs['person_id'],
'prestudent_id' => $udfs['prestudent_id']
);
if (isSuccess($validation))
{
// Load model UDF_model
$this->load->model('system/UDF_model', 'UDFModel');
$result = $this->UDFModel->saveUDFs($udfs);
$userdata['result'] = $result;
}
else
{
$userdata['result'] = $validation;
}
$this->session->set_userdata($userdata);
redirect('system/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;
}
}
+462 -32
View File
@@ -2,29 +2,73 @@
class DB_Model extends FHC_Model
{
// Default schema used by the models
const DEFAULT_SCHEMA = 'public';
// Default model class name postfix
const MODEL_POSTFIX = '_model';
// Query used to get the list of columns from a table
const QUERY_LIST_FIELDS = 'SELECT * FROM %s WHERE 0 = 1';
// Constants used to convert postgresql arrays and booleans to the php equivalent
const PGSQL_ARRAY_TYPE = '_';
const PGSQL_BOOLEAN_TYPE = 'bool';
const PGSQL_BOOLEAN_ARRAY_TYPE = '_bool';
const PGSQL_BOOLEAN_TRUE = 't';
const PGSQL_BOOLEAN_FALSE = 'f';
const MODEL_POSTFIX = '_model';
const DEFAULT_SCHEMA = 'public';
// UDF constants
const UDF_FIELD_NAME = 'udf_values';
const UDF_FIELD_TYPE = 'jsonb';
const UDF_FIELD_PREFIX = 'udf_';
const UDF_ATTRIBUTE_NAME = 'name';
const UDF_TYPE_NAME = 'type';
const UDF_CHKBOX_TYPE = 'checkbox';
const UDF_DROPDOWN_TYPE = 'dropdown';
const UDF_MULTIPLEDROPDOWN_TYPE = 'multipledropdown';
const UDF_FIELD_JSON_DESCRIPTION = 'jsons';
// UDF validation attributes
const UDF_REGEX = 'regex';
const UDF_REQUIRED = 'required';
const UDF_MAX_VALUE = 'max-value';
const UDF_MIN_VALUE = 'min-value';
const UDF_REGEX_LANG = 'php';
const UDF_MAX_LENGTH = 'max-length';
const UDF_MIN_LENGTH = 'min-length';
// String values of booleans
const STRING_TRUE = 'true';
const STRING_FALSE = 'false';
const STRING_NULL = 'null';
protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ...
protected $pk; // Name of the PrimaryKey for DB-Update, Load, ...
protected $hasSequence; // False if this table has a composite primary key that is not using a sequence
// True if this table has a primary key that uses a sequence
protected $UDFs; // Contains the UDFs
/**
* Constructor
*/
function __construct($dbTable = null, $pk = null, $hasSequence = true)
{
// Call parent constructor
parent::__construct();
$this->dbTable = $dbTable;
// Set properties
$this->pk = $pk;
$this->UDFs = array();
$this->dbTable = $dbTable;
$this->hasSequence = $hasSequence;
// Loads DB conns and confs
$this->load->database();
}
/** ---------------------------------------------------------------
/**
* Insert Data into DB-Table
*
* @param array $data DataArray for Insert
@@ -39,6 +83,9 @@ class DB_Model extends FHC_Model
// Checks rights
if ($isEntitled = $this->_isEntitled(PermissionLib::INSERT_RIGHT)) return $isEntitled;
// UDFs
if (isError($validate = $this->_manageUDFs($data))) return $validate;
// DB-INSERT
if ($this->db->insert($this->dbTable, $data))
{
@@ -68,11 +115,14 @@ class DB_Model extends FHC_Model
return error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
/**
* Replace Data in DB-Table
*
* @param array $data DataArray for Replacement
* @return array
*
* DEPRECATED: to be updated, not maintained
*
*/
public function replace($data)
{
@@ -82,15 +132,15 @@ class DB_Model extends FHC_Model
// Checks rights
if ($isEntitled = $this->_isEntitled(PermissionLib::REPLACE_RIGHT)) return $isEntitled;
// DB-REPLACE
if ($this->db->replace($this->dbTable, $data))
return success($this->db->insert_id());
else
return error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
/**
* Update Data in DB-Table
*
* @param string $id PK for DB-Table
@@ -107,7 +157,10 @@ class DB_Model extends FHC_Model
// Checks rights
if ($isEntitled = $this->_isEntitled(PermissionLib::UPDATE_RIGHT)) return $isEntitled;
// UDFs
if (isError($validate = $this->_manageUDFs($data, $id))) return $validate;
// DB-UPDATE
// Check for composite Primary Key
if (is_array($id))
@@ -125,7 +178,7 @@ class DB_Model extends FHC_Model
return error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
/**
* Delete data from DB-Table
*
* @param string $id Primary Key for DELETE
@@ -159,7 +212,7 @@ class DB_Model extends FHC_Model
return error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
/**
* Load single data from DB-Table
*
* @param string $id ID (Primary Key) for SELECT ... WHERE
@@ -196,7 +249,7 @@ class DB_Model extends FHC_Model
return error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
/**
* Load data from DB-Table with a where clause
*
* @return array
@@ -219,7 +272,7 @@ class DB_Model extends FHC_Model
return error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
/**
* Load data and convert a record into a list of data from the main table,
* and linked to every element, the data from the side tables
*
@@ -287,7 +340,7 @@ class DB_Model extends FHC_Model
{
// Converts the object that contains data, from the returned CI's object to an array
// with the postgresql array and boolean types converterd
$resultArray = $this->toPhp($resultDB);//var_dump($resultArray);
$resultArray = $this->toPhp($resultDB);
// Array that will contain all the mainTable records, and to each record the linked data
// of a side table
$returnArray = array();
@@ -364,7 +417,7 @@ class DB_Model extends FHC_Model
return $result;
}
/** ---------------------------------------------------------------
/**
* Add a table to join with
*
* @return void
@@ -380,7 +433,7 @@ class DB_Model extends FHC_Model
return success(true);
}
/** ---------------------------------------------------------------
/**
* Add order clause
*
* @return void
@@ -396,7 +449,7 @@ class DB_Model extends FHC_Model
return success(true);
}
/** ---------------------------------------------------------------
/**
* Add select clause
*
* @return void
@@ -412,7 +465,7 @@ class DB_Model extends FHC_Model
return success(true);
}
/** ---------------------------------------------------------------
/**
* Add distinct clause
*
* @return void
@@ -422,7 +475,7 @@ class DB_Model extends FHC_Model
$this->db->distinct();
}
/** ---------------------------------------------------------------
/**
* Add limit clause
*
* @return void
@@ -445,7 +498,7 @@ class DB_Model extends FHC_Model
return success(true);
}
/** ---------------------------------------------------------------
/**
* Add a table in the from clause
*
* @return void
@@ -468,7 +521,7 @@ class DB_Model extends FHC_Model
return success(true);
}
/** ---------------------------------------------------------------
/**
* Add one or more fields in the group by clause
*
* @return void
@@ -488,7 +541,7 @@ class DB_Model extends FHC_Model
return success(true);
}
/** ---------------------------------------------------------------
/**
* Reset the query builder state
*
* @return void
@@ -498,7 +551,7 @@ class DB_Model extends FHC_Model
$this->db->reset_query();
}
/** ---------------------------------------------------------------
/**
* This method call the method escape from class CI_DB_driver, therefore:
* this method determines the data type so that it can escape only string data.
* It also automatically adds single quotes around the data so you dont have to
@@ -510,7 +563,7 @@ class DB_Model extends FHC_Model
return $this->db->escape($value);
}
/** ---------------------------------------------------------------
/**
* Convert PG-Boolean to PHP-Boolean
*
* @param char $b PG-Char to convert
@@ -533,7 +586,7 @@ class DB_Model extends FHC_Model
return $val;
}
/** ---------------------------------------------------------------
/**
* Convert PG-Array to PHP-Array
*
* @param string $s PG-String to convert
@@ -623,7 +676,72 @@ class DB_Model extends FHC_Model
return $result;
}
/** ---------------------------------------------------------------
/**
* Return the property UDFs
*/
public function getUDFs()
{
return $this->UDFs;
}
/**
* Return one selected element of UDFs
*/
public function getUDF($udf)
{
if (isset($this->UDFs[$udf]))
{
return $this->UDFs[$udf];
}
return null;
}
/**
* Checks if this table has the field udf_values
*/
public function hasUDF()
{
return $this->fieldExists(DB_Model::UDF_FIELD_NAME);
}
/**
* Returns an array that contains a list of columns names of this table
*/
public function listFields()
{
$listFields = array();
// Workaround to get metadata from this table
$result = $this->db->query(sprintf(DB_Model::QUERY_LIST_FIELDS, $this->dbTable));
if (is_object($result))
{
$listFields = $result->list_fields();
}
return $listFields;
}
/**
* Checks if this table has a field == $field
*/
public function fieldExists($field)
{
$exists = true;
// If $field is not found in the list of fields of this table
if (array_search($field, $this->listFields()) === false)
{
$exists = false;
}
return $exists;
}
// ----------------------------------------------------------------------------
/**
* Invalid ID
*
* @param array $i Array with indexes.
@@ -702,6 +820,301 @@ class DB_Model extends FHC_Model
return $result;
}
// ----------------------------------------------------------------------------
/**
* Returns all the UDF for this table
*/
private function _getUDFsDefinitions()
{
$this->load->model('system/UDF_model', 'UDFModel');
$schema = DB_Model::DEFAULT_SCHEMA;
$table = $this->dbTable;
$dotPos = strpos($table, '.');
if (is_numeric($dotPos) && $dotPos > 0)
{
$tmpArray = explode('.', $table);
$schema = $tmpArray[0];
$table = $tmpArray[1];
}
$this->UDFModel->addSelect(DB_Model::UDF_FIELD_JSON_DESCRIPTION);
$udfResults = $this->UDFModel->loadWhere(
array(
'schema' => $schema,
'table' => $table
)
);
return $udfResults;
}
/**
* Move UDFs from $data to $UDFs
*/
private function _popUDFParameters(&$data)
{
foreach($data as $key => $val)
{
if (substr($key, 0, 4) == DB_Model::UDF_FIELD_PREFIX)
{
$this->UDFs[$key] = $val; // stores UDF value into property UDFs
unset($data[$key]); // remove from data
}
}
}
/**
* Validates UDF value
*/
private function _validateUDFs($decodedUDFValidation, $udfName, $udfType, $udfValue)
{
$returnArrayValidation = array(); // returned value
//
if ((((isset($decodedUDFValidation->validation->{DB_Model::UDF_REQUIRED})
&& $decodedUDFValidation->validation->{DB_Model::UDF_REQUIRED} === false)
|| !isset($decodedUDFValidation->validation->{DB_Model::UDF_REQUIRED}))
&& $udfValue == null)
|| $udfType == DB_Model::UDF_CHKBOX_TYPE)
{
// NOP
}
else
{
// If $udfValue is not an array, then store it inside a new array
$tmpUdfValues = $udfValue;
if (!is_array($udfValue))
{
$tmpUdfValues = array($udfValue);
}
// Loops through all the supplied UDFs values
foreach($tmpUdfValues as $udfValIndx => $udfVal)
{
// If the single UDF value is not an array or an object
if (!is_array($udfVal) && !is_object($udfVal))
{
// If the UDF value is numeric (integer, float, double...)
if (is_numeric($udfVal))
{
// If min value attribute is present in the validation for this UDF,
// then checks if the value of this UDF is compliant to this attribute
if (isset($decodedUDFValidation->{DB_Model::UDF_MIN_VALUE})
&& $udfVal < $decodedUDFValidation->{DB_Model::UDF_MIN_VALUE})
{
// validation is failed and the error is stored in $returnArrayValidation
$returnArrayValidation[] = error($udfName, EXIT_VALIDATION_UDF_MIN_VALUE);
}
// If max value attribute is present in the validation for this UDF,
// then checks if the value of this UDF is compliant to this attribute
if (isset($decodedUDFValidation->{DB_Model::UDF_MAX_VALUE})
&& $udfVal > $decodedUDFValidation->{DB_Model::UDF_MAX_VALUE})
{
// validation is failed and the error is stored in $returnArrayValidation
$returnArrayValidation[] = error($udfName, EXIT_VALIDATION_UDF_MAX_VALUE);
}
}
$strUdfVal = strval($udfVal); // store in $strUdfVal the string conversion of $udfVal
// If min length attribute is present in the validation for this UDF,
// then checks if the value of this UDF is compliant to this attribute
if (isset($decodedUDFValidation->{DB_Model::UDF_MIN_LENGTH}) && isset($strUdfVal)
&& strlen($strUdfVal) < $decodedUDFValidation->{DB_Model::UDF_MIN_LENGTH})
{
// validation is failed and the error is stored in $returnArrayValidation
$returnArrayValidation[] = error($udfName, EXIT_VALIDATION_UDF_MIN_LENGTH);
}
// If max length attribute is present in the validation for this UDF,
// then checks if the value of this UDF is compliant to this attribute
if (isset($decodedUDFValidation->{DB_Model::UDF_MAX_LENGTH}) && isset($strUdfVal)
&& strlen($strUdfVal) > $decodedUDFValidation->{DB_Model::UDF_MAX_LENGTH})
{
// validation is failed and the error is stored in $returnArrayValidation
$returnArrayValidation[] = error($udfName, EXIT_VALIDATION_UDF_MAX_LENGTH);
}
// If $udfVal is a string
if (is_string($udfVal))
{
// Search for a php regular expression in the validation of this UDF, if one is found
// then checks if the value of this UDF is compliant to this attribute
if (isset($decodedUDFValidation->{DB_Model::UDF_REGEX})
&& is_array($decodedUDFValidation->{DB_Model::UDF_REGEX}))
{
foreach($decodedUDFValidation->{DB_Model::UDF_REGEX} as $regexIndx => $regex)
{
if ($regex->language == DB_Model::UDF_REGEX_LANG)
{
if (preg_match($regex->expression, $udfVal) != 1)
{
// validation is failed and the error is stored in $returnArrayValidation
$returnArrayValidation[] = error($udfName, EXIT_VALIDATION_UDF_REGEX);
}
}
}
}
}
}
else // otherwise the validation is failed and the error is stored in $returnArrayValidation
{
$returnArrayValidation[] = error($udfName, EXIT_VALIDATION_UDF_NOT_VALID_VAL);
}
}
}
// If no UDF validation errors were raised, it's a success!!
if (count($returnArrayValidation) == 0)
{
$returnArrayValidation = success(true);
}
return $returnArrayValidation;
}
/**
* Manage UDFs
*/
private function _manageUDFs(&$data, $id = null)
{
$validate = success(true); // returned value
// Contains a list of validation errors for the UDFs that have not passed the validation
$notValidUDFsArray = array();
if ($this->hasUDF()) // Checks if this table has UDFs
{
$resultUDFsDefinitions = $this->_getUDFsDefinitions(); // retrieves UDFs definitions for this table
if (hasData($resultUDFsDefinitions)) // standard check if everything is ok and data are present
{
// Get udf values from $data & clean udf values from $data
// NOTE: Must be performed here because the load method populates the property UDFs too
$this->_popUDFParameters($data);
$requiredUDFsArray = array(); // contains a list of required UDFs
// Contains the UDFs values to be stored
// NOTE: the UDFs supplied that are not present in the UDF definition of this table, will be discarded
$toBeStoredUDFsArray = array();
// Decodes json that define the UDFs for this table
$decodedUDFDefinitions = json_decode(
$resultUDFsDefinitions->retval[0]->{DB_Model::UDF_FIELD_JSON_DESCRIPTION}
);
// Loops through the UDFs definitions
for($i = 0; $i < count($decodedUDFDefinitions); $i++)
{
$decodedUDFDefinition = $decodedUDFDefinitions[$i]; // Definition of a single UDF
// If validation rules are present for this UDF description and the required attribute is === true
// then add this UDF into $requiredUDFsArray
if(isset($decodedUDFDefinition->validation)
&& isset($decodedUDFDefinition->validation->{DB_Model::UDF_REQUIRED})
&& $decodedUDFDefinition->validation->{DB_Model::UDF_REQUIRED} === true)
{
$requiredUDFsArray[$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}] = error(
$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME},
EXIT_VALIDATION_UDF_REQUIRED
);
}
// Loops through the UDFs values that should be stored
foreach($this->UDFs as $key => $val)
{
// If this is the definition of this UDF
if ($decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME} == $key)
{
if (isset($decodedUDFDefinition->validation)) // If validation rules are present for this UDF
{
// Validation!!!
$validate = $this->_validateUDFs(
$decodedUDFDefinition->validation, //
$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}, //
$decodedUDFDefinition->{DB_Model::UDF_TYPE_NAME},
$val //
);
// If the validation attribute required is === true for this UDF
// and this UDF is present in the array $requiredUDFsArray
// then removes this UDF from the array $requiredUDFsArray
// because this UDF is present in the property UDFs (the list of UDFs that should be stored)
// therefore it was supplied
if (isset($decodedUDFDefinition->validation->{DB_Model::UDF_REQUIRED})
&& $decodedUDFDefinition->validation->{DB_Model::UDF_REQUIRED} === true
&& isset($requiredUDFsArray[$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}])
&& $val != null)
{
unset($requiredUDFsArray[$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}]);
}
}
// If validation is ok copy the value that is to be stored into $toBeStoredUDFsArray
if (isSuccess($validate))
{
$toBeStoredUDFsArray[$key] = $val;
}
else // otherwise store the validation error in $notValidUDFsArray
{
$notValidUDFsArray[] = $validate;
}
}
}
}
// Copies the remaining required UDFs into $notValidUDFsArray
// because they were not supplied, therefore must be notified as error
foreach($requiredUDFsArray as $key => $val)
{
$notValidUDFsArray[] = array($val);
}
// If the validation of all the supplied UDFs is ok
if (count($notValidUDFsArray) == 0)
{
// An update is performed, then in this case it preserves the values
// of the UDF that are not updated
if ($id != null)
{
$record = $this->load($id); // retrive the DB record
// Checks that everything is ok and that there is only one record
if (isSuccess($record) && count($record->retval) == 1)
{
$recordFields = (array)$record->retval[0]; // convert to an array
foreach($recordFields as $fieldName => $fieldValue)
{
// If this field is an UDF
if (substr($fieldName, 0, 4) == DB_Model::UDF_FIELD_PREFIX)
{
// If this field is not present in the given parameters
// then copy it from the DB without changes
if (!array_key_exists($fieldName, $toBeStoredUDFsArray))
{
$toBeStoredUDFsArray[$fieldName] = $fieldValue;
}
}
}
}
}
$encodedToBeStoredUDFs = json_encode($toBeStoredUDFsArray); // encode to json
if ($encodedToBeStoredUDFs !== false) // if encode was ok
{
// Save the supplied UDFs values
$data[DB_Model::UDF_FIELD_NAME] = $encodedToBeStoredUDFs;
}
}
else // otherwise the returning value will be the list of UDFs validation errors
{
$validate = error($notValidUDFsArray, EXIT_VALIDATION_UDF);
}
}
}
return $validate;
}
/**
* Checks if the caller is entitled to perform this operation with this right
*/
@@ -744,8 +1157,9 @@ class DB_Model extends FHC_Model
for($i = 0; $i < count($metaDataArray); $i++) // Looking for booleans and arrays
{
// If array type or boolean type
if (strpos($metaDataArray[$i]->type, DB_Model::PGSQL_ARRAY_TYPE) !== false ||
$metaDataArray[$i]->type == DB_Model::PGSQL_BOOLEAN_TYPE)
if (strpos($metaDataArray[$i]->type, DB_Model::PGSQL_ARRAY_TYPE) !== false
|| $metaDataArray[$i]->type == DB_Model::PGSQL_BOOLEAN_TYPE
|| $metaDataArray[$i]->name == DB_Model::UDF_FIELD_NAME)
{
// Name and type of the field to be converted
$toBeConverted = new stdClass();
@@ -767,7 +1181,7 @@ class DB_Model extends FHC_Model
for($i = 0; $i < count($resultsArray); $i++)
{
// Single element
$tmpResult = $resultsArray[$i];
$resultElement = $resultsArray[$i];
// Looping on fields to be converted
for($j = 0; $j < count($toBeConverterdArray); $j++)
{
@@ -777,15 +1191,31 @@ class DB_Model extends FHC_Model
// Array type
if (strpos($toBeConverted->type, DB_Model::PGSQL_ARRAY_TYPE) !== false)
{
$tmpResult->{$toBeConverted->name} = $this->pgsqlArrayToPhpArray(
$tmpResult->{$toBeConverted->name},
$resultElement->{$toBeConverted->name} = $this->pgsqlArrayToPhpArray(
$resultElement->{$toBeConverted->name},
$toBeConverted->type == DB_Model::PGSQL_BOOLEAN_ARRAY_TYPE
);
}
// Boolean type
else if ($toBeConverted->type == DB_Model::PGSQL_BOOLEAN_TYPE)
{
$tmpResult->{$toBeConverted->name} = $this->pgBoolPhp($tmpResult->{$toBeConverted->name});
$resultElement->{$toBeConverted->name} = $this->pgBoolPhp($resultElement->{$toBeConverted->name});
}
// UDF
else if ($toBeConverted->type == DB_Model::UDF_FIELD_TYPE
&& substr($toBeConverted->name, 0, 4) == DB_Model::UDF_FIELD_PREFIX)
{
$jsonValues = json_decode($resultElement->{$toBeConverted->name}); // decode UDFs values
if ($jsonValues != null) // if decode is ok
{
// For every UDF
foreach($jsonValues as $key => $value)
{
$resultElement->{$key} = $value; // create a new element called like the UDF
$this->UDFs[$key] = $value; // stores the UDF in the property UDFs
}
}
unset($resultElement->{$toBeConverted->name}); // remove udf_values from the response
}
}
}
+14 -7
View File
@@ -1,9 +1,16 @@
<?php
if ( ! defined('BASEPATH'))
exit('No direct script access allowed');
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// DB-Errormessages
$lang['fhc_'.FHC_MODEL_ERROR] = 'Fehler in Model';
$lang['fhc_'.FHC_NODBTABLE] = '"dbTable" ist nicht gesetzt!';
$lang['fhc_'.FHC_NORIGHT] = 'Rechte sind nicht ausreichend!';
$lang['fhc_'.FHC_MODEL_ERROR] = 'Fehler in Model';
$lang['fhc_'.FHC_NODBTABLE] = '"dbTable" ist nicht gesetzt!';
$lang['fhc_'.FHC_NORIGHT] = 'Rechte sind nicht ausreichend!';
$lang['fhc_'.EXIT_VALIDATION_UDF] = 'UDF validation has been failed';
$lang['fhc_'.EXIT_VALIDATION_UDF_MIN_VALUE] = 'UDF validation has been failed - MIN VALUE';
$lang['fhc_'.EXIT_VALIDATION_UDF_MAX_VALUE] = 'UDF validation has been failed - MAX VALUE';
$lang['fhc_'.EXIT_VALIDATION_UDF_MIN_LENGTH] = 'UDF validation has been failed - MIN LENGTH';
$lang['fhc_'.EXIT_VALIDATION_UDF_MAX_LENGTH] = 'UDF validation has been failed - MAX LENGTH';
$lang['fhc_'.EXIT_VALIDATION_UDF_REGEX] = 'UDF validation has been failed - REGEX';
$lang['fhc_'.EXIT_VALIDATION_UDF_REQUIRED] = 'UDF validation has been failed - REQUIRED';
$lang['fhc_'.EXIT_VALIDATION_UDF_NOT_VALID_VAL] = 'UDF validation has been failed - Not valid value, object or array given';
@@ -6,3 +6,11 @@ if ( ! defined('BASEPATH'))
$lang['fhc_'.FHC_MODEL_ERROR] = 'Error in Model';
$lang['fhc_'.FHC_NODBTABLE] = 'dbTable is not set!';
$lang['fhc_'.FHC_NORIGHT] = 'rights are missing!';
$lang['fhc_'.EXIT_VALIDATION_UDF] = 'UDF validation has been failed';
$lang['fhc_'.EXIT_VALIDATION_UDF_MIN_VALUE] = 'UDF validation has been failed - MIN VALUE';
$lang['fhc_'.EXIT_VALIDATION_UDF_MAX_VALUE] = 'UDF validation has been failed - MAX VALUE';
$lang['fhc_'.EXIT_VALIDATION_UDF_MIN_LENGTH] = 'UDF validation has been failed - MIN LENGTH';
$lang['fhc_'.EXIT_VALIDATION_UDF_MAX_LENGTH] = 'UDF validation has been failed - MAX LENGTH';
$lang['fhc_'.EXIT_VALIDATION_UDF_REGEX] = 'UDF validation has been failed - REGEX';
$lang['fhc_'.EXIT_VALIDATION_UDF_REQUIRED] = 'UDF validation has been failed - REQUIRED';
$lang['fhc_'.EXIT_VALIDATION_UDF_NOT_VALID_VAL] = 'UDF validation has been failed - Not valid value, object or array given';
File diff suppressed because it is too large Load Diff
+197
View File
@@ -0,0 +1,197 @@
<?php
class UDF_model extends DB_Model
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->dbTable = 'system.tbl_udf';
$this->pk = array('schema', 'table');
$this->hasSequence = false;
}
/**
*
*/
public function execQuery($query, $parametersArray = null)
{
//
if (
(
substr($query, 0, 6) == 'SELECT'
|| substr($query, 0, 4) == 'WITH'
)
&&
(
!stripos($query, 'INSERT')
&& !stripos($query, 'UPDATE')
&& !stripos($query, 'DELETE')
)
)
{
return parent::execQuery($query, $parametersArray);
}
else
{
return error('You are allowed to run only query for reading data');
}
}
/**
*
*/
public function saveUDFs($udfs)
{
$result = error('No way man!');
$resultPerson = success('person');
$resultPrestudent = success('prestudent');
$person_id = $udfs['person_id'];
unset($udfs['person_id']);
$prestudent_id = $udfs['prestudent_id'];
unset($udfs['prestudent_id']);
$jsons = array();
//
if (isset($person_id))
{
// Load model Person_model
$this->load->model('person/Person_model', 'PersonModel');
$result = $this->load(array('public', 'tbl_person'));
if (isSuccess($result) && count($result->retval) == 1)
{
$jsons = json_decode($result->retval[0]->jsons);
}
$udfs = $this->_fillMissingChkboxUDF($udfs, $jsons);
$udfs = $this->_fillMissingDropdownUDF($udfs, $jsons);
$udfs = $this->_fillMissingTextUDF($udfs, $jsons);
$resultPerson = $this->PersonModel->update($person_id, $udfs);
}
//
if (isset($prestudent_id))
{
// Load model Prestudent_model
$this->load->model('crm/Prestudent_model', 'PrestudentModel');
$result = $this->load(array('public', 'tbl_prestudent'));
if (isSuccess($result) && count($result->retval) == 1)
{
$jsons = json_decode($result->retval[0]->jsons);
}
$udfs = $this->_fillMissingChkboxUDF($udfs, $jsons);
$udfs = $this->_fillMissingDropdownUDF($udfs, $jsons);
$udfs = $this->_fillMissingTextUDF($udfs, $jsons);
$resultPrestudent = $this->PrestudentModel->update($prestudent_id, $udfs);
}
if (isSuccess($resultPerson) && isSuccess($resultPrestudent))
{
$result = success(array($resultPerson->retval, $resultPrestudent->retval));
}
else if(isError($resultPerson))
{
$result = $resultPerson;
}
else if(isError($resultPrestudent))
{
$result = $resultPrestudent;
}
return $result;
}
/**
*
*/
private function _fillMissingChkboxUDF($udfs, $jsons)
{
$_fillMissingChkboxUDF = $udfs;
foreach($jsons as $udfDescription)
{
if ($udfDescription->{DB_Model::UDF_TYPE_NAME} == DB_Model::UDF_CHKBOX_TYPE)
{
if (!isset($_fillMissingChkboxUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}]))
{
$_fillMissingChkboxUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = false;
}
else
{
if ($_fillMissingChkboxUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] == DB_Model::STRING_FALSE)
{
$_fillMissingChkboxUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = false;
}
else if ($_fillMissingChkboxUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] == DB_Model::STRING_TRUE)
{
$_fillMissingChkboxUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = true;
}
}
}
}
return $_fillMissingChkboxUDF;
}
/**
*
*/
private function _fillMissingDropdownUDF($udfs, $jsons)
{
$_fillMissingDropdownUDF = $udfs;
foreach($jsons as $udfDescription)
{
if ($udfDescription->{DB_Model::UDF_TYPE_NAME} == DB_Model::UDF_DROPDOWN_TYPE
|| $udfDescription->{DB_Model::UDF_TYPE_NAME} == DB_Model::UDF_MULTIPLEDROPDOWN_TYPE)
{
if (!isset($_fillMissingDropdownUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}]))
{
$_fillMissingDropdownUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = null;
}
else if($_fillMissingDropdownUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] == DB_Model::STRING_NULL)
{
$_fillMissingDropdownUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = null;
}
}
}
return $_fillMissingDropdownUDF;
}
/**
*
*/
private function _fillMissingTextUDF($udfs, $jsons)
{
$_fillMissingTextUDF = $udfs;
foreach($jsons as $udfDescription)
{
if ($udfDescription->{DB_Model::UDF_TYPE_NAME} == 'textarea'
|| $udfDescription->{DB_Model::UDF_TYPE_NAME} == 'textfield')
{
if (!isset($_fillMissingTextUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}]))
{
$_fillMissingTextUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = null;
}
else if(trim($_fillMissingTextUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}]) == '')
{
$_fillMissingTextUDF[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = null;
}
}
}
return $_fillMissingTextUDF;
}
}
+145
View File
@@ -0,0 +1,145 @@
<?php $this->load->view("templates/header", array("title" => "UDF", "widgetsCSS" => true)); ?>
<body style="background-color: #eff0f1;">
<?php
if ($result != null)
{
if (isSuccess($result))
{
?>
<div style="color: black;">
Saved!
</div>
<br>
<?php
}
else
{
?>
<div style="color: red;">
Error while saving!
</div>
<br>
<div style="color: red;">
<?php
$errors = $result->retval;
foreach ($errors as $error)
{
foreach ($error as $fieldError)
{
echo $fieldError->msg . ' -> ' . $fieldError->retval . '<br>';
}
}
?>
</div>
<br>
<br>
<br>
<?php
}
}
?>
<form action="<?php echo APP_ROOT; ?>index.ci.php/system/UDF/saveUDF" method="POST">
<div class="div-table">
<div class="div-row">
<div class="div-cell" style="font-size: 20px; font-weight: bold;">
Zusatzfelder
</div>
</div>
<div class="div-row">
<div class="div-cell">
&nbsp;
</div>
</div>
<div class="div-row">
<?php
if (isset($personUdfs))
{
?>
<div class="div-cell">
<?php
echo $this->widgetlib->UDFWidget(
array(
UDFWidgetTpl::SCHEMA_ARG_NAME => 'public',
UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person',
UDFWidgetTpl::UDFS_ARG_NAME => $personUdfs
)
);
?>
</div>
<div class="div-cell" style="width: 40px;">
&nbsp;
</div>
<?php
}
?>
<?php
if (isset($prestudentUdfs))
{
?>
<div class="div-cell">
<?php
echo $this->widgetlib->UDFWidget(
array(
UDFWidgetTpl::SCHEMA_ARG_NAME => 'public',
UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_prestudent',
UDFWidgetTpl::UDFS_ARG_NAME => $prestudentUdfs
)
);
?>
</div>
<?php
}
?>
</div>
<div class="div-row">
<div class="div-cell">
&nbsp;
</div>
</div>
<div class="div-row halign-right">
<?php
if (isset($personUdfs) && isset($prestudentUdfs))
{
?>
<div class="div-cell">
&nbsp;
</div>
<div class="div-cell">
&nbsp;
</div>
<?php
}
?>
<div class="div-cell halign-right">
<input type="submit" value="&nbsp;Speichern&nbsp;">
</div>
</div>
</div>
<?php
if (isset($personUdfs))
{
?>
<input type="hidden" name="person_id" value="<?php echo $personUdfs['person_id']; ?>">
<?php
}
?>
<?php
if (isset($prestudentUdfs))
{
?>
<input type="hidden" name="prestudent_id" value="<?php echo $prestudentUdfs['prestudent_id']; ?>">
<?php
}
?>
</form>
</body>
<?php $this->load->view("templates/footer"); ?>
+4
View File
@@ -15,6 +15,7 @@ isset($title) ? $title = 'VileSci - '.$title : $title = 'VileSci';
!isset($jsoneditor) ? $jsoneditor = false : $jsoneditor = $jsoneditor;
!isset($jsonforms) ? $jsonforms = false : $jsonforms = $jsonforms;
!isset($textile) ? $textile = false : $textile = $textile;
!isset($widgetsCSS) ? $widgetsCSS = false : $widgetsCSS = $widgetsCSS;
!isset($datepicker) ? $datepicker = false : $datepicker = $datepicker;
if ($tablesort || $jquery_checkboxes || $jquery_custom)
@@ -102,4 +103,7 @@ if($jquery && $jqueryComposer)
<link rel="stylesheet" type="text/css" href="<?php echo base_url('vendor/json-forms/dist/css/brutusin-json-forms.min.css'); ?>" />
<script type="text/javascript" src="<?php echo base_url('vendor/json-forms/dist/js/brutusin-json-forms.min.js'); ?>"></script>
<?php endif ?>
<?php if($widgetsCSS) : ?>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('skin/widgets.css'); ?>" />
<?php endif ?>
</head>
+39
View File
@@ -0,0 +1,39 @@
<?php Widget::printStartBlock(${Widget::HTML_ARG_NAME}); ?>
<div class="div-table">
<div class="div-row">
<?php
if (isset(${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]))
{
?>
<div class="div-cell-label valign-middle width-150px">
<label for="<?php echo ${Widget::HTML_ARG_NAME}[Widget::HTML_ID]; ?>">
<?php echo ${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]; ?>
</label>
</div>
<?php
}
?>
<div class="div-cell-data width-30px">
<input
type="checkbox"
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_ID); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_NAME); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidget::REQUIRED); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::REGEX); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::TITLE); ?>
<?php
$checked = '';
if (${CheckboxWidget::VALUE_FIELD} === true)
{
$checked = 'checked';
}
?>
<?php echo $checked; ?>
value="<?php echo CheckboxWidget::CHECKBOX_VALUE; ?>"
>
</div>
</div>
</div>
<?php Widget::printEndBlock(${Widget::HTML_ARG_NAME}); ?>
+71 -14
View File
@@ -1,14 +1,71 @@
<select id="<?php echo ${Widget::HTML_ARG_NAME}[Widget::HTML_ID]; ?>" name="<?php echo ${Widget::HTML_ARG_NAME}[Widget::HTML_NAME]; ?>">
<?php foreach(${DropdownWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME} as $element): ?>
<?php
$selected = '';
if ($element->{DropdownWidget::ID_FIELD} == ${DropdownWidget::SELECTED_ELEMENT})
{
$selected = 'selected';
}
?>
<option value="<?php echo $element->{DropdownWidget::ID_FIELD}; ?>" <?php echo $selected; ?>>
<?php echo $element->{DropdownWidget::DESCRIPTION_FIELD}; ?>
</option>
<?php endforeach; ?>
</select>
<?php Widget::printStartBlock(${Widget::HTML_ARG_NAME}); ?>
<div class="div-table">
<div class="div-row">
<?php
if (isset(${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]))
{
$align = "valign-middle";
if (isset(${Widget::HTML_ARG_NAME}[DropdownWidget::MULTIPLE]))
{
$align = "valign-top";
}
?>
<div class="div-cell width-150px <?php echo $align; ?>">
<label for="<?php echo ${Widget::HTML_ARG_NAME}[Widget::HTML_ID]; ?>">
<?php echo ${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]; ?>
</label>
</div>
<?php
}
?>
<div class="div-cell width-150px">
<select
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_ID); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_NAME); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, DropdownWidget::SIZE); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, DropdownWidget::MULTIPLE, false); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidget::REQUIRED); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::MAX_VALUE); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::MIN_VALUE); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::REGEX); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::TITLE); ?>
>
<?php
$elements = ${DropdownWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME};
$selectedElements = ${DropdownWidget::SELECTED_ELEMENT};
foreach($elements as $element)
{
$selected = '';
if (is_array($selectedElements))
{
foreach($selectedElements as $selectedElement)
{
if ($element->{DropdownWidget::ID_FIELD} == $selectedElement)
{
$selected = 'selected';
}
}
}
else
{
if ($element->{DropdownWidget::ID_FIELD} == $selectedElements)
{
$selected = 'selected';
}
}
?>
<option value="<?php echo $element->{DropdownWidget::ID_FIELD}; ?>" <?php echo $selected; ?>>
<?php echo $element->{DropdownWidget::DESCRIPTION_FIELD}; ?>
</option>
<?php
}
?>
</select>
</div>
</div>
</div>
<?php Widget::printEndBlock(${Widget::HTML_ARG_NAME}); ?>
+32
View File
@@ -0,0 +1,32 @@
<?php Widget::printStartBlock(${Widget::HTML_ARG_NAME}); ?>
<div class="div-table">
<div class="div-row">
<?php
if (isset(${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]))
{
?>
<div class="div-cell width-150px valign-top">
<label for="<?php echo ${Widget::HTML_ARG_NAME}[Widget::HTML_ID]; ?>">
<?php echo ${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]; ?>
</label>
</div>
<?php
}
?>
<div class="div-cell width-150px">
<textarea
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_ID); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_NAME); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, TextareaWidget::ROWS); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, TextareaWidget::COLS); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidget::REQUIRED); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::PLACEHOLDER); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::REGEX); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::TITLE); ?>
><?php echo ${TextareaWidget::TEXT}; ?></textarea>
</div>
</div>
</div>
<?php Widget::printEndBlock(${Widget::HTML_ARG_NAME}); ?>
+33
View File
@@ -0,0 +1,33 @@
<?php Widget::printStartBlock(${Widget::HTML_ARG_NAME}); ?>
<div class="div-table">
<div class="div-row">
<?php
if (isset(${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]))
{
?>
<div class="div-cell width-150px valign-middle">
<label for="<?php echo ${Widget::HTML_ARG_NAME}[Widget::HTML_ID]; ?>">
<?php echo ${Widget::HTML_ARG_NAME}[UDFWidgetTpl::LABEL]; ?>
</label>
</div>
<?php
}
?>
<div class="div-cell width-150px">
<input
type="text"
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_ID); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, Widget::HTML_NAME); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, TextfieldWidget::SIZE); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidget::REQUIRED); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::PLACEHOLDER); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::REGEX); ?>
<?php Widget::printAttribute(${Widget::HTML_ARG_NAME}, UDFWidgetTpl::TITLE); ?>
value="<?php echo ${TextfieldWidget::VALUE}; ?>"
>
</div>
</div>
</div>
<?php Widget::printEndBlock(${Widget::HTML_ARG_NAME}); ?>
+1 -1
View File
@@ -17,6 +17,6 @@ class Studiengang_widget extends DropdownWidget
'No studiengaenge found'
);
$this->loadDropDownView($widgetData);
$this->loadDropDownView();
}
}
+422
View File
@@ -0,0 +1,422 @@
<?php
/**
*
*/
class UDFWidget extends UDFWidgetTpl
{
const NAME = 'name';
const TYPE = 'type';
const REQUIRED = 'udf-required';
const VALIDATION = 'validation';
const LIST_VALUES = 'listValues';
const REGEX_LANGUAGE = 'js';
const PHRASES_APP_NAME = 'core';
public function display($widgetData)
{
$schema = $widgetData[UDFWidgetTpl::SCHEMA_ARG_NAME];
$table = $widgetData[UDFWidgetTpl::TABLE_ARG_NAME];
if (isset($widgetData[UDFWidgetTpl::FIELD_ARG_NAME]))
{
$field = $widgetData[UDFWidgetTpl::FIELD_ARG_NAME];
}
$udfResults = $this->_loadUDF($schema, $table);
if (hasData($udfResults))
{
$udf = $udfResults->retval[0];
if (isset($udf->jsons))
{
$jsonSchemas = json_decode($udf->jsons);
if (is_object($jsonSchemas) || is_array($jsonSchemas))
{
//
if (is_object($jsonSchemas))
{
$jsonSchemasArray = array($jsonSchemas);
}
else
{
$jsonSchemasArray = $jsonSchemas;
}
$found = false; //
$this->_sortJsonSchemas($jsonSchemasArray); //
//
foreach($jsonSchemasArray as $jsonSchema)
{
//
if (!isset($jsonSchema->{UDFWidget::TYPE}))
{
show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table));
break;
}
if (!isset($jsonSchema->{UDFWidget::NAME}))
{
show_error(sprintf('%s.%s: Attribute "name" not present in the json schema', $schema, $table));
break;
}
//
if ((isset($field) && $field == $jsonSchema->{UDFWidget::NAME}) || !isset($field))
{
$this->_setAttributesWithPhrases($jsonSchema);
$this->_setValidationAttributes($jsonSchema);
$this->_setNameAndId($jsonSchema);
$this->_render($jsonSchema);
//
if (isset($field) && $field == $jsonSchema->{UDFWidget::NAME})
{
$found = true;
break;
}
}
}
//
if (isset($field) && !$found)
{
show_error(sprintf('%s.%s: No schema present for field: %s', $schema, $table, $field));
}
}
else
{
show_error(sprintf('%s.%s: Not a valid json schema', $schema, $table));
}
}
else
{
show_error(sprintf('%s.%s: Does not contain "jsons" field', $schema, $table));
}
}
}
/**
*
*/
private function _setNameAndId($jsonSchema)
{
$this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $jsonSchema->{UDFWidget::NAME};
$this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $jsonSchema->{UDFWidget::NAME};
}
/**
*
*/
private function _sortJsonSchemas(&$jsonSchemasArray)
{
//
usort($jsonSchemasArray, function ($a, $b) {
//
if (!isset($a->sort))
{
$a->sort = 9999;
}
if (!isset($b->sort))
{
$b->sort = 9999;
}
if ($a->sort == $b->sort)
{
return 0;
}
return ($a->sort < $b->sort) ? -1 : 1;
});
}
/**
*
*/
private function _loadUDF($schema, $table)
{
// Loads UDF model
$this->_ci->load->model('system/UDF_model', 'UDFModel');
$udfResults = $this->_ci->UDFModel->loadWhere(
array(
'schema' => $schema,
'table' => $table
)
);
if (isError($udfResults))
{
if (is_object($udfResults) && isset($udfResults->retval))
{
show_error($udfResults->retval);
}
else if (is_string($udfResults))
{
show_error($udfResults);
}
else
{
show_error('UDFWidget: generic error occurred');
}
}
else if (!hasData($udfResults))
{
show_error(sprintf('%s.%s does not contain UDF', $schema, $table));
}
return $udfResults;
}
/**
*
*/
private function _render($jsonSchema)
{
// Type
if ($jsonSchema->{UDFWidget::TYPE} == 'checkbox')
{
$this->_renderCheckbox($jsonSchema);
}
else if ($jsonSchema->{UDFWidget::TYPE} == 'textfield')
{
$this->_renderTextfield($jsonSchema);
}
else if ($jsonSchema->{UDFWidget::TYPE} == 'textarea')
{
$this->_renderTextarea($jsonSchema);
}
else if ($jsonSchema->{UDFWidget::TYPE} == 'date')
{
}
else if ($jsonSchema->{UDFWidget::TYPE} == 'dropdown')
{
$this->_renderDropdown($jsonSchema);
}
else if ($jsonSchema->{UDFWidget::TYPE} == 'multipledropdown')
{
$this->_renderDropdown($jsonSchema, true);
}
}
/**
*
*/
private function _renderDropdown($jsonSchema, $multiple = false)
{
if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME])
&& isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]))
{
$this->_args[DropdownWidget::SELECTED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}];
}
else
{
$this->_args[DropdownWidget::SELECTED_ELEMENT] = null;
}
$dropdownWidgetUDF = new DropdownWidgetUDF($this->_name, $this->_args);
$parameters = array();
//
if (isset($jsonSchema->{UDFWidget::LIST_VALUES}->enum))
{
$parameters = $jsonSchema->{UDFWidget::LIST_VALUES}->enum;
}
//
else if (isset($jsonSchema->{UDFWidget::LIST_VALUES}->sql))
{
$queryResult = $this->UDFModel->execQuery($jsonSchema->{UDFWidget::LIST_VALUES}->sql);
if (hasData($queryResult))
{
$parameters = $queryResult->retval;
}
}
if ($multiple)
{
$dropdownWidgetUDF->setMultiple();
}
$dropdownWidgetUDF->render($parameters);
}
/**
*
*/
private function _renderTextarea($jsonSchema)
{
$textareaUDF = new TextareaWidgetUDF($this->_name, $this->_args);
$text = null;
if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME])
&& isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]))
{
$text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}];
}
$textareaUDF->render($text);
}
/**
*
*/
private function _renderTextfield($jsonSchema)
{
$textareaUDF = new TextfieldWidgetUDF($this->_name, $this->_args);
$text = null;
if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME])
&& isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]))
{
$text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}];
}
$textareaUDF->render($text);
}
/**
*
*/
private function _renderCheckbox($jsonSchema)
{
if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME])
&& isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]))
{
$this->_args[CheckboxWidget::VALUE_FIELD] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}];
}
else
{
$this->_args[CheckboxWidget::VALUE_FIELD] = Widget::HTML_DEFAULT_VALUE;
}
$checkboxWidgetUDF = new CheckboxWidgetUDF($this->_name, $this->_args);
$checkboxWidgetUDF->render();
}
/**
*
*/
private function _setAttributesWithPhrases($jsonSchema)
{
// Description, title and placeholder
if (isset($jsonSchema->{UDFWidgetTpl::LABEL})
|| isset($jsonSchema->{UDFWidgetTpl::TITLE})
|| isset($jsonSchema->{UDFWidgetTpl::PLACEHOLDER}))
{
//
$this->_ci->load->library('PhrasesLib');
//
if (isset($jsonSchema->{UDFWidgetTpl::LABEL}))
{
$tmpResult = $this->_ci->phraseslib->getPhrases(
UDFWidget::PHRASES_APP_NAME,
DEFAULT_LEHREINHEIT_SPRACHE,
$jsonSchema->{UDFWidgetTpl::LABEL},
null,
null,
'no'
);
if (hasData($tmpResult))
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::LABEL] = $tmpResult->retval[0]->text;
}
else
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::LABEL] = null;
}
}
//
if (isset($jsonSchema->{UDFWidgetTpl::TITLE}))
{
$tmpResult = $this->_ci->phraseslib->getPhrases(
UDFWidget::PHRASES_APP_NAME,
DEFAULT_LEHREINHEIT_SPRACHE,
$jsonSchema->{UDFWidgetTpl::TITLE},
null,
null,
'no'
);
if (hasData($tmpResult))
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::TITLE] = $tmpResult->retval[0]->text;
}
else
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::TITLE] = null;
}
}
//
if (isset($jsonSchema->{UDFWidgetTpl::PLACEHOLDER}))
{
$tmpResult = $this->_ci->phraseslib->getPhrases(
UDFWidget::PHRASES_APP_NAME,
DEFAULT_LEHREINHEIT_SPRACHE,
$jsonSchema->{UDFWidgetTpl::PLACEHOLDER},
null,
null,
'no'
);
if (hasData($tmpResult))
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER] = $tmpResult->retval[0]->text;
}
else
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER] = null;
}
}
}
}
/**
*
*/
private function _setValidationAttributes($jsonSchema)
{
// Validation
$this->_args[Widget::HTML_ARG_NAME][UDFWidget::REQUIRED] = null;
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = null;
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MAX_VALUE] = null;
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MIN_VALUE] = null;
if (isset($jsonSchema->validation))
{
if (isset($jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::REGEX})
&& is_array($jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::REGEX}))
{
foreach($jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::REGEX} as $regex)
{
if ($regex->language === UDFWidget::REGEX_LANGUAGE)
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = $regex->expression;
}
}
}
if (isset($jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::REQUIRED}))
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidget::REQUIRED] = $jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::REQUIRED};
}
if (isset($jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::MAX_VALUE}))
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MAX_VALUE] = $jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::MAX_VALUE};
}
if (isset($jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::MIN_VALUE}))
{
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MIN_VALUE] = $jsonSchema->{UDFWidget::VALIDATION}->{UDFWidgetTpl::MIN_VALUE};
}
}
}
}