Commit 323d9e4d authored by Gregor Anzelj's avatar Gregor Anzelj Committed by Gerrit Code Review
Browse files

Alter Pieform elements for page skin design. (Bug #1168213)



- Add the ability to set CSS syles for dropdown-menu items
- Add the ability to put multiple radio buttons on the same line

Change-Id: Iaa0fb0bc2cce85266552f4297d8d494c3ddd6e36
Signed-off-by: default avatarGregor Anzelj <gregor.anzelj@gmail.com>
Signed-off-by: Aaron Wells's avatarAaron Wells <aaronw@catalyst.net.nz>
parent bd68dfa0
......@@ -27,15 +27,21 @@
/**
* Renders a set of radio buttons for a form
*
* @param array $element The element to render
* @param Pieform $form The form to render the element for
* @param Pieform $form The form to render the element for
* @param array $element The element to render. In addition to the standard Pieform
* element attributes, it can also take the following optional
* attributes:
* - separator: The HTML string that should separate the radio
* buttons (defaults to \n, always has \n appended to it)
* - rowsize: How many radio buttons to print per row (defaults to 1)
* - nolabels: Don't print the labels next to the individual radio buttons.
* @return string The HTML for the element
*/
function pieform_element_radio(Pieform $form, $element) {/*{{{*/
function pieform_element_radio(Pieform $form, $element) {
if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) {
throw new PieformException('Radio elements should have at least one option');
}
$result = '';
$form_value = $form->get_value($element);
$id = $element['id'];
......@@ -45,6 +51,10 @@ function pieform_element_radio(Pieform $form, $element) {/*{{{*/
$separator = $element['separator'] . $separator;
}
$rowsize = isset($element['rowsize']) ? (int) $element['rowsize'] : 1;
$nolabels = isset($element['nolabels']) ? $element['nolabels'] : false;
$i = 0;
foreach ($element['options'] as $value => $data) {
$uid = $id . substr(md5(microtime()), 0, 4);
$element['id'] = $uid;
......@@ -60,16 +70,25 @@ function pieform_element_radio(Pieform $form, $element) {/*{{{*/
. $form->element_attributes($element)
. ' value="' . Pieform::hsc($value) . '"'
. (($form_value == $value) ? ' checked="checked"' : '')
. '> <label for="' . $form->get_name() . '_' . $uid . '">' . Pieform::hsc($text) . "</label>"
. ($description != '' ? '<div class="description">' . $description . '</div>' : '')
. $separator;
. '>';
if (!$nolabels) {
$result .= ' <label for="' . $form->get_name() . '_' . $uid . '">' . Pieform::hsc($text) . "</label>"
. ($description != '' ? '<div class="description">' . $description . '</div>' : '');
}
$i++;
if ($rowsize <= 1 || $i % $rowsize == 0) {
$result .= $separator;
}
}
$result = substr($result, 0, -strlen($separator));
// If there was a separator printed on the end, then remove it
if ($rowsize <= 1 || $i % $rowsize == 0) {
$result = substr($result, 0, -strlen($separator));
}
return $result;
}/*}}}*/
}
function pieform_element_radio_set_attributes($element) {/*{{{*/
function pieform_element_radio_set_attributes($element) {
$element['rules']['validateoptions'] = true;
return $element;
}/*}}}*/
}
......@@ -35,7 +35,7 @@
* @param array $element The element to render
* @return string The HTML for the element
*/
function pieform_element_select(Pieform $form, $element) {/*{{{*/
function pieform_element_select(Pieform $form, $element) {
if (!empty($element['multiple'])) {
$element['name'] .= '[]';
}
......@@ -64,13 +64,15 @@ function pieform_element_select(Pieform $form, $element) {/*{{{*/
. $form->element_attributes($element)
. (!empty($element['multiple']) ? ' multiple="multiple"' : '')
. (!empty($element['allowother']) ? ' onChange="pieform_select_other(this);"' : '')
. (!empty($element['width']) ? ' style="width: ' . $element['width'] . 'px;' : 'style="')
. (!empty($element['height']) ? ' height: ' . $element['height'] . 'px;"' : '"')
. ">\n";
if (!$optionsavailable) {
$result .= "\t<option></option>\n</select>";
return $result;
}
$values = $form->get_value($element);
$values = $form->get_value($element);
$optionselected = false;
if (!empty($element['allowother'])) {
$use_other = $values;
......@@ -120,9 +122,9 @@ function pieform_element_select(Pieform $form, $element) {/*{{{*/
. ">\n";
}
return $result;
}/*}}}*/
}
function pieform_element_select_render_options($options, $values, &$optionselected, $element) {/*{{{*/
function pieform_element_select_render_options($options, $values, &$optionselected, $element) {
$result = '';
foreach ($options as $key => $value) {
......@@ -131,7 +133,7 @@ function pieform_element_select_render_options($options, $values, &$optionselect
if (
(!is_array($values) && $key == $values)
||
(is_array($values) &&
(is_array($values) &&
(in_array($key, $values)
|| (isset($values[0]) && $values[0] === null && !$optionselected)))) {
$selected = ' selected="selected"';
......@@ -158,6 +160,14 @@ function pieform_element_select_render_options($options, $values, &$optionselect
$label = '';
}
// Add a CSS style for option
if (is_array($value) && isset($value['style'])) {
$style = ' style="' . Pieform::hsc($value['style']) . '"';
}
else {
$style = '';
}
// Get the value to display/put in the value attribute
if (is_array($value)) {
if (!isset($value['value'])) {
......@@ -169,13 +179,13 @@ function pieform_element_select_render_options($options, $values, &$optionselect
}
}
$result .= "\t<option value=\"" . Pieform::hsc($key) . "\"{$selected}{$label}{$disabled}>" . Pieform::hsc($value) . "</option>\n";
$result .= "\t<option value=\"" . Pieform::hsc($key) . "\"{$selected}{$label}{$disabled}{$style}>" . Pieform::hsc($value) . "</option>\n";
}
return $result;
}/*}}}*/
}
function pieform_element_select_set_attributes($element) {/*{{{*/
function pieform_element_select_set_attributes($element) {
if (!isset($element['collapseifoneoption'])) {
$element['collapseifoneoption'] = true;
}
......@@ -183,9 +193,9 @@ function pieform_element_select_set_attributes($element) {/*{{{*/
$element['rules']['validateoptions'] = true;
}
return $element;
}/*}}}*/
}
function pieform_element_select_get_value(Pieform $form, $element) {/*{{{*/
function pieform_element_select_get_value(Pieform $form, $element) {
if (empty($element['multiple'])) {
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if (isset($element['value'])) {
......@@ -232,9 +242,9 @@ function pieform_element_select_get_value(Pieform $form, $element) {/*{{{*/
}
return $values;
}/*}}}*/
}
function pieform_element_select_get_options($element) {/*{{{*/
function pieform_element_select_get_options($element) {
if (!isset($element['options']) && !isset($element['optgroups'])) {
return false;
}
......@@ -250,9 +260,9 @@ function pieform_element_select_get_options($element) {/*{{{*/
}
return $options;
}/*}}}*/
}
function pieform_element_select_get_inlinejs() {/*{{{*/
function pieform_element_select_get_inlinejs() {
$result = 'function pieform_select_other(el) {//{{{' . "\n";
$result .= ' var $el = $(el),' . "\n";
$result .= ' $$other = jQuery(\'#\' + $el.id + \'_other\');' . "\n";
......@@ -264,10 +274,11 @@ function pieform_element_select_get_inlinejs() {/*{{{*/
$result .= ' }' . "\n";
$result .= '}//}}}' . "\n";
return $result;
}/*}}}*/
function pieform_element_select_get_headdata() {/*{{{*/
}
function pieform_element_select_get_headdata() {
$result = '<script type="text/javascript">' . "\n";
$result .= pieform_element_select_get_inlinejs();
$result .= '</script>' . "\n";
return array($result);
}/*}}}*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment