PageExtension::updateCMSFields()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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
                    or if it contains forms (some search forms may be excempted).'
37
                ),
38
            ]
39
        );
40
        if (! (bool) $owner->NeverCachePublicly) {
41
            $fields->addFieldsToTab(
42
                'Root.Cache',
43
                [
44
                    NumericField::create(
45
                        'PublicCacheDurationInSeconds',
46
                        'In seconds, how long can this be cached for?'
47
                    )
48
                        ->setDescription(
49
                            'Use with care!<br />
50
                            Leave empty or zero to use the default value for the site<br />
51
                            This should only be used on pages that should be the same for all users and that should be accessible publicly.<br />
52
                            You can also set this value <a href="/admin/settings#Root_Caching">for the whole site</a>.<br />
53
                            Caching is ' . (SiteConfig::current_site_config()->HasCaching ? '' : 'NOT') . ' allowed on for this site.<br />
54
                            The current value for the whole site is ' . SiteConfig::current_site_config()->PublicCacheDurationInSeconds . ' seconds.<br />
55
                            '
56
                        ),
57
58
                ]
59
            );
60
        }
61
    }
62
63
    /**
64
     * Update Fields
65
     * @return FieldList
66
     */
67
    public function updateCMSFields(FieldList $fields)
68
    {
69
        $owner = $this->getOwner();
70
        if ($owner->PageCanBeCachedEntirely()) {
71
            $fields->push(
72
                LiteralField::create(
73
                    'CacheInfo',
74
                    '<p class="message warning">Careful: this page can be cached publicly for up to ' . $owner->PageCanBeCachedEntirelyDuration() . ' seconds.</p>'
75
                )
76
            );
77
        }
78
        return $fields;
79
    }
80
81
    public function PageCanBeCachedEntirely(): bool
82
    {
83
        $owner = $this->getOwner();
84
85
        if ($owner->NeverCachePublicly) {
86
            return false;
87
        }
88
        $sc = SiteConfig::current_site_config();
89
        if (!$sc->HasCaching) {
90
            return false;
91
        }
92
        if ($owner->PageCanBeCachedEntirelyDuration() <= 0) {
93
            return false;
94
        }
95
        if ($owner->hasMethod('updateCacheControl')) {
96
            user_error('Please use canCachePage instead of updateCacheControl', E_USER_ERROR);
97
        }
98
        if ($owner->hasMethod('canCachePage')) {
99
            user_error('Please add the canCachePage method to your controller', E_USER_ERROR);
100
        }
101
102
        return true;
103
    }
104
105
    public function PageCanBeCachedEntirelyDuration(): int
106
    {
107
        return (int) (
108
            $this->getOwner()->PublicCacheDurationInSeconds ?:
109
            SiteConfig::current_site_config()->PublicCacheDurationInSeconds);
110
    }
111
112
    public function EditCacheSettingsLink(): string
113
    {
114
        return str_replace(
115
            'admin/pages/edit/show/',
116
            'admin/pages/settings/show/',
117
            $this->getOwner()->CMSEditLink()
118
        ) . '#Root_Cache';
119
    }
120
}
121