-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathFeaturedVideo.php
More file actions
71 lines (49 loc) · 1.86 KB
/
Copy pathFeaturedVideo.php
File metadata and controls
71 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
class FeaturedVideo extends DataObject {
static $db = array(
'Name' => 'Text',
'Day' => 'Int',
'YouTubeID' => 'Varchar',
'Description' => 'Text',
'URLSegment' => 'Text'
);
static $has_one = array(
'PresentationCategoryPage' => 'PresentationCategoryPage'
);
private function generateURLSegment($title){
$filter = URLSegmentFilter::create();
$t = $filter->filter($title);
// Fallback to generic page name if path is empty (= no valid, convertable characters)
if(!$t || $t == '-' || $t == '-1') $t = "page-$this->ID";
return $t;
}
function onBeforeWrite()
{
parent::onBeforeWrite();
// If there is no URLSegment set, generate one from Title
if ((!$this->URLSegment || $this->URLSegment == 'new-presentation') && $this->Title != 'New Presentation') {
$this->URLSegment = $this->generateURLSegment($this->Title);
} else if ($this->isChanged('URLSegment')) {
// Make sure the URLSegment is valid for use in a URL
$segment = preg_replace('/[^A-Za-z0-9]+/', '-', $this->URLSegment);
$segment = preg_replace('/-+/', '-', $segment);
// If after sanitising there is no URLSegment, give it a reasonable default
if (!$segment) {
$segment = "presentation-" . $this->ID;
}
$this->URLSegment = $segment;
}
// Ensure that this object has a non-conflicting URLSegment value by appending number if needed
$count = 2;
while ($this->LookForExistingURLSegment($this->URLSegment)) {
$this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
$count++;
}
}
//Test whether the URLSegment exists already on another Video
function LookForExistingURLSegment($URLSegment)
{
return (DataObject::get_one('FeaturedVideo', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
}
}
?>