Skip to content
This repository
Browse code

API-CHANGE: SSF-168 initial rearranging the SS_Report class for use i…

…n SS3. Deprecating unnecessary methods, moving code around, rewriting get_reports method and writing unit tests
  • Loading branch information...
commit ee220bbccec4c40258c74288ba30c383b5ed6868 1 parent fe28058
Julian Seidenberg authored April 03, 2012
179  code/reports/Report.php
@@ -35,20 +35,14 @@
35 35
  * Showing reports to the user
36 36
  * ===========================
37 37
  * 
38  
- * Right now, all subclasses of SS_Report will be shown in the ReportAdmin.  However, we are planning
39  
- * on adding an explicit registration mechanism, so that you can decide which reports go in the
40  
- * report admin, and which go elsewhere (such as the side panel in the CMS).
  38
+ * Right now, all subclasses of SS_Report will be shown in the ReportAdmin. In SS3 there is only
  39
+ * one place where reports can go, so this class is greatly simplifed from from its version in SS2.
41 40
  * 
42 41
  * @package cms
43 42
  * @subpackage reports
44 43
  */
45 44
 class SS_Report extends ViewableData {
46 45
 	/**
47  
-	 * Report registry populated by {@link SS_Report::register()}
48  
-	 */
49  
-	private static $registered_reports = array();
50  
-
51  
-	/**
52 46
 	 * This is the title of the report,
53 47
 	 * used by the ReportAdmin templates.
54 48
 	 *
@@ -70,6 +64,22 @@ class SS_Report extends ViewableData {
70 64
 	 * Set by overriding in your subclass.
71 65
 	 */
72 66
 	protected $dataClass = 'SiteTree';
  67
+
  68
+	/**
  69
+	 * A field that specifies the sort order of this report
  70
+	 * @var int
  71
+	 */
  72
+	protected $sort = 0;
  73
+
  74
+	/**
  75
+	 * Reports which should not be collected and returned in get_reports
  76
+	 * @var array
  77
+	 */
  78
+	public static $excluded_reports = array(
  79
+		'SS_Report',
  80
+		'SS_ReportWrapper',
  81
+		'SideReportWrapper'
  82
+	);
73 83
 	
74 84
 	/**
75 85
 	 * Return the title of this report.
@@ -94,21 +104,11 @@ function description() {
94 104
 	}
95 105
 	
96 106
 	/**
97  
-	 * Return a FieldList specifying the search criteria for this report.
98  
-	 * 
99  
-	 * Override this method to define search criteria.
100  
-	 */
101  
-	function parameterFields() {
102  
-		return null;
103  
-	}
104  
-
105  
-	/**
106 107
 	 * Return the {@link SQLQuery} that provides your report data.
107 108
 	 */
108 109
 	function sourceQuery($params) {
109 110
 		if($this->hasMethod('sourceRecords')) {
110 111
 			$query = new SS_Report_FakeQuery($this, 'sourceRecords', $params);
111  
-			$query->setSortColumnMethod('sortColumns');
112 112
 			return $query;
113 113
 		} else {
114 114
 			user_error("Please override sourceQuery()/sourceRecords() and columns() or, if necessary, override getReportField()", E_USER_ERROR);
@@ -126,36 +126,75 @@ function records($params) {
126 126
 		}
127 127
 	}
128 128
 
  129
+
129 130
 	/**
130  
-	 * Return an map of columns for your report.
131  
-	 *  - The map keys will be the source columns for your report (in TableListField dot syntax)
132  
-	 *  - The values can either be a string (the column title), or a map containing the following
133  
-	 *    column parameters:
134  
-	 *    - title: The column title
135  
-	 *    - formatting: A formatting string passed to {@link TableListField::setFieldFormatting()}
  131
+	 * Return the data class for this report
136 132
 	 */
137  
-	function columns() {
138  
-		user_error("Please override sourceQuery() and columns() or, if necessary, override getReportField()", E_USER_ERROR);
  133
+	function dataClass() {
  134
+		return $this->dataClass;
139 135
 	}
140  
-	
141  
-	function sortColumns() {
142  
-		return array_keys($this->columns());
  136
+
  137
+
  138
+
  139
+	/**
  140
+	 * @deprecated 3.0
  141
+	 * All subclasses of SS_Report now appear in the report admin, no need to register or unregister.
  142
+	 *
  143
+	 * Register a report.
  144
+	 * @param $list The list to add the report to: "ReportAdmin" or "SideReports"
  145
+	 * @param $reportClass The class of the report to add.
  146
+	 * @param $priority The priority.  Higher numbers will appear furhter up in the reports list.
  147
+	 * The default value is zero.
  148
+	 */
  149
+	static function register($list, $reportClass, $priority = 0) {
143 150
 	}
144  
-	
  151
+
145 152
 	/**
146  
-	 * Return the number of records in this report with no filters applied.
  153
+	 * @deprecated 3.0
  154
+	 * All subclasses of SS_Report now appear in the report admin, no need to register or unregister.
147 155
 	 */
148  
-	function count() {
149  
-		return (int)$this->sourceQuery(array())->unlimitedRowCount();
  156
+	static function unregister($list, $reportClass) {
  157
+		self::excludeReport($reportClass);
150 158
 	}
151  
-	
  159
+
152 160
 	/**
153  
-	 * Return the data class for this report
  161
+	 * Exclude a certain report class from the list of Reports in the CMS
  162
+	 * @static
  163
+	 * @param $reportClass
154 164
 	 */
155  
-	function dataClass() {
156  
-		return $this->dataClass;
  165
+	static function excludeReport($reportClass) {
  166
+		self::$excluded_reports[] = $reportClass;   //add to the excluded reports, so this report doesn't get used
  167
+	}
  168
+
  169
+	/**
  170
+	 * Return the SS_Report objects making up the given list.
  171
+	 * @return An array of SS_Report objects
  172
+	 */
  173
+	static function get_reports() {
  174
+		$reports = ClassInfo::subclassesFor(get_called_class());
  175
+
  176
+		$reportsArray = array();
  177
+		if ($reports && count($reports) > 0) {
  178
+			//collect reports into array with an attribute for 'sort'
  179
+			foreach($reports as $report) {
  180
+				if (in_array($report, self::$excluded_reports)) continue;   //don't use the SS_Report superclass
  181
+				$reportObj = new $report;
  182
+				if (method_exists($reportObj,'sort')) $reportObj->sort = $reportObj->sort();  //use the sort method to specify the sort field
  183
+				$reportsArray[] = $reportObj;
  184
+			}
  185
+		}
  186
+
  187
+		//convert array into ArrayList
  188
+		$list = ArrayList::create($reportsArray);
  189
+
  190
+		//sort
  191
+		$list->sort('sort');
  192
+
  193
+		return $list;
157 194
 	}
158 195
 
  196
+	/////////////////////// UI METHODS ///////////////////////
  197
+
159 198
 
160 199
 	/**
161 200
 	 * Returns a FieldList with which to create the CMS editing form.
@@ -273,72 +312,10 @@ function canView($member = null) {
273 312
 	 * @return string
274 313
 	 */
275 314
 	function TreeTitle() {
276  
-		return $this->title();/* . ' (' . $this->count() . ')'; - this is too slow atm */
277  
-	}
278  
-	
279  
-	/**
280  
-	 * Return the ID of this Report class.
281  
-	 * Because it doesn't have a number, we
282  
-	 * use the class name as the ID.
283  
-	 *
284  
-	 * @return string
285  
-	 */
286  
-	function ID() {
287  
-		return $this->class;
288  
-	}
289  
-	
290  
-	/////////////////////////////////////////////////////////////////////////////////////////////
291  
-	
292  
-	/**
293  
-	 * Register a report.
294  
-	 * @param $list The list to add the report to: "ReportAdmin" or "SideReports"
295  
-	 * @param $reportClass The class of the report to add.
296  
-	 * @param $priority The priority.  Higher numbers will appear furhter up in the reports list.
297  
-	 * The default value is zero.
298  
-	 */
299  
-	static function register($list, $reportClass, $priority = 0) {
300  
-		if(strpos($reportClass, '(') === false && (!class_exists($reportClass) || !is_subclass_of($reportClass,'SS_Report'))) {
301  
-			user_error("SS_Report::register(): '$reportClass' is not a subclass of SS_Report", E_USER_WARNING);
302  
-			return;
303  
-		}
304  
-		
305  
-		self::$registered_reports[$list][$reportClass] = $priority;
306  
-	}
307  
-	
308  
-	/**
309  
-	 * Unregister a report, removing it from the list
310  
-	 */
311  
-	static function unregister($list, $reportClass) {
312  
-		unset(self::$registered_reports[$list][$reportClass]);
  315
+		return $this->title();
313 316
 	}
314 317
 	
315  
-	/**
316  
-	 * Return the SS_Report objects making up the given list.
317  
-	 * @return An array of SS_Report objects
318  
-	 */
319  
-	static function get_reports($list) {
320  
-		$output = array();
321  
-		if(isset(self::$registered_reports[$list])) {
322  
-			$listItems = self::$registered_reports[$list];
323 318
 
324  
-			// Sort by priority, preserving internal order of items with the same priority
325  
-			$groupedItems = array();
326  
-			foreach($listItems as $k => $v) {
327  
-				$groupedItems[$v][] = $k;
328  
-			}
329  
-			krsort($groupedItems);
330  
-			$sortedListItems = call_user_func_array('array_merge', $groupedItems);
331  
-			
332  
-			foreach($sortedListItems as $report) {
333  
-				if(strpos($report,'(') === false) $reportObj = new $report;
334  
-				else $reportObj = eval("return new $report;");
335  
-
336  
-				$output[$reportObj->ID()] = $reportObj;
337  
-			} 
338  
-		}
339  
-		
340  
-		return $output;
341  
-	}
342 319
 
343 320
 }
344 321
 
@@ -370,7 +347,7 @@ function __construct($obj, $method, $params) {
370 347
 		$this->method = $method;
371 348
 		$this->params = $params;
372 349
 	}
373  
-	
  350
+
374 351
 	/**
375 352
 	 * Provide a method that will return a list of columns that can be used to sort.
376 353
 	 */
36  tests/reports/ReportTest.php
... ...
@@ -1,18 +1,34 @@
1 1
 <?php
2 2
 
3 3
 class ReportTest extends SapphireTest {
4  
-	
5  
-	function testCanSortBy() {
6  
-		$report = new SSReportTest_FakeTest();
7  
-		$this->assertTrue($report->sourceQuery(array())->canSortBy('Title ASC'));
8  
-		$this->assertTrue($report->sourceQuery(array())->canSortBy('Title DESC'));
9  
-		$this->assertTrue($report->sourceQuery(array())->canSortBy('Title'));
10  
-	}
11 4
 
12 5
 	function testGetReports() {
13 6
 		$reports = SS_Report::get_reports();
14 7
 		$this->assertNotNull($reports, "Reports returned");
15  
-		Debug::Show($reports);
  8
+		$previousSort = 0;
  9
+		foreach($reports as $report) {
  10
+			$this->assertGreaterThanOrEqual($previousSort, $report->sort, "Reports are in correct sort order");
  11
+			$previousSort = $report->sort;
  12
+		}
  13
+	}
  14
+
  15
+	function testExcludeReport() {
  16
+		$reports = SS_Report::get_reports();
  17
+		$reportNames = array();
  18
+		foreach($reports as $report) {
  19
+			$reportNames[] = $report->class;
  20
+		}
  21
+		$this->assertContains('ReportTest_FakeTest',$reportNames,'ReportTest_FakeTest is in reports list');
  22
+
  23
+		//excluse one report
  24
+		SS_Report::excludeReport('ReportTest_FakeTest');
  25
+
  26
+		$reports = SS_Report::get_reports();
  27
+		$reportNames = array();
  28
+		foreach($reports as $report) {
  29
+			$reportNames[] = $report->class;
  30
+		}
  31
+		$this->assertNotContains('ReportTest_FakeTest',$reportNames,'ReportTest_FakeTest is NOT in reports list');
16 32
 	}
17 33
 }
18 34
 
@@ -39,12 +55,12 @@ function sort() {
39 55
 
40 56
 class ReportTest_FakeTest2 extends SS_Report implements TestOnly {
41 57
 	function title() {
42  
-		return 'Report title';
  58
+		return 'Report title 2';
43 59
 	}
44 60
 	function columns() {
45 61
 		return array(
46 62
 			"Title" => array(
47  
-				"title" => "Page Title"
  63
+				"title" => "Page Title 2"
48 64
 			)
49 65
 		);
50 66
 	}

0 notes on commit ee220bb

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