1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Used to edit the SiteTree->URLSegment property, and suggest input based on the serverside rules |
5
|
|
|
* defined through {@link SiteTree->generateURLSegment()} and {@link URLSegmentFilter}. |
6
|
|
|
* |
7
|
|
|
* Note: The actual conversion for saving the value takes place in the model layer. |
8
|
|
|
* |
9
|
|
|
* @package cms |
10
|
|
|
* @subpackage forms |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class SiteTreeURLSegmentField extends TextField { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $helpText, $urlPrefix, $urlSuffix, $defaultUrl; |
19
|
|
|
|
20
|
|
|
private static $allowed_actions = array( |
|
|
|
|
21
|
|
|
'suggest' |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
public function Value() { |
25
|
|
|
return rawurldecode($this->value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getAttributes() { |
29
|
|
|
return array_merge( |
30
|
|
|
parent::getAttributes(), |
31
|
|
|
array( |
32
|
|
|
'data-prefix' => $this->getURLPrefix(), |
33
|
|
|
'data-suffix' => '?stage=Stage', |
34
|
|
|
'data-default-url' => $this->getDefaultURL() |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function Field($properties = array()) { |
40
|
|
|
Requirements::javascript(CMS_DIR . '/javascript/SiteTreeURLSegmentField.js'); |
41
|
|
|
Requirements::add_i18n_javascript(CMS_DIR . '/javascript/lang', false, true); |
42
|
|
|
Requirements::css(CMS_DIR . "/css/screen.css"); |
43
|
|
|
return parent::Field($properties); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function suggest($request) { |
47
|
|
|
if(!$request->getVar('value')) { |
48
|
|
|
return $this->httpError(405, |
49
|
|
|
_t('SiteTreeURLSegmentField.EMPTY', 'Please enter a URL Segment or click cancel') |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
$page = $this->getPage(); |
53
|
|
|
|
54
|
|
|
// Same logic as SiteTree->onBeforeWrite |
55
|
|
|
$page->URLSegment = $page->generateURLSegment($request->getVar('value')); |
56
|
|
|
$count = 2; |
57
|
|
|
while(!$page->validURLSegment()) { |
58
|
|
|
$page->URLSegment = preg_replace('/-[0-9]+$/', null, $page->URLSegment) . '-' . $count; |
59
|
|
|
$count++; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
Controller::curr()->getResponse()->addHeader('Content-Type', 'application/json'); |
63
|
|
|
return Convert::raw2json(array('value' => $page->URLSegment)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return SiteTree |
68
|
|
|
*/ |
69
|
|
|
public function getPage() { |
70
|
|
|
$idField = $this->getForm()->Fields()->dataFieldByName('ID'); |
71
|
|
|
return ($idField && $idField->Value()) ? DataObject::get_by_id('SiteTree', $idField->Value()) : singleton('SiteTree'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $string The secondary text to show |
76
|
|
|
*/ |
77
|
|
|
public function setHelpText($string){ |
78
|
|
|
$this->helpText = $string; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string the secondary text to show in the template |
84
|
|
|
*/ |
85
|
|
|
public function getHelpText(){ |
86
|
|
|
return $this->helpText; |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param the url that prefixes the page url segment field |
92
|
|
|
*/ |
93
|
|
|
public function setURLPrefix($url){ |
94
|
|
|
$this->urlPrefix = $url; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return the url prefixes the page url segment field to show in template |
100
|
|
|
*/ |
101
|
|
|
public function getURLPrefix(){ |
102
|
|
|
return $this->urlPrefix; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getURLSuffix() { |
106
|
|
|
return $this->urlSuffix; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Indicator for UI to respond to changes accurately, |
111
|
|
|
* and auto-update the field value if changes to the default occur. |
112
|
|
|
* Does not set the field default value. |
113
|
|
|
*/ |
114
|
|
|
public function getDefaultURL(){ |
115
|
|
|
return $this->defaultUrl; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setDefaultURL($url) { |
119
|
|
|
$this->defaultUrl = $url; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setURLSuffix($suffix) { |
124
|
|
|
$this->urlSuffix = $suffix; |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function Type() { |
129
|
|
|
return 'text urlsegment'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getURL() { |
133
|
|
|
return Controller::join_links($this->getURLPrefix(), $this->Value(), $this->getURLSuffix()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function performReadonlyTransformation() { |
137
|
|
|
$newInst = parent::performReadonlyTransformation(); |
138
|
|
|
$newInst->helpText = $this->helpText; |
139
|
|
|
$newInst->urlPrefix = $this->urlPrefix; |
140
|
|
|
$newInst->urlSuffix = $this->urlSuffix; |
141
|
|
|
$newInst->defaultUrl = $this->defaultUrl; |
142
|
|
|
return $newInst; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Readonly version of a site tree URL segment field |
149
|
|
|
* |
150
|
|
|
* @package forms |
151
|
|
|
* @subpackage fields-basic |
152
|
|
|
*/ |
153
|
|
|
class SiteTreeURLSegmentField_Readonly extends SiteTreeURLSegmentField { |
154
|
|
|
protected $readonly = true; |
155
|
|
|
|
156
|
|
|
public function performReadonlyTransformation() { |
157
|
|
|
return clone $this; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|