2011-12-23 10:30:49 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-12-23 10:30:49 +01:00
|
|
|
* @subpackage security
|
|
|
|
*/
|
|
|
|
class MemberDatetimeOptionsetField extends OptionsetField {
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
const CUSTOM_OPTION = '__custom__';
|
|
|
|
|
2015-12-21 05:57:07 +01:00
|
|
|
/**
|
|
|
|
* Non-ambiguous date to use for the preview.
|
|
|
|
* Must be in 'y-MM-dd HH:mm:ss' format
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $preview_date = '25-12-2011 17:30:00';
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-11-30 12:27:38 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/javascript/MemberDatetimeOptionsetField.js');
|
2015-02-13 05:35:39 +01:00
|
|
|
$options = array();
|
|
|
|
$odd = false;
|
2012-11-30 12:27:38 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Add all options striped
|
|
|
|
$anySelected = false;
|
|
|
|
foreach($this->getSourceEmpty() as $value => $title) {
|
|
|
|
$odd = !$odd;
|
|
|
|
if(!$anySelected) {
|
|
|
|
$anySelected = $this->isSelectedValue($value, $this->Value());
|
2011-12-23 10:30:49 +01:00
|
|
|
}
|
2015-02-13 05:35:39 +01:00
|
|
|
$options[] = $this->getFieldOption($value, $title, $odd);
|
|
|
|
}
|
2011-12-23 10:30:49 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Add "custom" input field option
|
|
|
|
$options[] = $this->getCustomFieldOption(!$anySelected, !$odd);
|
2011-12-23 10:30:49 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Build fieldset
|
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'Options' => new ArrayList($options)
|
|
|
|
));
|
2011-12-23 10:30:49 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
return $this->customise($properties)->renderWith(
|
|
|
|
$this->getTemplates()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the "custom" selection field option
|
|
|
|
*
|
|
|
|
* @param bool $isChecked True if this is checked
|
|
|
|
* @param bool $odd Is odd striped
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
|
|
|
protected function getCustomFieldOption($isChecked, $odd) {
|
2011-12-23 10:30:49 +01:00
|
|
|
// Add "custom" input field
|
2015-02-13 05:35:39 +01:00
|
|
|
$option = $this->getFieldOption(
|
|
|
|
self::CUSTOM_OPTION,
|
|
|
|
_t('MemberDatetimeOptionsetField.Custom', 'Custom'),
|
|
|
|
$odd
|
|
|
|
);
|
|
|
|
$option->setField('isChecked', $isChecked);
|
|
|
|
$option->setField('CustomName', $this->getName().'[Custom]');
|
|
|
|
$option->setField('CustomValue', $this->Value());
|
|
|
|
if($this->Value()) {
|
2015-12-21 05:57:07 +01:00
|
|
|
$preview = Convert::raw2xml($this->previewFormat($this->Value()));
|
2015-02-13 05:35:39 +01:00
|
|
|
$option->setField('CustomPreview', $preview);
|
|
|
|
$option->setField('CustomPreviewLabel', _t('MemberDatetimeOptionsetField.Preview', 'Preview'));
|
|
|
|
}
|
|
|
|
return $option;
|
|
|
|
}
|
2011-12-23 10:30:49 +01:00
|
|
|
|
2015-12-21 05:57:07 +01:00
|
|
|
/**
|
|
|
|
* For a given format, generate a preview for the date
|
|
|
|
*
|
|
|
|
* @param string $format Date format
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function previewFormat($format) {
|
|
|
|
$date = $this->config()->preview_date;
|
|
|
|
$zendDate = new Zend_Date($date, 'y-MM-dd HH:mm:ss');
|
|
|
|
return $zendDate->toString($format);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOptionName() {
|
|
|
|
return parent::getOptionName() . '[Options]';
|
2011-12-23 10:30:49 +01:00
|
|
|
}
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
public function Type() {
|
|
|
|
return 'optionset memberdatetimeoptionset';
|
|
|
|
}
|
|
|
|
|
2011-12-23 10:30:49 +01:00
|
|
|
/**
|
|
|
|
* @todo Put this text into a template?
|
|
|
|
*/
|
2012-11-30 12:27:38 +01:00
|
|
|
public function getDescription() {
|
2014-08-15 08:53:05 +02:00
|
|
|
$output =
|
|
|
|
'<a href="#" class="toggle">'
|
2012-11-30 12:27:38 +01:00
|
|
|
. _t('MemberDatetimeOptionsetField.Toggle', 'Show formatting help')
|
|
|
|
. '</a>'
|
|
|
|
. '<ul class="toggle-content">'
|
|
|
|
. '<li>YYYY = ' . _t('MemberDatetimeOptionsetField.FOURDIGITYEAR', 'Four-digit year',
|
|
|
|
40, 'Help text describing what "YYYY" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>YY = ' . _t('MemberDatetimeOptionsetField.TWODIGITYEAR', 'Two-digit year',
|
|
|
|
40, 'Help text describing what "YY" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>MMMM = ' . _t('MemberDatetimeOptionsetField.FULLNAMEMONTH', 'Full name of month (e.g. June)',
|
|
|
|
40, 'Help text describing what "MMMM" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>MMM = ' . _t('MemberDatetimeOptionsetField.SHORTMONTH', 'Short name of month (e.g. Jun)',
|
|
|
|
40, 'Help text letting describing what "MMM" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>MM = ' . _t('MemberDatetimeOptionsetField.TWODIGITMONTH', 'Two-digit month (01=January, etc.)',
|
|
|
|
40, 'Help text describing what "MM" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>M = ' . _t('MemberDatetimeOptionsetField.MONTHNOLEADING', 'Month digit without leading zero',
|
|
|
|
40, 'Help text describing what "M" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>dd = ' . _t('MemberDatetimeOptionsetField.TWODIGITDAY', 'Two-digit day of month',
|
|
|
|
40, 'Help text describing what "dd" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>d = ' . _t('MemberDatetimeOptionsetField.DAYNOLEADING', 'Day of month without leading zero',
|
|
|
|
40, 'Help text describing what "d" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>hh = ' . _t('MemberDatetimeOptionsetField.TWODIGITHOUR', 'Two digits of hour (00 through 23)',
|
|
|
|
40, 'Help text describing what "hh" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>h = ' . _t('MemberDatetimeOptionsetField.HOURNOLEADING', 'Hour without leading zero',
|
|
|
|
40, 'Help text describing what "h" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>mm = ' . _t('MemberDatetimeOptionsetField.TWODIGITMINUTE',
|
2012-09-26 23:34:00 +02:00
|
|
|
'Two digits of minute (00 through 59)',
|
2012-11-30 12:27:38 +01:00
|
|
|
40, 'Help text describing what "mm" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>m = ' . _t('MemberDatetimeOptionsetField.MINUTENOLEADING', 'Minute without leading zero',
|
|
|
|
40, 'Help text describing what "m" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>ss = ' . _t('MemberDatetimeOptionsetField.TWODIGITSECOND',
|
2012-09-26 23:34:00 +02:00
|
|
|
'Two digits of second (00 through 59)',
|
2012-11-30 12:27:38 +01:00
|
|
|
40, 'Help text describing what "ss" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>s = ' . _t('MemberDatetimeOptionsetField.DIGITSDECFRACTIONSECOND',
|
2012-09-26 23:34:00 +02:00
|
|
|
'One or more digits representing a decimal fraction of a second',
|
2012-11-30 12:27:38 +01:00
|
|
|
40, 'Help text describing what "s" means in ISO date formatting') . '</li>'
|
|
|
|
. '<li>a = ' . _t('MemberDatetimeOptionsetField.AMORPM', 'AM (Ante meridiem) or PM (Post meridiem)',
|
|
|
|
40, 'Help text describing what "a" means in ISO date formatting') . '</li>'
|
|
|
|
. '</ul>';
|
2011-12-23 10:30:49 +01:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value) {
|
2015-02-13 05:35:39 +01:00
|
|
|
// Extract custom option from postback
|
|
|
|
if(is_array($value)) {
|
|
|
|
if(empty($value['Options'])) {
|
|
|
|
$value = '';
|
|
|
|
} elseif($value['Options'] === self::CUSTOM_OPTION) {
|
|
|
|
$value = $value['Custom'];
|
|
|
|
} else {
|
|
|
|
$value = $value['Options'];
|
|
|
|
}
|
2011-12-23 10:30:49 +01:00
|
|
|
}
|
2015-02-13 05:35:39 +01:00
|
|
|
|
|
|
|
return parent::setValue($value);
|
2011-12-23 10:30:49 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
/**
|
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2015-02-13 05:35:39 +01:00
|
|
|
$value = $this->Value();
|
|
|
|
if(!$value) {
|
|
|
|
return true; // no custom value, don't validate
|
|
|
|
}
|
2011-12-23 10:30:49 +01:00
|
|
|
|
|
|
|
// Check that the current date with the date format is valid or not
|
|
|
|
require_once 'Zend/Date.php';
|
|
|
|
$date = Zend_Date::now()->toString($value);
|
|
|
|
$valid = Zend_Date::isDate($date, $value);
|
|
|
|
if($valid) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-13 05:35:39 +01:00
|
|
|
|
|
|
|
// Fail
|
|
|
|
$validator->validationError(
|
|
|
|
$this->getName(),
|
|
|
|
_t(
|
|
|
|
'MemberDatetimeOptionsetField.DATEFORMATBAD',
|
|
|
|
"Date format is invalid"
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
2011-12-23 10:30:49 +01:00
|
|
|
}
|
2012-04-12 02:23:18 +02:00
|
|
|
}
|