From 96b844e349e6fd4f764f50f27e11dacc98c15d39 Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <renaud.legac@free.fr>
Date: Sun, 12 Dec 2010 16:49:20 +0000
Subject: [PATCH] Add a new tool to define grid filter.

---
 modules/plugin_dbui/cfgsvc.py | 45 +++++++++++++++++++++++++++++++++++
 modules/plugin_dbui/tools.py  | 34 +++++++++++++++++++-------
 2 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/modules/plugin_dbui/cfgsvc.py b/modules/plugin_dbui/cfgsvc.py
index 2ceab7f5..ea8d80f7 100644
--- a/modules/plugin_dbui/cfgsvc.py
+++ b/modules/plugin_dbui/cfgsvc.py
@@ -241,6 +241,50 @@ class CfgSvc(object):
 			
 		return li
 
+
+	def _getGridFilter(self, tablename):
+		""" Generate configuration object for the grid filter
+		
+		"""
+		di = {}
+		
+		if tablename in self._grid_filters:
+			
+			di = {'xtype':'fieldset',
+				  'defaults': {'anchor': '100%'},
+				  'items': [],
+				  'title': self._translate('Filter %s' % tablename)}
+			
+			filters = self._grid_filters[tablename].filters
+			for (fieldname, operator, comment) in filters:
+				
+				# get the standard form field
+				cfg = self._getFormField(tablename, fieldname)
+				
+				# remove default value for fields which are not combobox
+				if cfg['xtype'] != 'xcombobox':
+					cfg['value'] = ''
+					cfg['allowBlank'] = True
+				
+				# replace all textarea by textfield
+				if cfg['xtype'] == 'textarea':
+					cfg['xtype'] = 'textfield'
+				
+				# prepare the name for the where close
+				cfg['name'] = '[%s.%s]' % (tablename, fieldname)	
+					
+				# store information to customize the filter
+				cfg['filterComment'] = comment  
+				cfg['filterOperator'] = operator
+				cfg['filterType'] = self._db[tablename][fieldname].type
+				
+				di['items'].append(cfg)
+		
+		extjs = self._grid_filters[tablename].extjs
+		di.update(extjs)
+		
+		return di
+	
 	
 	def _getJsonStore(self, table):
 		""" Generate configuration object for JSON stores 
@@ -335,6 +379,7 @@ class CfgSvc(object):
 		model = {}	
 		model["colModel"] = self._getColumnsModel(tablename)
 		model["formModel"] = formModel
+		model["filterModel"] = self._getGridFilter(tablename)
 		model["store"] = self._getJsonStore(tablename)
 		model["table"] = tablename
 		model["title"] = str(self._translate(tablename.title()))
diff --git a/modules/plugin_dbui/tools.py b/modules/plugin_dbui/tools.py
index e3163f3d..54d05350 100644
--- a/modules/plugin_dbui/tools.py
+++ b/modules/plugin_dbui/tools.py
@@ -83,9 +83,9 @@ def customize_form_with_field_set(tablename, title, fields, spacer=0, **extjs):
 				Useful to tune the final height of the field set.
 				
 			extjs
-				Additional Ext JS configuration parameters
-				apply to the fieldset (See the documentation 
-				of Ext.form.FieldSet)
+				additional keyword arguments corresponding
+				to Ext.form.FieldSet configuration parameters
+				(See the Ext JS APIdocumentation)
 	
 	"""
 	p = _init_tool('field_sets')
@@ -157,9 +157,9 @@ def customize_grid_with_template_column(tablename,
 				via their fieldname.
 
 			extjs
-				Additional Ext JS configuration parameters
-				to be apply on the column 
-				(See the documentation of Ext.gid.Column)
+				additional keyword arguments corresponding
+				to Ext.grid.Column configuration parameters
+				(See the documentation of Ext.grid.Column)
 				
 	"""
 	p = _init_tool('template_columns')
@@ -191,12 +191,30 @@ def define_combined_field():
 	p = _init_tool('combined_fields')
 
 
-def define_grid_filter():
+def define_grid_filter(tablename, filters, **extjs):
 	""" Helper tool to define filter associate to grid.
-	
+
+			tablename
+				name of the database table
+				
+			filters
+				a list of tuple.
+				Each tuple contains three columns:
+						-- a string with the database field
+						-- a string with the operator
+						   the list of valid operator is defined in
+						   the method dbsvc._encode_query
+						-- a string with a comment for the tool tip
+			extjs
+				additional keyword arguments corresponding
+				to the configuration parameter of Ext.form.FieldSet
+				(see the Ext JS API documentation)
 	"""
 	p = _init_tool('grid_filters')
 
+	di = Storage(filters=filters, extjs=extjs)
+	p.dbui.grid_filters[tablename] = di
+
 
 def define_wizard():
 	""" Helper tool to define wizard in form.
-- 
GitLab