From 11d3d7d7c0ee560aab1a972f82f461ff69fb9b10 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 20 Apr 2017 18:28:20 +0200 Subject: [PATCH 01/48] 1st release --- application/config/fhcomplete.php | 1 + application/config/udfmasterschema.json | 71 ++++++++++++++++ application/controllers/system/TestUDF.php | 28 +++++++ application/libraries/WidgetLib.php | 43 ++++++++++ application/models/system/UDF_model.php | 15 ++++ application/views/system/testudf.php | 24 ++++++ application/widgets/UDF_widget.php | 96 ++++++++++++++++++++++ 7 files changed, 278 insertions(+) create mode 100644 application/config/udfmasterschema.json create mode 100644 application/controllers/system/TestUDF.php create mode 100644 application/models/system/UDF_model.php create mode 100644 application/views/system/testudf.php create mode 100644 application/widgets/UDF_widget.php diff --git a/application/config/fhcomplete.php b/application/config/fhcomplete.php index f51185a2d..976a5f433 100755 --- a/application/config/fhcomplete.php +++ b/application/config/fhcomplete.php @@ -208,6 +208,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', diff --git a/application/config/udfmasterschema.json b/application/config/udfmasterschema.json new file mode 100644 index 000000000..fa06dc80d --- /dev/null +++ b/application/config/udfmasterschema.json @@ -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"] +} \ No newline at end of file diff --git a/application/controllers/system/TestUDF.php b/application/controllers/system/TestUDF.php new file mode 100644 index 000000000..ee1f60dbb --- /dev/null +++ b/application/controllers/system/TestUDF.php @@ -0,0 +1,28 @@ +load->library('WidgetLib'); + } + + /** + * + */ + public function index() + { + $data = array ( + 'schema' => 'public', + 'table' => 'tbl_person', + 'field' => 'schuhgroesse' + ); + + $this->load->view('system/testudf', $data); + } +} \ No newline at end of file diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 52fbd0071..7117deedb 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -213,6 +213,38 @@ class WidgetLib return new $class($class, $data, $htmlArgs); } + /** + * + */ + public function UDFWidget($args, $htmlArgs = array()) + { + if (!empty($args[UDF_widget_tpl::SCHEMA_ARG_NAME]) + && !empty($args[UDF_widget_tpl::TABLE_ARG_NAME]) + && !empty($args[UDF_widget_tpl::FIELD_ARG_NAME])) + { + return $this->widget( + UDF_widget_tpl::WIDGET_NAME, + $args, + $htmlArgs + ); + } + else + { + if (empty($args[UDF_widget_tpl::SCHEMA_ARG_NAME])) + { + show_error(UDF_widget_tpl::SCHEMA_ARG_NAME.' parameter is missing!'); + } + if (empty($args[UDF_widget_tpl::TABLE_ARG_NAME])) + { + show_error(UDF_widget_tpl::TABLE_ARG_NAME.' parameter is missing!'); + } + if (empty($args[UDF_widget_tpl::FIELD_ARG_NAME])) + { + show_error(UDF_widget_tpl::FIELD_ARG_NAME.' parameter is missing!'); + } + } + } + /** * Enable cache for all partials with TTL, default TTL is 60 * @param int $ttl @@ -794,4 +826,15 @@ class DropdownWidget extends Widget array_unshift($this->elementsArray->retval, $element); } +} + +/** + * + */ +abstract class UDF_widget_tpl extends Widget +{ + const WIDGET_NAME = 'UDF_widget'; + const SCHEMA_ARG_NAME = 'schema'; + const TABLE_ARG_NAME = 'table'; + const FIELD_ARG_NAME = 'field'; } \ No newline at end of file diff --git a/application/models/system/UDF_model.php b/application/models/system/UDF_model.php new file mode 100644 index 000000000..85bfec8bd --- /dev/null +++ b/application/models/system/UDF_model.php @@ -0,0 +1,15 @@ +dbTable = 'system.tbl_udf'; + $this->pk = array('schema', 'table'); + $this->hasSequence = false; + } +} \ No newline at end of file diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php new file mode 100644 index 000000000..c6635aa1c --- /dev/null +++ b/application/views/system/testudf.php @@ -0,0 +1,24 @@ +load->view("templates/header", array("title" => "UDF")); ?> + + + +
+ UDFWidget: +
+ +
+ widgetlib->UDFWidget( + array( + UDF_widget_tpl::SCHEMA_ARG_NAME => $schema, + UDF_widget_tpl::TABLE_ARG_NAME => $table, + UDF_widget_tpl::FIELD_ARG_NAME => $field + ), + array('name' => 'schuhgroesse', 'id' => 'schuhgroesseUDF') + ); + ?> +
+ + + +load->view("templates/footer"); ?> \ No newline at end of file diff --git a/application/widgets/UDF_widget.php b/application/widgets/UDF_widget.php new file mode 100644 index 000000000..ebe0110f8 --- /dev/null +++ b/application/widgets/UDF_widget.php @@ -0,0 +1,96 @@ +_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('UDF_widget: generic error occurred'); + } + } + else if (!hasData($udfResults)) + { + show_error(sprintf('%s.%s does not contain UDF', $schema, $table)); + } + else + { + $udf = $udfResults->retval[0]; + if (isset($udf->jsons)) + { + $jsonSchema = json_decode($udf->jsons); + if (is_object($jsonSchema)) + { + if (isset($jsonSchema->type)) + { + var_dump($jsonSchema); + + if ($jsonSchema->type == 'checkbox') + { + + } + else if ($jsonSchema->type == 'textfield') + { + + } + else if ($jsonSchema->type == 'textarea') + { + + } + else if ($jsonSchema->type == 'date') + { + + } + else if ($jsonSchema->type == 'dropdown') + { + $dropdownWidget = new DropdownWidget(); + } + else if ($jsonSchema->type == 'multipledropdown') + { + + } + } + else + { + show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); + } + } + 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)); + } + } + } +} \ No newline at end of file From ebd892e333b30ee1c15a3eb7843716c143b729fb Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 21 Apr 2017 19:03:20 +0200 Subject: [PATCH 02/48] 2nd release --- application/libraries/WidgetLib.php | 30 ++++++++++++++++++++++++++ application/views/system/testudf.php | 2 +- application/views/widgets/dropdown.php | 2 +- application/widgets/UDF_widget.php | 5 +++-- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 7117deedb..d74692d0e 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -738,6 +738,36 @@ class DropdownWidget extends Widget private $elementsArray; // Array of elements to be place inside the dropdown + /** + * + */ + public function displayElements($elements, $widgetData) + { + $this->setElementsArray(success($this->convert($elements)), true); + $this->loadDropDownView($widgetData); + + echo $this->content(); + } + + /** + * + */ + public static function convert($elements) + { + $returnArray = array(); + + foreach($elements as $key => $val) + { + $element = new stdClass(); + $element->id = $key; + $element->description = $val; + + array_push($returnArray, $element); + } + + return $returnArray; + } + /** * Loads the dropdown view with all the elements to be displayed */ diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index c6635aa1c..9b6f7b75a 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -14,7 +14,7 @@ UDF_widget_tpl::TABLE_ARG_NAME => $table, UDF_widget_tpl::FIELD_ARG_NAME => $field ), - array('name' => 'schuhgroesse', 'id' => 'schuhgroesseUDF') + array('id' => 'schuhgroesseID', 'name' => 'schuhgroesseName') ); ?> diff --git a/application/views/widgets/dropdown.php b/application/views/widgets/dropdown.php index d511ece11..1c8ad8d15 100644 --- a/application/views/widgets/dropdown.php +++ b/application/views/widgets/dropdown.php @@ -2,7 +2,7 @@ {DropdownWidget::ID_FIELD} == ${DropdownWidget::SELECTED_ELEMENT}) + if ($element->{DropdownWidget::ID_FIELD} === ${DropdownWidget::SELECTED_ELEMENT}) { $selected = 'selected'; } diff --git a/application/widgets/UDF_widget.php b/application/widgets/UDF_widget.php index ebe0110f8..29a8757e2 100644 --- a/application/widgets/UDF_widget.php +++ b/application/widgets/UDF_widget.php @@ -50,7 +50,7 @@ class UDF_widget extends UDF_widget_tpl { if (isset($jsonSchema->type)) { - var_dump($jsonSchema); +// var_dump($jsonSchema); if ($jsonSchema->type == 'checkbox') { @@ -70,7 +70,8 @@ class UDF_widget extends UDF_widget_tpl } else if ($jsonSchema->type == 'dropdown') { - $dropdownWidget = new DropdownWidget(); + $dropdownWidget = new DropdownWidget($this->_name, $this->_args); + $dropdownWidget->displayElements($jsonSchema->listValues->enum, $this->_args); } else if ($jsonSchema->type == 'multipledropdown') { From 0fd34807ca4dd115a8244d312d3b18bb4901125a Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 27 Apr 2017 19:06:36 +0200 Subject: [PATCH 03/48] 3rd release --- application/controllers/system/TestUDF.php | 3 +- application/libraries/WidgetLib.php | 212 ++++++++++++++------- application/views/system/testudf.php | 9 +- application/views/widgets/dropdown.php | 54 +++++- application/widgets/Studiengang_widget.php | 2 +- application/widgets/UDFWidget.php | 165 ++++++++++++++++ application/widgets/UDF_widget.php | 97 ---------- 7 files changed, 362 insertions(+), 180 deletions(-) create mode 100644 application/widgets/UDFWidget.php delete mode 100644 application/widgets/UDF_widget.php diff --git a/application/controllers/system/TestUDF.php b/application/controllers/system/TestUDF.php index ee1f60dbb..087d916b6 100644 --- a/application/controllers/system/TestUDF.php +++ b/application/controllers/system/TestUDF.php @@ -20,7 +20,8 @@ class TestUDF extends VileSci_Controller $data = array ( 'schema' => 'public', 'table' => 'tbl_person', - 'field' => 'schuhgroesse' + 'field' => 'schuhgroesse', + 'selected' => array(1, 2) ); $this->load->view('system/testudf', $data); diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index d74692d0e..f751fcb7d 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -218,29 +218,29 @@ class WidgetLib */ public function UDFWidget($args, $htmlArgs = array()) { - if (!empty($args[UDF_widget_tpl::SCHEMA_ARG_NAME]) - && !empty($args[UDF_widget_tpl::TABLE_ARG_NAME]) - && !empty($args[UDF_widget_tpl::FIELD_ARG_NAME])) + if (!empty($args[UDFWidgetTpl::SCHEMA_ARG_NAME]) + && !empty($args[UDFWidgetTpl::TABLE_ARG_NAME]) + && !empty($args[UDFWidgetTpl::FIELD_ARG_NAME])) { return $this->widget( - UDF_widget_tpl::WIDGET_NAME, + UDFWidgetTpl::WIDGET_NAME, $args, $htmlArgs ); } else { - if (empty($args[UDF_widget_tpl::SCHEMA_ARG_NAME])) + if (empty($args[UDFWidgetTpl::SCHEMA_ARG_NAME])) { - show_error(UDF_widget_tpl::SCHEMA_ARG_NAME.' parameter is missing!'); + show_error(UDFWidgetTpl::SCHEMA_ARG_NAME.' parameter is missing!'); } - if (empty($args[UDF_widget_tpl::TABLE_ARG_NAME])) + if (empty($args[UDFWidgetTpl::TABLE_ARG_NAME])) { - show_error(UDF_widget_tpl::TABLE_ARG_NAME.' parameter is missing!'); + show_error(UDFWidgetTpl::TABLE_ARG_NAME.' parameter is missing!'); } - if (empty($args[UDF_widget_tpl::FIELD_ARG_NAME])) + if (empty($args[UDFWidgetTpl::FIELD_ARG_NAME])) { - show_error(UDF_widget_tpl::FIELD_ARG_NAME.' parameter is missing!'); + show_error(UDFWidgetTpl::FIELD_ARG_NAME.' parameter is missing!'); } } } @@ -692,38 +692,64 @@ class Widget extends Partial */ private function _setHtmlProperties($htmlArgs) { - if (isset($htmlArgs) && is_array($htmlArgs)) + // If $htmlArgs wasn't already stored in $this->_args + if (!isset($this->_args[Widget::HTML_ARG_NAME])) { $this->_args[Widget::HTML_ARG_NAME] = array(); - // Avoids that the elements of a same HTML page have the same name or id + // Avoids that elements in a HTML page have the same name or id $randomIdentifier = uniqid(rand(0, 1000)); + $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $randomIdentifier; + $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $randomIdentifier; - if (isset($htmlArgs[Widget::HTML_ID]) && trim($htmlArgs[Widget::HTML_ID]) != '') + foreach($htmlArgs as $argName => $argValue) { - $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $htmlArgs[Widget::HTML_ID]; - } - else - { - $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $randomIdentifier; - } - - if (isset($htmlArgs[Widget::HTML_NAME]) && trim($htmlArgs[Widget::HTML_NAME]) != '') - { - $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $htmlArgs[Widget::HTML_NAME]; - } - else - { - $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $randomIdentifier; + $this->_args[Widget::HTML_ARG_NAME][$argName] = $argValue; } } } + + /** + * + */ + public static function printAttribute($htmlArgs, $attribute, $isValuePresent = true) + { + if ($attribute != null) + { + if (isset($htmlArgs[$attribute])) + { + if ($isValuePresent === true) + { + $value = $htmlArgs[$attribute]; + + if (is_bool($value)) + { + $value = $value ? 'true' : 'false'; + } + + echo sprintf('%s="%s"', $attribute, $value); + } + else + { + echo $attribute; + } + } + } + } +} + +/** + * + */ +abstract class WidgetTpl extends Widget +{ + abstract public function render($parameters); } /** * It exends the Widget class to represent an HTML dropdown */ -class DropdownWidget extends Widget +class DropdownWidget extends WidgetTpl { // The name of the element of the data array given to the view // this element is an array of elements to be place inside the dropdown @@ -731,59 +757,58 @@ class DropdownWidget extends Widget // Name of the property that will be used to store the value attribute of the option tag const ID_FIELD = 'id'; // Name of the property that will be used to store the value between the option tags - const DESCRIPTION_FIELD = 'description'; // + const DESCRIPTION_FIELD = 'description'; // The name of the element of the data array given to the view // this element is used to tell what element of the dropdown is selected - const SELECTED_ELEMENT = 'selectedElement'; // + const SELECTED_ELEMENT = 'selectedElement'; - private $elementsArray; // Array of elements to be place inside the dropdown + const SIZE = 'size'; // + const MULTIPLE = 'multiple'; // /** * */ - public function displayElements($elements, $widgetData) + public function __construct($name, $args, $htmlArgs = array()) { - $this->setElementsArray(success($this->convert($elements)), true); - $this->loadDropDownView($widgetData); + parent::__construct($name, $args, $htmlArgs); - echo $this->content(); - } - - /** + // + if (!isset($this->_args[DropdownWidget::SELECTED_ELEMENT])) + { + $this->_args[DropdownWidget::SELECTED_ELEMENT] = Widget::HTML_DEFAULT_VALUE; + } + } + + /** * */ - public static function convert($elements) + public function render($parameters) { - $returnArray = array(); + $tmpElements = array(); - foreach($elements as $key => $val) + // + foreach($parameters['elements'] as $key => $val) { $element = new stdClass(); $element->id = $key; $element->description = $val; - array_push($returnArray, $element); + array_push($tmpElements, $element); } - return $returnArray; + $this->setElementsArray( + success($tmpElements), + true, + 'Select a value...', + 'No data found for this UDF' + ); + + $this->loadDropDownView(); + + echo $this->content(); } - - /** - * Loads the dropdown view with all the elements to be displayed - */ - protected function loadDropDownView($widgetData) - { - $widgetData[DropdownWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME] = $this->elementsArray->retval; - - if (!isset($widgetData[DropdownWidget::SELECTED_ELEMENT])) - { - $widgetData[DropdownWidget::SELECTED_ELEMENT] = Widget::HTML_DEFAULT_VALUE; - } - - $this->view('widgets/dropdown', $widgetData); - } - - /** + + /** * Add the correct select to the model used to load a list of elemets for this dropdown * @param model $model the model used to load elements * @param string $idName the name of the field that will used to be the value of the option tag @@ -801,7 +826,7 @@ class DropdownWidget extends Widget ) ); } - + /** * Set the array used to populate the dropdown * @param array $elements list used to populate this dropdown @@ -811,9 +836,11 @@ class DropdownWidget extends Widget * @param string $id value of the attribute value of the empty element */ protected function setElementsArray( - $elements, $emptyElement = false, $stdDescription = '' , $noDataDescription = '' , $id = Widget::HTML_DEFAULT_VALUE + $elements, $emptyElement = false, $stdDescription = '' , $noDataDescription = 'No data found' , $id = Widget::HTML_DEFAULT_VALUE ) { + $tmpElements = array(); + if (isError($elements)) { if (is_object($elements) && isset($elements->retval)) @@ -831,40 +858,85 @@ class DropdownWidget extends Widget } else { - $this->elementsArray = $elements; - if ($emptyElement === true) { - $this->addElementAtBeginning($stdDescription, $noDataDescription, $id); + $tmpElements = $this->addElementAtBeginning( + $elements, + $stdDescription, + $noDataDescription, + $id + ); } + else + { + $tmpElements = $elements->retval; + } + + $this->_args[DropdownWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME] = $tmpElements; } } /** * Adds an element to the beginning of the array */ - protected function addElementAtBeginning($stdDescription, $noDataDescription, $id) + protected function addElementAtBeginning($elements, $stdDescription, $noDataDescription, $id) { $element = new stdClass(); $element->id = $id; $element->description = $stdDescription; - if (!hasData($this->elementsArray)) + if (!hasData($elements)) { $element->description = $noDataDescription; } - array_unshift($this->elementsArray->retval, $element); + array_unshift($elements->retval, $element); + + return $elements->retval; + } + + /** + * Loads the dropdown view with all the elements to be displayed + */ + protected function loadDropDownView() + { + $this->view('widgets/dropdown', $this->_args); } } /** * */ -abstract class UDF_widget_tpl extends Widget +class MultipleDropdownWidget extends DropdownWidget { - const WIDGET_NAME = 'UDF_widget'; + /** + * + */ + public function __construct($name, $args, $htmlArgs = array()) + { + parent::__construct($name, $args, $htmlArgs); + + // + $this->_args[Widget::HTML_ARG_NAME][DropdownWidget::MULTIPLE] = 'multiple'; + } +} + +/** + * + */ +abstract class UDFWidgetTpl extends Widget +{ + const WIDGET_NAME = 'UDFWidget'; const SCHEMA_ARG_NAME = 'schema'; const TABLE_ARG_NAME = 'table'; const FIELD_ARG_NAME = 'field'; + + const TITLE = 'title'; + const DESCRIPTION = 'description'; + const PLACEHOLDER = 'placeholder'; + const DEFAULT_VALUE = 'defaultValue'; + const REGEX = 'regex'; + const REQUIRED = 'required'; + const MAX_VALUE = 'max_value'; + const MIN_VALUE = 'min_value'; } \ No newline at end of file diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index 9b6f7b75a..7c00bfe84 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -10,11 +10,12 @@ widgetlib->UDFWidget( array( - UDF_widget_tpl::SCHEMA_ARG_NAME => $schema, - UDF_widget_tpl::TABLE_ARG_NAME => $table, - UDF_widget_tpl::FIELD_ARG_NAME => $field + UDFWidgetTpl::SCHEMA_ARG_NAME => $schema, + UDFWidgetTpl::TABLE_ARG_NAME => $table, + UDFWidgetTpl::FIELD_ARG_NAME => $field, + DropdownWidget::SELECTED_ELEMENT => $selected ), - array('id' => 'schuhgroesseID', 'name' => 'schuhgroesseName') + array('id' => 'schuhgroesseID', 'name' => 'schuhgroesseName', 'size' => '6') ); ?> diff --git a/application/views/widgets/dropdown.php b/application/views/widgets/dropdown.php index 1c8ad8d15..6efba3c95 100644 --- a/application/views/widgets/dropdown.php +++ b/application/views/widgets/dropdown.php @@ -1,14 +1,54 @@ - + + + + + + + + +> + {DropdownWidget::ID_FIELD} === ${DropdownWidget::SELECTED_ELEMENT}) + + if (is_array($selectedElements)) { - $selected = 'selected'; + foreach($selectedElements as $selectedElement) + { + if ($element->{DropdownWidget::ID_FIELD} === $selectedElement) + { + $selected = 'selected'; + } + } } - ?> + else + { + if ($element->{DropdownWidget::ID_FIELD} === $selectedElements) + { + $selected = 'selected'; + } + } + ?> - + \ No newline at end of file diff --git a/application/widgets/Studiengang_widget.php b/application/widgets/Studiengang_widget.php index e87fa9cd1..a09c865f5 100644 --- a/application/widgets/Studiengang_widget.php +++ b/application/widgets/Studiengang_widget.php @@ -17,6 +17,6 @@ class Studiengang_widget extends DropdownWidget 'No studiengaenge found' ); - $this->loadDropDownView($widgetData); + $this->loadDropDownView(); } } \ No newline at end of file diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php new file mode 100644 index 000000000..92030fe27 --- /dev/null +++ b/application/widgets/UDFWidget.php @@ -0,0 +1,165 @@ +_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)); + } + else + { + $udf = $udfResults->retval[0]; + if (isset($udf->jsons)) + { + $jsonSchema = json_decode($udf->jsons); + if (is_object($jsonSchema)) + { + if (isset($jsonSchema->type)) + { + $this->_render($jsonSchema); + } + else + { + show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); + } + } + 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 _render($jsonSchema) + { +// var_dump($jsonSchema); + + // Description, title and placeholder + if (isset($jsonSchema->title) || isset($jsonSchema->description) || isset($jsonSchema->placeholder)) + { + $this->_ci->load->library('PhrasesLib'); + + if (isset($jsonSchema->title)) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::TITLE] = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->title); + } + + if (isset($jsonSchema->description)) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::DESCRIPTION] = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->description); + } + + if (isset($jsonSchema->placeholder)) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER] = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->placeholder); + } + } + + // Validation + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = null; + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REQUIRED] = 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->validation->regex) && is_array($jsonSchema->validation->regex)) + { + foreach($jsonSchema->validation->regex as $regex) + { + if ($regex->language === 'js') + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = $regex->expression; + } + } + } + + if (isset($jsonSchema->validation->required)) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REQUIRED] = $jsonSchema->validation->required; + } + + if (isset($jsonSchema->validation->{'max-value'})) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MAX_VALUE] = $jsonSchema->validation->{'max-value'}; + } + + if (isset($jsonSchema->validation->{'min-value'})) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MIN_VALUE] = $jsonSchema->validation->{'min-value'}; + } + } + +// var_dump($this->_args); + + // Type + if ($jsonSchema->type == 'checkbox') + { + + } + else if ($jsonSchema->type == 'textfield') + { + + } + else if ($jsonSchema->type == 'textarea') + { + + } + else if ($jsonSchema->type == 'date') + { + + } + else if ($jsonSchema->type == 'dropdown') + { + $dropdownWidget = new DropdownWidget($this->_name, $this->_args); + $dropdownWidget->render(array('elements' => $jsonSchema->listValues->enum)); + } + else if ($jsonSchema->type == 'multipledropdown') + { + $multipleDropdownWidget = new MultipleDropdownWidget($this->_name, $this->_args); + $multipleDropdownWidget->render(array('elements' => $jsonSchema->listValues->enum)); + } + } +} \ No newline at end of file diff --git a/application/widgets/UDF_widget.php b/application/widgets/UDF_widget.php deleted file mode 100644 index 29a8757e2..000000000 --- a/application/widgets/UDF_widget.php +++ /dev/null @@ -1,97 +0,0 @@ -_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('UDF_widget: generic error occurred'); - } - } - else if (!hasData($udfResults)) - { - show_error(sprintf('%s.%s does not contain UDF', $schema, $table)); - } - else - { - $udf = $udfResults->retval[0]; - if (isset($udf->jsons)) - { - $jsonSchema = json_decode($udf->jsons); - if (is_object($jsonSchema)) - { - if (isset($jsonSchema->type)) - { -// var_dump($jsonSchema); - - if ($jsonSchema->type == 'checkbox') - { - - } - else if ($jsonSchema->type == 'textfield') - { - - } - else if ($jsonSchema->type == 'textarea') - { - - } - else if ($jsonSchema->type == 'date') - { - - } - else if ($jsonSchema->type == 'dropdown') - { - $dropdownWidget = new DropdownWidget($this->_name, $this->_args); - $dropdownWidget->displayElements($jsonSchema->listValues->enum, $this->_args); - } - else if ($jsonSchema->type == 'multipledropdown') - { - - } - } - else - { - show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); - } - } - 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)); - } - } - } -} \ No newline at end of file From a6a6592db0fc88cf783f465ecdef0bc831b284fb Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 28 Apr 2017 17:17:31 +0200 Subject: [PATCH 04/48] 4th release --- application/libraries/WidgetLib.php | 4 ++-- application/views/system/testudf.php | 6 +----- application/views/widgets/dropdown.php | 4 ++-- application/widgets/UDFWidget.php | 30 +++++++++++++++++++++++--- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index f751fcb7d..3cca45d72 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -799,7 +799,7 @@ class DropdownWidget extends WidgetTpl $this->setElementsArray( success($tmpElements), true, - 'Select a value...', + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER], 'No data found for this UDF' ); @@ -932,7 +932,7 @@ abstract class UDFWidgetTpl extends Widget const FIELD_ARG_NAME = 'field'; const TITLE = 'title'; - const DESCRIPTION = 'description'; + const LABEL = 'description'; const PLACEHOLDER = 'placeholder'; const DEFAULT_VALUE = 'defaultValue'; const REGEX = 'regex'; diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index 7c00bfe84..d8f3439d4 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -1,10 +1,6 @@ load->view("templates/header", array("title" => "UDF")); ?> - -
- UDFWidget: -
- + load->view("templates/footer"); ?> \ No newline at end of file diff --git a/application/views/widgets/dropdown.php b/application/views/widgets/dropdown.php index 6efba3c95..c5b83603f 100644 --- a/application/views/widgets/dropdown.php +++ b/application/views/widgets/dropdown.php @@ -1,9 +1,9 @@ title)) { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::TITLE] = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->title); + $tmpResult = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->title, 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->description)) { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::DESCRIPTION] = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->description); + $tmpResult = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->description, 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->placeholder)) { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER] = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->placeholder); + $tmpResult = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->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; + } } } From 0456c2c0594da857955a24e1eaa1c7ec12664038 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 2 May 2017 18:55:54 +0200 Subject: [PATCH 05/48] 5th release --- application/libraries/WidgetLib.php | 82 +++----- application/widgets/UDFWidget.php | 312 ++++++++++++++++++---------- 2 files changed, 231 insertions(+), 163 deletions(-) diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 3cca45d72..5577db08c 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -738,18 +738,10 @@ class Widget extends Partial } } -/** - * - */ -abstract class WidgetTpl extends Widget -{ - abstract public function render($parameters); -} - /** * It exends the Widget class to represent an HTML dropdown */ -class DropdownWidget extends WidgetTpl +class DropdownWidget extends Widget { // The name of the element of the data array given to the view // this element is an array of elements to be place inside the dropdown @@ -779,33 +771,12 @@ class DropdownWidget extends WidgetTpl } } - /** - * - */ - public function render($parameters) - { - $tmpElements = array(); - - // - foreach($parameters['elements'] as $key => $val) - { - $element = new stdClass(); - $element->id = $key; - $element->description = $val; - - array_push($tmpElements, $element); - } - - $this->setElementsArray( - success($tmpElements), - true, - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER], - 'No data found for this UDF' - ); - - $this->loadDropDownView(); - - echo $this->content(); + /** + * + */ + public function setMultiple() + { + $this->_args[Widget::HTML_ARG_NAME][DropdownWidget::MULTIPLE] = 'multiple'; } /** @@ -904,21 +875,36 @@ class DropdownWidget extends WidgetTpl } } -/** - * - */ -class MultipleDropdownWidget extends DropdownWidget +class DropdownWidgetUDF extends DropdownWidget { /** * */ - public function __construct($name, $args, $htmlArgs = array()) + public function render($parameters) { - parent::__construct($name, $args, $htmlArgs); + $tmpElements = array(); // - $this->_args[Widget::HTML_ARG_NAME][DropdownWidget::MULTIPLE] = 'multiple'; - } + foreach($parameters as $key => $val) + { + $element = new stdClass(); + $element->id = $key; + $element->description = $val; + + array_push($tmpElements, $element); + } + + $this->setElementsArray( + success($tmpElements), + true, + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER], + 'No data found for this UDF' + ); + + $this->loadDropDownView(); + + echo $this->content(); + } } /** @@ -931,12 +917,12 @@ abstract class UDFWidgetTpl extends Widget const TABLE_ARG_NAME = 'table'; const FIELD_ARG_NAME = 'field'; - const TITLE = 'title'; - const LABEL = 'description'; + const TITLE = 'description'; + const LABEL = 'title'; const PLACEHOLDER = 'placeholder'; const DEFAULT_VALUE = 'defaultValue'; const REGEX = 'regex'; const REQUIRED = 'required'; - const MAX_VALUE = 'max_value'; - const MIN_VALUE = 'min_value'; + const MAX_VALUE = 'max-value'; + const MIN_VALUE = 'min-value'; } \ No newline at end of file diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index 5e81d38f1..4d9f926ed 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -5,12 +5,85 @@ */ class UDFWidget extends UDFWidgetTpl { + const REGEX_LANGUAGE = 'js'; + public function display($widgetData) { $schema = $widgetData[UDFWidgetTpl::SCHEMA_ARG_NAME]; $table = $widgetData[UDFWidgetTpl::TABLE_ARG_NAME]; $field = $widgetData[UDFWidgetTpl::FIELD_ARG_NAME]; + $udfResults = $this->_loadUDF($schema, $table, $field); + 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; + + foreach($jsonSchemasArray as $jsonSchema) + { + if (isset($jsonSchema->name) && $field === $jsonSchema->name) + { + if (isset($jsonSchema->type)) + { + $this->_setAttributesWithPhrases($jsonSchema); + + $this->_setValidationAttributes($jsonSchema); + + $this->_render($jsonSchema); + } + else + { + show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); + } + + $found = true; + break; + } + else + { + if (!isset($jsonSchema->name)) + { + show_error(sprintf('%s.%s: Attribute "name" not present in the json schema', $schema, $table)); + } + } + } + + if (!$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 _loadUDF($schema, $table, $field) + { // Loads UDF model $this->_ci->load->model('system/UDF_model', 'UDFModel'); @@ -40,33 +113,8 @@ class UDFWidget extends UDFWidgetTpl { show_error(sprintf('%s.%s does not contain UDF', $schema, $table)); } - else - { - $udf = $udfResults->retval[0]; - if (isset($udf->jsons)) - { - $jsonSchema = json_decode($udf->jsons); - if (is_object($jsonSchema)) - { - if (isset($jsonSchema->type)) - { - $this->_render($jsonSchema); - } - else - { - show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); - } - } - 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)); - } - } + + return $udfResults; } /** @@ -74,90 +122,6 @@ class UDFWidget extends UDFWidgetTpl */ private function _render($jsonSchema) { -// var_dump($jsonSchema); - - // Description, title and placeholder - if (isset($jsonSchema->title) || isset($jsonSchema->description) || isset($jsonSchema->placeholder)) - { - $this->_ci->load->library('PhrasesLib'); - - if (isset($jsonSchema->title)) - { - $tmpResult = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->title, 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->description)) - { - $tmpResult = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->description, 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->placeholder)) - { - $tmpResult = $this->_ci->phraseslib->getPhrases('core', 'German', $jsonSchema->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; - } - } - } - - // Validation - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = null; - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REQUIRED] = 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->validation->regex) && is_array($jsonSchema->validation->regex)) - { - foreach($jsonSchema->validation->regex as $regex) - { - if ($regex->language === 'js') - { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = $regex->expression; - } - } - } - - if (isset($jsonSchema->validation->required)) - { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REQUIRED] = $jsonSchema->validation->required; - } - - if (isset($jsonSchema->validation->{'max-value'})) - { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MAX_VALUE] = $jsonSchema->validation->{'max-value'}; - } - - if (isset($jsonSchema->validation->{'min-value'})) - { - $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MIN_VALUE] = $jsonSchema->validation->{'min-value'}; - } - } - -// var_dump($this->_args); - // Type if ($jsonSchema->type == 'checkbox') { @@ -177,13 +141,131 @@ class UDFWidget extends UDFWidgetTpl } else if ($jsonSchema->type == 'dropdown') { - $dropdownWidget = new DropdownWidget($this->_name, $this->_args); - $dropdownWidget->render(array('elements' => $jsonSchema->listValues->enum)); + $this->_renderDropdown($jsonSchema); } else if ($jsonSchema->type == 'multipledropdown') { - $multipleDropdownWidget = new MultipleDropdownWidget($this->_name, $this->_args); - $multipleDropdownWidget->render(array('elements' => $jsonSchema->listValues->enum)); + $this->_renderDropdown($jsonSchema, true); + } + } + + /** + * + */ + private function _renderDropdown($jsonSchema, $multiple = false) + { + $dropdownWidgetUDF = new DropdownWidgetUDF($this->_name, $this->_args); + + $paramters = array(); + + if (isset($jsonSchema->listValues->enum)) + { + $paramters = $jsonSchema->listValues->enum; + } + else if (isset($jsonSchema->listValues->sql)) + { + $paramters = $jsonSchema->listValues->sql; + } + + if ($multiple) + { + $dropdownWidgetUDF->setMultiple(); + } + + $dropdownWidgetUDF->render($paramters); + } + + /** + * + */ + 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('core', 'German', $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('core', 'German', $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('core', 'German', $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][UDFWidgetTpl::REGEX] = null; + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REQUIRED] = 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->validation->regex) && is_array($jsonSchema->validation->regex)) + { + foreach($jsonSchema->validation->regex as $regex) + { + if ($regex->language === UDFWidget::REGEX_LANGUAGE) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REGEX] = $regex->expression; + } + } + } + + if (isset($jsonSchema->validation->required)) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::REQUIRED] = $jsonSchema->validation->required; + } + + if (isset($jsonSchema->validation->{UDFWidgetTpl::MAX_VALUE})) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MAX_VALUE] = $jsonSchema->validation->{UDFWidgetTpl::MAX_VALUE}; + } + + if (isset($jsonSchema->validation->{UDFWidgetTpl::MIN_VALUE})) + { + $this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::MIN_VALUE] = $jsonSchema->validation->{UDFWidgetTpl::MIN_VALUE}; + } } } } \ No newline at end of file From dcedac961783cddf55747b374b0908d67726d0c8 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 4 May 2017 15:08:39 +0200 Subject: [PATCH 06/48] 6th release --- application/libraries/WidgetLib.php | 47 +++++++++++++++++++++++++++++ application/widgets/UDFWidget.php | 26 +++++++++++++--- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 5577db08c..15c90cfbd 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -875,6 +875,9 @@ class DropdownWidget extends Widget } } +/** + * + */ class DropdownWidgetUDF extends DropdownWidget { /** @@ -907,6 +910,50 @@ class DropdownWidgetUDF extends DropdownWidget } } +/** + * It exends the Widget class to represent an HTML textarea + */ +class TextareaWidget extends Widget +{ + const TEXT = 'text'; // + const ROWS = 'rows'; // + const COLS = 'cols'; // + + /** + * + */ + protected function setText($text) + { + $this->_args[DropdownWidget::TEXT] = $text; + } + + /** + * + */ + protected function loadTextareaView() + { + $this->view('widgets/textarea', $this->_args); + } +} + +/** + * + */ +class TextareaWidgetUDF extends TextareaWidget +{ + /** + * + */ + public function render($parameters) + { + $this->setText($parameters); + + $this->loadTextareaView(); + + echo $this->content(); + } +} + /** * */ diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index 4d9f926ed..a2686729a 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -133,7 +133,7 @@ class UDFWidget extends UDFWidgetTpl } else if ($jsonSchema->type == 'textarea') { - + $this->_renderTextarea($jsonSchema); } else if ($jsonSchema->type == 'date') { @@ -156,15 +156,15 @@ class UDFWidget extends UDFWidgetTpl { $dropdownWidgetUDF = new DropdownWidgetUDF($this->_name, $this->_args); - $paramters = array(); + $parameters = array(); if (isset($jsonSchema->listValues->enum)) { - $paramters = $jsonSchema->listValues->enum; + $parameters = $jsonSchema->listValues->enum; } else if (isset($jsonSchema->listValues->sql)) { - $paramters = $jsonSchema->listValues->sql; + $parameters = $jsonSchema->listValues->sql; } if ($multiple) @@ -172,7 +172,23 @@ class UDFWidget extends UDFWidgetTpl $dropdownWidgetUDF->setMultiple(); } - $dropdownWidgetUDF->render($paramters); + $dropdownWidgetUDF->render($parameters); + } + + /** + * + */ + private function _renderTextarea($jsonSchema) + { + $textareaUDF = new TextareaUDF($this->_name, $this->_args); + + $text = ''; + if (isset($jsonSchema->defaultValue)) + { + $text = $jsonSchema->defaultValue; + } + + $textareaUDF->render($text); } /** From bc4ff5c8e9fdd2f1115725310aefd54a465ff5c0 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 4 May 2017 15:09:06 +0200 Subject: [PATCH 07/48] 6th release --- application/views/widgets/textarea.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 application/views/widgets/textarea.php diff --git a/application/views/widgets/textarea.php b/application/views/widgets/textarea.php new file mode 100644 index 000000000..c8508670f --- /dev/null +++ b/application/views/widgets/textarea.php @@ -0,0 +1,24 @@ + + + + + \ No newline at end of file From 6a11cc09cf908d16dbbf4d7862b1784501fcf5c1 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 5 May 2017 18:45:28 +0200 Subject: [PATCH 08/48] 7th release --- application/controllers/system/TestUDF.php | 7 +- application/libraries/WidgetLib.php | 192 +++++++++++++++++++-- application/models/system/UDF_model.php | 27 +++ application/views/system/testudf.php | 85 ++++++++- application/views/widgets/checkbox.php | 40 +++++ application/views/widgets/textarea.php | 6 +- application/views/widgets/textfield.php | 21 +++ application/widgets/UDFWidget.php | 53 +++++- 8 files changed, 396 insertions(+), 35 deletions(-) create mode 100644 application/views/widgets/checkbox.php create mode 100644 application/views/widgets/textfield.php diff --git a/application/controllers/system/TestUDF.php b/application/controllers/system/TestUDF.php index 087d916b6..08ad72e1b 100644 --- a/application/controllers/system/TestUDF.php +++ b/application/controllers/system/TestUDF.php @@ -17,12 +17,7 @@ class TestUDF extends VileSci_Controller */ public function index() { - $data = array ( - 'schema' => 'public', - 'table' => 'tbl_person', - 'field' => 'schuhgroesse', - 'selected' => array(1, 2) - ); + $data = array(); $this->load->view('system/testudf', $data); } diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 15c90cfbd..bb4a46566 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -853,12 +853,12 @@ class DropdownWidget extends Widget protected function addElementAtBeginning($elements, $stdDescription, $noDataDescription, $id) { $element = new stdClass(); - $element->id = $id; - $element->description = $stdDescription; + $element->{DropdownWidget::ID_FIELD} = $id; + $element->{DropdownWidget::DESCRIPTION_FIELD} = $stdDescription; if (!hasData($elements)) { - $element->description = $noDataDescription; + $element->{DropdownWidget::DESCRIPTION_FIELD} = $noDataDescription; } array_unshift($elements->retval, $element); @@ -888,13 +888,35 @@ class DropdownWidgetUDF extends DropdownWidget $tmpElements = array(); // - foreach($parameters as $key => $val) + foreach($parameters as $parameter) { - $element = new stdClass(); - $element->id = $key; - $element->description = $val; - - array_push($tmpElements, $element); + // + if ((is_array($parameter) && count($parameter) == 2) + || (is_string($parameter) || is_numeric($parameter)) + || (is_object($parameter) && isset($parameter->{PARENT::ID_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD}))) + { + $element = new stdClass(); // + // + if (is_array($parameter) && count($parameter) == 2) + { + $element->{PARENT::ID_FIELD} = $parameter[0]; // + $element->{PARENT::DESCRIPTION_FIELD} = $parameter[1]; // + } + // + else if (is_string($parameter) || is_numeric($parameter)) + { + $element->{PARENT::ID_FIELD} = $parameter; // + $element->{PARENT::DESCRIPTION_FIELD} = $parameter; // + } + // + else if (is_object($parameter) && isset($parameter->{PARENT::ID_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD})) + { + $element->{PARENT::ID_FIELD} = $parameter->{PARENT::ID_FIELD}; // + $element->{PARENT::DESCRIPTION_FIELD} = $parameter->{PARENT::DESCRIPTION_FIELD}; // + } + + array_push($tmpElements, $element); // + } } $this->setElementsArray( @@ -924,7 +946,7 @@ class TextareaWidget extends Widget */ protected function setText($text) { - $this->_args[DropdownWidget::TEXT] = $text; + $this->_args[TextareaWidget::TEXT] = $text; } /** @@ -946,7 +968,14 @@ class TextareaWidgetUDF extends TextareaWidget */ public function render($parameters) { - $this->setText($parameters); + if ($parameters != null) + { + $this->setText($parameters); + } + else + { + $this->setText(''); + } $this->loadTextareaView(); @@ -954,6 +983,147 @@ class TextareaWidgetUDF extends TextareaWidget } } +/** + * It exends the Widget class to represent an HTML textarea + */ +class TextfieldWidget extends Widget +{ + const VALUE = 'text'; // + const SIZE = 'size'; // + + /** + * + */ + protected function setValue($value) + { + $this->_args[TextfieldWidget::VALUE] = $value; + } + + /** + * + */ + protected function loadTextfieldView() + { + $this->view('widgets/textfield', $this->_args); + } +} + +/** + * + */ +class TextfieldWidgetUDF extends TextfieldWidget +{ + /** + * + */ + public function render($parameters) + { + if ($parameters != null) + { + $this->setValue($parameters); + } + else + { + $this->setValue(''); + } + + $this->loadTextfieldView(); + + echo $this->content(); + } +} + +/** + * It exends the Widget class to represent an HTML dropdown + */ +class CheckboxWidget extends Widget +{ + // The name of the element of the data array given to the view + // this element is an array of elements to be place inside the dropdown + const WIDGET_DATA_VALUES_ARRAY_NAME = 'VALUES_ARRAY'; + // The name of the element of the data array given to the view + // this element is used to tell what element of the dropdown is selected + const CHECKED_VALUE = 'checkedValue'; + + /** + * + */ + public function __construct($name, $args, $htmlArgs = array()) + { + parent::__construct($name, $args, $htmlArgs); + + // + if (!isset($this->_args[CheckboxWidget::CHECKED_VALUE])) + { + $this->_args[CheckboxWidget::CHECKED_VALUE] = Widget::HTML_DEFAULT_VALUE; + } + } + + /** + * + */ + protected function setValuesArray($values) + { + if (isError($values)) + { + if (is_object($values) && isset($values->retval)) + { + show_error($values->retval); + } + else if (is_string($values)) + { + show_error($values); + } + else + { + show_error('Generic error occurred'); + } + } + else + { + $this->_args[CheckboxWidget::WIDGET_DATA_VALUES_ARRAY_NAME] = $values->retval; + } + } + + /** + * + */ + protected function loadCheckboxView() + { + $this->view('widgets/checkbox', $this->_args); + } +} + +/** + * + */ +class CheckboxWidgetUDF extends CheckboxWidget +{ + /** + * + */ + public function render($parameters) + { + $tmpValues = array(); + + // + foreach($parameters as $parameter) + { + // + if (is_string($parameter) || is_numeric($parameter)) + { + array_push($tmpValues, $parameter); // + } + } + + $this->setValuesArray(success($tmpValues)); + + $this->loadCheckboxView(); + + echo $this->content(); + } +} + /** * */ diff --git a/application/models/system/UDF_model.php b/application/models/system/UDF_model.php index 85bfec8bd..e33cdd275 100644 --- a/application/models/system/UDF_model.php +++ b/application/models/system/UDF_model.php @@ -12,4 +12,31 @@ class UDF_model extends DB_Model $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'); + } + } } \ No newline at end of file diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index d8f3439d4..1d5815446 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -6,12 +6,87 @@ widgetlib->UDFWidget( array( - UDFWidgetTpl::SCHEMA_ARG_NAME => $schema, - UDFWidgetTpl::TABLE_ARG_NAME => $table, - UDFWidgetTpl::FIELD_ARG_NAME => $field, - DropdownWidget::SELECTED_ELEMENT => $selected + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'schuhgroesse', + DropdownWidget::SELECTED_ELEMENT => array(42, 44) ), - array('id' => 'schuhgroesseID', 'name' => 'schuhgroesseName', 'size' => '6') + array('id' => 'schuhgroesseID', 'name' => 'schuhgroesseName', 'size' => '9') + ); + ?> + + +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'headSize' + ), + array('id' => 'headSizeID', 'name' => 'headSizeName') + ); + ?> +
+ +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'bellySize' + ), + array('id' => 'bellySizeID', 'name' => 'bellySizeName') + ); + ?> +
+ +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'nickname' + ), + array('id' => 'nicknameID', 'name' => 'nicknameName') + ); + ?> +
+ +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'age' + ), + array('id' => 'ageID', 'name' => 'ageName') + ); + ?> +
+ +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'agree' + ), + array('id' => 'agreeID', 'name' => 'agreeName') ); ?>
diff --git a/application/views/widgets/checkbox.php b/application/views/widgets/checkbox.php new file mode 100644 index 000000000..531aca9c3 --- /dev/null +++ b/application/views/widgets/checkbox.php @@ -0,0 +1,40 @@ + + + +
+ + + +> + + + value="" + + > + + +
\ No newline at end of file diff --git a/application/views/widgets/textarea.php b/application/views/widgets/textarea.php index c8508670f..44ee51953 100644 --- a/application/views/widgets/textarea.php +++ b/application/views/widgets/textarea.php @@ -8,17 +8,15 @@ - \ No newline at end of file diff --git a/application/views/widgets/textfield.php b/application/views/widgets/textfield.php new file mode 100644 index 000000000..ac0d22547 --- /dev/null +++ b/application/views/widgets/textfield.php @@ -0,0 +1,21 @@ + + + + + + + + + + + value="" +> \ No newline at end of file diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index a2686729a..920be185c 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -125,11 +125,11 @@ class UDFWidget extends UDFWidgetTpl // Type if ($jsonSchema->type == 'checkbox') { - + $this->_renderCheckbox($jsonSchema); } else if ($jsonSchema->type == 'textfield') { - + $this->_renderTextfield($jsonSchema); } else if ($jsonSchema->type == 'textarea') { @@ -155,16 +155,21 @@ class UDFWidget extends UDFWidgetTpl private function _renderDropdown($jsonSchema, $multiple = false) { $dropdownWidgetUDF = new DropdownWidgetUDF($this->_name, $this->_args); - $parameters = array(); + // if (isset($jsonSchema->listValues->enum)) { $parameters = $jsonSchema->listValues->enum; } + // else if (isset($jsonSchema->listValues->sql)) { - $parameters = $jsonSchema->listValues->sql; + $queryResult = $this->UDFModel->execQuery($jsonSchema->listValues->sql); + if (hasData($queryResult)) + { + $parameters = $queryResult->retval; + } } if ($multiple) @@ -180,15 +185,45 @@ class UDFWidget extends UDFWidgetTpl */ private function _renderTextarea($jsonSchema) { - $textareaUDF = new TextareaUDF($this->_name, $this->_args); + $textareaUDF = new TextareaWidgetUDF($this->_name, $this->_args); - $text = ''; - if (isset($jsonSchema->defaultValue)) + $textareaUDF->render(null); + } + + /** + * + */ + private function _renderTextfield($jsonSchema) + { + $textareaUDF = new TextfieldWidgetUDF($this->_name, $this->_args); + + $textareaUDF->render(null); + } + + /** + * + */ + private function _renderCheckbox($jsonSchema) + { + $checkboxWidgetUDF = new CheckboxWidgetUDF($this->_name, $this->_args); + $parameters = array(); + + // + if (isset($jsonSchema->listValues->enum)) { - $text = $jsonSchema->defaultValue; + $parameters = $jsonSchema->listValues->enum; + } + // + else if (isset($jsonSchema->listValues->sql)) + { + $queryResult = $this->UDFModel->execQuery($jsonSchema->listValues->sql); + if (hasData($queryResult)) + { + $parameters = $queryResult->retval; + } } - $textareaUDF->render($text); + $checkboxWidgetUDF->render($parameters); } /** From 51ccee4d1626fb259117f2e7ead120dd6e1ecba0 Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 8 May 2017 11:39:21 +0200 Subject: [PATCH 09/48] 8th release --- application/libraries/WidgetLib.php | 44 ++++++++++++++++++++------ application/views/system/testudf.php | 30 ++++++++++++++++++ application/views/widgets/checkbox.php | 12 +++---- application/widgets/UDFWidget.php | 6 ++++ 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index bb4a46566..cd139adb7 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -1040,10 +1040,14 @@ class CheckboxWidget extends Widget { // The name of the element of the data array given to the view // this element is an array of elements to be place inside the dropdown - const WIDGET_DATA_VALUES_ARRAY_NAME = 'VALUES_ARRAY'; + const WIDGET_DATA_ELEMENTS_ARRAY_NAME = 'ELEMENTS_ARRAY'; + // Name of the property that will be used to store the value attribute of the option tag + const VALUE_FIELD = 'value'; + // Name of the property that will be used to store the value between the option tags + const DESCRIPTION_FIELD = 'description'; // The name of the element of the data array given to the view // this element is used to tell what element of the dropdown is selected - const CHECKED_VALUE = 'checkedValue'; + const CHECKED_ELEMENT = 'checkedElement'; /** * @@ -1053,9 +1057,9 @@ class CheckboxWidget extends Widget parent::__construct($name, $args, $htmlArgs); // - if (!isset($this->_args[CheckboxWidget::CHECKED_VALUE])) + if (!isset($this->_args[CheckboxWidget::CHECKED_ELEMENT])) { - $this->_args[CheckboxWidget::CHECKED_VALUE] = Widget::HTML_DEFAULT_VALUE; + $this->_args[CheckboxWidget::CHECKED_ELEMENT] = Widget::HTML_DEFAULT_VALUE; } } @@ -1081,7 +1085,7 @@ class CheckboxWidget extends Widget } else { - $this->_args[CheckboxWidget::WIDGET_DATA_VALUES_ARRAY_NAME] = $values->retval; + $this->_args[CheckboxWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME] = $values->retval; } } @@ -1104,19 +1108,41 @@ class CheckboxWidgetUDF extends CheckboxWidget */ public function render($parameters) { - $tmpValues = array(); + $tmpElements = array(); // foreach($parameters as $parameter) { // - if (is_string($parameter) || is_numeric($parameter)) + if ((is_array($parameter) && count($parameter) == 2) + || (is_string($parameter) || is_numeric($parameter)) + || (is_object($parameter) && isset($parameter->{PARENT::VALUE_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD}))) { - array_push($tmpValues, $parameter); // + $element = new stdClass(); // + // + if (is_array($parameter) && count($parameter) == 2) + { + $element->{PARENT::VALUE_FIELD} = $parameter[0]; // + $element->{PARENT::DESCRIPTION_FIELD} = $parameter[1]; // + } + // + else if (is_string($parameter) || is_numeric($parameter)) + { + $element->{PARENT::VALUE_FIELD} = $parameter; // + $element->{PARENT::DESCRIPTION_FIELD} = $parameter; // + } + // + else if (is_object($parameter) && isset($parameter->{PARENT::VALUE_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD})) + { + $element->{PARENT::VALUE_FIELD} = $parameter->{PARENT::VALUE_FIELD}; // + $element->{PARENT::DESCRIPTION_FIELD} = $parameter->{PARENT::DESCRIPTION_FIELD}; // + } + + array_push($tmpElements, $element); // } } - $this->setValuesArray(success($tmpValues)); + $this->setValuesArray(success($tmpElements)); $this->loadCheckboxView(); diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index 1d5815446..dca677e4f 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -91,6 +91,36 @@ ?> +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'disagree' + ), + array('id' => 'disagreeID', 'name' => 'disagreeName') + ); + ?> +
+ +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'dunno' + ), + array('id' => 'dunnoID', 'name' => 'dunnoName') + ); + ?> +
+ load->view("templates/footer"); ?> \ No newline at end of file diff --git a/application/views/widgets/checkbox.php b/application/views/widgets/checkbox.php index 531aca9c3..b73ebb24a 100644 --- a/application/views/widgets/checkbox.php +++ b/application/views/widgets/checkbox.php @@ -15,14 +15,14 @@ > {CheckboxWidget::VALUE_FIELD} == $checkedElement) { $checked = 'checked'; } @@ -30,10 +30,10 @@ - value="" + value="{CheckboxWidget::VALUE_FIELD}; ?>" > - + {CheckboxWidget::DESCRIPTION_FIELD}; ?> diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index 920be185c..0f6fe934c 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -205,6 +205,12 @@ class UDFWidget extends UDFWidgetTpl */ private function _renderCheckbox($jsonSchema) { + // + if (!isset($this->_args[CheckboxWidget::CHECKED_ELEMENT])) + { + $this->_args[CheckboxWidget::CHECKED_ELEMENT] = $jsonSchema->defaultValue; + } + $checkboxWidgetUDF = new CheckboxWidgetUDF($this->_name, $this->_args); $parameters = array(); From a5aea17fa6cd3f7adae8b3b80d516c8c70e821ba Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 8 May 2017 18:46:38 +0200 Subject: [PATCH 10/48] UDF fields are present in json output as other fields --- application/core/DB_Model.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 369d8bb46..04ec7c67a 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -9,6 +9,8 @@ class DB_Model extends FHC_Model const PGSQL_BOOLEAN_FALSE = 'f'; const MODEL_POSTFIX = '_model'; const DEFAULT_SCHEMA = 'public'; + const UDF_FIELD_NAME = 'udf_values'; + const UDF_FIELD_TYPE = 'jsonb'; protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ... protected $pk; // Name of the PrimaryKey for DB-Update, Load, ... @@ -744,8 +746,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(); @@ -787,6 +790,19 @@ class DB_Model extends FHC_Model { $tmpResult->{$toBeConverted->name} = $this->pgBoolPhp($tmpResult->{$toBeConverted->name}); } + // UDF + else if ($toBeConverted->type == DB_Model::UDF_FIELD_TYPE) + { + $jsonValues = json_decode($tmpResult->{$toBeConverted->name}); + if ($jsonValues != null) + { + foreach($jsonValues as $key => $value) + { + $tmpResult->{$key} = $value; + } + } + unset($tmpResult->{$toBeConverted->name}); + } } } // Returns DB data as an array From e143aa8815212e52417d663610d7b79cb49986d5 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 9 May 2017 18:14:18 +0200 Subject: [PATCH 11/48] 1st preview --- application/controllers/system/TestUDF.php | 9 +- application/core/DB_Model.php | 25 +++++ application/libraries/WidgetLib.php | 41 +++++-- application/views/system/testudf.php | 113 ++----------------- application/views/widgets/checkbox.php | 4 +- application/views/widgets/dropdown.php | 4 +- application/views/widgets/textarea.php | 4 +- application/views/widgets/textfield.php | 4 +- application/widgets/UDFWidget.php | 119 ++++++++++++++++----- 9 files changed, 178 insertions(+), 145 deletions(-) diff --git a/application/controllers/system/TestUDF.php b/application/controllers/system/TestUDF.php index 08ad72e1b..40ed5a04b 100644 --- a/application/controllers/system/TestUDF.php +++ b/application/controllers/system/TestUDF.php @@ -10,6 +10,9 @@ class TestUDF extends VileSci_Controller // Loads the widget library $this->load->library('WidgetLib'); + + // + $this->load->model('person/Person_model', 'PersonModel'); } /** @@ -17,7 +20,11 @@ class TestUDF extends VileSci_Controller */ public function index() { - $data = array(); + $person = $this->PersonModel->load(1); + + $data = array( + 'udfs' => $this->PersonModel->getUDFs() + ); $this->load->view('system/testudf', $data); } diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 04ec7c67a..bf16a777f 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -17,6 +17,8 @@ class DB_Model extends FHC_Model 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; // + function __construct($dbTable = null, $pk = null, $hasSequence = true) { parent::__construct(); @@ -24,6 +26,7 @@ class DB_Model extends FHC_Model $this->pk = $pk; $this->hasSequence = $hasSequence; $this->load->database(); + $this->UDFs = array(); } /** --------------------------------------------------------------- @@ -704,6 +707,27 @@ class DB_Model extends FHC_Model return $result; } + /** + * + */ + public function getUDFs() + { + return $this->UDFs; + } + + /** + * + */ + public function getUDF($udf) + { + if (isset($this->UDFs[$udf])) + { + return $this->UDFs[$udf]; + } + + return null; + } + /** * Checks if the caller is entitled to perform this operation with this right */ @@ -799,6 +823,7 @@ class DB_Model extends FHC_Model foreach($jsonValues as $key => $value) { $tmpResult->{$key} = $value; + $this->UDFs[$key] = $value; // } } unset($tmpResult->{$toBeConverted->name}); diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index cd139adb7..6a0b8cdb8 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -218,10 +218,14 @@ class WidgetLib */ public function UDFWidget($args, $htmlArgs = array()) { - if (!empty($args[UDFWidgetTpl::SCHEMA_ARG_NAME]) - && !empty($args[UDFWidgetTpl::TABLE_ARG_NAME]) - && !empty($args[UDFWidgetTpl::FIELD_ARG_NAME])) + if (!empty($args[UDFWidgetTpl::SCHEMA_ARG_NAME]) && !empty($args[UDFWidgetTpl::TABLE_ARG_NAME])) { + // + if (empty($args[UDFWidgetTpl::FIELD_ARG_NAME]) && !isset($htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK])) + { + $htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK] = true; + } + return $this->widget( UDFWidgetTpl::WIDGET_NAME, $args, @@ -238,10 +242,6 @@ class WidgetLib { show_error(UDFWidgetTpl::TABLE_ARG_NAME.' parameter is missing!'); } - if (empty($args[UDFWidgetTpl::FIELD_ARG_NAME])) - { - show_error(UDFWidgetTpl::FIELD_ARG_NAME.' parameter is missing!'); - } } } @@ -646,6 +646,8 @@ class Widget extends Partial const HTML_NAME = 'name'; // HTML name attribute const HTML_ID = 'id'; // HTML id attribute + const EXTERNAL_BLOCK = 'externalBlock'; // + /** * It gets also the htmlArgs array as parameter, it will be used to set the HTML properties */ @@ -736,6 +738,30 @@ class Widget extends Partial } } } + + /** + * + */ + public static function printStartBlock($htmlArgs) + { + if (isset($htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK]) + && $htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK] === true) + { + echo '
'; + } + } + + /** + * + */ + public static function printEndBlock($htmlArgs) + { + if (isset($htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK]) + && $htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK] === true) + { + echo '
'; + } + } } /** @@ -1159,6 +1185,7 @@ abstract class UDFWidgetTpl extends Widget const SCHEMA_ARG_NAME = 'schema'; const TABLE_ARG_NAME = 'table'; const FIELD_ARG_NAME = 'field'; + const UDFS_ARG_NAME = 'udfs'; const TITLE = 'description'; const LABEL = 'title'; diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index dca677e4f..1791e4190 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -2,20 +2,12 @@ -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'schuhgroesse', - DropdownWidget::SELECTED_ELEMENT => array(42, 44) - ), - array('id' => 'schuhgroesseID', 'name' => 'schuhgroesseName', 'size' => '9') - ); - ?> -
+
+
+
+
+

@@ -24,99 +16,8 @@ array( UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'headSize' - ), - array('id' => 'headSizeID', 'name' => 'headSizeName') - ); - ?> -
- -
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'bellySize' - ), - array('id' => 'bellySizeID', 'name' => 'bellySizeName') - ); - ?> -
- -
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'nickname' - ), - array('id' => 'nicknameID', 'name' => 'nicknameName') - ); - ?> -
- -
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'age' - ), - array('id' => 'ageID', 'name' => 'ageName') - ); - ?> -
- -
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'agree' - ), - array('id' => 'agreeID', 'name' => 'agreeName') - ); - ?> -
- -
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'disagree' - ), - array('id' => 'disagreeID', 'name' => 'disagreeName') - ); - ?> -
- -
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'dunno' - ), - array('id' => 'dunnoID', 'name' => 'dunnoName') + UDFWidgetTpl::UDFS_ARG_NAME => $udfs + ) ); ?>
diff --git a/application/views/widgets/checkbox.php b/application/views/widgets/checkbox.php index b73ebb24a..8fb459850 100644 --- a/application/views/widgets/checkbox.php +++ b/application/views/widgets/checkbox.php @@ -1,3 +1,4 @@ + - \ No newline at end of file + + \ No newline at end of file diff --git a/application/views/widgets/dropdown.php b/application/views/widgets/dropdown.php index c5b83603f..6494d180f 100644 --- a/application/views/widgets/dropdown.php +++ b/application/views/widgets/dropdown.php @@ -1,3 +1,4 @@ + - \ No newline at end of file + + \ No newline at end of file diff --git a/application/views/widgets/textarea.php b/application/views/widgets/textarea.php index 44ee51953..920211b69 100644 --- a/application/views/widgets/textarea.php +++ b/application/views/widgets/textarea.php @@ -1,3 +1,4 @@ + > - \ No newline at end of file + + \ No newline at end of file diff --git a/application/views/widgets/textfield.php b/application/views/widgets/textfield.php index ac0d22547..ce01bd463 100644 --- a/application/views/widgets/textfield.php +++ b/application/views/widgets/textfield.php @@ -1,3 +1,4 @@ + value="" -> \ No newline at end of file +> + \ No newline at end of file diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index 0f6fe934c..91aef73df 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -11,9 +11,13 @@ class UDFWidget extends UDFWidgetTpl { $schema = $widgetData[UDFWidgetTpl::SCHEMA_ARG_NAME]; $table = $widgetData[UDFWidgetTpl::TABLE_ARG_NAME]; - $field = $widgetData[UDFWidgetTpl::FIELD_ARG_NAME]; - $udfResults = $this->_loadUDF($schema, $table, $field); + 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]; @@ -22,6 +26,7 @@ class UDFWidget extends UDFWidgetTpl $jsonSchemas = json_decode($udf->jsons); if (is_object($jsonSchemas) || is_array($jsonSchemas)) { + // if (is_object($jsonSchemas)) { $jsonSchemasArray = array($jsonSchemas); @@ -31,38 +36,45 @@ class UDFWidget extends UDFWidgetTpl $jsonSchemasArray = $jsonSchemas; } - $found = false; + $found = false; // + $this->_sortJsonSchemas($jsonSchemasArray); // + + // foreach($jsonSchemasArray as $jsonSchema) { - if (isset($jsonSchema->name) && $field === $jsonSchema->name) + // + if (!isset($jsonSchema->type)) { - if (isset($jsonSchema->type)) - { - $this->_setAttributesWithPhrases($jsonSchema); - - $this->_setValidationAttributes($jsonSchema); - - $this->_render($jsonSchema); - } - else - { - show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); - } - - $found = true; + show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); break; } - else + if (!isset($jsonSchema->name)) { - if (!isset($jsonSchema->name)) + show_error(sprintf('%s.%s: Attribute "name" not present in the json schema', $schema, $table)); + break; + } + + // + if ((isset($field) && $field == $jsonSchema->name) || !isset($field)) + { + $this->_setAttributesWithPhrases($jsonSchema); + + $this->_setValidationAttributes($jsonSchema); + + $this->_render($jsonSchema); + + // + if (isset($field) && $field == $jsonSchema->name) { - show_error(sprintf('%s.%s: Attribute "name" not present in the json schema', $schema, $table)); + $found = true; + break; } } } - if (!$found) + // + if (isset($field) && !$found) { show_error(sprintf('%s.%s: No schema present for field: %s', $schema, $table, $field)); } @@ -82,7 +94,33 @@ class UDFWidget extends UDFWidgetTpl /** * */ - private function _loadUDF($schema, $table, $field) + 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'); @@ -154,6 +192,13 @@ class UDFWidget extends UDFWidgetTpl */ private function _renderDropdown($jsonSchema, $multiple = false) { + + if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + { + $this->_args[DropdownWidget::SELECTED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + } + $dropdownWidgetUDF = new DropdownWidgetUDF($this->_name, $this->_args); $parameters = array(); @@ -186,8 +231,15 @@ class UDFWidget extends UDFWidgetTpl private function _renderTextarea($jsonSchema) { $textareaUDF = new TextareaWidgetUDF($this->_name, $this->_args); - - $textareaUDF->render(null); + $text = null; + + if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + { + $text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + } + + $textareaUDF->render($text); } /** @@ -196,8 +248,15 @@ class UDFWidget extends UDFWidgetTpl private function _renderTextfield($jsonSchema) { $textareaUDF = new TextfieldWidgetUDF($this->_name, $this->_args); - - $textareaUDF->render(null); + $text = null; + + if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + { + $text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + } + + $textareaUDF->render($text); } /** @@ -205,6 +264,12 @@ class UDFWidget extends UDFWidgetTpl */ private function _renderCheckbox($jsonSchema) { + if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + { + $this->_args[CheckboxWidget::CHECKED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + } + // if (!isset($this->_args[CheckboxWidget::CHECKED_ELEMENT])) { From f092867602f2b8cd2e651a2853e3738527491256 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 10 May 2017 09:54:23 +0200 Subject: [PATCH 12/48] 2nd preview --- application/controllers/system/TestUDF.php | 4 +++- application/libraries/WidgetLib.php | 6 ++++-- application/views/system/testudf.php | 13 +++++++++++++ application/widgets/UDFWidget.php | 11 +++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/application/controllers/system/TestUDF.php b/application/controllers/system/TestUDF.php index 40ed5a04b..19a01c8cc 100644 --- a/application/controllers/system/TestUDF.php +++ b/application/controllers/system/TestUDF.php @@ -20,7 +20,9 @@ class TestUDF extends VileSci_Controller */ public function index() { - $person = $this->PersonModel->load(1); + $person_id = $this->input->get('person_id'); + + $person = $this->PersonModel->load($person_id); $data = array( 'udfs' => $this->PersonModel->getUDFs() diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 6a0b8cdb8..7e01209a7 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -647,6 +647,8 @@ class Widget extends Partial const HTML_ID = 'id'; // HTML id attribute const EXTERNAL_BLOCK = 'externalBlock'; // + const EXTERNAL_START_BLOCK_HTML_TAG = '
'; // + const EXTERNAL_END_BLOCK_HTML_TAG = '
'; // /** * It gets also the htmlArgs array as parameter, it will be used to set the HTML properties @@ -747,7 +749,7 @@ class Widget extends Partial if (isset($htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK]) && $htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK] === true) { - echo '
'; + echo Widget::EXTERNAL_START_BLOCK_HTML_TAG; } } @@ -759,7 +761,7 @@ class Widget extends Partial if (isset($htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK]) && $htmlArgs[Widget::HTML_ARG_NAME][Widget::EXTERNAL_BLOCK] === true) { - echo '
'; + echo Widget::EXTERNAL_END_BLOCK_HTML_TAG; } } } diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index 1791e4190..d51dd7549 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -2,6 +2,19 @@ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::FIELD_ARG_NAME => 'schuhgroesse', + DropdownWidget::SELECTED_ELEMENT => $udfs['schuhgroesse'] + ), + array('name' => 'schuhgroesseName', 'id' => 'schuhgroesseId') + ); + ?> +


diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index 91aef73df..f4fc66c74 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -62,6 +62,8 @@ class UDFWidget extends UDFWidgetTpl $this->_setValidationAttributes($jsonSchema); + $this->_setNameAndId($jsonSchema); + $this->_render($jsonSchema); // @@ -91,6 +93,15 @@ class UDFWidget extends UDFWidgetTpl } } + /** + * + */ + private function _setNameAndId($jsonSchema) + { + $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $jsonSchema->name; + $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $jsonSchema->name; + } + /** * */ From 30c31211aa2caea918185f6179aa0d82eb0a5edb Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 8 Jun 2017 18:26:32 +0200 Subject: [PATCH 13/48] Added insert and update for UDFs, first version --- application/core/DB_Model.php | 202 ++++++++++++++++++++++++++++------ 1 file changed, 169 insertions(+), 33 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index bf16a777f..692d87065 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -11,14 +11,18 @@ class DB_Model extends FHC_Model const DEFAULT_SCHEMA = 'public'; const UDF_FIELD_NAME = 'udf_values'; const UDF_FIELD_TYPE = 'jsonb'; + const UDF_FIELD_PREFIX = 'udf_'; 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; // + protected $UDFs; // Contains the udfs + /** + * Constructor + */ function __construct($dbTable = null, $pk = null, $hasSequence = true) { parent::__construct(); @@ -29,7 +33,7 @@ class DB_Model extends FHC_Model $this->UDFs = array(); } - /** --------------------------------------------------------------- + /** * Insert Data into DB-Table * * @param array $data DataArray for Insert @@ -44,6 +48,9 @@ class DB_Model extends FHC_Model // Checks rights if ($isEntitled = $this->_isEntitled(PermissionLib::INSERT_RIGHT)) return $isEntitled; + // UDFs + $this->_manageUDFInsert($data); + // DB-INSERT if ($this->db->insert($this->dbTable, $data)) { @@ -73,11 +80,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) { @@ -94,8 +104,53 @@ class DB_Model extends FHC_Model else return error($this->db->error(), FHC_DB_ERROR); } - - /** --------------------------------------------------------------- + + /** + * Manage UDFs on update + */ + private function _manageUDFUpdate($id, &$data) + { + // Checks if this table has udf + if ($this->_hasUDF()) + { + $dbVersionArray = explode('.', $this->db->version()); + if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 5) + { + $this->addSelect(DB_Model::UDF_FIELD_NAME); + $result = $this->load($id); + if (hasData($result)) + { + // Get udf values from $data & clean udf values from $data + // Must be performed here because the load method populates UDFs too + $this->_popUDF($data); + + $jsonb = (array)$result->retval[0]; + if (count($jsonb) > 0) + { + foreach($this->UDFs as $key => $val) + { + if (isset($jsonb[$key])) + { + $jsonb[$key] = $val; + } + } + + $jsonEncodedUDFs = json_encode($jsonb); + if ($jsonEncodedUDFs !== false) + { + $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + } + } + } + } + else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 5) + { + // TODO + } + } + } + + /** * Update Data in DB-Table * * @param string $id PK for DB-Table @@ -112,7 +167,15 @@ class DB_Model extends FHC_Model // Checks rights if ($isEntitled = $this->_isEntitled(PermissionLib::UPDATE_RIGHT)) return $isEntitled; - + + // UDFs + $this->_manageUDFUpdate($id, $data); + + var_dump($this->UDFs); + var_dump($data); + + exit; + // DB-UPDATE // Check for composite Primary Key if (is_array($id)) @@ -130,7 +193,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 @@ -164,7 +227,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 @@ -201,7 +264,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 @@ -224,7 +287,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 * @@ -292,7 +355,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(); @@ -369,7 +432,7 @@ class DB_Model extends FHC_Model return $result; } - /** --------------------------------------------------------------- + /** * Add a table to join with * * @return void @@ -385,7 +448,7 @@ class DB_Model extends FHC_Model return success(true); } - /** --------------------------------------------------------------- + /** * Add order clause * * @return void @@ -401,7 +464,7 @@ class DB_Model extends FHC_Model return success(true); } - /** --------------------------------------------------------------- + /** * Add select clause * * @return void @@ -417,7 +480,7 @@ class DB_Model extends FHC_Model return success(true); } - /** --------------------------------------------------------------- + /** * Add distinct clause * * @return void @@ -427,7 +490,7 @@ class DB_Model extends FHC_Model $this->db->distinct(); } - /** --------------------------------------------------------------- + /** * Add limit clause * * @return void @@ -450,7 +513,7 @@ class DB_Model extends FHC_Model return success(true); } - /** --------------------------------------------------------------- + /** * Add a table in the from clause * * @return void @@ -473,7 +536,7 @@ class DB_Model extends FHC_Model return success(true); } - /** --------------------------------------------------------------- + /** * Add one or more fields in the group by clause * * @return void @@ -493,7 +556,7 @@ class DB_Model extends FHC_Model return success(true); } - /** --------------------------------------------------------------- + /** * Reset the query builder state * * @return void @@ -503,7 +566,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 don’t have to @@ -515,7 +578,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 @@ -538,7 +601,7 @@ class DB_Model extends FHC_Model return $val; } - /** --------------------------------------------------------------- + /** * Convert PG-Array to PHP-Array * * @param string $s PG-String to convert @@ -628,7 +691,30 @@ class DB_Model extends FHC_Model return $result; } - /** --------------------------------------------------------------- + /** + * Return 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; + } + + // ---------------------------------------------------------------------------- + + /** * Invalid ID * * @param array $i Array with indexes. @@ -707,25 +793,75 @@ class DB_Model extends FHC_Model return $result; } + // ---------------------------------------------------------------------------- + /** - * + * Checks if for $dbTable are present UDFs */ - public function getUDFs() + private function _hasUDF() { - return $this->UDFs; + $hasUDF = false; + + $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]; + } + + $udfResults = $this->UDFModel->loadWhere( + array( + 'schema' => $schema, + 'table' => $table + ) + ); + + if (hasData($udfResults)) + { + $hasUDF = true; + } + + return $hasUDF; } /** - * + * Move UDFs from $data to $UDFs */ - public function getUDF($udf) + private function _popUDF(&$data) { - if (isset($this->UDFs[$udf])) + foreach($data as $key => $val) { - return $this->UDFs[$udf]; + if (substr($key, 0, 4) == DB_Model::UDF_FIELD_PREFIX) + { + $this->UDFs[$key] = $val; + unset($data[$key]); // remove from data + } + } + } + + /** + * Manage UDFs on insert + */ + private function _manageUDFInsert(&$data) + { + // Checks if this table has udf + if ($this->_hasUDF()) + { + // Get udf values from $data & clean udf values from $data + $this->_popUDF($data); + // Encode UDFs to json and store them into $data + $jsonEncodedUDFs = json_encode($this->UDFs); + if ($jsonEncodedUDFs !== false) + { + $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + } } - - return null; } /** @@ -823,7 +959,7 @@ class DB_Model extends FHC_Model foreach($jsonValues as $key => $value) { $tmpResult->{$key} = $value; - $this->UDFs[$key] = $value; // + $this->UDFs[$key] = $value; // Stores the udfs in UDFs } } unset($tmpResult->{$toBeConverted->name}); From 294c65a9bf9950e73ead97ab79e4596bdb97100a Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 9 Jun 2017 15:45:33 +0200 Subject: [PATCH 14/48] Added controller UDF to read UDFs definitions UDFs update&insert second version --- application/controllers/api/v1/system/UDF.php | 56 +++++++++++ application/core/DB_Model.php | 97 +++++++++---------- 2 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 application/controllers/api/v1/system/UDF.php diff --git a/application/controllers/api/v1/system/UDF.php b/application/controllers/api/v1/system/UDF.php new file mode 100644 index 000000000..d9bf96422 --- /dev/null +++ b/application/controllers/api/v1/system/UDF.php @@ -0,0 +1,56 @@ +load->model('system/UDF_model', 'UDFModel'); + } + + /** + * @return void + */ + public function getUDF() + { + $schema = $this->get('schema'); + $table = $this->get('table'); + + if (isset($schema) || isset($table)) + { + $result = $this->UDFModel->loadWhere( + array( + 'schema' => $schema, + 'table' => $table + ) + ); + + $this->response($result, REST_Controller::HTTP_OK); + } + else + { + $result = $this->UDFModel->load(); + + $this->response($result, REST_Controller::HTTP_OK); + } + } +} \ No newline at end of file diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 692d87065..9c78d4ce0 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -97,7 +97,7 @@ 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()); @@ -105,51 +105,6 @@ class DB_Model extends FHC_Model return error($this->db->error(), FHC_DB_ERROR); } - /** - * Manage UDFs on update - */ - private function _manageUDFUpdate($id, &$data) - { - // Checks if this table has udf - if ($this->_hasUDF()) - { - $dbVersionArray = explode('.', $this->db->version()); - if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 5) - { - $this->addSelect(DB_Model::UDF_FIELD_NAME); - $result = $this->load($id); - if (hasData($result)) - { - // Get udf values from $data & clean udf values from $data - // Must be performed here because the load method populates UDFs too - $this->_popUDF($data); - - $jsonb = (array)$result->retval[0]; - if (count($jsonb) > 0) - { - foreach($this->UDFs as $key => $val) - { - if (isset($jsonb[$key])) - { - $jsonb[$key] = $val; - } - } - - $jsonEncodedUDFs = json_encode($jsonb); - if ($jsonEncodedUDFs !== false) - { - $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; - } - } - } - } - else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 5) - { - // TODO - } - } - } - /** * Update Data in DB-Table * @@ -171,11 +126,6 @@ class DB_Model extends FHC_Model // UDFs $this->_manageUDFUpdate($id, $data); - var_dump($this->UDFs); - var_dump($data); - - exit; - // DB-UPDATE // Check for composite Primary Key if (is_array($id)) @@ -864,6 +814,51 @@ class DB_Model extends FHC_Model } } + /** + * Manage UDFs on update + */ + private function _manageUDFUpdate($id, &$data) + { + // Checks if this table has udf + if ($this->_hasUDF()) + { + $dbVersionArray = explode('.', $this->db->version()); + if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 4) // If postgresql version is <= 9.4 + { + $this->addSelect(DB_Model::UDF_FIELD_NAME); + $result = $this->load($id); + if (hasData($result)) + { + // Get udf values from $data & clean udf values from $data + // Must be performed here because the load method populates UDFs too + $this->_popUDF($data); + + $jsonb = (array)$result->retval[0]; // convert the result to an array + if (count($jsonb) > 0) // if UDFs are present + { + foreach($this->UDFs as $key => $val) + { + if (isset($jsonb[$key])) + { + $jsonb[$key] = $val; + } + } + + $jsonEncodedUDFs = json_encode($jsonb); // encode to json + if ($jsonEncodedUDFs !== false) + { + $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + } + } + } + } + else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 4) // If postgresql version is > 9.4 + { + // TODO: use jsonb_set + } + } + } + /** * Checks if the caller is entitled to perform this operation with this right */ From 3706131ed797e727a185d20f082db85d758b98ee Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 12 Jun 2017 13:53:57 +0200 Subject: [PATCH 15/48] Use udf_ as prefix --- application/views/system/testudf.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php index d51dd7549..1d3c50f77 100644 --- a/application/views/system/testudf.php +++ b/application/views/system/testudf.php @@ -8,8 +8,8 @@ array( UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'schuhgroesse', - DropdownWidget::SELECTED_ELEMENT => $udfs['schuhgroesse'] + UDFWidgetTpl::FIELD_ARG_NAME => 'udf_schuhgroesse', + DropdownWidget::SELECTED_ELEMENT => $udfs['udf_schuhgroesse'] ), array('name' => 'schuhgroesseName', 'id' => 'schuhgroesseId') ); From 86a1eb876eb84b663044ab42df06b4969fd6962c Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 14 Jun 2017 15:24:08 +0200 Subject: [PATCH 16/48] UDF update bug fix --- application/core/DB_Model.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 9c78d4ce0..78b2113a9 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -838,10 +838,7 @@ class DB_Model extends FHC_Model { foreach($this->UDFs as $key => $val) { - if (isset($jsonb[$key])) - { - $jsonb[$key] = $val; - } + $jsonb[$key] = $val; } $jsonEncodedUDFs = json_encode($jsonb); // encode to json From e8d395f4f10a829122e4f9bab991b7dba5fd0d05 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 14 Jun 2017 16:18:43 +0200 Subject: [PATCH 17/48] UDF update and insert bug fix --- application/core/DB_Model.php | 100 +++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 78b2113a9..45b10a841 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -752,6 +752,19 @@ class DB_Model extends FHC_Model { $hasUDF = false; + if (hasData($this->_getUDF())) + { + $hasUDF = true; + } + + return $hasUDF; + } + + /** + * Returns all the UDF for this table + */ + private function _getUDF() + { $this->load->model('system/UDF_model', 'UDFModel'); $schema = DB_Model::DEFAULT_SCHEMA; @@ -765,6 +778,7 @@ class DB_Model extends FHC_Model $table = $tmpArray[1]; } + $this->UDFModel->addSelect('jsons'); $udfResults = $this->UDFModel->loadWhere( array( 'schema' => $schema, @@ -772,12 +786,7 @@ class DB_Model extends FHC_Model ) ); - if (hasData($udfResults)) - { - $hasUDF = true; - } - - return $hasUDF; + return $udfResults; } /** @@ -796,17 +805,35 @@ class DB_Model extends FHC_Model } /** - * Manage UDFs on insert + * Manage UDFs */ - private function _manageUDFInsert(&$data) + private function _manageUDF(&$data) { - // Checks if this table has udf - if ($this->_hasUDF()) + $udfs = $this->_getUDF(); + if (hasData($udfs)) { // Get udf values from $data & clean udf values from $data + // Must be performed here because the load method populates UDFs too $this->_popUDF($data); - // Encode UDFs to json and store them into $data - $jsonEncodedUDFs = json_encode($this->UDFs); + + $jsonb = array(); + + $udfsDecoded = json_decode($udfs->retval[0]->jsons); + + for($i = 0; $i < count($udfsDecoded); $i++) + { + $udfDescription = $udfsDecoded[$i]; + + foreach($this->UDFs as $key => $val) + { + if ($udfDescription->name == $key) + { + $jsonb[$key] = $val; + } + } + } + + $jsonEncodedUDFs = json_encode($jsonb); // encode to json if ($jsonEncodedUDFs !== false) { $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; @@ -814,46 +841,33 @@ class DB_Model extends FHC_Model } } + /** + * Manage UDFs on insert + */ + private function _manageUDFInsert(&$data) + { + $this->_manageUDF($data); + } + /** * Manage UDFs on update */ private function _manageUDFUpdate($id, &$data) { - // Checks if this table has udf - if ($this->_hasUDF()) + $dbVersionArray = explode('.', $this->db->version()); + if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 4) // If postgresql version is <= 9.4 { - $dbVersionArray = explode('.', $this->db->version()); - if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 4) // If postgresql version is <= 9.4 + $this->addSelect(DB_Model::UDF_FIELD_NAME); + $result = $this->load($id); + if (hasData($result)) // if UDFs are present { - $this->addSelect(DB_Model::UDF_FIELD_NAME); - $result = $this->load($id); - if (hasData($result)) - { - // Get udf values from $data & clean udf values from $data - // Must be performed here because the load method populates UDFs too - $this->_popUDF($data); - - $jsonb = (array)$result->retval[0]; // convert the result to an array - if (count($jsonb) > 0) // if UDFs are present - { - foreach($this->UDFs as $key => $val) - { - $jsonb[$key] = $val; - } - - $jsonEncodedUDFs = json_encode($jsonb); // encode to json - if ($jsonEncodedUDFs !== false) - { - $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; - } - } - } - } - else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 4) // If postgresql version is > 9.4 - { - // TODO: use jsonb_set + $this->_manageUDF($data); } } + else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 4) // If postgresql version is > 9.4 + { + // TODO: use jsonb_set + } } /** From 3ac8d7d604f154f9643c9506401996020a0dd74d Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 19 Jun 2017 12:15:28 +0200 Subject: [PATCH 18/48] - Added parameter decode to API /system/UDF/UDF to decode json - UDF bugfix: now it checks if a table has the column udf_values before performing an insert or update --- application/controllers/api/v1/system/UDF.php | 29 ++++- application/core/DB_Model.php | 116 +++++++----------- 2 files changed, 72 insertions(+), 73 deletions(-) diff --git a/application/controllers/api/v1/system/UDF.php b/application/controllers/api/v1/system/UDF.php index d9bf96422..bb8a039f9 100644 --- a/application/controllers/api/v1/system/UDF.php +++ b/application/controllers/api/v1/system/UDF.php @@ -32,9 +32,12 @@ class UDF extends APIv1_Controller */ 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( @@ -43,14 +46,32 @@ class UDF extends APIv1_Controller 'table' => $table ) ); - - $this->response($result, REST_Controller::HTTP_OK); } else { $result = $this->UDFModel->load(); - - $this->response($result, REST_Controller::HTTP_OK); + } + + if ($decode) + { + $this->_jsonDecodeResult($result); + } + + $this->response($result, REST_Controller::HTTP_OK); + } + + /** + * + */ + 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); + } } } } \ No newline at end of file diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 45b10a841..87acddee3 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -12,6 +12,7 @@ class DB_Model extends FHC_Model const UDF_FIELD_NAME = 'udf_values'; const UDF_FIELD_TYPE = 'jsonb'; const UDF_FIELD_PREFIX = 'udf_'; + const UDF_QUERY_CHECK = 'SELECT * FROM %s WHERE 0 = 1'; protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ... protected $pk; // Name of the PrimaryKey for DB-Update, Load, ... @@ -49,7 +50,7 @@ class DB_Model extends FHC_Model if ($isEntitled = $this->_isEntitled(PermissionLib::INSERT_RIGHT)) return $isEntitled; // UDFs - $this->_manageUDFInsert($data); + $this->_manageUDF($data); // DB-INSERT if ($this->db->insert($this->dbTable, $data)) @@ -124,7 +125,7 @@ class DB_Model extends FHC_Model if ($isEntitled = $this->_isEntitled(PermissionLib::UPDATE_RIGHT)) return $isEntitled; // UDFs - $this->_manageUDFUpdate($id, $data); + $this->_manageUDF($data); // DB-UPDATE // Check for composite Primary Key @@ -662,6 +663,24 @@ class DB_Model extends FHC_Model return null; } + /** + * Checks if this tables has the field udf_values + */ + public function hasUDF() + { + $hasUDF = true; + + // Workaround to get metadata from this table + $result = $this->db->query(sprintf(DB_Model::UDF_QUERY_CHECK, $this->dbTable)); + // If udf_values is not found in the list of filds of this table + if (array_search(DB_Model::UDF_FIELD_NAME, $result->list_fields()) === false) + { + $hasUDF = false; + } + + return $hasUDF; + } + // ---------------------------------------------------------------------------- /** @@ -745,25 +764,10 @@ class DB_Model extends FHC_Model // ---------------------------------------------------------------------------- - /** - * Checks if for $dbTable are present UDFs - */ - private function _hasUDF() - { - $hasUDF = false; - - if (hasData($this->_getUDF())) - { - $hasUDF = true; - } - - return $hasUDF; - } - /** * Returns all the UDF for this table */ - private function _getUDF() + private function _getUDFParamters() { $this->load->model('system/UDF_model', 'UDFModel'); @@ -792,7 +796,7 @@ class DB_Model extends FHC_Model /** * Move UDFs from $data to $UDFs */ - private function _popUDF(&$data) + private function _popUDFParameters(&$data) { foreach($data as $key => $val) { @@ -809,64 +813,38 @@ class DB_Model extends FHC_Model */ private function _manageUDF(&$data) { - $udfs = $this->_getUDF(); - if (hasData($udfs)) + if ($this->hasUDF()) // Checks if this table has UDFs { - // Get udf values from $data & clean udf values from $data - // Must be performed here because the load method populates UDFs too - $this->_popUDF($data); - - $jsonb = array(); - - $udfsDecoded = json_decode($udfs->retval[0]->jsons); - - for($i = 0; $i < count($udfsDecoded); $i++) + $udfs = $this->_getUDFParamters(); + if (hasData($udfs)) { - $udfDescription = $udfsDecoded[$i]; + // Get udf values from $data & clean udf values from $data + // Must be performed here because the load method populates UDFs too + $this->_popUDFParameters($data); - foreach($this->UDFs as $key => $val) + $jsonb = array(); + + $udfsDecoded = json_decode($udfs->retval[0]->jsons); + + for($i = 0; $i < count($udfsDecoded); $i++) { - if ($udfDescription->name == $key) + $udfDescription = $udfsDecoded[$i]; + + foreach($this->UDFs as $key => $val) { - $jsonb[$key] = $val; + if ($udfDescription->name == $key) + { + $jsonb[$key] = $val; + } } } + + $jsonEncodedUDFs = json_encode($jsonb); // encode to json + if ($jsonEncodedUDFs !== false) + { + $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + } } - - $jsonEncodedUDFs = json_encode($jsonb); // encode to json - if ($jsonEncodedUDFs !== false) - { - $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; - } - } - } - - /** - * Manage UDFs on insert - */ - private function _manageUDFInsert(&$data) - { - $this->_manageUDF($data); - } - - /** - * Manage UDFs on update - */ - private function _manageUDFUpdate($id, &$data) - { - $dbVersionArray = explode('.', $this->db->version()); - if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 4) // If postgresql version is <= 9.4 - { - $this->addSelect(DB_Model::UDF_FIELD_NAME); - $result = $this->load($id); - if (hasData($result)) // if UDFs are present - { - $this->_manageUDF($data); - } - } - else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 4) // If postgresql version is > 9.4 - { - // TODO: use jsonb_set } } From b73bf1dd325ea432f69fafda5a9f4df5021a159b Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 19 Jun 2017 12:39:08 +0200 Subject: [PATCH 19/48] - Added method listFields to DB_Model - Added method fieldExists to DB_Model --- application/controllers/api/v1/system/UDF.php | 2 +- application/core/DB_Model.php | 40 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/application/controllers/api/v1/system/UDF.php b/application/controllers/api/v1/system/UDF.php index bb8a039f9..517ef305d 100644 --- a/application/controllers/api/v1/system/UDF.php +++ b/application/controllers/api/v1/system/UDF.php @@ -61,7 +61,7 @@ class UDF extends APIv1_Controller } /** - * + * Decode to json the column jsons for every result set */ private function _jsonDecodeResult(&$result) { diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 87acddee3..11c748582 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -12,7 +12,7 @@ class DB_Model extends FHC_Model const UDF_FIELD_NAME = 'udf_values'; const UDF_FIELD_TYPE = 'jsonb'; const UDF_FIELD_PREFIX = 'udf_'; - const UDF_QUERY_CHECK = 'SELECT * FROM %s WHERE 0 = 1'; + const QUERY_LIST_FIELDS = 'SELECT * FROM %s WHERE 0 = 1'; protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ... protected $pk; // Name of the PrimaryKey for DB-Update, Load, ... @@ -664,21 +664,45 @@ class DB_Model extends FHC_Model } /** - * Checks if this tables has the field udf_values + * Checks if this table has the field udf_values */ public function hasUDF() { - $hasUDF = true; + 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::UDF_QUERY_CHECK, $this->dbTable)); - // If udf_values is not found in the list of filds of this table - if (array_search(DB_Model::UDF_FIELD_NAME, $result->list_fields()) === false) + $result = $this->db->query(sprintf(DB_Model::QUERY_LIST_FIELDS, $this->dbTable)); + + if (is_object($result)) { - $hasUDF = false; + $listFields = $result->list_fields(); } - return $hasUDF; + 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; } // ---------------------------------------------------------------------------- From 950da84cf3305d9e262f54d0262543bead007162 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 21 Jun 2017 11:05:20 +0200 Subject: [PATCH 20/48] - Added constant EXIT_VALIDATION_UDF into constants.php - Added message 'fhc_'.EXIT_VALIDATION_UDF into fhc_model_lang - Added method _validateUDFs into DB_Model - Renamed _manageUDF to _manageUDFs into DB_Model - Renamed _getUDFParamters to _getUDFs into DB_Model - Introduced first version of UDF validation --- application/config/constants.php | 6 +- application/core/DB_Model.php | 118 ++++++++++++++++-- application/language/de-AT/fhc_model_lang.php | 14 +-- 3 files changed, 116 insertions(+), 22 deletions(-) diff --git a/application/config/constants.php b/application/config/constants.php index 1746157a9..9d5092b74 100755 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -101,5 +101,7 @@ 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', 9); // UDF validation has been failed + +define('EXIT_AUTO_MIN', 1000); // lowest automatically-assigned error code +define('EXIT_AUTO_MAX', 2000); // highest automatically-assigned error code \ No newline at end of file diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 11c748582..728365185 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -19,19 +19,24 @@ class DB_Model extends FHC_Model 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 + protected $UDFs; // Contains the UDFs /** * Constructor */ function __construct($dbTable = null, $pk = null, $hasSequence = true) { + // Call parent constructor parent::__construct(); + + // Set properties $this->dbTable = $dbTable; $this->pk = $pk; $this->hasSequence = $hasSequence; - $this->load->database(); $this->UDFs = array(); + + // Loads DB conns and confs + $this->load->database(); } /** @@ -50,7 +55,7 @@ class DB_Model extends FHC_Model if ($isEntitled = $this->_isEntitled(PermissionLib::INSERT_RIGHT)) return $isEntitled; // UDFs - $this->_manageUDF($data); + if (isError($validate = $this->_manageUDFs($data))) return $validate; // DB-INSERT if ($this->db->insert($this->dbTable, $data)) @@ -125,7 +130,7 @@ class DB_Model extends FHC_Model if ($isEntitled = $this->_isEntitled(PermissionLib::UPDATE_RIGHT)) return $isEntitled; // UDFs - $this->_manageUDF($data); + if (isError($validate = $this->_manageUDFs($data))) return $validate; // DB-UPDATE // Check for composite Primary Key @@ -643,7 +648,7 @@ class DB_Model extends FHC_Model } /** - * Return UDFs + * Return $this->UDFs */ public function getUDFs() { @@ -791,7 +796,7 @@ class DB_Model extends FHC_Model /** * Returns all the UDF for this table */ - private function _getUDFParamters() + private function _getUDFs() { $this->load->model('system/UDF_model', 'UDFModel'); @@ -832,14 +837,84 @@ class DB_Model extends FHC_Model } } + /** + * Validates UDF value + */ + private function _validateUDFs($udfDescription, $udfValue) + { + $isValid = true; + + $tmpUdfValues = $udfValue; + if (!is_array($udfValue)) + { + $tmpUdfValues = array($udfValue); + } + + // + $validation = null; + if (isset($udfDescription->validation)) + { + $validation = $udfDescription->validation; + } + + // + if ($validation != null) + { + foreach($tmpUdfValues as $udfValIndx => $udfVal) + { + // + if (isset($validation->{'max-value'}) && $udfVal > $validation->{'max-value'}) + { + $isValid = false; + break; + } + + // + if (isset($validation->{'min-value'}) && $udfVal < $validation->{'min-value'}) + { + $isValid = false; + break; + } + + // + if (isset($validation->regex) && is_array($validation->regex)) + { + foreach($validation->regex as $regexIndx => $regex) + { + if ($regex->language == 'php') + { + if (preg_match($regex->expression, $udfVal) != 1) + { + $isValid = false; + break; + } + } + } + } + } + } + + // + if ($isValid === true) + { + return success(true); + } + else + { + return error($udfDescription->name, EXIT_VALIDATION_UDF); + } + } + /** * Manage UDFs */ - private function _manageUDF(&$data) + private function _manageUDFs(&$data) { + $validate = error('manageUDFs', EXIT_DATABASE); + if ($this->hasUDF()) // Checks if this table has UDFs { - $udfs = $this->_getUDFParamters(); + $udfs = $this->_getUDFs(); if (hasData($udfs)) { // Get udf values from $data & clean udf values from $data @@ -858,18 +933,35 @@ class DB_Model extends FHC_Model { if ($udfDescription->name == $key) { - $jsonb[$key] = $val; + // Validation of UDF parameter value + $validate = $this->_validateUDFs($udfDescription, $val); + + // If validation is ok copy the value + if (isSuccess($validate)) + { + $jsonb[$key] = $val; + } + else // otherwise stop the elaboration + { + break; + } } } } - $jsonEncodedUDFs = json_encode($jsonb); // encode to json - if ($jsonEncodedUDFs !== false) + // If validation was ok + if (isSuccess($validate)) { - $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + $jsonEncodedUDFs = json_encode($jsonb); // encode to json + if ($jsonEncodedUDFs !== false) + { + $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + } } } } + + return $validate; } /** @@ -967,7 +1059,7 @@ class DB_Model extends FHC_Model foreach($jsonValues as $key => $value) { $tmpResult->{$key} = $value; - $this->UDFs[$key] = $value; // Stores the udfs in UDFs + $this->UDFs[$key] = $value; // Stores the UDFs in $this->UDFs } } unset($tmpResult->{$toBeConverted->name}); diff --git a/application/language/de-AT/fhc_model_lang.php b/application/language/de-AT/fhc_model_lang.php index 4acf23341..68932dd4a 100644 --- a/application/language/de-AT/fhc_model_lang.php +++ b/application/language/de-AT/fhc_model_lang.php @@ -1,9 +1,9 @@ - Date: Wed, 21 Jun 2017 17:41:25 +0200 Subject: [PATCH 21/48] Migration script 3.3: - Creates table system.tbl_udf - Adds column udf_values to tables tbl_person and tbl_prestudent in public schema --- system/dbupdate_3.3.php | 70 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/system/dbupdate_3.3.php b/system/dbupdate_3.3.php index 0e7772306..003515c6c 100644 --- a/system/dbupdate_3.3.php +++ b/system/dbupdate_3.3.php @@ -268,6 +268,72 @@ if($result = $db->db_query("SELECT view_definition FROM information_schema.views } } +// Creates table system.tbl_udf if it doesn't exist and grants privileges +if(!$result = @$db->db_query("SELECT 1 FROM system.tbl_udf LIMIT 1")) +{ + $qry = ' + CREATE TABLE system.tbl_udf ( + "schema" VARCHAR(32) NOT NULL, + "table" VARCHAR(128) NOT NULL, + "jsons" JSONB NOT NULL, + CONSTRAINT tbl_udf_pkey PRIMARY KEY("schema", "table") + );'; + if(!$db->db_query($qry)) + echo 'system.tbl_udf: '.$db->db_last_error().'
'; + else + echo '
system.tbl_udf table created'; + + $qry = 'COMMENT ON COLUMN system.tbl_udf.schema IS \'Schema of the table\';'; + if(!$db->db_query($qry)) + echo 'Adding comment to system.tbl_udf.schema: '.$db->db_last_error().'
'; + else + echo '
Added comment to system.tbl_udf.schema'; + + $qry = 'COMMENT ON COLUMN system.tbl_udf.table IS \'Table name\';'; + if(!$db->db_query($qry)) + echo 'Adding comment to system.tbl_udf.table: '.$db->db_last_error().'
'; + else + echo '
Added comment to system.tbl_udf.table'; + + $qry = 'COMMENT ON COLUMN system.tbl_udf.jsons IS \'JSON schema\';'; + if(!$db->db_query($qry)) + echo 'Adding comment to system.tbl_udf.jsons: '.$db->db_last_error().'
'; + else + echo '
Added comment to system.tbl_udf.jsons'; + + $qry = 'GRANT SELECT ON TABLE system.tbl_udf TO web;'; + if(!$db->db_query($qry)) + echo 'system.tbl_udf: '.$db->db_last_error().'
'; + else + echo '
Granted privileges to web on system.tbl_udf'; + + $qry = 'GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE system.tbl_udf TO vilesci;'; + if(!$db->db_query($qry)) + echo 'system.tbl_udf: '.$db->db_last_error().'
'; + else + echo '
Granted privileges to vilesci on system.tbl_udf'; +} + +// Add column udf_values to public.tbl_person +if(!$result = @$db->db_query("SELECT udf_values FROM public.tbl_person LIMIT 1")) +{ + $qry = 'ALTER TABLE public.tbl_person ADD COLUMN udf_values JSONB;'; + if(!$db->db_query($qry)) + echo 'public.tbl_person: '.$db->db_last_error().'
'; + else + echo '
Added column udf_values to table public.tbl_person'; +} + +// Add column udf_values to public.tbl_prestudent +if(!$result = @$db->db_query("SELECT udf_values FROM public.tbl_prestudent LIMIT 1")) +{ + $qry = 'ALTER TABLE public.tbl_prestudent ADD COLUMN udf_values JSONB;'; + if(!$db->db_query($qry)) + echo 'public.tbl_prestudent: '.$db->db_last_error().'
'; + else + echo '
Added column udf_values to table public.tbl_prestudent'; +} + // *** Pruefung und hinzufuegen der neuen Attribute und Tabellen echo '

Pruefe Tabellen und Attribute!

'; @@ -526,6 +592,7 @@ $tabellen=array( "system.tbl_webservicerecht" => array("webservicerecht_id","berechtigung_kurzbz","methode","attribut","insertamum","insertvon","updateamum","updatevon","klasse"), "system.tbl_webservicetyp" => array("webservicetyp_kurzbz","beschreibung"), "system.tbl_server" => array("server_kurzbz","beschreibung"), + "system.tbl_udf" => array("schema", "table", "jsons"), "wawi.tbl_betriebsmittelperson" => array("betriebsmittelperson_id","betriebsmittel_id","person_id", "anmerkung", "kaution", "ausgegebenam", "retouram","insertamum", "insertvon","updateamum", "updatevon","ext_id","uid"), "wawi.tbl_betriebsmittel" => array("betriebsmittel_id","betriebsmitteltyp","oe_kurzbz", "ort_kurzbz", "beschreibung", "nummer", "hersteller","seriennummer", "bestellung_id","bestelldetail_id", "afa","verwendung","anmerkung","reservieren","updateamum","updatevon","insertamum","insertvon","ext_id","inventarnummer","leasing_bis","inventuramum","inventurvon","anschaffungsdatum","anschaffungswert","hoehe","breite","tiefe","nummer2","verplanen"), "wawi.tbl_betriebsmittel_betriebsmittelstatus" => array("betriebsmittelbetriebsmittelstatus_id","betriebsmittel_id","betriebsmittelstatus_kurzbz", "datum", "updateamum", "updatevon", "insertamum", "insertvon","anmerkung"), @@ -607,5 +674,4 @@ if (!$result=@$db->db_query($sql_query)) } if($error==false) echo '
Gegenpruefung fehlerfrei'; - -?> +?> \ No newline at end of file From c026aecfbe711d15b37f29724d16c16ef60b173d Mon Sep 17 00:00:00 2001 From: Stefan Puraner Date: Thu, 22 Jun 2017 08:29:51 +0200 Subject: [PATCH 22/48] changed api method to return status 'Interessent' --- application/models/organisation/Studiengang_model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/models/organisation/Studiengang_model.php b/application/models/organisation/Studiengang_model.php index 385c874a2..ec98577f1 100644 --- a/application/models/organisation/Studiengang_model.php +++ b/application/models/organisation/Studiengang_model.php @@ -214,7 +214,7 @@ class Studiengang_model extends DB_Model ), 'public.tbl_prestudent.person_id = '.$this->escape($person_id). ' AND public.tbl_prestudentstatus.studiensemester_kurzbz = '.$this->escape($studiensemester_kurzbz). - ' AND (public.tbl_prestudentstatus.status_kurzbz = \'Interessent\' OR public.tbl_prestudentstatus.status_kurzbz = \'Bewerber\')', + ' AND (public.tbl_prestudentstatus.status_kurzbz = \'Interessent\')', array( 'prestudenten', 'prestudentstatus', From a7d3214dd681183cd974c73ca4cd7bd05f454e24 Mon Sep 17 00:00:00 2001 From: Stefan Puraner Date: Thu, 22 Jun 2017 11:32:56 +0200 Subject: [PATCH 23/48] fixed return value of method '_manageUDFs' --- application/core/DB_Model.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 728365185..d6c70f5dd 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -960,6 +960,10 @@ class DB_Model extends FHC_Model } } } + else + { + return success(true); + } return $validate; } From be9fc8121e4df8fd5b30e00979fe2608e07192c4 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 23 Jun 2017 18:54:34 +0200 Subject: [PATCH 24/48] - Added exit codes for udf validation to constants.php - Added messages for udf validation to fhc_model_lang - Added MAX_LENGTH and MIN_LENGTH to UDFWidgetTpl in WidgetLib - UDF validation second version in DB_Model --- application/config/constants.php | 8 +- application/core/DB_Model.php | 85 ++++++++++++------- application/language/de-AT/fhc_model_lang.php | 8 +- application/libraries/WidgetLib.php | 8 +- 4 files changed, 77 insertions(+), 32 deletions(-) diff --git a/application/config/constants.php b/application/config/constants.php index 9d5092b74..5fa56e91b 100755 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -101,7 +101,13 @@ 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_VALIDATION_UDF', 9); // UDF validation has been failed +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', 11); // UDF validation has been failed -> MIN LENGTH +define('EXIT_VALIDATION_UDF_MAX_LENGTH', 12); // UDF validation has been failed -> MAX LENGTH +define('EXIT_VALIDATION_UDF_REGEX', 13); // UDF validation has been failed -> REGEX +define('EXIT_VALIDATION_UDF_REQUIRED', 14); // UDF validation has been failed -> REQUIRED define('EXIT_AUTO_MIN', 1000); // lowest automatically-assigned error code define('EXIT_AUTO_MAX', 2000); // highest automatically-assigned error code \ No newline at end of file diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index d6c70f5dd..f37c60ad3 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -2,17 +2,36 @@ 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 QUERY_LIST_FIELDS = 'SELECT * FROM %s WHERE 0 = 1'; + const UDF_ATTRIBUTE_NAME = 'name'; + + // UDF validation attributes + const UDF_REGEX = 'regex'; + const UDF_REGEX_LANG = 'php'; + const UDF_REQUIRED = 'required'; + const UDF_MAX_VALUE = 'max-value'; + const UDF_MIN_VALUE = 'min-value'; + const UDF_MAX_LENGTH = 'max-length'; + const UDF_MIN_LENGTH = 'min-length'; protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ... protected $pk; // Name of the PrimaryKey for DB-Update, Load, ... @@ -843,6 +862,7 @@ class DB_Model extends FHC_Model private function _validateUDFs($udfDescription, $udfValue) { $isValid = true; + $valid = success(true); $tmpUdfValues = $udfValue; if (!is_array($udfValue)) @@ -863,29 +883,43 @@ class DB_Model extends FHC_Model foreach($tmpUdfValues as $udfValIndx => $udfVal) { // - if (isset($validation->{'max-value'}) && $udfVal > $validation->{'max-value'}) + if (isset($validation->{DB_Model::UDF_MAX_VALUE}) && $udfVal > $validation->{DB_Model::UDF_MAX_VALUE}) { - $isValid = false; + $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MAX_VALUE); break; } // - if (isset($validation->{'min-value'}) && $udfVal < $validation->{'min-value'}) + if (isset($validation->{DB_Model::UDF_MIN_VALUE}) && $udfVal < $validation->{DB_Model::UDF_MIN_VALUE}) { - $isValid = false; + $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MIN_VALUE); break; } // - if (isset($validation->regex) && is_array($validation->regex)) + if (isset($validation->{DB_Model::UDF_MAX_LENGTH}) && $udfVal > $validation->{DB_Model::UDF_MAX_LENGTH}) { - foreach($validation->regex as $regexIndx => $regex) + $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MAX_LENGTH); + break; + } + + // + if (isset($validation->{DB_Model::UDF_MIN_LENGTH}) && $udfVal < $validation->{DB_Model::UDF_MIN_LENGTH}) + { + $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MIN_LENGTH); + break; + } + + // + if (isset($validation->{DB_Model::UDF_REGEX}) && is_array($validation->{DB_Model::UDF_REGEX})) + { + foreach($validation->{DB_Model::UDF_REGEX} as $regexIndx => $regex) { - if ($regex->language == 'php') + if ($regex->language == DB_Model::UDF_REGEX_LANG) { if (preg_match($regex->expression, $udfVal) != 1) { - $isValid = false; + $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_REGEX); break; } } @@ -894,15 +928,7 @@ class DB_Model extends FHC_Model } } - // - if ($isValid === true) - { - return success(true); - } - else - { - return error($udfDescription->name, EXIT_VALIDATION_UDF); - } + return $valid; } /** @@ -910,7 +936,8 @@ class DB_Model extends FHC_Model */ private function _manageUDFs(&$data) { - $validate = error('manageUDFs', EXIT_DATABASE); + $validate = success(true); + $notValidUDFsArray = array(); if ($this->hasUDF()) // Checks if this table has UDFs { @@ -931,10 +958,10 @@ class DB_Model extends FHC_Model foreach($this->UDFs as $key => $val) { - if ($udfDescription->name == $key) + if ($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME} == $key) { // Validation of UDF parameter value - $validate = $this->_validateUDFs($udfDescription, $val); + // $validate = $this->_validateUDFs($udfDescription, $val); // If validation is ok copy the value if (isSuccess($validate)) @@ -943,14 +970,14 @@ class DB_Model extends FHC_Model } else // otherwise stop the elaboration { - break; + $notValidUDFsArray[] = $validate; } } } } // If validation was ok - if (isSuccess($validate)) + if (count($notValidUDFsArray) == 0) { $jsonEncodedUDFs = json_encode($jsonb); // encode to json if ($jsonEncodedUDFs !== false) @@ -958,12 +985,12 @@ class DB_Model extends FHC_Model $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; } } + else + { + $validate = error($notValidUDFsArray, EXIT_VALIDATION_UDF); + } } } - else - { - return success(true); - } return $validate; } diff --git a/application/language/de-AT/fhc_model_lang.php b/application/language/de-AT/fhc_model_lang.php index 68932dd4a..27c444f77 100644 --- a/application/language/de-AT/fhc_model_lang.php +++ b/application/language/de-AT/fhc_model_lang.php @@ -6,4 +6,10 @@ if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $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'; \ No newline at end of file +$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'; \ No newline at end of file diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 7e01209a7..8040b7911 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -1179,7 +1179,8 @@ class CheckboxWidgetUDF extends CheckboxWidget } /** - * + * Defined here because these constants can be used + * in the views also */ abstract class UDFWidgetTpl extends Widget { @@ -1189,12 +1190,17 @@ abstract class UDFWidgetTpl extends Widget const FIELD_ARG_NAME = 'field'; const UDFS_ARG_NAME = 'udfs'; + // HTML components const TITLE = 'description'; const LABEL = 'title'; const PLACEHOLDER = 'placeholder'; const DEFAULT_VALUE = 'defaultValue'; + + // Validation attributes const REGEX = 'regex'; const REQUIRED = 'required'; const MAX_VALUE = 'max-value'; const MIN_VALUE = 'min-value'; + const MAX_LENGTH = 'max-length'; + const MIN_LENGTH = 'min-length'; } \ No newline at end of file From d3a675bf5297df51798d5e9c8c1574ee50b2057c Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 28 Jun 2017 12:04:41 +0200 Subject: [PATCH 25/48] Testing phase --- application/config/constants.php | 8 +-- application/core/DB_Model.php | 107 +++++++++++++++++++++---------- 2 files changed, 76 insertions(+), 39 deletions(-) diff --git a/application/config/constants.php b/application/config/constants.php index 5fa56e91b..7e910087e 100755 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -104,10 +104,10 @@ define('EXIT_DATABASE', 8); // database error 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', 11); // UDF validation has been failed -> MIN LENGTH -define('EXIT_VALIDATION_UDF_MAX_LENGTH', 12); // UDF validation has been failed -> MAX LENGTH -define('EXIT_VALIDATION_UDF_REGEX', 13); // UDF validation has been failed -> REGEX -define('EXIT_VALIDATION_UDF_REQUIRED', 14); // UDF validation has been failed -> REQUIRED +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_AUTO_MIN', 1000); // lowest automatically-assigned error code define('EXIT_AUTO_MAX', 2000); // highest automatically-assigned error code \ No newline at end of file diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index f37c60ad3..c16a32a9b 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -49,10 +49,10 @@ class DB_Model extends FHC_Model parent::__construct(); // Set properties - $this->dbTable = $dbTable; $this->pk = $pk; - $this->hasSequence = $hasSequence; $this->UDFs = array(); + $this->dbTable = $dbTable; + $this->hasSequence = $hasSequence; // Loads DB conns and confs $this->load->database(); @@ -859,11 +859,11 @@ class DB_Model extends FHC_Model /** * Validates UDF value */ - private function _validateUDFs($udfDescription, $udfValue) + private function _validateUDFs($validation, $udfName, $udfValue) { - $isValid = true; - $valid = success(true); + $valid = array(); + // $tmpUdfValues = $udfValue; if (!is_array($udfValue)) { @@ -871,45 +871,45 @@ class DB_Model extends FHC_Model } // - $validation = null; - if (isset($udfDescription->validation)) + foreach($tmpUdfValues as $udfValIndx => $udfVal) { - $validation = $udfDescription->validation; - } - - // - if ($validation != null) - { - foreach($tmpUdfValues as $udfValIndx => $udfVal) + // + if (is_numeric($udfVal)) { // if (isset($validation->{DB_Model::UDF_MAX_VALUE}) && $udfVal > $validation->{DB_Model::UDF_MAX_VALUE}) { - $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MAX_VALUE); - break; + $valid[] = error($udfName, EXIT_VALIDATION_UDF_MAX_VALUE); } // if (isset($validation->{DB_Model::UDF_MIN_VALUE}) && $udfVal < $validation->{DB_Model::UDF_MIN_VALUE}) { - $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MIN_VALUE); - break; + $valid[] = error($udfName, EXIT_VALIDATION_UDF_MIN_VALUE); + } + } + + // + if (!is_array($udfVal) && !is_object($udfVal)) + { + $strUdfVal = strval($udfVal); // + + // + if (isset($validation->{DB_Model::UDF_MAX_LENGTH}) && isset($strUdfVal) && strlen($strUdfVal) > $validation->{DB_Model::UDF_MAX_LENGTH}) + { + $valid[] = error($udfName, EXIT_VALIDATION_UDF_MAX_LENGTH); } // - if (isset($validation->{DB_Model::UDF_MAX_LENGTH}) && $udfVal > $validation->{DB_Model::UDF_MAX_LENGTH}) + if (isset($validation->{DB_Model::UDF_MIN_LENGTH}) && isset($strUdfVal) && strlen($strUdfVal) < $validation->{DB_Model::UDF_MIN_LENGTH}) { - $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MAX_LENGTH); - break; + $valid[] = error($udfName, EXIT_VALIDATION_UDF_MIN_LENGTH); } - - // - if (isset($validation->{DB_Model::UDF_MIN_LENGTH}) && $udfVal < $validation->{DB_Model::UDF_MIN_LENGTH}) - { - $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_MIN_LENGTH); - break; - } - + } + + // + if (is_string($udfVal)) + { // if (isset($validation->{DB_Model::UDF_REGEX}) && is_array($validation->{DB_Model::UDF_REGEX})) { @@ -919,8 +919,7 @@ class DB_Model extends FHC_Model { if (preg_match($regex->expression, $udfVal) != 1) { - $valid = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_REGEX); - break; + $valid[] = error($udfName, EXIT_VALIDATION_UDF_REGEX); } } } @@ -928,6 +927,12 @@ class DB_Model extends FHC_Model } } + // + if (count($valid) == 0) + { + $valid = success(true); + } + return $valid; } @@ -952,23 +957,50 @@ class DB_Model extends FHC_Model $udfsDecoded = json_decode($udfs->retval[0]->jsons); + $requiredArray = array(); // + + // for($i = 0; $i < count($udfsDecoded); $i++) { - $udfDescription = $udfsDecoded[$i]; + $udfDescription = $udfsDecoded[$i]; // + if(isset($udfDescription->validation) + && isset($udfDescription->validation->required) + && $udfDescription->validation->required === true) + { + $requiredArray[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_REQUIRED); + } + + // foreach($this->UDFs as $key => $val) { + // if ($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME} == $key) { - // Validation of UDF parameter value - // $validate = $this->_validateUDFs($udfDescription, $val); + // Validation of UDF parameter value, if validation rules are present + if (isset($udfDescription->validation)) + { + $validate = $this->_validateUDFs( + $udfDescription->validation, // + $udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, // + $val // + ); + + if (isset($udfDescription->validation->required) && $udfDescription->validation->required === true) + { + if (isset($requiredArray[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}])) + { + unset($requiredArray[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}]); + } + } + } // If validation is ok copy the value if (isSuccess($validate)) { $jsonb[$key] = $val; } - else // otherwise stop the elaboration + else // { $notValidUDFsArray[] = $validate; } @@ -976,6 +1008,11 @@ class DB_Model extends FHC_Model } } + foreach($requiredArray as $key => $val) + { + $notValidUDFsArray[] = array($val); + } + // If validation was ok if (count($notValidUDFsArray) == 0) { From 2c0ca4be98e0e5d24a49d72a7b590a5ac4beba7c Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 28 Jun 2017 12:41:57 +0200 Subject: [PATCH 26/48] Bug fix dbupdate_3.3 --- system/dbupdate_3.3.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/system/dbupdate_3.3.php b/system/dbupdate_3.3.php index 3cc0623c8..97dcb5da0 100644 --- a/system/dbupdate_3.3.php +++ b/system/dbupdate_3.3.php @@ -268,7 +268,6 @@ if($result = $db->db_query("SELECT view_definition FROM information_schema.views } } -<<<<<<< HEAD // Creates table system.tbl_udf if it doesn't exist and grants privileges if(!$result = @$db->db_query("SELECT 1 FROM system.tbl_udf LIMIT 1")) { @@ -333,7 +332,8 @@ if(!$result = @$db->db_query("SELECT udf_values FROM public.tbl_prestudent LIMIT echo 'public.tbl_prestudent: '.$db->db_last_error().'
'; else echo '
Added column udf_values to table public.tbl_prestudent'; -======= +} + // Spalten mailversand,teilnehmer_anonym,termine_anonym in campus.tbl_coodle if(!$result = @$db->db_query("SELECT mailversand FROM campus.tbl_coodle LIMIT 1;")) { @@ -356,7 +356,6 @@ if(!$result = @$db->db_query("SELECT onlinebewerbung_studienplan FROM lehre.tbl_ echo 'lehre.tbl_studienplan: '.$db->db_last_error().'
'; else echo '
lehre.tbl_studienplan: Spalte onlinebewerbung_studienplan hinzugefuegt!
'; ->>>>>>> master } // *** Pruefung und hinzufuegen der neuen Attribute und Tabellen @@ -617,7 +616,7 @@ $tabellen=array( "system.tbl_webservicerecht" => array("webservicerecht_id","berechtigung_kurzbz","methode","attribut","insertamum","insertvon","updateamum","updatevon","klasse"), "system.tbl_webservicetyp" => array("webservicetyp_kurzbz","beschreibung"), "system.tbl_server" => array("server_kurzbz","beschreibung"), - "system.tbl_udf" => array("schema", "table", "jsons"), + "system.tbl_udf" => array("schema", "tbl_udf.table", "jsons"), "wawi.tbl_betriebsmittelperson" => array("betriebsmittelperson_id","betriebsmittel_id","person_id", "anmerkung", "kaution", "ausgegebenam", "retouram","insertamum", "insertvon","updateamum", "updatevon","ext_id","uid"), "wawi.tbl_betriebsmittel" => array("betriebsmittel_id","betriebsmitteltyp","oe_kurzbz", "ort_kurzbz", "beschreibung", "nummer", "hersteller","seriennummer", "bestellung_id","bestelldetail_id", "afa","verwendung","anmerkung","reservieren","updateamum","updatevon","insertamum","insertvon","ext_id","inventarnummer","leasing_bis","inventuramum","inventurvon","anschaffungsdatum","anschaffungswert","hoehe","breite","tiefe","nummer2","verplanen"), "wawi.tbl_betriebsmittel_betriebsmittelstatus" => array("betriebsmittelbetriebsmittelstatus_id","betriebsmittel_id","betriebsmittelstatus_kurzbz", "datum", "updateamum", "updatevon", "insertamum", "insertvon","anmerkung"), From e2f87534ff022bdc9e09dce0dd84cf8dd11eb1e9 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 30 Jun 2017 13:28:24 +0200 Subject: [PATCH 27/48] - Added new constants that represent the exit codes for the UDF validation - Added new error messages for the UDF validation --- application/config/constants.php | 1 + application/language/de-AT/fhc_model_lang.php | 3 ++- application/language/en-US/fhc_model_lang.php | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/application/config/constants.php b/application/config/constants.php index 7e910087e..68a5261e7 100755 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -108,6 +108,7 @@ define('EXIT_VALIDATION_UDF_MIN_LENGTH', 13); // UDF validation has been failed 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 \ No newline at end of file diff --git a/application/language/de-AT/fhc_model_lang.php b/application/language/de-AT/fhc_model_lang.php index 27c444f77..8d030c940 100644 --- a/application/language/de-AT/fhc_model_lang.php +++ b/application/language/de-AT/fhc_model_lang.php @@ -12,4 +12,5 @@ $lang['fhc_'.EXIT_VALIDATION_UDF_MAX_VALUE] = 'UDF validation has been failed - $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'; \ No newline at end of file +$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'; \ No newline at end of file diff --git a/application/language/en-US/fhc_model_lang.php b/application/language/en-US/fhc_model_lang.php index c000598a6..406d5da4b 100644 --- a/application/language/en-US/fhc_model_lang.php +++ b/application/language/en-US/fhc_model_lang.php @@ -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'; \ No newline at end of file From 3abfcbc429e88b9b725ed94cfb1e457ddf44ce78 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 30 Jun 2017 13:31:21 +0200 Subject: [PATCH 28/48] - Better naming convention for variables - Method toPhp has a more strict check about UDF - Added constant UDF_FIELD_JSON_DESCRIPTION - Added new code comments and improved the old ones - Improved code formatting - UDF validation now checks also the required fields - UDF validation now checks also if the UDF value is an array or an object and discard them - Method _getUDFs renamed to _getUDFsDefinitions --- application/core/DB_Model.php | 237 ++++++++++++++++++++-------------- 1 file changed, 138 insertions(+), 99 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index c16a32a9b..064cd1b9e 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -23,6 +23,7 @@ class DB_Model extends FHC_Model const UDF_FIELD_TYPE = 'jsonb'; const UDF_FIELD_PREFIX = 'udf_'; const UDF_ATTRIBUTE_NAME = 'name'; + const UDF_FIELD_JSON_DESCRIPTION = 'jsons'; // UDF validation attributes const UDF_REGEX = 'regex'; @@ -667,7 +668,7 @@ class DB_Model extends FHC_Model } /** - * Return $this->UDFs + * Return the property UDFs */ public function getUDFs() { @@ -815,7 +816,7 @@ class DB_Model extends FHC_Model /** * Returns all the UDF for this table */ - private function _getUDFs() + private function _getUDFsDefinitions() { $this->load->model('system/UDF_model', 'UDFModel'); @@ -830,7 +831,7 @@ class DB_Model extends FHC_Model $table = $tmpArray[1]; } - $this->UDFModel->addSelect('jsons'); + $this->UDFModel->addSelect(DB_Model::UDF_FIELD_JSON_DESCRIPTION); $udfResults = $this->UDFModel->loadWhere( array( 'schema' => $schema, @@ -850,7 +851,7 @@ class DB_Model extends FHC_Model { if (substr($key, 0, 4) == DB_Model::UDF_FIELD_PREFIX) { - $this->UDFs[$key] = $val; + $this->UDFs[$key] = $val; // stores UDF value into property UDFs unset($data[$key]); // remove from data } } @@ -859,81 +860,99 @@ class DB_Model extends FHC_Model /** * Validates UDF value */ - private function _validateUDFs($validation, $udfName, $udfValue) + private function _validateUDFs($decodedUDFValidation, $udfName, $udfValue) { - $valid = array(); + $returnArrayValidation = array(); // returned value - // + // 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 (is_numeric($udfVal)) - { - // - if (isset($validation->{DB_Model::UDF_MAX_VALUE}) && $udfVal > $validation->{DB_Model::UDF_MAX_VALUE}) - { - $valid[] = error($udfName, EXIT_VALIDATION_UDF_MAX_VALUE); - } - - // - if (isset($validation->{DB_Model::UDF_MIN_VALUE}) && $udfVal < $validation->{DB_Model::UDF_MIN_VALUE}) - { - $valid[] = error($udfName, EXIT_VALIDATION_UDF_MIN_VALUE); - } - } - - // + // If the single UDF value is not an array or an object if (!is_array($udfVal) && !is_object($udfVal)) { - $strUdfVal = strval($udfVal); // - - // - if (isset($validation->{DB_Model::UDF_MAX_LENGTH}) && isset($strUdfVal) && strlen($strUdfVal) > $validation->{DB_Model::UDF_MAX_LENGTH}) + // If the UDF value is numeric (integer, float, double...) + if (is_numeric($udfVal)) { - $valid[] = error($udfName, EXIT_VALIDATION_UDF_MAX_LENGTH); - } - - // - if (isset($validation->{DB_Model::UDF_MIN_LENGTH}) && isset($strUdfVal) && strlen($strUdfVal) < $validation->{DB_Model::UDF_MIN_LENGTH}) - { - $valid[] = error($udfName, EXIT_VALIDATION_UDF_MIN_LENGTH); - } - } - - // - if (is_string($udfVal)) - { - // - if (isset($validation->{DB_Model::UDF_REGEX}) && is_array($validation->{DB_Model::UDF_REGEX})) - { - foreach($validation->{DB_Model::UDF_REGEX} as $regexIndx => $regex) + // 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}) { - if ($regex->language == DB_Model::UDF_REGEX_LANG) + // 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 (preg_match($regex->expression, $udfVal) != 1) + if ($regex->language == DB_Model::UDF_REGEX_LANG) { - $valid[] = error($udfName, EXIT_VALIDATION_UDF_REGEX); + 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 (count($valid) == 0) + // If no UDF validation errors were raised, it's a success!! + if (count($returnArrayValidation) == 0) { - $valid = success(true); + $returnArrayValidation = success(true); } - return $valid; + return $returnArrayValidation; } /** @@ -941,66 +960,81 @@ class DB_Model extends FHC_Model */ private function _manageUDFs(&$data) { - $validate = success(true); + $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 { - $udfs = $this->_getUDFs(); - if (hasData($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 - // Must be performed here because the load method populates UDFs too + // NOTE: Must be performed here because the load method populates the property UDFs too $this->_popUDFParameters($data); - $jsonb = array(); + $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(); - $udfsDecoded = json_decode($udfs->retval[0]->jsons); + // Decodes json that define the UDFs for this table + $decodedUDFDefinitions = json_decode( + $resultUDFsDefinitions->retval[0]->{DB_Model::UDF_FIELD_JSON_DESCRIPTION} + ); - $requiredArray = array(); // - - // - for($i = 0; $i < count($udfsDecoded); $i++) + // Loops through the UDFs definitions + for($i = 0; $i < count($decodedUDFDefinitions); $i++) { - $udfDescription = $udfsDecoded[$i]; // + $decodedUDFDefinition = $decodedUDFDefinitions[$i]; // Definition of a single UDF - if(isset($udfDescription->validation) - && isset($udfDescription->validation->required) - && $udfDescription->validation->required === true) + // 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->required) + && $decodedUDFDefinition->validation->required === true) { - $requiredArray[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}] = error($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, EXIT_VALIDATION_UDF_REQUIRED); + $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 ($udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME} == $key) + // If this is the definition of this UDF + if ($decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME} == $key) { - // Validation of UDF parameter value, if validation rules are present - if (isset($udfDescription->validation)) + // If validation rules are present for this UDF + if (isset($decodedUDFDefinition->validation)) { + // Validation!!! $validate = $this->_validateUDFs( - $udfDescription->validation, // - $udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}, // + $decodedUDFDefinition->validation, // + $decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}, // $val // ); - if (isset($udfDescription->validation->required) && $udfDescription->validation->required === true) + // 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->required) + && $decodedUDFDefinition->validation->required === true + && isset($requiredUDFsArray[$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}])) { - if (isset($requiredArray[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}])) - { - unset($requiredArray[$udfDescription->{DB_Model::UDF_ATTRIBUTE_NAME}]); - } + unset($requiredUDFsArray[$decodedUDFDefinition->{DB_Model::UDF_ATTRIBUTE_NAME}]); } } - // If validation is ok copy the value + // If validation is ok copy the value that is to be stored into $toBeStoredUDFsArray if (isSuccess($validate)) { - $jsonb[$key] = $val; + $toBeStoredUDFsArray[$key] = $val; } - else // + else // otherwise store the validation error in $notValidUDFsArray { $notValidUDFsArray[] = $validate; } @@ -1008,21 +1042,24 @@ class DB_Model extends FHC_Model } } - foreach($requiredArray as $key => $val) + // 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 validation was ok + // If the validation of all the supplied UDFs is ok if (count($notValidUDFsArray) == 0) { - $jsonEncodedUDFs = json_encode($jsonb); // encode to json - if ($jsonEncodedUDFs !== false) + $encodedToBeStoredUDFs = json_encode($toBeStoredUDFsArray); // encode to json + if ($encodedToBeStoredUDFs !== false) // if encode was ok { - $data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs; + // Save the supplied UDFs values + $data[DB_Model::UDF_FIELD_NAME] = $encodedToBeStoredUDFs; } } - else + else // otherwise the returning value will be the list of UDFs validation errors { $validate = error($notValidUDFsArray, EXIT_VALIDATION_UDF); } @@ -1098,7 +1135,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++) { @@ -1108,29 +1145,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) + else if ($toBeConverted->type == DB_Model::UDF_FIELD_TYPE + && substr($toBeConverted->name, 0, 4) == DB_Model::UDF_FIELD_PREFIX) { - $jsonValues = json_decode($tmpResult->{$toBeConverted->name}); - if ($jsonValues != null) + $jsonValues = json_decode($resultElement->{$toBeConverted->name}); // decode UDFs values + if ($jsonValues != null) // if decode is ok { + // For every UDF foreach($jsonValues as $key => $value) { - $tmpResult->{$key} = $value; - $this->UDFs[$key] = $value; // Stores the UDFs in $this->UDFs + $resultElement->{$key} = $value; // create a new element called like the UDF + $this->UDFs[$key] = $value; // stores the UDF in the property UDFs } } - unset($tmpResult->{$toBeConverted->name}); + unset($resultElement->{$toBeConverted->name}); // remove udf_values from the response } } } From a530cd22b4863235655fa3523f7691687793ea59 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 6 Jul 2017 11:07:43 +0200 Subject: [PATCH 29/48] - Bugfix UDF dropdown, wrong check for selected value - UDF checkbox now is only one value, no multiple checkbox anymore - Added method saveUDFs to model UDF_model - Added new constants to resolve json schema parameters --- application/controllers/api/v1/system/UDF.php | 58 +++++++++ application/libraries/WidgetLib.php | 110 +++++++++--------- application/models/system/UDF_model.php | 49 ++++++++ application/views/widgets/checkbox.php | 32 ++--- application/views/widgets/dropdown.php | 4 +- application/widgets/UDFWidget.php | 66 ++++++----- 6 files changed, 209 insertions(+), 110 deletions(-) diff --git a/application/controllers/api/v1/system/UDF.php b/application/controllers/api/v1/system/UDF.php index 517ef305d..5d2ec8ee7 100644 --- a/application/controllers/api/v1/system/UDF.php +++ b/application/controllers/api/v1/system/UDF.php @@ -60,6 +60,64 @@ class UDF extends APIv1_Controller $this->response($result, REST_Controller::HTTP_OK); } + /** + * + */ + public function postUDF() + { + $udfs = $this->post(); + $validation = $this->_validate($udfs); + + var_dump($udfs);exit; + + 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 */ diff --git a/application/libraries/WidgetLib.php b/application/libraries/WidgetLib.php index 8040b7911..b1da1670c 100644 --- a/application/libraries/WidgetLib.php +++ b/application/libraries/WidgetLib.php @@ -1066,9 +1066,6 @@ class TextfieldWidgetUDF extends TextfieldWidget */ class CheckboxWidget extends Widget { - // The name of the element of the data array given to the view - // this element is an array of elements to be place inside the dropdown - const WIDGET_DATA_ELEMENTS_ARRAY_NAME = 'ELEMENTS_ARRAY'; // Name of the property that will be used to store the value attribute of the option tag const VALUE_FIELD = 'value'; // Name of the property that will be used to store the value between the option tags @@ -1090,31 +1087,19 @@ class CheckboxWidget extends Widget $this->_args[CheckboxWidget::CHECKED_ELEMENT] = Widget::HTML_DEFAULT_VALUE; } } - + /** * */ - protected function setValuesArray($values) + protected function setValue($value) { - if (isError($values)) + $tmpValue = $value; + if (is_array($value) && count($value) > 0) { - if (is_object($values) && isset($values->retval)) - { - show_error($values->retval); - } - else if (is_string($values)) - { - show_error($values); - } - else - { - show_error('Generic error occurred'); - } - } - else - { - $this->_args[CheckboxWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME] = $values->retval; + $tmpValue = array_values($value)[0]; } + + $this->_args[CheckboxWidget::VALUE_FIELD] = $tmpValue; } /** @@ -1136,45 +1121,60 @@ class CheckboxWidgetUDF extends CheckboxWidget */ public function render($parameters) { - $tmpElements = array(); - - // - foreach($parameters as $parameter) + if ($parameters != null) { - // - if ((is_array($parameter) && count($parameter) == 2) - || (is_string($parameter) || is_numeric($parameter)) - || (is_object($parameter) && isset($parameter->{PARENT::VALUE_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD}))) - { - $element = new stdClass(); // - // - if (is_array($parameter) && count($parameter) == 2) - { - $element->{PARENT::VALUE_FIELD} = $parameter[0]; // - $element->{PARENT::DESCRIPTION_FIELD} = $parameter[1]; // - } - // - else if (is_string($parameter) || is_numeric($parameter)) - { - $element->{PARENT::VALUE_FIELD} = $parameter; // - $element->{PARENT::DESCRIPTION_FIELD} = $parameter; // - } - // - else if (is_object($parameter) && isset($parameter->{PARENT::VALUE_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD})) - { - $element->{PARENT::VALUE_FIELD} = $parameter->{PARENT::VALUE_FIELD}; // - $element->{PARENT::DESCRIPTION_FIELD} = $parameter->{PARENT::DESCRIPTION_FIELD}; // - } - - array_push($tmpElements, $element); // - } + $this->setValue($parameters); + } + else + { + $this->setValue(''); } - - $this->setValuesArray(success($tmpElements)); $this->loadCheckboxView(); echo $this->content(); + +// $tmpElements = array(); + + // +// foreach($parameters as $parameter) +// { +// // +// if ((is_array($parameter) && count($parameter) == 2) +// || (is_string($parameter) || is_numeric($parameter)) +// || (is_object($parameter) && isset($parameter->{PARENT::VALUE_FIELD}) +// && isset($parameter->{PARENT::DESCRIPTION_FIELD}))) +// { +// $element = new stdClass(); // +// // +// if (is_array($parameter) && count($parameter) == 2) +// { +// $element->{PARENT::VALUE_FIELD} = $parameter[0]; // +// $element->{PARENT::DESCRIPTION_FIELD} = $parameter[1]; // +// } +// // +// else if (is_string($parameter) || is_numeric($parameter)) +// { +// $element->{PARENT::VALUE_FIELD} = $parameter; // +// $element->{PARENT::DESCRIPTION_FIELD} = $parameter; // +// } +// // +// else if (is_object($parameter) && isset($parameter->{PARENT::VALUE_FIELD}) +// && isset($parameter->{PARENT::DESCRIPTION_FIELD})) +// { +// $element->{PARENT::VALUE_FIELD} = $parameter->{PARENT::VALUE_FIELD}; // +// $element->{PARENT::DESCRIPTION_FIELD} = $parameter->{PARENT::DESCRIPTION_FIELD}; // +// } +// +// array_push($tmpElements, $element); // +// } +// } +// +// $this->setValuesArray(success($tmpElements)); +// +// $this->loadCheckboxView(); +// +// echo $this->content(); } } diff --git a/application/models/system/UDF_model.php b/application/models/system/UDF_model.php index e33cdd275..86b2f7dc8 100644 --- a/application/models/system/UDF_model.php +++ b/application/models/system/UDF_model.php @@ -39,4 +39,53 @@ class UDF_model extends DB_Model 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'); + + // + if (isset($udfs['person_id'])) + { + // Load model Person_model + $this->load->model('person/Person_model', 'PersonModel'); + + $person_id = $udfs['person_id']; + unset($udfs['person_id']); + + $resultPerson = $this->PersonModel->update($person_id, $udfs); + } + + // + if (isset($udfs['prestudent_id'])) + { + // Load model Prestudent_model + $this->load->model('crm/Prestudent_model', 'PrestudentModel'); + + $prestudent_id = $udfs['prestudent_id']; + unset($udfs['prestudent_id']); + + $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; + } } \ No newline at end of file diff --git a/application/views/widgets/checkbox.php b/application/views/widgets/checkbox.php index 8fb459850..d6a233115 100644 --- a/application/views/widgets/checkbox.php +++ b/application/views/widgets/checkbox.php @@ -9,34 +9,22 @@ -
+ -> {CheckboxWidget::VALUE_FIELD} == $checkedElement) - { - $checked = 'checked'; - } - ?> - - value="{CheckboxWidget::VALUE_FIELD}; ?>" - - > - {CheckboxWidget::DESCRIPTION_FIELD}; ?> - -
+ + value="" +> + \ No newline at end of file diff --git a/application/views/widgets/dropdown.php b/application/views/widgets/dropdown.php index 6494d180f..3cbdbff23 100644 --- a/application/views/widgets/dropdown.php +++ b/application/views/widgets/dropdown.php @@ -32,7 +32,7 @@ { foreach($selectedElements as $selectedElement) { - if ($element->{DropdownWidget::ID_FIELD} === $selectedElement) + if ($element->{DropdownWidget::ID_FIELD} == $selectedElement) { $selected = 'selected'; } @@ -40,7 +40,7 @@ } else { - if ($element->{DropdownWidget::ID_FIELD} === $selectedElements) + if ($element->{DropdownWidget::ID_FIELD} == $selectedElements) { $selected = 'selected'; } diff --git a/application/widgets/UDFWidget.php b/application/widgets/UDFWidget.php index f4fc66c74..2147177f5 100644 --- a/application/widgets/UDFWidget.php +++ b/application/widgets/UDFWidget.php @@ -5,6 +5,9 @@ */ class UDFWidget extends UDFWidgetTpl { + const NAME = 'name'; + const TYPE = 'type'; + const LIST_VALUES = 'listValues'; const REGEX_LANGUAGE = 'js'; public function display($widgetData) @@ -44,19 +47,19 @@ class UDFWidget extends UDFWidgetTpl foreach($jsonSchemasArray as $jsonSchema) { // - if (!isset($jsonSchema->type)) + if (!isset($jsonSchema->{UDFWidget::TYPE})) { show_error(sprintf('%s.%s: Attribute "type" not present in the json schema', $schema, $table)); break; } - if (!isset($jsonSchema->name)) + 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->name) || !isset($field)) + if ((isset($field) && $field == $jsonSchema->{UDFWidget::NAME}) || !isset($field)) { $this->_setAttributesWithPhrases($jsonSchema); @@ -67,7 +70,7 @@ class UDFWidget extends UDFWidgetTpl $this->_render($jsonSchema); // - if (isset($field) && $field == $jsonSchema->name) + if (isset($field) && $field == $jsonSchema->{UDFWidget::NAME}) { $found = true; break; @@ -98,8 +101,8 @@ class UDFWidget extends UDFWidgetTpl */ private function _setNameAndId($jsonSchema) { - $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $jsonSchema->name; - $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $jsonSchema->name; + $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $jsonSchema->{UDFWidget::NAME}; + $this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $jsonSchema->{UDFWidget::NAME}; } /** @@ -172,27 +175,27 @@ class UDFWidget extends UDFWidgetTpl private function _render($jsonSchema) { // Type - if ($jsonSchema->type == 'checkbox') + if ($jsonSchema->{UDFWidget::TYPE} == 'checkbox') { $this->_renderCheckbox($jsonSchema); } - else if ($jsonSchema->type == 'textfield') + else if ($jsonSchema->{UDFWidget::TYPE} == 'textfield') { $this->_renderTextfield($jsonSchema); } - else if ($jsonSchema->type == 'textarea') + else if ($jsonSchema->{UDFWidget::TYPE} == 'textarea') { $this->_renderTextarea($jsonSchema); } - else if ($jsonSchema->type == 'date') + else if ($jsonSchema->{UDFWidget::TYPE} == 'date') { } - else if ($jsonSchema->type == 'dropdown') + else if ($jsonSchema->{UDFWidget::TYPE} == 'dropdown') { $this->_renderDropdown($jsonSchema); } - else if ($jsonSchema->type == 'multipledropdown') + else if ($jsonSchema->{UDFWidget::TYPE} == 'multipledropdown') { $this->_renderDropdown($jsonSchema, true); } @@ -205,23 +208,23 @@ class UDFWidget extends UDFWidgetTpl { if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) - && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}])) { - $this->_args[DropdownWidget::SELECTED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + $this->_args[DropdownWidget::SELECTED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]; } $dropdownWidgetUDF = new DropdownWidgetUDF($this->_name, $this->_args); $parameters = array(); // - if (isset($jsonSchema->listValues->enum)) + if (isset($jsonSchema->{UDFWidget::LIST_VALUES}->enum)) { - $parameters = $jsonSchema->listValues->enum; + $parameters = $jsonSchema->{UDFWidget::LIST_VALUES}->enum; } // - else if (isset($jsonSchema->listValues->sql)) + else if (isset($jsonSchema->{UDFWidget::LIST_VALUES}->sql)) { - $queryResult = $this->UDFModel->execQuery($jsonSchema->listValues->sql); + $queryResult = $this->UDFModel->execQuery($jsonSchema->{UDFWidget::LIST_VALUES}->sql); if (hasData($queryResult)) { $parameters = $queryResult->retval; @@ -245,9 +248,9 @@ class UDFWidget extends UDFWidgetTpl $text = null; if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) - && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}])) { - $text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + $text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]; } $textareaUDF->render($text); @@ -262,9 +265,9 @@ class UDFWidget extends UDFWidgetTpl $text = null; if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) - && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}])) { - $text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + $text = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]; } $textareaUDF->render($text); @@ -276,32 +279,33 @@ class UDFWidget extends UDFWidgetTpl private function _renderCheckbox($jsonSchema) { if (isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME]) - && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name])) + && isset($this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}])) { - $this->_args[CheckboxWidget::CHECKED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->name]; + $this->_args[CheckboxWidget::CHECKED_ELEMENT] = $this->_args[UDFWidgetTpl::UDFS_ARG_NAME][$jsonSchema->{UDFWidget::NAME}]; } // if (!isset($this->_args[CheckboxWidget::CHECKED_ELEMENT])) { - $this->_args[CheckboxWidget::CHECKED_ELEMENT] = $jsonSchema->defaultValue; + $this->_args[CheckboxWidget::CHECKED_ELEMENT] = $jsonSchema->{UDFWidgetTpl::DEFAULT_VALUE}; } $checkboxWidgetUDF = new CheckboxWidgetUDF($this->_name, $this->_args); $parameters = array(); // - if (isset($jsonSchema->listValues->enum)) + if (isset($jsonSchema->{UDFWidget::LIST_VALUES}->enum)) { - $parameters = $jsonSchema->listValues->enum; + $parameters = $jsonSchema->{UDFWidget::LIST_VALUES}->enum; } // - else if (isset($jsonSchema->listValues->sql)) + else if (isset($jsonSchema->{UDFWidget::LIST_VALUES}->sql)) { - $queryResult = $this->UDFModel->execQuery($jsonSchema->listValues->sql); + $queryResult = $this->UDFModel->execQuery($jsonSchema->{UDFWidget::LIST_VALUES}->sql); if (hasData($queryResult)) - { - $parameters = $queryResult->retval; + { + $tmpResult = (array)$queryResult->retval[0]; + $parameters = $tmpResult; } } From df51fb29886dec58fecb28d763ceef9dfd724c2d Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 6 Jul 2017 11:17:12 +0200 Subject: [PATCH 30/48] Added method postUDF to controller system/UDF/UDF --- application/controllers/api/v1/system/UDF.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/controllers/api/v1/system/UDF.php b/application/controllers/api/v1/system/UDF.php index 5d2ec8ee7..252332cce 100644 --- a/application/controllers/api/v1/system/UDF.php +++ b/application/controllers/api/v1/system/UDF.php @@ -68,8 +68,6 @@ class UDF extends APIv1_Controller $udfs = $this->post(); $validation = $this->_validate($udfs); - var_dump($udfs);exit; - if (isSuccess($validation)) { $caller = null; From 5d16a79cd370fb1bfff095ffd70f307c8edd42f2 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 7 Jul 2017 19:14:15 +0200 Subject: [PATCH 31/48] - Added UDF tab in fas - Added global constant FAS_UDF --- .../system/{TestUDF.php => UDF.php} | 12 +++- application/views/system/testudf.php | 40 ----------- application/views/system/udf.php | 30 ++++++++ config/global.config-default.inc.php | 3 + content/student/studentenoverlay.xul.php | 13 +++- content/student/studentoverlay.js.php | 21 ++++++ content/udf.js.php | 67 +++++++++++++++++ content/udf.xul.php | 72 +++++++++++++++++++ 8 files changed, 213 insertions(+), 45 deletions(-) rename application/controllers/system/{TestUDF.php => UDF.php} (65%) delete mode 100644 application/views/system/testudf.php create mode 100644 application/views/system/udf.php create mode 100644 content/udf.js.php create mode 100644 content/udf.xul.php diff --git a/application/controllers/system/TestUDF.php b/application/controllers/system/UDF.php similarity index 65% rename from application/controllers/system/TestUDF.php rename to application/controllers/system/UDF.php index 19a01c8cc..c3170d6aa 100644 --- a/application/controllers/system/TestUDF.php +++ b/application/controllers/system/UDF.php @@ -2,7 +2,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); -class TestUDF extends VileSci_Controller +class UDF extends VileSci_Controller { public function __construct() { @@ -24,10 +24,16 @@ class TestUDF extends VileSci_Controller $person = $this->PersonModel->load($person_id); + $udfs = $this->PersonModel->getUDFs(); + + $udfs['person_id'] = 1; + $udfs['prestudent_id'] = 1; + $udfs['caller'] = 'system/UDF?person_id=1'; + $data = array( - 'udfs' => $this->PersonModel->getUDFs() + 'udfs' => $udfs ); - $this->load->view('system/testudf', $data); + $this->load->view('system/udf', $data); } } \ No newline at end of file diff --git a/application/views/system/testudf.php b/application/views/system/testudf.php deleted file mode 100644 index 1d3c50f77..000000000 --- a/application/views/system/testudf.php +++ /dev/null @@ -1,40 +0,0 @@ -load->view("templates/header", array("title" => "UDF")); ?> - - - -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::FIELD_ARG_NAME => 'udf_schuhgroesse', - DropdownWidget::SELECTED_ELEMENT => $udfs['udf_schuhgroesse'] - ), - array('name' => 'schuhgroesseName', 'id' => 'schuhgroesseId') - ); - ?> -
- -
-
-
-
-
-
- -
- widgetlib->UDFWidget( - array( - UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', - UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', - UDFWidgetTpl::UDFS_ARG_NAME => $udfs - ) - ); - ?> -
- - - -load->view("templates/footer"); ?> \ No newline at end of file diff --git a/application/views/system/udf.php b/application/views/system/udf.php new file mode 100644 index 000000000..89ea770bb --- /dev/null +++ b/application/views/system/udf.php @@ -0,0 +1,30 @@ +load->view("templates/header", array("title" => "UDF")); ?> + + + +
+ +
+ widgetlib->UDFWidget( + array( + UDFWidgetTpl::SCHEMA_ARG_NAME => 'public', + UDFWidgetTpl::TABLE_ARG_NAME => 'tbl_person', + UDFWidgetTpl::UDFS_ARG_NAME => $udfs + ) + ); + ?> +
+ +
+ +
+ + + + +
+ + + +load->view("templates/footer"); ?> \ No newline at end of file diff --git a/config/global.config-default.inc.php b/config/global.config-default.inc.php index ba24752e3..87e51a74a 100644 --- a/config/global.config-default.inc.php +++ b/config/global.config-default.inc.php @@ -99,6 +99,9 @@ define('FAS_REIHUNGSTEST_PUNKTE', false); // Legt fest ob Messages im FAS angezeigt werden true|false define('FAS_MESSAGES',false); +// Enable (true) or disable (false) the UDF tab +define('FAS_UDF', true); + // Legt fest ob Aufnahmegruppen bei Reihungstests verwaltet werden true|false define('FAS_REIHUNGSTEST_AUFNAHMEGRUPPEN',false); diff --git a/content/student/studentenoverlay.xul.php b/content/student/studentenoverlay.xul.php index 0ef95958e..ac7ed0cfa 100644 --- a/content/student/studentenoverlay.xul.php +++ b/content/student/studentenoverlay.xul.php @@ -484,6 +484,11 @@ else if(!defined('FAS_MESSAGES') || FAS_MESSAGES==true) echo ''; ?> + + '; + ?> @@ -516,9 +521,13 @@ else ?>