From 7d245c3803b260c4cd9e23da407982edda1ea82d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 23 Dec 2011 10:30:49 +0100 Subject: [PATCH] MINOR Reduced unnecessary autoloading --- admin/code/CMSMenu.php | 13 +- forms/MemberDatetimeOptionsetField.php | 109 +++++++++++++++++ security/Member.php | 112 +----------------- .../MemberDatetimeOptionsetFieldTest.php | 6 +- 4 files changed, 120 insertions(+), 120 deletions(-) create mode 100644 forms/MemberDatetimeOptionsetField.php diff --git a/admin/code/CMSMenu.php b/admin/code/CMSMenu.php index becb5261d..36d7826a3 100644 --- a/admin/code/CMSMenu.php +++ b/admin/code/CMSMenu.php @@ -290,14 +290,13 @@ class CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider $subClasses = array_unique($subClasses); foreach($subClasses as $key => $className) { // Remove abstract classes and LeftAndMain - $classReflection = new ReflectionClass($className); - if( - !$classReflection->isInstantiable() - || 'LeftAndMain' == $className - || ClassInfo::classImplements($className, 'TestOnly') - ) { + if('LeftAndMain' == $className || ClassInfo::classImplements($className, 'TestOnly')) { unset($subClasses[$key]); - } + } else { + // Separate conditional to avoid autoloading the class + $classReflection = new ReflectionClass($className); + if(!$classReflection->isInstantiable()) unset($subClasses[$key]); + } } return $subClasses; diff --git a/forms/MemberDatetimeOptionsetField.php b/forms/MemberDatetimeOptionsetField.php new file mode 100644 index 000000000..158a5da0b --- /dev/null +++ b/forms/MemberDatetimeOptionsetField.php @@ -0,0 +1,109 @@ +getSource(); + + foreach($source as $key => $value) { + // convert the ID to an HTML safe value (dots are not replaced, as they are valid in an ID attribute) + $itemID = $this->id() . '_' . preg_replace('/[^\.a-zA-Z0-9\-\_]/', '_', $key); + if($key == $this->value) { + $useValue = false; + $checked = " checked=\"checked\""; + } else { + $checked = ""; + } + + $odd = ($odd + 1) % 2; + $extraClass = $odd ? "odd" : "even"; + $extraClass .= " val" . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $key); + $disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? "disabled=\"disabled\"" : ""; + $ATT_key = Convert::raw2att($key); + + $options .= "
  • name\" type=\"radio\" value=\"$key\"$checked $disabled class=\"radio\" />
  • \n"; + } + + // Add "custom" input field + $value = ($this->value && !array_key_exists($this->value, $this->source)) ? $this->value : null; + $checked = ($value) ? " checked=\"checked\"" : ''; + $options .= "
  • " + . sprintf("", $itemID, $this->name, $checked) + . sprintf('', $itemID, _t('MemberDatetimeOptionsetField.Custom', 'Custom')) + . sprintf("\n", $this->name, $value) + . sprintf("", $this->Link() . '/validate'); + $options .= ($value) ? sprintf( + '(%s: "%s")', + _t('MemberDatetimeOptionsetField.Preview', 'Preview'), + Zend_Date::now()->toString($value) + ) : ''; + $options .= "" . _t('MemberDatetimeOptionsetField.TOGGLEHELP', 'Toggle formatting help') . ""; + $options .= "
    "; + $options .= $this->getFormattingHelpText(); + $options .= "
    "; + $options .= "
  • \n"; + + $id = $this->id(); + return "\n"; + } + + /** + * @todo Put this text into a template? + */ + function getFormattingHelpText() { + $output = ''; + return $output; + } + + function setValue($value) { + if($value == '__custom__') { + $value = isset($_REQUEST[$this->name . '_custom']) ? $_REQUEST[$this->name . '_custom'] : null; + } + if($value) { + parent::setValue($value); + } + } + + function validate() { + $value = isset($_POST[$this->name . '_custom']) ? $_POST[$this->name . '_custom'] : null; + if(!$value) return true; // no custom value, don't validate + + // Check that the current date with the date format is valid or not + $validator = $this->form ? $this->form->getValidator() : null; + require_once 'Zend/Date.php'; + $date = Zend_Date::now()->toString($value); + $valid = Zend_Date::isDate($date, $value); + if($valid) { + return true; + } else { + if($validator) { + $validator->validationError($this->name, _t('MemberDatetimeOptionsetField.DATEFORMATBAD',"Date format is invalid"), "validation", false); + } + return false; + } + } +} \ No newline at end of file diff --git a/security/Member.php b/security/Member.php index 495151124..7c8a81a29 100644 --- a/security/Member.php +++ b/security/Member.php @@ -1173,7 +1173,7 @@ class Member extends DataObject { $dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat) . sprintf(' (%s)', _t('Member.DefaultDateTime', 'default')); $mainFields->push( - $dateFormatField = new Member_DatetimeOptionsetField( + $dateFormatField = new MemberDatetimeOptionsetField( 'DateFormat', $this->fieldLabel('DateFormat'), $dateFormatMap @@ -1189,7 +1189,7 @@ class Member extends DataObject { $timeFormatMap[$defaultTimeFormat] = Zend_Date::now()->toString($defaultTimeFormat) . sprintf(' (%s)', _t('Member.DefaultDateTime', 'default')); $mainFields->push( - $timeFormatField = new Member_DatetimeOptionsetField( + $timeFormatField = new MemberDatetimeOptionsetField( 'TimeFormat', $this->fieldLabel('TimeFormat'), $timeFormatMap @@ -1781,112 +1781,4 @@ class Member_Validator extends RequiredFields { return $js; } -} -/** - * @package sapphire - * @subpackage security - */ -class Member_DatetimeOptionsetField extends OptionsetField { - - function Field() { - Requirements::javascript(THIRDPARTY_DIR . '/thirdparty/jquery/jquery.js'); - Requirements::javascript(SAPPHIRE_DIR . '/javascript/MemberDatetimeOptionsetField.js'); - - $options = ''; - $odd = 0; - $source = $this->getSource(); - - foreach($source as $key => $value) { - // convert the ID to an HTML safe value (dots are not replaced, as they are valid in an ID attribute) - $itemID = $this->id() . '_' . preg_replace('/[^\.a-zA-Z0-9\-\_]/', '_', $key); - if($key == $this->value) { - $useValue = false; - $checked = " checked=\"checked\""; - } else { - $checked = ""; - } - - $odd = ($odd + 1) % 2; - $extraClass = $odd ? "odd" : "even"; - $extraClass .= " val" . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $key); - $disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? "disabled=\"disabled\"" : ""; - $ATT_key = Convert::raw2att($key); - - $options .= "
  • name\" type=\"radio\" value=\"$key\"$checked $disabled class=\"radio\" />
  • \n"; - } - - // Add "custom" input field - $value = ($this->value && !array_key_exists($this->value, $this->source)) ? $this->value : null; - $checked = ($value) ? " checked=\"checked\"" : ''; - $options .= "
  • " - . sprintf("", $itemID, $this->name, $checked) - . sprintf('', $itemID, _t('MemberDatetimeOptionsetField.Custom', 'Custom')) - . sprintf("\n", $this->name, $value) - . sprintf("", $this->Link() . '/validate'); - $options .= ($value) ? sprintf( - '(%s: "%s")', - _t('MemberDatetimeOptionsetField.Preview', 'Preview'), - Zend_Date::now()->toString($value) - ) : ''; - $options .= "" . _t('MemberDatetimeOptionsetField.TOGGLEHELP', 'Toggle formatting help') . ""; - $options .= "
    "; - $options .= $this->getFormattingHelpText(); - $options .= "
    "; - $options .= "
  • \n"; - - $id = $this->id(); - return "\n"; - } - - /** - * @todo Put this text into a template? - */ - function getFormattingHelpText() { - $output = ''; - return $output; - } - - function setValue($value) { - if($value == '__custom__') { - $value = isset($_REQUEST[$this->name . '_custom']) ? $_REQUEST[$this->name . '_custom'] : null; - } - if($value) { - parent::setValue($value); - } - } - - function validate() { - $value = isset($_POST[$this->name . '_custom']) ? $_POST[$this->name . '_custom'] : null; - if(!$value) return true; // no custom value, don't validate - - // Check that the current date with the date format is valid or not - $validator = $this->form ? $this->form->getValidator() : null; - require_once 'Zend/Date.php'; - $date = Zend_Date::now()->toString($value); - $valid = Zend_Date::isDate($date, $value); - if($valid) { - return true; - } else { - if($validator) { - $validator->validationError($this->name, _t('MemberDatetimeOptionsetField.DATEFORMATBAD',"Date format is invalid"), "validation", false); - } - return false; - } - } } \ No newline at end of file diff --git a/tests/forms/MemberDatetimeOptionsetFieldTest.php b/tests/forms/MemberDatetimeOptionsetFieldTest.php index 80f36c114..b2a381651 100644 --- a/tests/forms/MemberDatetimeOptionsetFieldTest.php +++ b/tests/forms/MemberDatetimeOptionsetFieldTest.php @@ -17,7 +17,7 @@ class MemberDatetimeOptionsetFieldTest extends SapphireTest { 'dd/MM/yyyy' => Zend_Date::now()->toString('dd/MM/yyyy'), ); $dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat) . ' (default)'; - $field = new Member_DatetimeOptionsetField( + $field = new MemberDatetimeOptionsetField( 'DateFormat', 'Date format', $dateFormatMap @@ -34,7 +34,7 @@ class MemberDatetimeOptionsetFieldTest extends SapphireTest { 'H:mm' => Zend_Date::now()->toString('H:mm'), ); $timeFormatMap[$defaultTimeFormat] = Zend_Date::now()->toString($defaultTimeFormat) . ' (default)'; - $field = new Member_DatetimeOptionsetField( + $field = new MemberDatetimeOptionsetField( 'TimeFormat', 'Time format', $timeFormatMap @@ -82,7 +82,7 @@ class MemberDatetimeOptionsetFieldTest extends SapphireTest { } function testDateFormValid() { - $field = new Member_DatetimeOptionsetField('DateFormat', 'DateFormat'); + $field = new MemberDatetimeOptionsetField('DateFormat', 'DateFormat'); $this->assertTrue($field->validate()); $_POST['DateFormat_custom'] = 'dd MM yyyy'; $this->assertTrue($field->validate());