Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Filter Report using form values


Go to End


3 Posts   2819 Views

Avatar
ec8or

Community Member, 18 Posts

26 January 2011 at 10:45pm

Hi,

I need some help filtering the DataObjects returned from a Report. Basically I have a custom query using groupby and COUNT() to display the number of bookings made to individual Service Centrer, and now I would like to be able to filter these results using a start and end date. Have tried to set this up using a Report but if i try to submit the Form using any custom classes I get "Method 'doReport' not found in class 'Form'" and not sure what I could extend do include that function... maybe this would be better in the ModelAdmin but couldn't figure out how to do the groupby and MySQL COUNT there. Any help or pointers would be appreciated!

<?php
class BookingsReport extends SSReport {
	protected $title = "Bookings report";
	protected $description = "Booking requests submitted by email";
    
	function getCMSFields() {
        $fields = new FieldSet();
        $fields->push(new CalendarDateField('StartDate', 'StartDate'));
        $fields->push(new CalendarDateField('EndDate', 'EndDate'));
        $fields->push(new FormAction('doReport', 'Run Report'));
        $fields->push($this->ReportField);
		return $fields;
	}

    function doReport() {
        echo "1"; 
    }

Attached Files
Avatar
ajshort

Community Member, 244 Posts

27 January 2011 at 2:08am

Hey,

To add a form for filtering the report items, you don't want to overload getCMSFields - that deals with actually generating the report fields. What you want to do is overload parameterFields to return a FieldSet containing the fields you want to show in the filter form, and then overload sourceRecords to actually apply the filter. Check out the broken links report code for an example of how this is done, but basically you just want to check if a from/to date is set in sourceRecords, and if so use it to filter a DataObject::get() call.

Note that the jquery popup date picker field won't work in the report admin area if you want to use it. I had to create a custom extension which would include the JS requirements, as well as a little but of custom CSS to make it look OK. However, if you're happy with a text field for date input you'll have no problems.

Avatar
ec8or

Community Member, 18 Posts

27 January 2011 at 11:18pm

Thanks aj, got it now.

Looks like I am stuck with 2.3 for this one though so will stick a few static reports in there and save this for later :)