Skip to content

Commit

Permalink
NEW Move code from silverstripe/sitewidecontent-report
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 20, 2024
1 parent 3a49f94 commit 136fa79
Show file tree
Hide file tree
Showing 10 changed files with 669 additions and 0 deletions.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Silverstripe backend.

There are also a few CMS reports that comes out of the box:
- A "Users, Groups and Permissions" report allowing administrators to get a quick overview of who has access to the CMS.
- A "Site-wide content report" report allowing administrators to get a quick overview of content across the site.

## Troubleshooting

Expand All @@ -34,6 +35,85 @@ SilverStripe\Reports\Report:
```
Note that some reports may have overridden the `getCount` method, and for those reports this may not apply.

## Customising the "Site-wide content report"

In order to customise the columns included in a report you can build a custom extension and apply it to the
SitewideContentReport as necessary.

The built in `SitewideContentTaxonomy` extension, for instance, adds a custom columns extracted from the `silverstripe/taxonomy` modules, and can be used as a base for developing further extensions:

For instance, in order to add a new Page field to the report you could add an extension similar to the below:

```php
use SilverStripe\Core\Extension;
use SilverStripe\Reports\SiteWideContentReport\SiteWideContentReport;
class MyReportExtension extends Extension {
protected function updateColumns($itemType, &$columns) {
if($itemType !== 'Pages') {
return;
}
$columns["Price"] = [
"title" => _t(SitewideContentReport::class . ".Price", "Price"),
"formatting" => function ($value, $item) use ($mainSiteLabel) {
return number_format($value, 2, '.', ',');
},
];
}
}
```

The `$columns` array can have any number of items added, where the key of the array represents the
field name to be included.

Each item can be either a literal string (which will be used as the title), or an array that may contain
the following key => value pairs:

* `title`: The title to use for the column header
* `format`: A method callback which will take the raw value and original object, and return a formatted
string.
* `datasource`: If the value for this column isn't a direct field on the original object, a custom callback
can be set here. Unlike `format` this callback will only take a single parameter, the original object.
* `printonly`: Set to true if this column is only visible on the print or export-to-csv views.
* `casting`: Specify a field type (e.g. `Text` or `Int`) in order to assist with field casting. This is not
necessary if `formatting` is used.

## Performance considerations

### Large data sets

If your project has a large number of pages or files, you may experience server timeouts while trying to export
this report to CSV. To avoid this issue, you can either increase your server timeout limit, or you can install
and configure the [silverstripe/gridfieldqueuedexport module](https://github.com/silverstripe/silverstripe-gridfieldqueuedexport)
which allows for CSV generation to offloaded to a queued job in the background.

An example of configuring this module in your project:

```php
use SilverStripe\Core\Extension;
use SilverStripe\Forms\GridField\GridFieldComponent;
use SilverStripe\GridfieldQueuedExport\Forms\GridFieldQueuedExportButton;
class SitewideContentReportQueuedExportExtension extends Extension
{
protected function updateExportButton(GridFieldComponent &$exportButton)
{
$exportButton = new GridFieldQueuedExportButton('buttons-after-left');
}
}
```

Apply the example Extension above with YAML configuration in your project:

```yaml
---
Name: queuedsitewidecontentreport
---
SilverStripe\Reports\SitewideContentReport\SitewideContentReport:
extensions:
- SitewideContentReportQueuedExportExtension
```

## Links ##

* [License](./LICENSE)
8 changes: 8 additions & 0 deletions _config/sitewidecontent-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
Name: reports-sitewidecontenttaxonomy
Only:
moduleexists: silverstripe/taxonomy
---
SilverStripe\Reports\SiteWideContentReport\SitewideContentReport:
extensions:
- SilverStripe\Reports\SiteWideContentReport\Model\SitewideContentTaxonomy
24 changes: 24 additions & 0 deletions code/SiteWideContentReport/Form/GridFieldBasicContentReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace SilverStripe\Reports\SiteWideContentReport\Form;

use SilverStripe\Forms\GridField\GridField;
use SilverStripe\ORM\DataObject;

class GridFieldBasicContentReport extends GridField
{
/**
* @param int $total
* @param int $index
* @param DataObject $record
*
* @return array
*/
protected function getRowAttributes($total, $index, $record)
{
$attributes = parent::getRowAttributes($total, $index, $record);
$this->extend('updateRowAttributes', $total, $index, $record, $attributes);

return $attributes;
}
}
76 changes: 76 additions & 0 deletions code/SiteWideContentReport/Model/SitewideContentTaxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace SilverStripe\Reports\SiteWideContentReport\Model;

use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Extension;
use SilverStripe\Reports\SiteWideContentReport\SitewideContentReport;
use SilverStripe\Taxonomy\TaxonomyTerm;

/**
* Provides taxonomy integration for sitewide content report.
*
* @extends Extension<SitewideContentReport>
*/
class SitewideContentTaxonomy extends Extension
{
/**
* Name of field to get tags from.
*
* @config
*
* @var string
*/
private static $tag_field = 'Terms';

/**
* Update columns to include taxonomy details.
*
* @param string $itemType (i.e 'Pages' or 'Files')
* @param array $columns Columns
*/
protected function updateColumns($itemType, &$columns)
{
if ($itemType !== 'Pages') {
return;
}

// Check if pages has the tags field
if (!SitewideContentTaxonomy::enabled()) {
return;
}

// Set column
$field = Config::inst()->get(__CLASS__, 'tag_field');
$columns['Terms'] = [
'printonly' => true, // Hide on page report
'title' => _t('SilverStripe\\SiteWideContentReport\\SitewideContentReport.Tags', 'Tags'),
'datasource' => function ($record) use ($field) {
if ($record->hasMethod($field)) {
$tags = $record->$field()->column('Name');

return implode(', ', $tags);
}

return '';
},
];
}

/**
* Check if this field is enabled.
*
* @return bool
*/
public static function enabled()
{
if (!class_exists(TaxonomyTerm::class)) {
return false;
}

// Check if pages has the tags field
$field = Config::inst()->get(__CLASS__, 'tag_field');

return singleton('Page')->hasMethod($field);
}
}
Loading

0 comments on commit 136fa79

Please sign in to comment.