Issues (3)

code/Link.php (2 issues)

1
<?php
2
namespace WebOfTalent\Links;
3
4
use SilverStripe\CMS\Model\SiteTree;
5
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
6
use SilverStripe\View\Requirements;
7
use SilverStripe\Forms\HiddenField;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\TreeDropdownField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\ORM\DataObject;
13
/**
14
 * Defines the Link page type.
15
 */
16
class Link extends DataObject
17
{
18
    private static $table_name = 'Link';
19
20
    private static $db = [
21
        'URL' => 'Text',
22
        'Title' => 'Text',
23
        'Description' => 'HTMLText',
24
        'LinkType' => "Enum('External,Internal')",
25
        'SortOrder' => 'Int',
26
27 1
    ];
28
29 1
    private static $classesToAddLinksTo = ['Page'];
30
31 1
    private static $has_one = [
32 1
        'LinksFolder' => 'WebOfTalent\Links\LinksFolder',
33
        'InternalPage' => SiteTree::class
34 1
    ];
35 1
36 1
    private static $many_many = [
37 1
        'Pages' => SiteTree::class
38 1
    ];
39
40 1
    public function getCMSFields()
41 1
    {
42 1
43
        Requirements::javascript('weboftalent/links:/javascript/linkedit.js');
44 1
45
        $localeField = new HiddenField('Locale');
46 1
        $localeField->setValue($this->LinksFolder()->Locale);
47
48
        $fields = new FieldList(
49
            new TextField('Title', 'Link title'),
50
            new DropdownField('LinkType', 'Internal or External Link',
51
                singleton('WebOfTalent\Links\Link')->dbObject('LinkType')->enumValues()
52
            ),
53
0 ignored issues
show
Empty lines are not allowed in multi-line function calls
Loading history...
54
            new TextField('URL'),
55
            new TreeDropdownField('InternalPageID', 'Choose an internal link', SiteTree::class),
56
            new HTMLEditorField('Description'),
57
            $localeField
58
        );
59
60
        return $fields;
61
    }
62
63
    public function LoadLink()
0 ignored issues
show
Method name "Link::LoadLink" is not in camel caps format
Loading history...
64
    {
65
        $refreshedLink = DataObject::get_one('WebOfTalent\Links\Link', 'Link_Live.ID='.$this->ID);
66
67
        return $refreshedLink->URL;
68
    }
69
70
    public function getWebsiteAddress()
71
    {
72
        $result = $this->URL;
73
74
        if ($this->LinkType == 'Internal') {
75
            $targetPage = DataObject::get_by_id('Page', $this->InternalPageID);
76 1
            if ($targetPage) {
77
                $result = $targetPage->Link();
78
            } else {
79
                $result = '#';
80
            }
81
        }
82
83
        if (!$result) {
84
            $result = '#';
85
        }
86
87
        return $result;
88
    }
89
}
90