diff options
author | mattpiwik <matthieu.aubry@gmail.com> | 2010-04-07 14:26:32 +0400 |
---|---|---|
committer | mattpiwik <matthieu.aubry@gmail.com> | 2010-04-07 14:26:32 +0400 |
commit | 900a5301896c9fbef4e4b54776929bcad96149ff (patch) | |
tree | c0a6e7280088800cb95fa2b1e0db6dc3a85dc8d6 /libs/HTML | |
parent | e2636eef27a37c85ccf1d01fd3d442ea24864343 (diff) |
Refs #56
* Adding timezone during piwik install. Will set default timezone, and use it for the first created website.
* Had to apply manual patch to QuickForm Select to handle optgroups, added unit tests to check patch is applied
* Skips the Database check screen if there was no error
git-svn-id: http://dev.piwik.org/svn/trunk@2058 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'libs/HTML')
-rw-r--r-- | libs/HTML/QuickForm/select.php | 127 |
1 files changed, 116 insertions, 11 deletions
diff --git a/libs/HTML/QuickForm/select.php b/libs/HTML/QuickForm/select.php index 1708fcadb6..801a718390 100644 --- a/libs/HTML/QuickForm/select.php +++ b/libs/HTML/QuickForm/select.php @@ -299,8 +299,27 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { * @access public * @return void */ - function addOption($text, $value, $attributes=null) - { + + // CLY - modification to allow optgroups + // function addOption($text, $value, $attributes=null) + function addOption($text, $value, $attributes=null, &$optGroup=null) { + // if text is an array, start an optgroup + if (is_array($text)) { + if (is_array($optGroup)) { + $optGroup[$value]['options'] = array(); + $optGroup =& $optGroup[$value]['options']; + } + else { + $this->_options[$value]['options'] = array(); + $optGroup =& $this->_options[$value]['options']; + }; + foreach($text as $key=>$val) { + $this->addOption($val, $key, null, $optGroup); + } + // done all the options in the optgroup + return; + } + // end mod if (null === $attributes) { $attributes = array('value' => (string)$value); } else { @@ -316,7 +335,33 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { } $this->_updateAttrArray($attributes, array('value' => (string)$value)); } - $this->_options[] = array('text' => $text, 'attr' => $attributes); + // CLY - modification to allow optgroups + // if $optGroup is an array, add the option to it + if (is_array($optGroup)) { + $optGroup[$text]['attr'] = $attributes; + } + // if $optGroup is a string, add the option to the option group + // used if directly adding an option to an optgroup + elseif (is_string($optGroup)) { + $optGroups = explode($optGroup, ','); + $target =& $this->_options; + foreach($optGroups as $group) { + // create the option group if it does not exist + if (empty($target[$group]['options'])) { + $target[$group]['options'] = array(); + } + $target =& $target[$group]['options']; + } + // foreach + // add the option + $target[$text]['attr'] = $attributes; + } + // else if there are attributes, add them to the option + elseif (is_array($attributes)) { + $this->_options[$text]['attr'] = $attributes; + } + // $this->_options[] = array('text' => $text, 'attr' => $attributes); + // end mod } // end func addOption // }}} @@ -493,7 +538,11 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { } $strHtml .= $tabs . '<select' . $attrString . ">\n"; - $strValues = is_array($this->_values)? array_map('strval', $this->_values): array(); + // CLY - modified to allow optgroups + foreach ($this->_options as $text=>$option) { + $strHtml .= $tabs . $this->_optionToHtml($text, $option); + } + /* $strValues = is_array($this->_values)? array_map('strval', $this->_values): array(); foreach ($this->_options as $option) { if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) { $option['attr']['selected'] = 'selected'; @@ -501,7 +550,8 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { $strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' . $option['text'] . "</option>\n"; } - + */ + // end mod return $strHtml . $tabs . '</select>'; } } //end func toHtml @@ -509,6 +559,40 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { // }}} // {{{ getFrozenHtml() +// CLY - new function to allow optgroups + /** + * Returns an OPTION in HTML + * + * This function is called recursively to support optgroups + * + * @param string $text Display text for the option + * @param array $option The option + * @since ?? + * @access private + * @return string + */ + // Creates the HTML for an option + function _optionToHtml($text, $option) + { + $tabs = $this->_getTabs(); + // if an option has options it's an optgroup + if (isset($option['options'])) { + $strHtml = $tabs . "<optgroup label=\"$text\">\n"; + foreach($option['options'] as $txt=>$opt) { + $strHtml .= $tabs . $this->_optionToHtml($txt, $opt); + } + $strHtml .= $tabs . "</optgroup>\n"; + return($strHtml); + } + // else it's an option + else { + if (is_array($this->_values) && in_array((string)$option['attr']['value'], $this->_values)) { + $this->_updateAttrArray($option['attr'], array('selected' => 'selected')); + } + return("\t<option" . $this->_getAttrString($option['attr']) . ">$text</option>\n"); + } + } + /** * Returns the value of field without HTML tags * @@ -568,12 +652,17 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { if (is_array($value) && !empty($this->_options)) { $cleanValue = null; foreach ($value as $v) { - for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) { - if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) { - $cleanValue[] = $v; - break; - } - } + // CLY - modified to allow optgroups + if ($this->_isInOptGroup($v, $this->_options)) { + $cleanValue[] = $v; + } +// for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) { +// if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) { +// $cleanValue[] = $v; +// break; +// } +// } + // end mod } } else { $cleanValue = $value; @@ -608,6 +697,22 @@ class HTML_QuickForm_select extends HTML_QuickForm_element { return parent::onQuickFormEvent($event, $arg, $caller); } } + + function _isInOptGroup($v, $opts) { + $isInOptGroup = false; + foreach ($opts as $opt) { + if (isset($opt['options'])) { + $isInOptGroup = $this->_isInOptGroup($v, $opt['options']); + } + else { + if ($v == $opt['attr']['value']) { + $isInOptGroup = true; + } + } + if ($isInOptGroup) break; + } + return $isInOptGroup; + } // }}} } //end class HTML_QuickForm_select |