Passed
Push — master ( 8e5bf0...202659 )
by Nicolaas
03:49
created

PageExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 33
c 4
b 2
f 0
dl 0
loc 63
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateSettingsFields() 0 28 3
A updateCMSFields() 0 16 4
1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Extensions;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\NumericField;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\Forms\LiteralField;
10
use SilverStripe\SiteConfig\SiteConfig;
11
12
/**
13
 * Class \Sunnysideup\SimpleTemplateCaching\Extensions\PageExtension.
14
 *
15
 * @property Page|PageExtension $owner
16
 * @property bool $NeverCachePublicly
17
 * @property int $PublicCacheDurationInSeconds
18
 */
19
class PageExtension extends Extension
20
{
21
    private static $db = [
22
        'NeverCachePublicly' => 'Boolean',
23
        'PublicCacheDurationInSeconds' => 'Int',
24
    ];
25
26
    public function updateSettingsFields(FieldList $fields)
27
    {
28
        $owner = $this->getOwner();
29
        $fields->addFieldsToTab(
30
            'Root.Cache',
31
            [
32
                CheckboxField::create(
33
                    'NeverCachePublicly',
34
                    'Never cache this page.
35
                    This should be checked if this page can show different information for different users or different situations.'
36
                ),
37
            ]
38
        );
39
        if (! (bool) $owner->NeverCachePublicly) {
40
            $fields->addFieldsToTab(
41
                'Root.Cache',
42
                [
43
                    NumericField::create(
44
                        'PublicCacheDurationInSeconds',
45
                        'In seconds, how long can this be cached for?'
46
                    )
47
                        ->setDescription(
48
                            'Use with care!<br />
49
                            Leave empty or zero to use the default value for the site<br />
50
                            This should only be used on pages that should be the same for all users and that should be accessible publicly.<br />
51
                            You can also set this value <a href="/admin/settings#Root_Caching">for the whole site</a>.<br />
52
                            Caching is ' . (SiteConfig::current_site_config()->HasCaching ? '' : 'NOT') . ' allowed on for this site.<br />
53
                            The current value for the whole site is ' . SiteConfig::current_site_config()->PublicCacheDurationInSeconds . ' seconds.<br />
54
                            '
55
                        ),
56
57
                ]
58
            );
59
        }
60
    }
61
62
    /**
63
     * Update Fields
64
     * @return FieldList
65
     */
66
    public function updateCMSFields(FieldList $fields)
67
    {
68
69
        $owner = $this->getOwner();
70
        $sc = SiteConfig::current_site_config();
71
        if ($sc->HasCaching || $owner->PublicCacheDurationInSeconds) {
72
            if (! $owner->NeverCachePublicly) {
73
                $fields->push(
74
                    LiteralField::create(
75
                        'CacheInfo',
76
                        '<p class="message warning">This page can be cached for ' . $this->owner->PublicCacheDurationInSeconds . ' seconds.</p>'
77
                    )
78
                );
79
            }
80
        }
81
        return $fields;
82
    }
83
}
84