|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use SilverStripe\ORM\ArrayList; |
|
4
|
|
|
/** |
|
5
|
|
|
* @package framework |
|
6
|
|
|
* @subpackage security |
|
7
|
|
|
*/ |
|
8
|
|
|
class MemberDatetimeOptionsetField extends OptionsetField { |
|
9
|
|
|
|
|
10
|
|
|
const CUSTOM_OPTION = '__custom__'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Non-ambiguous date to use for the preview. |
|
14
|
|
|
* Must be in 'y-MM-dd HH:mm:ss' format |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $preview_date = '25-12-2011 17:30:00'; |
|
19
|
|
|
|
|
20
|
|
|
private static $casting = ['Description' => 'HTMLText']; |
|
21
|
|
|
|
|
22
|
|
|
private $descriptionTemplate = ''; |
|
23
|
|
|
|
|
24
|
|
|
public function Field($properties = array()) { |
|
25
|
|
|
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberDatetimeOptionsetField.js'); |
|
26
|
|
|
$options = array(); |
|
27
|
|
|
$odd = false; |
|
28
|
|
|
|
|
29
|
|
|
// Add all options striped |
|
30
|
|
|
$anySelected = false; |
|
31
|
|
|
foreach($this->getSourceEmpty() as $value => $title) { |
|
32
|
|
|
$odd = !$odd; |
|
33
|
|
|
if(!$anySelected) { |
|
34
|
|
|
$anySelected = $this->isSelectedValue($value, $this->Value()); |
|
35
|
|
|
} |
|
36
|
|
|
$options[] = $this->getFieldOption($value, $title, $odd); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Add "custom" input field option |
|
40
|
|
|
$options[] = $this->getCustomFieldOption(!$anySelected, !$odd); |
|
41
|
|
|
|
|
42
|
|
|
// Build fieldset |
|
43
|
|
|
$properties = array_merge($properties, array( |
|
44
|
|
|
'Options' => new ArrayList($options) |
|
45
|
|
|
)); |
|
46
|
|
|
|
|
47
|
|
|
return $this->customise($properties)->renderWith( |
|
48
|
|
|
$this->getTemplates() |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create the "custom" selection field option |
|
54
|
|
|
* |
|
55
|
|
|
* @param bool $isChecked True if this is checked |
|
56
|
|
|
* @param bool $odd Is odd striped |
|
57
|
|
|
* @return ArrayData |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getCustomFieldOption($isChecked, $odd) { |
|
60
|
|
|
// Add "custom" input field |
|
61
|
|
|
$option = $this->getFieldOption( |
|
62
|
|
|
self::CUSTOM_OPTION, |
|
63
|
|
|
_t('MemberDatetimeOptionsetField.Custom', 'Custom'), |
|
64
|
|
|
$odd |
|
65
|
|
|
); |
|
66
|
|
|
$option->setField('isChecked', $isChecked); |
|
67
|
|
|
$option->setField('CustomName', $this->getName().'[Custom]'); |
|
68
|
|
|
$option->setField('CustomValue', $this->Value()); |
|
69
|
|
|
if($this->Value()) { |
|
70
|
|
|
$preview = Convert::raw2xml($this->previewFormat($this->Value())); |
|
71
|
|
|
$option->setField('CustomPreview', $preview); |
|
72
|
|
|
$option->setField('CustomPreviewLabel', _t('MemberDatetimeOptionsetField.Preview', 'Preview')); |
|
73
|
|
|
} |
|
74
|
|
|
return $option; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* For a given format, generate a preview for the date |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $format Date format |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function previewFormat($format) { |
|
84
|
|
|
$date = $this->config()->preview_date; |
|
|
|
|
|
|
85
|
|
|
$zendDate = new Zend_Date($date, 'y-MM-dd HH:mm:ss'); |
|
86
|
|
|
return $zendDate->toString($format); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getOptionName() { |
|
90
|
|
|
return parent::getOptionName() . '[Options]'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function Type() { |
|
94
|
|
|
return 'optionset memberdatetimeoptionset'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getDescription() { |
|
98
|
|
|
if ($template = $this->getDescriptionTemplate()) { |
|
99
|
|
|
return $this->renderWith($template); |
|
100
|
|
|
} |
|
101
|
|
|
return parent::getDescription(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getDescriptionTemplate() { |
|
105
|
|
|
return $this->descriptionTemplate; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setDescriptionTemplate($template) { |
|
109
|
|
|
$this->descriptionTemplate = $template; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function setValue($value) { |
|
113
|
|
|
// Extract custom option from postback |
|
114
|
|
|
if(is_array($value)) { |
|
115
|
|
|
if(empty($value['Options'])) { |
|
116
|
|
|
$value = ''; |
|
117
|
|
|
} elseif($value['Options'] === self::CUSTOM_OPTION) { |
|
118
|
|
|
$value = $value['Custom']; |
|
119
|
|
|
} else { |
|
120
|
|
|
$value = $value['Options']; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return parent::setValue($value); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Validate this field |
|
129
|
|
|
* |
|
130
|
|
|
* @param Validator $validator |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
public function validate($validator) { |
|
134
|
|
|
$value = $this->Value(); |
|
135
|
|
|
if(!$value) { |
|
136
|
|
|
return true; // no custom value, don't validate |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Check that the current date with the date format is valid or not |
|
140
|
|
|
require_once 'Zend/Date.php'; |
|
141
|
|
|
$date = Zend_Date::now()->toString($value); |
|
142
|
|
|
$valid = Zend_Date::isDate($date, $value); |
|
143
|
|
|
if($valid) { |
|
144
|
|
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Fail |
|
148
|
|
|
$validator->validationError( |
|
149
|
|
|
$this->getName(), |
|
150
|
|
|
_t( |
|
151
|
|
|
'MemberDatetimeOptionsetField.DATEFORMATBAD', |
|
152
|
|
|
"Date format is invalid" |
|
153
|
|
|
), |
|
154
|
|
|
"validation" |
|
155
|
|
|
); |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.