2011-11-14 12:29:58 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Forms;
|
|
|
|
|
2016-08-10 06:08:39 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\View\Requirements;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-11-14 12:29:58 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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()) {
|
2012-05-10 14:18:22 +02:00
|
|
|
return parent::Field($properties);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-08-10 06:08:39 +02:00
|
|
|
/**
|
2016-09-09 01:26:24 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-08-10 06:08:39 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
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');
|
2016-08-10 06:08:39 +02:00
|
|
|
return ($idField && $idField->Value())
|
|
|
|
? SiteTree::get()->byID($idField->Value())
|
|
|
|
: SiteTree::singleton();
|
2011-11-14 12:29:58 +01:00
|
|
|
}
|
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
|
2016-06-16 06:57:19 +02:00
|
|
|
* @return $this
|
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
|
|
|
/**
|
2016-06-16 06:57:19 +02:00
|
|
|
* @param string $url the url that prefixes the page url segment field
|
|
|
|
* @return $this
|
2012-02-11 05:10:54 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2016-06-16 06:57:19 +02:00
|
|
|
* @return string the url prefixes the page url segment field to show in template
|
2012-02-11 05:10:54 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2016-06-16 06:57:19 +02:00
|
|
|
* @return string Indicator for UI to respond to changes accurately,
|
2015-02-20 23:33:59 +01:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|