From d370bbc3d3550bb5c6015215c9af48a812e240e9 Mon Sep 17 00:00:00 2001 From: tbrehm <t.brehm@ispconfig.org> Date: Fri, 08 Jun 2012 05:43:10 -0400 Subject: [PATCH] Implemented: FS#2280 - Extend the tform library to apply filters on form values --- interface/lib/classes/tform.inc.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 54 insertions(+), 4 deletions(-) diff --git a/interface/lib/classes/tform.inc.php b/interface/lib/classes/tform.inc.php index dde9052..2690c81 100644 --- a/interface/lib/classes/tform.inc.php +++ b/interface/lib/classes/tform.inc.php @@ -172,8 +172,14 @@ if(isset($record[$table_idx])) $new_record[$table_idx] = intval($record[$table_idx ]); if(is_array($record)) { - foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) { - switch ($field['datatype']) { + foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) { + + //* Apply filter to record value. + if(isset($field['filters']) && is_array($field['filters'])) { + $record[$key] = $this->filterField($key, (isset($record[$key]))?$record[$key]:'', $field['filters'], 'SHOW'); + } + + switch ($field['datatype']) { case 'VARCHAR': $new_record[$key] = $record[$key]; break; @@ -619,8 +625,16 @@ if(is_array($record)) { foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) { - - if(isset($field['validators']) && is_array($field['validators'])) $this->validateField($key, (isset($record[$key]))?$record[$key]:'', $field['validators']); + + //* Apply filter to record value + if(isset($field['filters']) && is_array($field['filters'])) { + $record[$key] = $this->filterField($key, (isset($record[$key]))?$record[$key]:'', $field['filters'], 'SAVE'); + } + + //* Validate record value + if(isset($field['validators']) && is_array($field['validators'])) { + $this->validateField($key, (isset($record[$key]))?$record[$key]:'', $field['validators']); + } switch ($field['datatype']) { case 'VARCHAR': @@ -704,6 +718,42 @@ } return $new_record; } + + /** + * process the filters for a given field. + * + * @param field_name = Name of the field + * @param field_value = value of the field + * @param filters = Array of filters + * @param filter_event = 'SAVE'or 'SHOW' + * @return record + */ + + function filterField($field_name, $field_value, $filters, $filter_event) { + + global $app; + + $returnval = ''; + + //* Loop trough all filters + foreach($filters as $filter) { + if($filter['event'] == $filter_event) { + switch ($filter['type']) { + case 'TOLOWER': + $returnval = strtolower($field_value); + break; + case 'TOUPPER': + $returnval = strtoupper($field_value); + break; + default: + $this->errorMessage .= "Unknown Filter: ".$filter['type']; + break; + } + } + } + + return $returnval; + } /** * process the validators for a given field. -- Gitblit v1.9.1