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}.
|
2016-01-06 00:42:07 +01:00
|
|
|
*
|
2011-11-14 12:29:58 +01:00
|
|
|
* 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 {
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-01-06 00:42:07 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
2012-04-14 08:15:49 +02:00
|
|
|
*/
|
2015-02-20 23:33:59 +01:00
|
|
|
protected $helpText, $urlPrefix, $urlSuffix, $defaultUrl;
|
2016-03-08 21:50:55 +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(),
|
2015-02-20 23:33:59 +01:00
|
|
|
'data-suffix' => '?stage=Stage',
|
|
|
|
'data-default-url' => $this->getDefaultURL()
|
2013-02-04 00:44:50 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Field($properties = array()) {
|
2016-04-19 13:22:18 +02:00
|
|
|
Requirements::javascript(CMS_DIR . '/client/dist/js/SiteTreeURLSegmentField.js');
|
|
|
|
Requirements::add_i18n_javascript(CMS_DIR . '/client/src/lang', false, true);
|
|
|
|
Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css');
|
2012-05-10 14:18:22 +02:00
|
|
|
return parent::Field($properties);
|
|
|
|
}
|
2016-03-08 21:50:55 +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++;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-11-14 12:29:58 +01:00
|
|
|
Controller::curr()->getResponse()->addHeader('Content-Type', 'application/json');
|
|
|
|
return Convert::raw2json(array('value' => $page->URLSegment));
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-11-14 12:29:58 +01:00
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
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){
|
2015-02-20 23:33:59 +01:00
|
|
|
$this->helpText = $string;
|
|
|
|
return $this;
|
2012-02-11 05:10:54 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-02-11 05:10:54 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-02-11 05:10:54 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-02-11 05:10:54 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
2015-02-20 23:33:59 +01:00
|
|
|
return $this;
|
2012-02-11 05:10:54 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-02-11 05:10:54 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
public function getURLSuffix() {
|
|
|
|
return $this->urlSuffix;
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:33:59 +01:00
|
|
|
/**
|
|
|
|
* @return Indicator for UI to respond to changes accurately,
|
|
|
|
* and auto-update the field value if changes to the default occur.
|
|
|
|
* Does not set the field default value.
|
|
|
|
*/
|
|
|
|
public function getDefaultURL(){
|
|
|
|
return $this->defaultUrl;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2015-02-20 23:33:59 +01:00
|
|
|
public function setDefaultURL($url) {
|
|
|
|
$this->defaultUrl = $url;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
public function setURLSuffix($suffix) {
|
|
|
|
$this->urlSuffix = $suffix;
|
2015-02-20 23:33:59 +01:00
|
|
|
return $this;
|
2013-02-04 00:44:50 +01:00
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
2016-03-15 00:48:46 +01:00
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
$newInst = parent::performReadonlyTransformation();
|
|
|
|
$newInst->helpText = $this->helpText;
|
|
|
|
$newInst->urlPrefix = $this->urlPrefix;
|
|
|
|
$newInst->urlSuffix = $this->urlSuffix;
|
|
|
|
$newInst->defaultUrl = $this->defaultUrl;
|
|
|
|
return $newInst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Readonly version of a site tree URL segment field
|
|
|
|
*
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
|
|
|
*/
|
|
|
|
class SiteTreeURLSegmentField_Readonly extends SiteTreeURLSegmentField {
|
|
|
|
protected $readonly = true;
|
|
|
|
|
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
return clone $this;
|
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|