Mothership Reports Cogule
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Mothership Reports

The Message\Mothership\Report cogule provides a framework for providing reports in a new tab in the control panel.

Creating a new report

Registering reports

Register your reports in the Service container of the module it's in. This is an example of code in MS-User:

public function registerReports($services)
	$services['user.user_summary'] = $services->factory(function($c) {
		return new User\Report\UserSummary(

	$services['user.reports'] = function($c) {
		$reports = new ReportCollection;
			return $reports;

Creating reports

Create a folder Reports within Src if there isn't already one. All reports for this module will be saved in here.

Initial set up

All reports must extend AbstractReport and will use:

  • Message\Cog\DB\QueryBuilderInterface;
  • Message\Cog\DB\QueryBuilderFactory;
  • Message\Cog\Routing\UrlGenerator;

Set the:

  • name: Used as an identifier and for naming the download file.
  • displayName: Used on the front-end.
  • description: Used on the front-end.
  • reportGroup: Used to group similar reports on the dashboard.

Set all the filters and charts you want to use in the report.

Set any default filters you want for the report when it's first viewed.

This report displays TableChart, and uses filters DateRange and Choices. It sets the form default StartDate for 1 month ago.

		parent::__construct($builderFactory, $routingGenerator);
		$this->_setDisplayName('Payments & Refunds');
			This report displays all payments & refunds.
			By default it includes all data from the last month (by completed date).
		$this->_charts[]   = new TableChart;
		$this->_filters->add(new DateRange);
		$startDate = new \DateTime;
			->setStartDate($startDate->setTimestamp(strtotime(date('Y-m-d H:i')." -1 month")));
		// Params for Choices filter: unique filter name, label, choices, multi-choice
		$this->_filters->add(new Choices(
				'payment' => 'Payment',
				'refund' => 'Refund',

All reports need this function:

public function getCharts()
	$data = $this->_dataTransform($this->_getQuery()->run(), "json");
	$columns = $this->_parseColumns($this->getColumns());
		foreach ($this->_charts as $chart) {
	return $this->_charts;

Set all the columns needed into an array with the key as the name and value as type of data that's expected. This type is used for Google Charts.

public function getColumns()
	return [
		'Date'         => 'string',
		'Created by'   => 'string',
		'Currency'     => 'string',
		'Method'       => 'string',
		'Amount'       => 'number',
		'Type'         => 'string',
		'Order/Return' => 'string',

Using QueryBuilder create your report query. A simple example is the User Summary report:

protected function _getQuery()
	$queryBuilder = $this->_builderFactory->getQueryBuilder();
		->select('user.user_id AS "ID"')
		->select('created_at AS "Created"')
		->select('CONCAT(surname,", ",forename) AS "User"')
		->select('email AS "Email"')

	return $queryBuilder->getQuery();


This takes the data from the query and converts it into either a string in JSON format for use in Google Charts, or a simple array for the CSV download.

For more complicated data you might need to pass in some optional properties. See:

For example, to order dates in their numerical representation rather than alphabetically you will need to send the timestamp as a value but the string as formatted value.

	'v' => $row->Created,
	'f' => date('Y-m-d H:i', $row->Created)

Another example is for currency values:

	'v' => (float) $row->Gross,
	'f' => (string) number_format($row->Gross,2,'.',',')

For links, if you send just the html link as the value it will order the values using the full html rather than the display text. This is fine if the text is the same values used to create the URL, but in most cases the ID isn't what will be displayed. In this case, the user's name is sent as the value and the html (using the user-id) is sent as the formatted value.

	'v' => utf8_encode($row->User),
	'f' => (string) '
		<a href ="'.$this->generateUrl('ms.cp.user.admin.detail.edit',
		['userID' => $row->ID]).'">'

Any strings which may contain special characters need encoded to UTF-8, as User does in the above example.

The full code used in the User Summary report:

	protected function _dataTransform($data, $output = null)
		$result = [];

		if ($output === "json") {

			foreach ($data as $row) {

				$result[] = [
					$row->User ? [
						'v' => utf8_encode($row->User),
						'f' => (string) '<a href ="'.$this->generateUrl('ms.cp.user.admin.detail.edit', ['userID' => $row->ID]).'">'.ucwords(utf8_encode($row->User)).'</a>'
					] : $row->User,
						'v' => $row->Created,
						'f' => date('Y-m-d H:i', $row->Created)

			return json_encode($result);

		} else {

			foreach ($data as $row) {
				$result[] = [
					date('Y-m-d H:i', $row->Created),
			return $result;




This has two datetime form fields to select a range of data between the two.


This is a singular date form field to select data from one specific date.


The Choices form field can be customised depending on what data you are getting in the report.

When adding a choice form you must add the following parameters:

  • A unique filter name, it cannot be the same as any other report
  • A label for displaying on the form
  • The choices
  • Whether the field is multiple-choice
$this->_filters->add(new Choices(
	"Sale Type",
			'Order' => 'Order',
			'Return' => 'Return',
			'Exchange' => 'Exchange',
			'shipping' => 'Shipping',



TableChart is currently the only chart available.