Skip to content
This repository
Browse code

NEW Reinstated filters in ReportAdmin

- Moved report table generation back to its original location in Report->getCMSFields(),
in order to keep it customisable rather than duplicating it in ReportAdmin.
- Using History.js to set URL state and reload panel
- Namespacing filter GET parameters in order to only include them in URL state
  • Loading branch information...
commit 57c615d6b232deb00e0275227ae6e5d109b625d0 1 parent d77c43d
Ingo Schommer authored July 12, 2012
42  code/controllers/ReportAdmin.php
@@ -51,6 +51,7 @@ public function init() {
51 51
 
52 52
 		// Always block the HtmlEditorField.js otherwise it will be sent with an ajax request
53 53
 		Requirements::block(FRAMEWORK_DIR . '/javascript/HtmlEditorField.js');
  54
+		Requirements::javascript(CMS_DIR . '/javascript/ReportAdmin.js');
54 55
 	}
55 56
 
56 57
 	/**
@@ -147,48 +148,12 @@ function providePermissions() {
147 148
 	}
148 149
 
149 150
 	public function getEditForm($id = null, $fields = null) {
150  
-		$fields = new FieldList();
151  
-		
152 151
 		$report = $this->reportObject;
153  
-
154 152
 		if($report) {
155  
-			// List all reports
156  
-			$gridFieldConfig = GridFieldConfig::create()->addComponents(
157  
-				new GridFieldToolbarHeader(),
158  
-				new GridFieldSortableHeader(),
159  
-				new GridFieldDataColumns(),
160  
-				new GridFieldPaginator(),
161  
-				new GridFieldPrintButton(),
162  
-				new GridFieldExportButton()
163  
-			);
164  
-			$gridField = new GridField('Report',$report->title(), $report->sourceRecords(array(), null, null), $gridFieldConfig);
165  
-			$columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns');
166  
-			$displayFields = array();
167  
-			$fieldCasting = array();
168  
-			$fieldFormatting = array();
169  
-			
170  
-			// Parse the column information
171  
-			foreach($report->columns() as $source => $info) {
172  
-				if(is_string($info)) $info = array('title' => $info);
173  
-				
174  
-				if(isset($info['formatting'])) $fieldFormatting[$source] = $info['formatting'];
175  
-				if(isset($info['csvFormatting'])) $csvFieldFormatting[$source] = $info['csvFormatting'];
176  
-				if(isset($info['casting'])) $fieldCasting[$source] = $info['casting'];
177  
-
178  
-				if(isset($info['link']) && $info['link']) {
179  
-					$link = singleton('CMSPageEditController')->Link('show');
180  
-					$fieldFormatting[$source] = '<a href=\"' . $link . '/$ID\">$value</a>';
181  
-				}
182  
-
183  
-				$displayFields[$source] = isset($info['title']) ? $info['title'] : $source;
184  
-			}
185  
-			$columns->setDisplayFields($displayFields);
186  
-			$columns->setFieldCasting($fieldCasting);
187  
-			$columns->setFieldFormatting($fieldFormatting);
188  
-
189  
-			$fields->push($gridField);
  153
+			$fields = $report->getCMSFields();
190 154
 		} else {
191 155
 			// List all reports
  156
+			$fields = new FieldList();
192 157
 			$gridFieldConfig = GridFieldConfig::create()->addComponents(
193 158
 				new GridFieldToolbarHeader(),
194 159
 				new GridFieldSortableHeader(),
@@ -210,6 +175,7 @@ public function getEditForm($id = null, $fields = null) {
210 175
 		$actions = new FieldList();
211 176
 		$form = new Form($this, "EditForm", $fields, $actions);
212 177
 		$form->addExtraClass('cms-edit-form cms-panel-padded center ' . $this->BaseCSSClasses());
  178
+		$form->loadDataFrom($this->request->getVars());
213 179
 
214 180
 		$this->extend('updateEditForm', $form);
215 181
 
175  code/reports/Report.php
@@ -97,8 +97,7 @@ function description() {
97 97
 	 */
98 98
 	function sourceQuery($params) {
99 99
 		if($this->hasMethod('sourceRecords')) {
100  
-			$query = new SS_Report_FakeQuery($this, 'sourceRecords', $params);
101  
-			return $query;
  100
+			return $this->sourceRecords()->dataQuery();
102 101
 		} else {
103 102
 			user_error("Please override sourceQuery()/sourceRecords() and columns() or, if necessary, override getReportField()", E_USER_ERROR);
104 103
 		}
@@ -108,14 +107,20 @@ function sourceQuery($params) {
108 107
 	 * Return a SS_List records for this report.
109 108
 	 */
110 109
 	function records($params) {
111  
-		if($this->hasMethod('sourceRecords')) return $this->sourceRecords($params, null, null);
112  
-		else {
  110
+		if($this->hasMethod('sourceRecords')) {
  111
+			return $this->sourceRecords($params, null, null);
  112
+		} else {
113 113
 			$query = $this->sourceQuery();
114  
-			return singleton($this->dataClass())->buildDataObjectSet($query->execute(), "DataObjectSet", $query);
  114
+			$results = new ArrayList();
  115
+			foreach($query->execute() as $data) {
  116
+				$class = $this->dataClass();
  117
+				$result = new $class($data);
  118
+				$results->push($result);
  119
+			}
  120
+			return $results;
115 121
 		}
116 122
 	}
117 123
 
118  
-
119 124
 	/**
120 125
 	 * Return the data class for this report
121 126
 	 */
@@ -132,8 +137,6 @@ function getLink($action = null) {
132 137
 		);
133 138
 	}
134 139
 
135  
-
136  
-
137 140
 	/**
138 141
 	 * @deprecated 3.0
139 142
 	 * All subclasses of SS_Report now appear in the report admin, no need to register or unregister.
@@ -171,7 +174,6 @@ static function add_excluded_reports($reportClass) {
171 174
 		}
172 175
 	}
173 176
 
174  
-
175 177
 	/**
176 178
 	 * Return an array of excluded reports. That is, reports that will not be included in
177 179
 	 * the list of reports in report admin in the CMS.
@@ -225,31 +227,31 @@ static function get_reports() {
225 227
 	 * @return FieldList
226 228
 	 */
227 229
 	function getCMSFields() {
228  
-		$fields = new FieldList(
229  
-			new LiteralField(
230  
-				'ReportTitle', 
231  
-				 "<h3>{$this->title()}</h3>"
232  
-			)
233  
-		);
  230
+		$fields = new FieldList();
  231
+
  232
+		if($title = $this->title()) {
  233
+			$fields->push(new LiteralField('ReportTitle', "<h3>{$title}</h3>"));
  234
+		}
234 235
 		
235  
-		if($this->description()) $fields->push(
236  
-			new LiteralField('ReportDescription', "<p>" . $this->description() . "</p>"));
  236
+		if($description = $this->description()) {
  237
+			$fields->push(new LiteralField('ReportDescription', "<p>" . $description . "</p>"));
  238
+		}
237 239
 			
238 240
 		// Add search fields is available
239  
-		if($params = $this->parameterFields()) {
240  
-			$filters = new FieldGroup('Filters');
241  
-			foreach($params as $param) {
242  
-				if ($param instanceof HiddenField) $fields->push($param);
243  
-				else $filters->push($param);
  241
+		if($fields = $this->parameterFields()) {
  242
+			foreach($fields as $field) {
  243
+				// Namespace fields for easier handling in form submissions
  244
+				$field->setName(sprintf('filters[%s]', $field->getName()));
  245
+				$field->addExtraClass('no-change-track'); // ignore in changetracker
  246
+				$fields->push($field);
244 247
 			}
245  
-			$fields->push($filters);
246 248
 
247 249
 			// Add a search button
248  
-			$fields->push(new FormAction('updatereport', 'Filter'));
  250
+			$fields->push(new FormAction('updatereport', _t('GridField.Filter')));
249 251
 		}
250 252
 		
251 253
 		$fields->push($this->getReportField());
252  
-		
  254
+
253 255
 		$this->extend('updateCMSFields', $fields);
254 256
 		
255 257
 		return $fields;
@@ -263,7 +265,7 @@ function getCMSActions() {
263 265
 	}
264 266
 	
265 267
 	/**
266  
-	 * Return a field, such as a {@link ComplexTableField} that is
  268
+	 * Return a field, such as a {@link GridField} that is
267 269
 	 * used to show and manipulate data relating to this report.
268 270
 	 * 
269 271
 	 * Generally, you should override {@link columns()} and {@link records()} to make your report,
@@ -272,11 +274,24 @@ function getCMSActions() {
272 274
 	 * @return FormField subclass
273 275
 	 */
274 276
 	function getReportField() {
275  
-		$columnTitles = array();
276  
-		$fieldFormatting = array();
277  
-		$csvFieldFormatting = array();
  277
+		// TODO Remove coupling with global state
  278
+		$params = isset($_REQUEST['filters']) ? $_REQUEST['filters'] : array();
  279
+		$items = $this->sourceRecords($params, null, null);
  280
+
  281
+		$gridFieldConfig = GridFieldConfig::create()->addComponents(
  282
+			new GridFieldToolbarHeader(),
  283
+			new GridFieldSortableHeader(),
  284
+			new GridFieldDataColumns(),
  285
+			new GridFieldPaginator(),
  286
+			new GridFieldPrintButton(),
  287
+			new GridFieldExportButton()
  288
+		);
  289
+		$gridField = new GridField('Report',$this->title(), $items, $gridFieldConfig);
  290
+		$columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns');
  291
+		$displayFields = array();
278 292
 		$fieldCasting = array();
279  
-		
  293
+		$fieldFormatting = array();
  294
+
280 295
 		// Parse the column information
281 296
 		foreach($this->columns() as $source => $info) {
282 297
 			if(is_string($info)) $info = array('title' => $info);
@@ -284,28 +299,19 @@ function getReportField() {
284 299
 			if(isset($info['formatting'])) $fieldFormatting[$source] = $info['formatting'];
285 300
 			if(isset($info['csvFormatting'])) $csvFieldFormatting[$source] = $info['csvFormatting'];
286 301
 			if(isset($info['casting'])) $fieldCasting[$source] = $info['casting'];
287  
-			$columnTitles[$source] = isset($info['title']) ? $info['title'] : $source;
288  
-		}
289  
-		
290  
-		// To do: implement pagination
291  
-		$query = $this->sourceQuery($_REQUEST);
292  
-			
293  
-		$tlf = new TableListField('ReportContent', $this->dataClass(), $columnTitles);
294  
-		$tlf->setCustomQuery($query);
295  
-		$tlf->setShowPagination(true);
296  
-		$tlf->setPageSize(50);
297  
-		$tlf->setPermissions(array('export', 'print'));
298  
-		
299  
-		// Hack to figure out if we are printing
300  
-		if (isset($_REQUEST['url']) && array_pop(explode('/', $_REQUEST['url'])) == 'printall') {
301  
-			$tlf->setTemplate('SSReportTableField');
  302
+
  303
+			if(isset($info['link']) && $info['link']) {
  304
+				$link = singleton('CMSPageEditController')->Link('show');
  305
+				$fieldFormatting[$source] = '<a href=\"' . $link . '/$ID\">$value</a>';
  306
+			}
  307
+
  308
+			$displayFields[$source] = isset($info['title']) ? $info['title'] : $source;
302 309
 		}
303  
-		
304  
-		if($fieldFormatting) $tlf->setFieldFormatting($fieldFormatting);
305  
-		if($csvFieldFormatting) $tlf->setCSVFieldFormatting($csvFieldFormatting);
306  
-		if($fieldCasting) $tlf->setFieldCasting($fieldCasting);
  310
+		$columns->setDisplayFields($displayFields);
  311
+		$columns->setFieldCasting($fieldCasting);
  312
+		$columns->setFieldFormatting($fieldFormatting);
307 313
 
308  
-		return $tlf;
  314
+		return $gridField;
309 315
 	}
310 316
 	
311 317
 	/**
@@ -332,76 +338,7 @@ function canView($member = null) {
332 338
 	function TreeTitle() {
333 339
 		return $this->title();
334 340
 	}
335  
-	
336  
-
337  
-
338  
-}
339  
-
340  
-/**
341  
- * This is an object that can be used to dress up a more complex querying mechanism in the clothing
342  
- * of a SQLQuery object.  This means that you can inject it into a TableListField.
343  
- * 
344  
- * Use it like this:
345  
- * 
346  
- *     function sourceQuery($params) {
347  
- *         return new SS_Report_FakeQuery($this, 'sourceRecords', $params)
348  
- *     }
349  
- *     function sourceRecords($params, $sort, $limit) {
350  
- *         // Do some stuff
351  
- *         // Return a SS_List of actual objects.
352  
- *     }
353  
- * 
354  
- * This object is used by the default implementation of sourceQuery() on SS_Report, to make use of
355  
- * a sourceReords() method if one exists.
356  
- */
357  
-class SS_Report_FakeQuery extends SQLQuery {
358  
-	public $orderby;
359  
-	public $limit;
360  
-	
361  
-	protected $obj, $method, $params;
362  
-	
363  
-	function __construct($obj, $method, $params) {
364  
-		$this->obj = $obj;
365  
-		$this->method = $method;
366  
-		$this->params = $params;
367  
-	}
368 341
 
369  
-	/**
370  
-	 * Provide a method that will return a list of columns that can be used to sort.
371  
-	 */
372  
-	function setSortColumnMethod($sortColMethod) {
373  
-		$this->sortColMethod = $sortColMethod;
374  
-	}
375  
-
376  
-	function limit($limit, $offset = 0) {
377  
-		$this->limit = $limit;
378  
-	}
379  
-	
380  
-	function unlimitedRowCount($column = null) {
381  
-		$source = $this->obj->{$this->method}($this->params, null, null);
382  
-		return $source ? $source->Count() : 0;
383  
-	}
384  
-	
385  
-	function execute() {
386  
-		$output = array();
387  
-		$source = $this->obj->{$this->method}($this->params, $this->orderby, $this->limit);
388  
-		if($source) foreach($source as $item) {
389  
-			$mapItem = $item->toMap();
390  
-			$mapItem['RecordClassName'] = get_class($item);
391  
-			$output[] = $mapItem;
392  
-		}
393  
-		return $output;
394  
-	}
395  
-	
396  
-	function canSortBy($fieldName) {
397  
-		$fieldName = preg_replace('/(\s+?)(A|DE)SC$/', '', $fieldName);
398  
-		if($this->sortColMethod) {
399  
-			$columns = $this->obj->{$this->sortColMethod}();
400  
-			return in_array($fieldName, $columns);
401  
-		} else {
402  
-			return false;
403  
-		}
404  
-	}
405 342
 }
406 343
 
407 344
 /**
18  javascript/ReportAdmin.js
... ...
@@ -0,0 +1,18 @@
  1
+/**
  2
+ * File: ReportAdmin.js
  3
+ */
  4
+
  5
+(function($) {
  6
+	$.entwine('ss', function($){
  7
+		$('.ReportAdmin .cms-edit-form').entwine({
  8
+			onsubmit: function(e) {
  9
+				var url = document.location.href, params = this.find(':input[name^=filters]').serializeArray();
  10
+				params = $.grep(params, function(param) {return (param.value);}); // filter out empty
  11
+				if(params) url = $.path.addSearchParams(url, params);
  12
+				$('.cms-container').loadPanel(url);
  13
+				return false;
  14
+			}
  15
+		});
  16
+	
  17
+	});
  18
+})(jQuery);

0 notes on commit 57c615d

Please sign in to comment.
Something went wrong with that request. Please try again.