Skip to content
This repository
Browse code

ENHANCEMENT: Use GridFields to show reports in the ReportAdmin

  • Loading branch information...
commit 4c76c8563c314e4abaadf0f7315e72c6125b1a1f 1 parent 029f833
Andrew O'Neil authored April 04, 2012
70  code/controllers/ReportAdmin.php
@@ -58,9 +58,9 @@ function canView($member = null) {
58 58
 		return false;
59 59
 	}
60 60
 
61  
-	function currentPageID() {
  61
+	function currentReport() {
62 62
 		$id = parent::currentPageID();
63  
-		$reports = SS_Report::get_reports('ReportAdmin');
  63
+		$reports = SS_Report::get_reports();
64 64
 		return (isset($reports[$id])) ? $reports[$id] : null;
65 65
 	}
66 66
 
@@ -72,7 +72,7 @@ function currentPageID() {
72 72
 	 */
73 73
 	public function Reports() {
74 74
  		$output = new ArrayList();
75  
-		foreach(SS_Report::get_reports('ReportAdmin') as $report) {
  75
+		foreach(SS_Report::get_reports() as $report) {
76 76
 			if($report->canView()) $output->push($report);
77 77
 		}
78 78
 		return $output;
@@ -90,7 +90,7 @@ public function Reports() {
90 90
 	 * @return boolean
91 91
 	 */
92 92
 	public static function has_reports() {
93  
-		return sizeof(SS_Report::get_reports('ReportAdmin')) > 0;
  93
+		return sizeof(SS_Report::get_reports()) > 0;
94 94
 	}
95 95
 	
96 96
 	public function updatereport() {
@@ -107,5 +107,67 @@ function providePermissions() {
107 107
 			)
108 108
 		);
109 109
 	}
  110
+
  111
+	public function getEditForm($id = null, $fields = null) {
  112
+		$fields = new FieldList();
  113
+		
  114
+		$report = $this->currentReport();
  115
+
  116
+		if($report) {
  117
+			// List all reports
  118
+			$gridFieldConfig = GridFieldConfig::create()->addComponents(
  119
+				new GridFieldToolbarHeader(),
  120
+				new GridFieldSortableHeader(),
  121
+				new GridFieldDataColumns(),
  122
+				new GridFieldPaginator(),
  123
+				new GridFieldPrintButton(),
  124
+				new GridFieldExportButton()
  125
+			);
  126
+			$gridField = new GridField('Report',$report->title(), $report->sourceRecords(array(), null, null), $gridFieldConfig);
  127
+			$displayFields = array();
  128
+			$fieldCasting = array();
  129
+			$fieldFormatting = array();
  130
+			
  131
+			// Parse the column information
  132
+			foreach($report->columns() as $source => $info) {
  133
+				if(is_string($info)) $info = array('title' => $info);
  134
+				
  135
+				if(isset($info['formatting'])) $fieldFormatting[$source] = $info['formatting'];
  136
+				if(isset($info['csvFormatting'])) $csvFieldFormatting[$source] = $info['csvFormatting'];
  137
+				if(isset($info['casting'])) $fieldCasting[$source] = $info['casting'];
  138
+
  139
+				$displayFields[$source] = isset($info['title']) ? $info['title'] : $source;
  140
+			}
  141
+			$gridField->setDisplayFields($displayFields);
  142
+			$gridField->setFieldCasting($fieldCasting);
  143
+			$gridField->setFieldFormatting($fieldFormatting);
  144
+
  145
+			$fields->push($gridField);
  146
+		} else {
  147
+			// List all reports
  148
+			$gridFieldConfig = GridFieldConfig::create()->addComponents(
  149
+				new GridFieldToolbarHeader(),
  150
+				new GridFieldSortableHeader(),
  151
+				new GridFieldDataColumns()
  152
+			);
  153
+			$gridField = new GridField('Reports','Reports', $this->Reports(), $gridFieldConfig);
  154
+			$gridField->setDisplayFields(array(
  155
+				'title' => 'Title',
  156
+				'description' => 'Description'
  157
+			));
  158
+			$gridField->setFieldFormatting(array(
  159
+				'title' => '<a href=\"$Link\">$value</a>'
  160
+			));
  161
+			$fields->push($gridField);
  162
+		}
  163
+
  164
+		$actions = new FieldList();
  165
+		$form = new Form($this, "EditForm", $fields, $actions);
  166
+		$form->addExtraClass('cms-edit-form cms-panel-padded center ' . $this->BaseCSSClasses());
  167
+
  168
+		$this->extend('updateEditForm', $form);
  169
+
  170
+		return $form;
  171
+	}
110 172
 }
111 173
 
6  code/reports/Report.php
@@ -134,6 +134,10 @@ function dataClass() {
134 134
 		return $this->dataClass;
135 135
 	}
136 136
 
  137
+	function getLink() {
  138
+		return 'admin/reports/show/' . $this->class;
  139
+	}
  140
+
137 141
 
138 142
 
139 143
 	/**
@@ -197,7 +201,7 @@ static function get_reports() {
197 201
 				if (in_array($report, self::$excluded_reports)) continue;   //don't use the SS_Report superclass
198 202
 				$reportObj = new $report;
199 203
 				if (method_exists($reportObj,'sort')) $reportObj->sort = $reportObj->sort();  //use the sort method to specify the sort field
200  
-				$reportsArray[] = $reportObj;
  204
+				$reportsArray[$report] = $reportObj;
201 205
 			}
202 206
 		}
203 207
 
25  templates/Includes/ReportAdmin_Content.ss
... ...
@@ -1,13 +1,28 @@
1  
-<div class="cms-content center $BaseCSSClasses" data-layout-type="border">
  1
+<div class="cms-content center ss-tabset $BaseCSSClasses" data-layout-type="border">
2 2
 
3 3
 	<div class="cms-content-header north">
4  
-		<h2><% _t('REPORTS','Reports') %></h2>
5  
-	</div>
  4
+		<div>
  5
+			<% control EditForm %>
  6
+				<% if Backlink %>
  7
+					<a class="backlink ss-ui-button cms-panel-link" data-icon="back" href="$Backlink">
  8
+						<% _t('Back', 'Back') %>
  9
+					</a>
  10
+				<% end_if %>
6 11
 
7  
-	$Tools
  12
+				<h2 id="page-title-heading">
  13
+				<% control Controller %>
  14
+					<% include CMSBreadcrumbs %>
  15
+				<% end_control %>
  16
+				</h2>
  17
+			<% end_control %>
  18
+		
  19
+		</div>
  20
+	</div>
8 21
 
9  
-	<div class="cms-content-fields center ui-widget-content">
  22
+	<div class="cms-content-fields center ui-widget-content" data-layout-type="border">
  23
+		
10 24
 		$EditForm
  25
+		
11 26
 	</div>
12 27
 	
13 28
 </div>
11  templates/Includes/ReportAdmin_Tools.ss
... ...
@@ -1,11 +0,0 @@
1  
-<div class="cms-content-tools west" id="cms-content-tools-ReportAdmin">
2  
-	
3  
-	<ul class="ui-widget-content">
4  
-	<% control Reports %>
5  
-		<li id="record-$ID">
6  
-			<a href="admin/reports/show/$ID" title="$TreeDescription">$TreeTitle</a>
7  
-		</li>
8  
-	<% end_control %>
9  
-	</ul>
10  
-	
11  
-</div>

0 notes on commit 4c76c85

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