FooterHolder::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\PageTypes;
4
5
use SilverStripe\CMS\Model\RedirectorPage;
6
use SilverStripe\Forms\FieldList;
7
8
/**
9
 * FooterHolder is intended as an invisible container for footer links and pages.
10
 * All child pages will be shown within the footer area of the site.
11
 * Use **RedirectorPage** if you just need a link.
12
 */
13
14
class FooterHolder extends RedirectorPage
15
{
16
17
    private static $description = 'Holder page that displays all child pages as links in the footer';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
18
19
    private static $singular_name = 'Footer Holder';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
20
21
    private static $plural_name = 'Footer Holders';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
22
23
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
24
        'ShowInMenus' => 0,
25
        'ShowInSearch' => 0,
26
    ];
27
28
    private static $table_name = 'FooterHolder';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
29
30
    public function getCMSFields()
31
    {
32
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
33
            $fields->removeByName('RedirectorDescHeader');
34
            $fields->removeByName('RedirectionType');
35
            $fields->removeByName('LinkToID');
36
            $fields->removeByName('ExternalURL');
37
        });
38
        return parent::getCMSFields();
39
    }
40
41
    /**
42
     * Return the link to the first child page.
43
     */
44
    public function redirectionLink()
45
    {
46
        $childPage = $this->Children()->first();
47
48
        if ($childPage) {
49
            // If we're linking to another redirectorpage then just return the URLSegment, to prevent a cycle of
50
            // redirector pages from causing an infinite loop.  Instead, they will cause a 30x redirection loop in
51
            // the browser, but this can be handled sufficiently gracefully by the browser.
52
            if ($childPage instanceof RedirectorPage) {
53
                return $childPage->regularLink();
54
            }
55
            // For all other pages, just return the link of the page.
56
            return $childPage->Link();
57
        }
58
    }
59
60
    public function syncLinkTracking()
61
    {
62
        // If we don't have anything to link to, then we have a broken link.
63
        if (!$this->Children()) {
64
            $this->HasBrokenLink = true;
0 ignored issues
show
Bug Best Practice introduced by
The property HasBrokenLink does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
65
        }
66
    }
67
}
68