silverstripe /
silverstripe-controllerpolicy
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SilverStripe\ControllerPolicy; |
||||
| 4 | |||||
| 5 | use SilverStripe\Forms\FieldList; |
||||
| 6 | use SilverStripe\Forms\LiteralField; |
||||
| 7 | use SilverStripe\Forms\TextField; |
||||
| 8 | use SilverStripe\ORM\DataExtension; |
||||
| 9 | use SilverStripe\Security\Permission; |
||||
| 10 | use SilverStripe\Security\Security; |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * This extension leverages the CachingPolicy's ability to customise the max-age per originator. |
||||
| 14 | * The configuration option is surfaced to the CMS UI. The extension needs to be added |
||||
| 15 | * to the object related to the policed controller. |
||||
| 16 | */ |
||||
| 17 | class PageControlledPolicy extends DataExtension |
||||
| 18 | { |
||||
| 19 | /** |
||||
| 20 | * @var array |
||||
| 21 | */ |
||||
| 22 | private static $db = [ |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 23 | 'MaxAge' => 'Varchar' |
||||
| 24 | ]; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * Extension point for the CachingPolicy. |
||||
| 28 | * |
||||
| 29 | * @param int $cacheAge The original cache age value (in seconds) |
||||
| 30 | * @return int|null The new cache age value (in seconds) |
||||
| 31 | */ |
||||
| 32 | public function getCacheAge($cacheAge) |
||||
|
0 ignored issues
–
show
The parameter
$cacheAge is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 33 | { |
||||
| 34 | if ($this->owner->MaxAge != '') { |
||||
| 35 | return (int)($this->owner->MaxAge * 60); |
||||
| 36 | } |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * @param FieldList $fields |
||||
| 41 | */ |
||||
| 42 | public function updateCMSFields(FieldList $fields) |
||||
| 43 | { |
||||
| 44 | // Only admins are allowed to modify this. |
||||
| 45 | $member = Security::getCurrentUser(); |
||||
| 46 | if (!$member || !Permission::checkMember($member, 'ADMIN')) { |
||||
|
0 ignored issues
–
show
|
|||||
| 47 | return; |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | $fields->addFieldsToTab('Root.Caching', [ |
||||
| 51 | LiteralField::create('Instruction', '<p>The following field controls the length of time the page will ' . |
||||
| 52 | 'be cached for. You will not be able to see updates to this page for at most the specified ' . |
||||
| 53 | 'amount of minutes. Leave empty to set back to the default configured for your site. Set ' . |
||||
| 54 | 'to 0 to explicitly disable caching for this page.</p>'), |
||||
| 55 | TextField::create('MaxAge', 'Custom cache timeout [minutes]') |
||||
| 56 | ]); |
||||
| 57 | } |
||||
| 58 | } |
||||
| 59 |