Custom Filterable Reports – SilverStripe CMS

To keep this simple I am going to give users the ability to assign a colour to each page on the site and then generate a report of any pages on the site that have been assigned a particular colour.

1. Setting Up. First you need to add an Enum storing some colours to the Page class and add a new dropdown field to allow the user to select the colour on each page. mysite/code/Page.php

<?php
class Page extends SiteTree {

	public static $db = array(
		'Colour' => "Enum('Red, Blue, Green', 'Green')"
	);

	public static $has_one = array(
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();

		//add colour selecteor for the SSBits reporting tut
		//get a list of enum values to populate the dropdown
		$colourValues = $this->dbObject('Colour')->enumValues();
		$fields->addFieldToTab("Root.Content.Main", new DropdownField('Colour', 'Choose a Colour', $colourValues));

		return $fields;
	}

}
class Page_Controller extends ContentController {

	/**
	 * An array of actions that can be accessed via a request. Each array element should be an action name, and the
	 * permissions or conditions required to allow the user to access it.
	 *
	 * <code>
	 * array (
	 *     'action', // anyone can access this action
	 *     'action' => true, // same as above
	 *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
	 *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
	 * );
	 * </code>
	 *
	 * @var array
	 */
	public static $allowed_actions = array (
	);

	public function init() {
		parent::init();

		// Note: you should use SS template require tags inside your templates 
		// instead of putting Requirements calls here.  However these are 
		// included so that our older themes still work
		Requirements::themedCSS('layout'); 
		Requirements::themedCSS('typography'); 
		Requirements::themedCSS('form'); 
	}
}

2. Creating the Report. Now that each page can have a colour assigned to it we can get down to creating the report itself.  First create a new file, let’s call it PageColourReport.php, and set up the new class. mysite/code/PageColourReport.php

<?php
/** 
 * This report will list all of the pages in the CMS of a certain colour
 */
class PageColourReport extends SS_Report 
{
	function title() 
	{
        // this is the title of the report
		return "Page Colour Report";
	}
	//Search for the matching pages to display
	function sourceRecords($params, $sort, $limit) 
	{
		if(isset($params['Colour']))
		{
			$Pages = DataObject::get("SiteTree", "Colour = '" . $params['Colour'] ."'", "Created DESC", Null, $params['ResultsLimit']);			
			return $Pages;
		}
	}
	function columns() 
	{
		$fields = array(
			'Title' => array(
				'title' => 'Page name',
				'formatting' => '<a href=\"admin/show/{$ID}\" title=\"Edit page\">{$value}</a>'
			),
			'Created' => array(
				'title' => 'Created',
				'casting' => 'SS_Datetime->Full'
			),
			'LastEdited' => array(
				'title' => 'Last Edited',
				'casting' => 'SS_Datetime->Ago'
			)
		);
		return $fields;
	}
	function parameterFields() 
	{
		$params = new FieldSet();
		//Colour filter
		$colours = singleton('Page')->dbObject('Colour')->enumValues();
		$params->push(new DropdownField(
			"Colour", 
			"Colour", 
			$colours
		));
		//Result Limit
		$ResultLimitOptions = array(
			1 => '1 Page',
			50 => '50 Pages',
			100 => '100 Pages',
			200 => '200 Pages',
			500 => '500 Pages',
			Null => 'All Pages'
		);
		$params->push(new DropdownField(
			"ResultsLimit", 
			"Limit results to", 
			$ResultLimitOptions,
			50
		));		
		return $params;
	}	
	}

3. Finally you need to remember to let silverstripe know about the new report by adding the following line to your _config.php file. mysite/_config.php

SS_Report::register("ReportAdmin", "PageColourReport");

Looking for quality SilverStripe Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 3 = 6

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>