1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author nicolaas [at] sunnysideup.co.nz |
5
|
|
|
* To Do: |
6
|
|
|
* 1. write documenation |
7
|
|
|
* 2. change $config into NON-static |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class SimpleDateField extends DateField |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Americans should set this to TRUE. |
16
|
|
|
* For people who write |
17
|
|
|
* 10-Nov-2012, to mean 10 November, you an leave it as FALSE |
18
|
|
|
* For sites with customers from all over, you will have to tell them the |
19
|
|
|
* preferred format. You can use the placeholder value for this. |
20
|
|
|
* |
21
|
|
|
* @var Boolean |
22
|
|
|
*/ |
23
|
|
|
private static $month_before_day = false; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The PHP date function formatting for showing the final date. |
27
|
|
|
* @var String |
28
|
|
|
*/ |
29
|
|
|
private static $default_fancy_date_format = 'j F Y'; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* What would you like the place holder value to be? |
33
|
|
|
* @var String |
34
|
|
|
*/ |
35
|
|
|
private static $placeholder_value = '31 jan 2123'; |
|
|
|
|
36
|
|
|
|
37
|
|
|
public function __construct($name, $title = null, $value = null, $form = null, $config = array()) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
parent::__construct($name, $title, $value, $form); |
|
|
|
|
40
|
|
|
$this->setConfig("dmyfields", false); |
41
|
|
|
$this->setConfig("showcalendar", false); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function Field($options = array()) |
45
|
|
|
{ |
46
|
|
|
//GENERAL |
47
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); |
48
|
|
|
Requirements::javascript("datefield_simplified/javascript/SimpleDateField.js"); |
49
|
|
|
$this->addExtraClass("simpledatefield"); |
50
|
|
|
$this->setAttribute("placeholder", $this->Config()->get("placeholder_value")); |
51
|
|
|
$html = parent::Field($options); |
52
|
|
|
$fieldID = $this->id(); |
53
|
|
|
$url = Convert::raw2js(Director::absoluteBaseURL().Config::inst()->get("SimpleDateField_Controller", "url")."/ajaxvalidation/"); |
54
|
|
|
$objectID = $fieldID."_OBJECT"; |
55
|
|
|
Requirements::customScript( |
56
|
|
|
" |
57
|
|
|
var $objectID = new SimpleDateFieldAjaxValidationAPI('".$fieldID."'); |
58
|
|
|
$objectID.init(); |
59
|
|
|
$objectID.setVar('url', '$url'); |
60
|
|
|
", |
61
|
|
|
'func_SimpleDateField'.$fieldID |
62
|
|
|
); |
63
|
|
|
return $html; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the internal value to ISO date format. |
68
|
|
|
* |
69
|
|
|
* @param String|Array $val |
70
|
|
|
*/ |
71
|
|
|
public function setValue($val) |
72
|
|
|
{ |
73
|
|
|
$date = $this->ConvertToTSorERROR($val); |
|
|
|
|
74
|
|
|
if (is_numeric($date) && intval($date) == $date && $date > 0) { |
75
|
|
|
$val = date("Y-m-d", $date); |
76
|
|
|
} else { |
77
|
|
|
$val = null; |
78
|
|
|
} |
79
|
|
|
return parent::setValue($val); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* changes the raw input into TimeStamp Date OR ERROR String |
85
|
|
|
* |
86
|
|
|
* @param String $rawInput |
87
|
|
|
* |
88
|
|
|
* @return Int | String |
|
|
|
|
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
public function ConvertToTSorERROR($rawInput) |
92
|
|
|
{ |
93
|
|
|
$settings = $this->getConfig(); |
94
|
|
|
if (!isset($settings['dateformat'])) { |
95
|
|
|
$settings['dateformat'] = $this->Config()->get("default_fancy_date_format"); |
96
|
|
|
} |
97
|
|
|
$tsOrError = null; |
98
|
|
|
if ($this->Config()->get("month_before_day")) { |
99
|
|
|
$cleanedInput = str_replace("-", "/", $rawInput); |
100
|
|
|
} else { |
101
|
|
|
$cleanedInput = str_replace("/", "-", $rawInput); |
102
|
|
|
} |
103
|
|
|
if ($cleanedInput) { |
104
|
|
|
$tsOrError = intval(strtotime($cleanedInput)); |
105
|
|
|
} |
106
|
|
|
if (is_numeric($tsOrError) && $tsOrError > 0) { |
107
|
|
View Code Duplication |
if (isset($settings['min']) && $minDate = $settings['min']) { |
|
|
|
|
108
|
|
|
$minDate = strtotime($minDate); |
109
|
|
|
if ($minDate) { |
110
|
|
|
if ($minDate > $tsOrError) { |
111
|
|
|
$tsOrError = sprintf(_t('SimpleDateField.VALIDDATEMINDATE', "Your date can not be before %s."), date($settings['dateformat'], $minDate)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
View Code Duplication |
if (isset($settings['max']) && $maxDate = $settings['max']) { |
|
|
|
|
116
|
|
|
// ISO or strtotime() |
|
|
|
|
117
|
|
|
$maxDate = strtotime($maxDate); |
118
|
|
|
if ($maxDate) { |
119
|
|
|
if ($maxDate < $tsOrError) { |
120
|
|
|
$tsOrError = sprintf(_t('SimpleDateField.VALIDDATEMAXDATE', "Your date can not be after %s."), date($settings['dateformat'], $maxDate)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
if (!$tsOrError) { |
126
|
|
|
if (!trim($rawInput)) { |
127
|
|
|
$tsOrError = sprintf(_t('SimpleDateField.VALIDDATEDATE_NOENTRY', "You need to enter a date."), $rawInput); |
128
|
|
|
} else { |
129
|
|
|
$tsOrError = sprintf(_t('SimpleDateField.VALIDDATEDATE_CANTMAKEDATE', "We did not understand the date you entered '%s'."), $rawInput); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
return $tsOrError; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Turns user input into a formatted Date or an Error Message ... |
137
|
|
|
* |
138
|
|
|
* @param String $rawInput |
139
|
|
|
* @return String |
140
|
|
|
*/ |
141
|
|
|
public function ConverToFancyDate($rawInput) |
142
|
|
|
{ |
143
|
|
|
$settings = $this->getConfig(); |
|
|
|
|
144
|
|
|
$tsOrError = $this->ConvertToTSorERROR($rawInput); |
145
|
|
|
if (is_numeric($tsOrError) && intval($tsOrError)) { |
146
|
|
|
$array = array( |
147
|
|
|
"value" => date($this->Config()->get("default_fancy_date_format"), $tsOrError), |
148
|
|
|
"success" => 1 |
149
|
|
|
); |
150
|
|
|
} else { |
151
|
|
|
$array = array( |
152
|
|
|
"value" => $tsOrError, |
153
|
|
|
"success" => 0 |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
return Convert::raw2json($array); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
class SimpleDateField_Controller extends Controller |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
private static $allowed_actions = array( |
|
|
|
|
164
|
|
|
"ajaxvalidation" => true |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
private static $url = 'formfields-simpledatefield'; |
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
* @param HTTPRequest |
172
|
|
|
* @return String (JSON) |
173
|
|
|
*/ |
174
|
|
|
public function ajaxvalidation($request) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$rawInput = ''; |
177
|
|
|
if (isset($_GET["value"])) { |
178
|
|
|
$rawInput = ($_GET["value"]); |
179
|
|
|
} |
180
|
|
|
$obj = Injector::inst()->get("SimpleDateField", $asSingleton = true, array("temp", "temp")); |
181
|
|
|
return $obj->ConverToFancyDate($rawInput); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
class SimpleDateField_Editable extends EditableFormField |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
private static $db = array( |
|
|
|
|
189
|
|
|
"ShowCalendar" => "Boolean", |
190
|
|
|
"OnlyPastDates" => "Boolean", |
191
|
|
|
"OnlyFutureDates" => "Boolean", |
192
|
|
|
"MonthBeforeDay" => "Boolean", |
193
|
|
|
"ExplanationForEnteringDates" => "Varchar(120)" |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
private static $singular_name = 'Simple Date Field'; |
|
|
|
|
197
|
|
|
|
198
|
|
|
private static $plural_name = 'Simple Date Fields'; |
|
|
|
|
199
|
|
|
|
200
|
|
|
public function Icon() |
201
|
|
|
{ |
202
|
|
|
return 'userforms/images/editabledatefield.png'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function canEdit($member = null) |
206
|
|
|
{ |
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getFieldConfiguration() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$fields = parent::getFieldConfiguration(); |
213
|
|
|
// eventually replace hard-coded "Fields"? |
214
|
|
|
$baseName = "Fields[$this->ID]"; |
215
|
|
|
$ShowCalendar = ($this->getSetting('ShowCalendar')) ? $this->getSetting('ShowCalendar') : '0'; |
216
|
|
|
$OnlyPastDates = ($this->getSetting('OnlyPastDates')) ? $this->getSetting('OnlyPastDates') : '0'; |
217
|
|
|
$OnlyFutureDates = ($this->getSetting('OnlyFutureDates')) ? $this->getSetting('OnlyFutureDates') : '0'; |
218
|
|
|
$MonthBeforeDay = ($this->getSetting('MonthBeforeDay')) ? $this->getSetting('MonthBeforeDay') : '0'; |
219
|
|
|
$ExplanationForEnteringDates = ($this->getSetting('ExplanationForEnteringDates')) ? $this->getSetting('ExplanationForEnteringDates') : ''; |
220
|
|
|
$extraFields = new FieldList( |
221
|
|
|
new FieldGroup( |
222
|
|
|
_t('SimpleDateField_Editable.DATESETTINGS', 'Date Settings'), |
223
|
|
|
new CheckboxField($baseName . "[CustomSettings][ShowCalendar]", "Show Calendar", $ShowCalendar), |
224
|
|
|
new CheckboxField($baseName . "[CustomSettings][OnlyPastDates]", "Only Past Dates?", $OnlyPastDates), |
225
|
|
|
new CheckboxField($baseName . "[CustomSettings][OnlyFutureDates]", "Only Future Dates?", $OnlyFutureDates), |
226
|
|
|
new CheckboxField($baseName . "[CustomSettings][MonthBeforeDay]", "Month before day (e.g. Jan 11 2011)?", $MonthBeforeDay), |
227
|
|
|
new TextField($baseName . "[CustomSettings][ExplanationForEnteringDates]", "Explanation for entering dates", $ExplanationForEnteringDates) |
228
|
|
|
) |
229
|
|
|
); |
230
|
|
|
$fields->merge($extraFields); |
231
|
|
|
return $fields; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function getFormField() |
235
|
|
|
{ |
236
|
|
|
$field = new SimpleDateField($this->Name, $this->Title); |
237
|
|
|
if ($this->getSetting('ShowCalendar')) { |
238
|
|
|
$field->setConfig("showcalendar", true); |
239
|
|
|
} |
240
|
|
|
if ($this->getSetting('OnlyPastDates')) { |
241
|
|
|
$field->setConfig("max", "today"); |
242
|
|
|
Config::inst()->update("SimpleDateField", "placeholder_value", '31 jan 1974'); |
243
|
|
|
} elseif ($this->getSetting('OnlyFutureDates')) { |
244
|
|
|
$field->setConfig("min", "today"); |
245
|
|
|
Config::inst()->update("SimpleDateField", "placeholder_value", '31 jan 2023'); |
246
|
|
|
} |
247
|
|
|
if ($this->getSetting('MonthBeforeDay')) { |
248
|
|
|
$field->setConfig("dateformat", 'l F j Y'); |
249
|
|
|
Config::inst()->update("SimpleDateField", "default_fancy_date_format", 'l F j Y'); |
250
|
|
|
Config::inst()->update("SimpleDateField", "month_before_day", true); |
251
|
|
|
} else { |
252
|
|
|
Config::inst()->update("SimpleDateField", "default_fancy_date_format", 'l j F Y'); |
253
|
|
|
Config::inst()->update("SimpleDateField", "month_before_day", false); |
254
|
|
|
} |
255
|
|
|
if ($this->getSetting('ExplanationForEnteringDates')) { |
256
|
|
|
$field->setRightTitle($this->getSetting('ExplanationForEnteringDates')); |
257
|
|
|
} |
258
|
|
|
return $field; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.