Write reports with Twig.
To install Reports, follow these steps:
- Download & unzip the file and place the
reports
directory into yourcraft/plugins
directory - Install plugin in the Craft Control Panel under Settings > Plugins
- The plugin folder should be named
reports
for Craft to see it.
Reports works on Craft 2.4.x and higher;
Reports allow you to collect data by writing Twig tags as normal. All you need to in the end is pass that data to the .prepare()
method, like the following example:
Example report in Twig:
{% set newsCount = craft.entries.section('news').limit(null).total() %}
{{ craft.reports.prepare({
columns: ['Total news entries'],
rows: [ [ newsCount ] ]
}) }}
List all users in a specific user group that have logged in within the last 30 days:
{% set loginPeriod = now|date_modify('-30 days') %}
{% set users = craft.users.group('clients').lastLoginDate('> ' ~ loginPeriod) %}
{% set userRows = [] %}
{% for user in users %}
{% set userRows = userRows|merge([ [user.username, user.getName(), user.email] ]) %}
{% endfor %}
{{ craft.reports.prepare({
columns: ['Username', 'Name', 'Email'],
rows: userRows
}) }}
The result is available in the control panel, or as a CSV export.
You can register report types from other plugins and use templates for both options and results:
public function registerReports()
{
$types = [];
$folders = IOHelper::getFolders(craft()->path->getPluginsPath().'tester/templates/reports/');
foreach($folders as $folder){
$name = IOHelper::getFolderName($folder, false);
$types[$name] = 'reports/'.$name;
}
return $types;
}
Use the reports forms variable to access cp forms macros
{{ craft.reports.forms('dateTimeField', {
id: 'startDate',
label: "Start Date"|t,
name: 'options[startDate]',
value: options.startDate
}) }}
The options variable is now available in the results.twig
folder structure:
templates
reports
reportType
results.twig
settings.twig
Based on the work by Superbig