|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin: SEOToolbox |
|
4
|
|
|
* Author: Dylan Grech |
|
5
|
|
|
* Copyright: 2016 |
|
6
|
|
|
* |
|
7
|
|
|
* Automated Link is a dataobject that contains all the data |
|
8
|
|
|
* about a link that should be created automatically to a page |
|
9
|
|
|
* |
|
10
|
|
|
* @method SiteTree|null Page() |
|
11
|
|
|
*/ |
|
12
|
|
|
class AutomatedLink extends DataObject implements PermissionProvider { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
private static $db = array( |
|
|
|
|
|
|
15
|
|
|
'Phrase' => 'VARCHAR(255)', |
|
16
|
|
|
'TitleTag' => 'VARCHAR(255)', |
|
17
|
|
|
'AnchorTag' => 'VARCHAR(255)', |
|
18
|
|
|
'NewWindow' => 'Boolean', |
|
19
|
|
|
'NoFollow' => 'Boolean', |
|
20
|
|
|
'SelfLinking' => 'Boolean', |
|
21
|
|
|
'CaseSensitive' => 'Boolean', |
|
22
|
|
|
'MaxLinksPerPage' => 'INT', |
|
23
|
|
|
'Priority' => 'Int' |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
private static $defaults = array( |
|
|
|
|
|
|
27
|
|
|
'MaxLinksPerPage' => 10 |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $default_sort = 'Priority'; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
33
|
|
|
'Page' => 'SiteTree' |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
private static $summary_fields = array( 'Phrase', 'PointsTo' ); |
|
|
|
|
|
|
37
|
|
|
private static $searchable_fields = array( 'Phrase' ); |
|
|
|
|
|
|
38
|
|
|
private static $singular_name = 'Automated Link'; |
|
|
|
|
|
|
39
|
|
|
private static $plural_name = 'Automated Links'; |
|
|
|
|
|
|
40
|
|
|
private static $parsableFields = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the url of the page linked to this object |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function PointsTo(){ |
|
48
|
|
|
return $this->Page()->Link(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return the phrase set for this object |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function Title(){ |
|
57
|
|
|
return $this->Phrase; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return the rendered version of this object |
|
62
|
|
|
* |
|
63
|
|
|
* @return String |
|
64
|
|
|
*/ |
|
65
|
|
|
public function forTemplate(){ |
|
66
|
|
|
return $this->getHTML(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
function canView( $member = false ){ |
|
|
|
|
|
|
70
|
|
|
return Permission::check('AUTOMATEDLINK_VIEW'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
function canEdit( $member = false ){ |
|
|
|
|
|
|
74
|
|
|
return Permission::check('AUTOMATEDLINK_EDIT'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
function canDelete( $member = false ){ |
|
|
|
|
|
|
78
|
|
|
return Permission::check('AUTOMATEDLINK_DELETE'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function canCreate( $member = false ){ |
|
|
|
|
|
|
82
|
|
|
return Permission::check('AUTOMATEDLINK_CREATE'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
function providePermissions() { |
|
|
|
|
|
|
86
|
|
|
return array( |
|
87
|
|
|
'AUTOMATEDLINK_VIEW' => 'View Automated Links', |
|
88
|
|
|
'AUTOMATEDLINK_EDIT' => 'Edit Automated Links', |
|
89
|
|
|
'AUTOMATEDLINK_DELETE' => 'Delete Automated Links', |
|
90
|
|
|
'AUTOMATEDLINK_CREATE' => 'Create Automated Links', |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function requireDefaultRecords(){ |
|
95
|
|
|
parent::requireDefaultRecords(); |
|
96
|
|
|
|
|
97
|
|
|
// Update all links to redirector pages during dev/build |
|
98
|
|
|
foreach( self::get() as $link ) { |
|
99
|
|
|
$link->CheckAndUpdateDestination( true ); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns the HTML Representation of this object |
|
105
|
|
|
* |
|
106
|
|
|
* @param String $originalPhrase |
|
107
|
|
|
* @return String |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getHTML($originalPhrase = NULL) { |
|
110
|
|
|
$link = ($this->PageID) ? $this->Page()->Link() : '#'; |
|
|
|
|
|
|
111
|
|
|
$title = ($this->TitleTag) ? "title='{$this->TitleTag}'" : ''; |
|
|
|
|
|
|
112
|
|
|
$nofollow = ($this->NoFollow) ? 'rel="nofollow"' : ''; |
|
|
|
|
|
|
113
|
|
|
$newtab = ($this->NewWindow) ? 'target="_blank"' : ''; |
|
|
|
|
|
|
114
|
|
|
$anchor = ($originalPhrase) ? $originalPhrase : $this->Phrase; |
|
|
|
|
|
|
115
|
|
|
$link = ($this->AnchorTag) ? rtrim($link, '#').'#'.$this->AnchorTag : $link; |
|
|
|
|
|
|
116
|
|
|
return "<a href=\"$link\" $title $nofollow $newtab data-id=\"{$this->ID}\">{$anchor}</a>"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getCMSFields() { |
|
120
|
|
|
$fields = FieldList::create(TabSet::create('Root')); |
|
121
|
|
|
|
|
122
|
|
|
$fields->addFieldsToTab('Root.LinkSettings', array( |
|
123
|
|
|
TextField::create('Phrase', 'Phrase to search for', $this->Phrase, 255), |
|
|
|
|
|
|
124
|
|
|
TextField::create('TitleTag', 'Title Tag', $this->TitleTag, 255), |
|
|
|
|
|
|
125
|
|
|
TextField::create('AnchorTag', 'Anchor Tag(#)', $this->AnchorTag, 255), |
|
|
|
|
|
|
126
|
|
|
FieldGroup::create( |
|
127
|
|
|
CheckboxField::create('NoFollow'), |
|
128
|
|
|
CheckboxField::create('NewWindow'), |
|
129
|
|
|
CheckboxField::create('SelfLinking', 'Allow page to link to itself'), |
|
130
|
|
|
CheckboxField::create('CaseSensitive', 'Match the case of the phrase') |
|
131
|
|
|
), |
|
132
|
|
|
NumericField::create('MaxLinksPerPage', 'Maximum amount of this link to be created on a single page( 0 = unlimited )'), |
|
133
|
|
|
TreeDropdownField::create('PageID', 'Page to link to', 'SiteTree') |
|
134
|
|
|
)); |
|
135
|
|
|
|
|
136
|
|
|
$settings = GlobalAutoLinkSettings::get_current(); |
|
137
|
|
|
if ($settings) { |
|
138
|
|
|
$fields->addFieldsToTab('Root.Global', array( |
|
139
|
|
|
NumericField::create( |
|
140
|
|
|
'Global_MaxLinksPerPage', |
|
141
|
|
|
'Maximum amount of links a single page can have ( 0 = unlimited )', |
|
142
|
|
|
$settings->MaxLinksPerPage |
|
143
|
|
|
), |
|
144
|
|
|
TextField::create( |
|
145
|
|
|
'Global_ExcludeTags', |
|
146
|
|
|
'Do not include links into these HTML Tags ( comma seperated )', |
|
147
|
|
|
$settings->ExcludeTags |
|
148
|
|
|
), |
|
149
|
|
|
TextField::create( |
|
150
|
|
|
'Global_AddTo', |
|
151
|
|
|
'Page types where links should be created in ( leave blank for all page types )', |
|
152
|
|
|
$settings->AddTo ), |
|
153
|
|
|
TextField::create( |
|
154
|
|
|
'Global_IncludeIn', |
|
155
|
|
|
'Include Links into these fields ( comma seperated & field must support html injection )', |
|
156
|
|
|
$settings->IncludeIn |
|
157
|
|
|
) |
|
158
|
|
|
)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $fields; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function getCMSValidator() { |
|
165
|
|
|
return new RequiredFields(array('Phrase', 'PageID')); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Save the Global Settings into the |
|
170
|
|
|
* Global Auto Link Settings Object |
|
171
|
|
|
* |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
|
|
public function onBeforeWrite() { |
|
175
|
|
|
parent::onBeforeWrite(); |
|
176
|
|
|
|
|
177
|
|
|
$settings = GlobalAutoLinkSettings::get_current(); |
|
178
|
|
|
if ($settings) { |
|
179
|
|
|
|
|
180
|
|
|
foreach ($this->getChangedFields() as $field => $value) { |
|
181
|
|
|
if (strpos($field, 'Global_') === 0 && isset($value['after'])) { |
|
182
|
|
|
$field = str_replace('Global_', '', $field); |
|
183
|
|
|
$settings->$field = $value['after']; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$settings->write(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->CheckAndUpdateDestination(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Checks if the destination is a redirector page if so |
|
195
|
|
|
* it updates it to the destination of the redirector page |
|
196
|
|
|
* |
|
197
|
|
|
* @Boolean $write - Write the changes if any |
|
198
|
|
|
* @return void |
|
199
|
|
|
*/ |
|
200
|
|
|
public function CheckAndUpdateDestination( $write = false ){ |
|
201
|
|
|
$this->extend('beforeCheckAndUpdateDestination', $write); |
|
202
|
|
|
|
|
203
|
|
|
if( $this->PageID && $this->Page() && |
|
|
|
|
|
|
204
|
|
|
$this->Page()->ClassName == 'RedirectorPage' && |
|
205
|
|
|
$this->Page()->LinkToID && $this->Page()->RedirectionType == 'Internal' ) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->PageID = $this->Page()->LinkToID; |
|
|
|
|
|
|
208
|
|
|
if( $write ) { |
|
209
|
|
|
$this->write(); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Checks if the field is parable |
|
216
|
|
|
* |
|
217
|
|
|
* @param SiteTree $page |
|
218
|
|
|
* @param String $field |
|
219
|
|
|
* @return Boolean |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function isFieldParsable(SiteTree $page, $field) { |
|
222
|
|
|
if (!isset(self::$parsableFields[$page->ID]) || !isset(self::$parsableFields[$page->ID][$field])) { |
|
223
|
|
|
$fields = array(); |
|
224
|
|
|
|
|
225
|
|
View Code Duplication |
foreach (ClassInfo::ancestry($page->ClassName, true) as $class) |
|
|
|
|
|
|
226
|
|
|
$fields = array_merge($fields, (array) DataObject::database_fields($class)); |
|
227
|
|
|
|
|
228
|
|
|
self::$parsableFields[$page->ID][$field] = |
|
229
|
|
|
(Boolean) array_key_exists($field, $fields) && strtolower($fields[$field]) === 'htmltext' && $page->$field; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return self::$parsableFields[$page->ID][$field]; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Checks if this link can be added to the provided |
|
237
|
|
|
* page and field |
|
238
|
|
|
* |
|
239
|
|
|
* @param ContentController $controller |
|
240
|
|
|
* @return Boolean |
|
241
|
|
|
*/ |
|
242
|
|
|
public function canBeAdded( ContentController $controller ){ |
|
243
|
|
|
return ( $this->SelfLinking || $controller->ID != $this->PageID ); |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Turn the string passed into a DOMDocument object |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $html |
|
250
|
|
|
* @return DOMDocument |
|
251
|
|
|
*/ |
|
252
|
|
|
public static function constructDOMDocument($html){ |
|
253
|
|
|
if( class_exists( 'HTML5_Parser' ) ){ |
|
254
|
|
|
$html5 = HTML5_Parser::parse( $html ); |
|
255
|
|
|
if($html5 instanceof DOMNodeList){ |
|
256
|
|
|
$dom = new DOMDocument(); |
|
257
|
|
|
while($html5->length > 0) { |
|
258
|
|
|
$dom->appendChild($html5->item(0)); |
|
259
|
|
|
} |
|
260
|
|
|
}else{ |
|
261
|
|
|
$dom = $html5; |
|
262
|
|
|
} |
|
263
|
|
|
} else{ |
|
264
|
|
|
$dom = new DOMDocument(); |
|
265
|
|
|
$dom->loadHTML( $html ); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $dom; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.