From 6a11cc09cf908d16dbbf4d7862b1784501fcf5c1 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 5 May 2017 18:45:28 +0200 Subject: [PATCH] 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); } /**