Skip to content

Commit 2d2fd17

Browse files
committed
- Added funtion to convert currency formatted numbers back to floating numbers.
- Improved getSearchSQL() function so that users can use their native date format so search for dates in lists.
1 parent 936c119 commit 2d2fd17

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ public function currency_format($number, $view = '') {
129129
return number_format((double)$number, $number_format_decimals, $number_format_dec_point, $number_format_thousands_sep);
130130
}
131131

132+
//* convert currency formatted number back to floating number
133+
public function currency_unformat($number) {
134+
global $app;
135+
136+
$number_format_dec_point = $app->lng('number_format_dec_point');
137+
$number_format_thousands_sep = $app->lng('number_format_thousands_sep');
138+
if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = '';
139+
140+
if($number_format_thousands_sep != '') $number = str_replace($number_format_thousands_sep, '', $number);
141+
if($number_format_dec_point != '.' && $number_format_dec_point != '') $number = str_replace($number_format_dec_point, '.', $number);
142+
143+
return (double)$number;
144+
}
145+
132146
public function get_ispconfig_url() {
133147
global $app;
134148

interface/lib/classes/listform.inc.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,56 @@ public function getSearchSQL($sql_where = '')
181181
foreach($this->listDef['item'] as $i) {
182182
$field = $i['field'];
183183
$table = $i['table'];
184+
185+
$searchval = $_SESSION['search'][$list_name][$search_prefix.$field];
186+
// format user date format to MySQL date format 0000-00-00
187+
if($i['datatype'] == 'DATE' && $this->lng('conf_format_dateshort') != 'Y-m-d'){
188+
$dateformat = preg_replace("@[^Ymd]@", "", $this->lng('conf_format_dateshort'));
189+
$yearpos = strpos($dateformat, 'Y') + 1;
190+
$monthpos = strpos($dateformat, 'm') + 1;
191+
$daypos = strpos($dateformat, 'd') + 1;
192+
193+
$full_date_trans = array ('Y' => '((?:19|20)\d\d)',
194+
'm' => '(0[1-9]|1[012])',
195+
'd' => '(0[1-9]|[12][0-9]|3[01])'
196+
);
197+
// d.m.Y Y/m/d
198+
$full_date_regex = strtr(preg_replace("@[^Ymd]@", "[^0-9]", $this->lng('conf_format_dateshort')), $full_date_trans);
199+
//echo $full_date_regex;
200+
201+
if (preg_match("@^\d+$@", $_SESSION['search'][$list_name][$search_prefix.$field])) { // we just have digits
202+
$searchval = $_SESSION['search'][$list_name][$search_prefix.$field];
203+
} elseif(preg_match("@^[^0-9]?\d+[^0-9]?$@", $_SESSION['search'][$list_name][$search_prefix.$field])){ // 10. or .10.
204+
$searchval = preg_replace("@[^0-9]@", "", $_SESSION['search'][$list_name][$search_prefix.$field]);
205+
} elseif(preg_match("@^[^0-9]?(\d{1,2})[^0-9]((?:19|20)\d\d)$@", $_SESSION['search'][$list_name][$search_prefix.$field], $matches)){ // 10.2013
206+
$month = $matches[1];
207+
$year = $matches[2];
208+
$searchval = $year.'-'.$month;
209+
} elseif(preg_match("@^((?:19|20)\d\d)[^0-9](\d{1,2})[^0-9]?$@", $_SESSION['search'][$list_name][$search_prefix.$field], $matches)){ // 2013-10
210+
$month = $matches[2];
211+
$year = $matches[1];
212+
$searchval = $year.'-'.$month;
213+
} elseif(preg_match("@^[^0-9]?(\d{1,2})[^0-9](\d{1,2})[^0-9]?$@", $_SESSION['search'][$list_name][$search_prefix.$field], $matches)){ // 04.10.
214+
if($monthpos < $daypos){
215+
$month = $matches[1];
216+
$day = $matches[2];
217+
} else {
218+
$month = $matches[2];
219+
$day = $matches[1];
220+
}
221+
$searchval = $month.'-'.$day;
222+
} elseif (preg_match("@^".$full_date_regex."$@", $_SESSION['search'][$list_name][$search_prefix.$field], $matches)) {
223+
//print_r($matches);
224+
$day = $matches[$daypos];
225+
$month = $matches[$monthpos];
226+
$year = $matches[$yearpos];
227+
$searchval = $year.'-'.$month.'-'.$day;
228+
}
229+
}
230+
184231
// if($_REQUEST[$search_prefix.$field] != '') $sql_where .= " $field ".$i["op"]." '".$i["prefix"].$_REQUEST[$search_prefix.$field].$i["suffix"]."' and";
185-
if(isset($_SESSION['search'][$list_name][$search_prefix.$field]) && $_SESSION['search'][$list_name][$search_prefix.$field] != ''){
186-
$sql_where .= " ".($table != ''? $table.'.' : $this->listDef['table'].'.')."$field ".$i['op']." '".$app->db->quote($i['prefix'].$_SESSION['search'][$list_name][$search_prefix.$field].$i['suffix'])."' and";
232+
if(isset($searchval) && $searchval != ''){
233+
$sql_where .= " ".($table != ''? $table.'.' : $this->listDef['table'].'.')."$field ".$i['op']." '".$app->db->quote($i['prefix'].$searchval.$i['suffix'])."' and";
187234
}
188235
}
189236
}

0 commit comments

Comments
 (0)