From 0fd34807ca4dd115a8244d312d3b18bb4901125a Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 27 Apr 2017 19:06:36 +0200 Subject: [PATCH] 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