2011-11-14 12:29:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to edit the SiteTree->URLSegment property, and suggest input based on the serverside rules
|
|
|
|
* defined through {@link SiteTree->generateURLSegment()} and {@link URLSegmentFilter}.
|
|
|
|
*
|
|
|
|
* Note: The actual conversion for saving the value takes place in the model layer.
|
2012-04-14 08:15:49 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage forms
|
2011-11-14 12:29:58 +01:00
|
|
|
*/
|
2012-04-14 08:15:49 +02:00
|
|
|
|
2011-11-14 12:29:58 +01:00
|
|
|
class SiteTreeURLSegmentField extends TextField {
|
|
|
|
|
2012-04-14 08:15:49 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-04 00:44:50 +01:00
|
|
|
protected $helpText, $urlPrefix, $urlSuffix;
|
2012-02-11 05:10:54 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $allowed_actions = array(
|
2011-11-14 12:29:58 +01:00
|
|
|
'suggest'
|
|
|
|
);
|
2012-05-10 14:18:22 +02:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Value() {
|
2012-05-14 15:12:33 +02:00
|
|
|
return rawurldecode($this->value);
|
|
|
|
}
|
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array(
|
|
|
|
'data-prefix' => $this->getURLPrefix(),
|
|
|
|
'data-suffix' => '?stage=Stage'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-05-10 14:18:22 +02:00
|
|
|
Requirements::javascript(CMS_DIR . '/javascript/SiteTreeURLSegmentField.js');
|
2012-11-08 03:53:43 +01:00
|
|
|
Requirements::add_i18n_javascript(CMS_DIR . '/javascript/lang', false, true);
|
|
|
|
Requirements::css(CMS_DIR . "/css/screen.css");
|
2012-05-10 14:18:22 +02:00
|
|
|
return parent::Field($properties);
|
|
|
|
}
|
2011-11-14 12:29:58 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function suggest($request) {
|
2013-04-05 15:06:32 +02:00
|
|
|
if(!$request->getVar('value')) {
|
|
|
|
return $this->httpError(405,
|
|
|
|
_t('SiteTreeURLSegmentField.EMPTY', 'Please enter a URL Segment or click cancel')
|
|
|
|
);
|
|
|
|
}
|
2011-11-14 12:29:58 +01:00
|
|
|
$page = $this->getPage();
|
|
|
|
|
|
|
|
// Same logic as SiteTree->onBeforeWrite
|
|
|
|
$page->URLSegment = $page->generateURLSegment($request->getVar('value'));
|
|
|
|
$count = 2;
|
|
|
|
while(!$page->validURLSegment()) {
|
|
|
|
$page->URLSegment = preg_replace('/-[0-9]+$/', null, $page->URLSegment) . '-' . $count;
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::curr()->getResponse()->addHeader('Content-Type', 'application/json');
|
|
|
|
return Convert::raw2json(array('value' => $page->URLSegment));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SiteTree
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function getPage() {
|
2012-04-05 23:00:04 +02:00
|
|
|
$idField = $this->getForm()->Fields()->dataFieldByName('ID');
|
2011-11-14 12:29:58 +01:00
|
|
|
return ($idField && $idField->Value()) ? DataObject::get_by_id('SiteTree', $idField->Value()) : singleton('SiteTree');
|
|
|
|
}
|
2012-02-11 05:10:54 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-10 21:35:13 +01:00
|
|
|
* @param string $string The secondary text to show
|
2012-02-11 05:10:54 +01:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setHelpText($string){
|
2012-02-11 05:10:54 +01:00
|
|
|
$this->helpText = $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string the secondary text to show in the template
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function getHelpText(){
|
2012-02-11 05:10:54 +01:00
|
|
|
return $this->helpText;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param the url that prefixes the page url segment field
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setURLPrefix($url){
|
2012-02-11 05:10:54 +01:00
|
|
|
$this->urlPrefix = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the url prefixes the page url segment field to show in template
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function getURLPrefix(){
|
2012-02-11 05:10:54 +01:00
|
|
|
return $this->urlPrefix;
|
|
|
|
}
|
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
public function getURLSuffix() {
|
|
|
|
return $this->urlSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setURLSuffix($suffix) {
|
|
|
|
$this->urlSuffix = $suffix;
|
|
|
|
}
|
2012-01-03 17:57:04 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Type() {
|
2012-05-10 14:18:22 +02:00
|
|
|
return 'text urlsegment';
|
2012-01-03 17:57:04 +01:00
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
public function getURL() {
|
|
|
|
return Controller::join_links($this->getURLPrefix(), $this->Value(), $this->getURLSuffix());
|
|
|
|
}
|
|
|
|
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|