/
ReportAdmin.php
executable file
·209 lines (172 loc) · 5.22 KB
/
ReportAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Reports section of the CMS.
*
* All reports that should show in the ReportAdmin section
* of the CMS need to subclass {@link SSReport}, and implement
* the appropriate methods and variables that are required.
*
* @see SSReport
*
* @package cms
* @subpackage reports
*/
class ReportAdmin extends LeftAndMain {
static $url_segment = 'reports';
static $url_rule = '/$Action/$ID';
static $menu_title = 'Reports';
static $template_path = null; // defaults to (project)/templates/email
public function init() {
parent::init();
Requirements::javascript(CMS_DIR . '/javascript/ReportAdmin_left.js');
Requirements::javascript(CMS_DIR . '/javascript/ReportAdmin_right.js');
Requirements::css(CMS_DIR . '/css/ReportAdmin.css');
// Set custom options for TinyMCE specific to ReportAdmin
HtmlEditorConfig::get('cms')->setOption('ContentCSS', project() . '/css/editor.css');
HtmlEditorConfig::get('cms')->setOption('Lang', i18n::get_tinymce_lang());
// Always block the HtmlEditorField.js otherwise it will be sent with an ajax request
Requirements::block(SAPPHIRE_DIR . '/javascript/HtmlEditorField.js');
}
/**
* Does the parent permission checks, but also
* makes sure that instantiatable subclasses of
* {@link Report} exist. By default, the CMS doesn't
* include any Reports, so there's no point in showing
*
* @param Member $member
* @return boolean
*/
function canView($member = null) {
if(!$member && $member !== FALSE) {
$member = Member::currentUser();
}
if(!parent::canView($member)) return false;
$hasViewableSubclasses = false;
$subClasses = array_values(ClassInfo::subclassesFor('SSReport'));
foreach($subClasses as $subclass) {
// Remove abstract classes and LeftAndMain
$classReflection = new ReflectionClass($subclass);
if($classReflection->isInstantiable() && $subclass != 'SSReport') {
if(singleton($subclass)->canView()) $hasViewableSubclasses = true;
}
}
return $hasViewableSubclasses;
}
/**
* Return a DataObjectSet of SSReport subclasses
* that are available for use.
*
* @return DataObjectSet
*/
public function Reports() {
$processedReports = array();
$subClasses = ClassInfo::subclassesFor('SSReport');
if($subClasses) {
foreach($subClasses as $subClass) {
if($subClass != 'SSReport') {
$processedReports[] = new $subClass();
}
}
}
$reports = new DataObjectSet($processedReports);
return $reports;
}
/**
* Show a report based on the URL query string.
*
* @param HTTPRequest $request The HTTP request object
*/
public function show($request) {
$params = $request->allParams();
return $this->showWithEditForm($params, $this->reportEditFormFor($params['ID']));
}
/**
* @TODO What does this do?
*
* @param unknown_type $params
* @param unknown_type $editForm
* @return unknown
*/
protected function showWithEditForm($params, $editForm) {
if(isset($params['ID'])) Session::set('currentPage', $params['ID']);
if(isset($params['OtherID'])) Session::set('currentOtherID', $params['OtherID']);
if(Director::is_ajax()) {
SSViewer::setOption('rewriteHashlinks', false);
$result = $this->customise(array(
'EditForm' => $editForm
))->renderWith($this->getTemplatesWithSuffix('_right'));
return $this->getLastFormIn($result);
}
return array();
}
/**
* For the current report that the user is viewing,
* return a Form instance with the fields for that
* report.
*
* @return Form
*/
public function EditForm() {
$ids = array();
$id = isset($_REQUEST['ID']) ? $_REQUEST['ID'] : Session::get('currentPage');
$subClasses = ClassInfo::subclassesFor('SSReport');
if($subClasses) {
foreach($subClasses as $subClass) {
if($subClass != 'SSReport') {
$obj = new $subClass();
$ids[] = $obj->ID();
}
}
}
if($id && in_array($id, $ids)) return $this->reportEditFormFor($id);
else return false;
}
/**
* Return a Form instance with fields for the
* particular report currently viewed.
*
* @TODO Dealing with multiple data types for the
* $id parameter is confusing. Ideally, it should
* deal with only one.
*
* @param id|string $id The ID of the report, or class name
* @return Form
*/
public function reportEditFormFor($id) {
$page = false;
$fields = new FieldSet();
$actions = new FieldSet();
if(is_numeric($id)) $page = DataObject::get_by_id('SiteTree', $id);
$reportClass = is_object($page) ? 'SSReport_' . $page->ClassName : $id;
$obj = new $reportClass();
if($obj) $fields = $obj->getCMSFields();
$idField = new HiddenField('ID');
$idField->setValue($id);
$fields->push($idField);
$form = new Form($this, 'EditForm', $fields, $actions);
return $form;
}
/**
* Determine if we have reports and need
* to display the "Reports" main menu item
* in the CMS.
*
* The test for an existance of a report
* is done by checking for a subclass of
* "SSReport" that exists.
*
* @return boolean
*/
public static function has_reports() {
$subClasses = ClassInfo::subclassesFor('SSReport');
if($subClasses) {
foreach($subClasses as $subClass) {
if($subClass != 'SSReport') {
return true;
}
}
}
return false;
}
}
?>