1+ <?php
2+ /**
3+ * Copyright 2016 OpenStack Foundation
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ * Unless required by applicable law or agreed to in writing, software
9+ * distributed under the License is distributed on an "AS IS" BASIS,
10+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ * See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ **/
14+
15+
16+ class FuturePublishDate extends DataExtension
17+ {
18+ private static $ db = array (
19+ 'PublishDate ' => 'SS_DateTime '
20+ );
21+
22+ public function updateCMSFields (FieldList $ fields ) {
23+ $ datetimeField = new DatetimeField ( 'PublishDate ' , 'Publish From (UTC) ' );
24+
25+ $ dateField = $ datetimeField ->getDateField ();
26+ $ dateField ->setConfig ( 'dateformat ' , 'yyyy-MM-dd ' );
27+ $ dateField ->setConfig ( 'showcalendar ' , true );
28+
29+ $ timeField = $ datetimeField ->getTimeField ();
30+ $ timeField ->setConfig ( 'timeformat ' , 'H:m:s ' );
31+
32+ $ fields ->insertBefore ( $ datetimeField , 'Content ' );
33+ }
34+
35+ public function populateDefaults () {
36+ $ this ->owner ->PublishDate = SS_Datetime::now ();
37+ }
38+
39+ public function onAfterWrite () {
40+ if ($ this ->owner ->ClassName == 'RedirectorPage ' ) {
41+ $ page = $ this ->owner ->LinkTo ();
42+ $ page ->PublishDate = $ this ->owner ->PublishDate ;
43+ $ page ->write ();
44+ }
45+
46+ parent ::onAfterWrite ();
47+ }
48+
49+ }
50+
51+ class FuturePublishDateController extends Extension
52+ {
53+ public function beforeCallActionHandler ($ request , $ action ) {
54+ if ('index ' !== $ action || $ this ->owner ->is_a ('ErrorPage_Controller ' )) {
55+ return ;
56+ }
57+
58+ $ isDraftPreview = 'Stage ' === $ request ->getVar ('stage ' );
59+
60+ if ( !$ isDraftPreview
61+ && $ this ->owner ->PublishDate
62+ && strtotime ($ this ->owner ->PublishDate .' UTC ' ) > time ()
63+ ){
64+ // bug in SS 3.1 in OldPageRedirector
65+ // $this->owner->httpError( 404 );
66+
67+ $ response = $ request ->isMedia () ? null : ErrorPage::response_for (404 );
68+ if ($ response ) {
69+ return $ response ;
70+ }
71+
72+ throw new SS_HTTPResponse_Exception ('404 Not Found ' , 404 );
73+ }
74+ }
75+ }
0 commit comments