1 | <?php |
||
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() { |
||
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() { |
||
73 | |||
74 | /** |
||
75 | * @param string $string The secondary text to show |
||
76 | */ |
||
77 | public function setHelpText($string){ |
||
81 | |||
82 | /** |
||
83 | * @return string the secondary text to show in the template |
||
84 | */ |
||
85 | public function getHelpText(){ |
||
89 | |||
90 | /** |
||
91 | * @param the url that prefixes the page url segment field |
||
92 | */ |
||
93 | public function setURLPrefix($url){ |
||
97 | |||
98 | /** |
||
99 | * @return the url prefixes the page url segment field to show in template |
||
100 | */ |
||
101 | public function getURLPrefix(){ |
||
104 | |||
105 | public function getURLSuffix() { |
||
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(){ |
||
117 | |||
118 | public function setDefaultURL($url) { |
||
122 | |||
123 | public function setURLSuffix($suffix) { |
||
127 | |||
128 | public function Type() { |
||
131 | |||
132 | public function getURL() { |
||
135 | |||
136 | public function performReadonlyTransformation() { |
||
137 | $newInst = parent::performReadonlyTransformation(); |
||
138 | $newInst->helpText = $this->helpText; |
||
144 | } |
||
145 | |||
160 |