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
|
|
|
|
*/
|
|
|
|
protected $helpText, $urlPrefix;
|
2012-02-11 05:10:54 +01:00
|
|
|
|
2011-11-14 12:29:58 +01:00
|
|
|
static $allowed_actions = array(
|
|
|
|
'suggest'
|
|
|
|
);
|
|
|
|
|
|
|
|
function suggest($request) {
|
|
|
|
if(!$request->getVar('value')) return $this->httpError(405);
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string the secondary text to show
|
|
|
|
*/
|
|
|
|
function setHelpText($string){
|
|
|
|
$this->helpText = $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string the secondary text to show in the template
|
|
|
|
*/
|
|
|
|
function getHelpText(){
|
|
|
|
return $this->helpText;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param the url that prefixes the page url segment field
|
|
|
|
*/
|
|
|
|
function setURLPrefix($url){
|
|
|
|
$this->urlPrefix = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the url prefixes the page url segment field to show in template
|
|
|
|
*/
|
|
|
|
function getURLPrefix(){
|
|
|
|
return $this->urlPrefix;
|
|
|
|
}
|
|
|
|
|
2012-01-03 17:57:04 +01:00
|
|
|
|
|
|
|
function Type() {
|
|
|
|
return 'text sitetreeurlsegment';
|
|
|
|
}
|
2011-11-14 12:29:58 +01:00
|
|
|
}
|