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