From 8431629ed22da02cb86878cc1451e4085efcc221 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Mon, 23 Mar 2015 08:38:30 +0000 Subject: [PATCH] * All apps: nummeric custom-fields (float or new integer) sort nummeric in lists --- .../inc/class.customfields_widget.inc.php | 2 ++ ...lass.etemplate_widget_customfields.inc.php | 1 + etemplate/inc/class.so_sql_cf.inc.php | 19 +++++++++++++++--- infolog/inc/class.infolog_so.inc.php | 15 ++++++++++++-- phpgwapi/inc/class.egw_db.inc.php | 20 +++++++++++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/etemplate/inc/class.customfields_widget.inc.php b/etemplate/inc/class.customfields_widget.inc.php index 909b1becaa..03d7eff32f 100644 --- a/etemplate/inc/class.customfields_widget.inc.php +++ b/etemplate/inc/class.customfields_widget.inc.php @@ -45,6 +45,7 @@ class customfields_widget */ var $cf_types = array( 'text' => 'Text', + 'int' => 'Integer', 'float' => 'Float', 'label' => 'Label', 'select' => 'Selectbox', @@ -333,6 +334,7 @@ class customfields_widget } break; case 'float': + case 'int': $known_options = array('min'=>null, 'max'=>null,'size' => $field['len'], 'precision' => null); $options = array_merge($known_options, $field['values']); $input =& boetemplate::empty_cell($field['type'],$this->prefix.$lname,array( diff --git a/etemplate/inc/class.etemplate_widget_customfields.inc.php b/etemplate/inc/class.etemplate_widget_customfields.inc.php index cdc34868f5..2425c7c58b 100644 --- a/etemplate/inc/class.etemplate_widget_customfields.inc.php +++ b/etemplate/inc/class.etemplate_widget_customfields.inc.php @@ -29,6 +29,7 @@ class etemplate_widget_customfields extends etemplate_widget_transformer */ protected static $cf_types = array( 'text' => 'Text', + 'int' => 'Integer', 'float' => 'Float', 'label' => 'Label', 'select' => 'Selectbox', diff --git a/etemplate/inc/class.so_sql_cf.inc.php b/etemplate/inc/class.so_sql_cf.inc.php index 8ad716ceb7..824751c92b 100644 --- a/etemplate/inc/class.so_sql_cf.inc.php +++ b/etemplate/inc/class.so_sql_cf.inc.php @@ -534,8 +534,21 @@ class so_sql_cf extends so_sql // we found a customfield, so we split that part by space char in order to get Sorting Direction and Fieldname $buff = explode(' ',trim($v)); $orderDir = array_pop($buff); - $key = trim(implode(' ',$buff)); - $order_by = str_replace($v,'extra_order.'.$this->extra_value.' IS NULL,extra_order.'.$this->extra_value.' '.$orderDir,$order_by); + $key = substr(trim(implode(' ',$buff)), 1); + switch($this->customfields[$key]['type']) + { + case 'int': + $order_by = str_replace($v, 'extra_order.'.$this->extra_value.' IS NULL,'. + $this->db->to_int('extra_order.'.$this->extra_value).' '.$orderDir, $order_by); + break; + case 'float': + $order_by = str_replace($v, 'extra_order.'.$this->extra_value.' IS NULL,'. + $this->db->to_double('extra_order.'.$this->extra_value).' '.$orderDir, $order_by); + break; + default: + $order_by = str_replace($v, 'extra_order.'.$this->extra_value.' IS NULL,extra_order.'. + $this->extra_value.' '.$orderDir, $order_by); + } // postgres requires that expressions in order by appear in the columns of a distinct select if ($this->db->Type != 'mysql') { @@ -546,7 +559,7 @@ class so_sql_cf extends so_sql $extra_cols[] = 'extra_order.'.$this->extra_value; $extra_cols[] = 'extra_order.'.$this->extra_value.' IS NULL'; } - $join .= $this->extra_join_order.' AND extra_order.'.$this->extra_key.'='.$this->db->quote(substr($key,1)); + $join .= $this->extra_join_order.' AND extra_order.'.$this->extra_key.'='.$this->db->quote($key); } } } diff --git a/infolog/inc/class.infolog_so.inc.php b/infolog/inc/class.infolog_so.inc.php index 0560b3b1ee..538d436e8f 100644 --- a/infolog/inc/class.infolog_so.inc.php +++ b/infolog/inc/class.infolog_so.inc.php @@ -712,7 +712,7 @@ class infolog_so if ($val[0] == '#') { $sortbycf = substr($val,1); - $val = "cfsortcrit"; + $val = "cfsortcrit IS NULL,cfsortcrit"; } else { @@ -862,7 +862,18 @@ class infolog_so $info_customfield = ''; if ($sortbycf != '') { - $info_customfield = ", (SELECT DISTINCT info_extra_value FROM $this->extra_table sub2 WHERE sub2.info_id=main.info_id AND info_extra_name=".$this->db->quote($sortbycf).") AS cfsortcrit "; + $sort_col = "(SELECT DISTINCT info_extra_value FROM $this->extra_table sub2 WHERE sub2.info_id=main.info_id AND info_extra_name=".$this->db->quote($sortbycf).")"; + if (!isset($custom_fields)) $custom_fields = config::get_customfields('infolog'); + switch($custom_fields[$sortbycf]['type']) + { + case 'int': + $sort_col = $this->db->to_int($sort_col); + break; + case 'float': + $sort_col = $this->db->to_double($sort_col); + break; + } + $info_customfield = ", $sort_col AS cfsortcrit "; } //echo "SELECT $distinct main.* $info_customfield $sql_query $ordermethod"."
"; do diff --git a/phpgwapi/inc/class.egw_db.inc.php b/phpgwapi/inc/class.egw_db.inc.php index eeb67a1c9c..9efbd8ff46 100644 --- a/phpgwapi/inc/class.egw_db.inc.php +++ b/phpgwapi/inc/class.egw_db.inc.php @@ -1300,6 +1300,24 @@ class egw_db return false; } + /** + * Cast a column or sql expression to integer, necessary at least for postgreSQL or MySQL for sorting + * + * @param string $expr + * @return string + */ + function to_double($expr) + { + switch($this->Type) + { + case 'pgsql': + return $expr.'::double'; + case 'mysql': + return 'CAST('.$expr.' AS DECIMAL(24,3))'; + } + return $expr; + } + /** * Cast a column or sql expression to integer, necessary at least for postgreSQL * @@ -1312,6 +1330,8 @@ class egw_db { case 'pgsql': return $expr.'::integer'; + case 'mysql': + return 'CAST('.$expr.' AS SIGNED)'; } return $expr; }