Permalink
Browse files

Add "preset" attribute to form fields and adapt list/checkboxes multi…

…ple default/preset values
  • Loading branch information...
1 parent f1d3723 commit c2faf119a9ebd7abf21b081e7a5e21f03afd1848 @chdemko committed Jun 24, 2012
@@ -165,6 +165,22 @@
*/
protected $value;
+ /**
+ * The default value of the form field.
+ *
+ * @var mixed
+ * @since 12.2
+ */
+ protected $default;
+
+ /**
+ * The preset value of the form field.
+ *
+ * @var mixed
+ * @since 12.2
+ */
+ protected $preset;
+
/**
* The label's CSS class of the form field
*
@@ -272,6 +288,12 @@ public function __get($name)
case 'title':
return $this->getTitle();
break;
+ case 'default':
+ return $this->getDefault();
+ break;
+ case 'preset':
+ return $this->getPreset();
+ break;
}
return null;
@@ -294,6 +316,87 @@ public function setForm(JForm $form)
return $this;
}
+ /**
+ * Method to get the preset value for a field (used for setup)
+ *
+ * @return mixed The preset value
+ *
+ * @since 12.2
+ */
+ protected function getPreset()
+ {
+ if (!isset($this->preset))
+ {
+ if (isset($this->element['preset']))
+ {
+ /*
+ * Get the value for the form field if not set.
+ * Default to the translated version of the 'preset' attribute
+ * if 'translate_preset' attribute if set to 'true' or '1'
+ * else the value of the 'preset' attribute for the field.
+ */
+ $preset = (string) $this->element['preset'];
+ if (($translate = $this->element['translate_preset']) && ((string) $translate == 'true' || (string) $translate == '1'))
+ {
+ $lang = JFactory::getLanguage();
+ if ($lang->hasKey($preset))
+ {
+ $debug = $lang->setDebug(false);
+ $preset = JText::_($preset);
+ $lang->setDebug($debug);
+ }
+ else
+ {
+ $preset = JText::_($preset);
+ }
+ }
+ }
+ else
+ {
+ $preset = self::getDefault();
+ }
+ $this->preset = $preset;
+ }
+ return $this->preset;
+ }
+
+ /**
+ * Method to get the default value for a field (used for filtering)
+ *
+ * @return mixed The default value
+ *
+ * @since 12.2
+ */
+ protected function getDefault()
+ {
+ if (!isset($this->default))
+ {
+ /*
+ * Get the value for the form field if not set.
+ * Default to the translated version of the 'default' attribute
+ * if 'translate_default' attribute if set to 'true' or '1'
+ * else the value of the 'default' attribute for the field.
+ */
+ $default = (string) $this->element['default'];
+ if (($translate = $this->element['translate_default']) && ((string) $translate == 'true' || (string) $translate == '1'))
+ {
+ $lang = JFactory::getLanguage();
+ if ($lang->hasKey($default))
+ {
+ $debug = $lang->setDebug(false);
+ $default = JText::_($default);
+ $lang->setDebug($debug);
+ }
+ else
+ {
+ $default = JText::_($default);
+ }
+ }
+ $this->default = $default;
+ }
+ return $this->default;
+ }
+
/**
* Method to attach a JForm object to the field.
*
@@ -378,7 +481,14 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
$this->id = $this->getId($id, $this->fieldname);
// Set the field default value.
- $this->value = $value;
+ if ($value === null)
+ {
+ $this->value = $this->getPreset();
+ }
+ else
+ {
+ $this->value = $value;
+ }
// Set the CSS class of field label
$this->labelClass = (string) $element['labelclass'];
@@ -51,7 +51,6 @@ protected function getInput()
// Initialize some field attributes.
$class = $this->element['class'] ? ' class="checkboxes ' . (string) $this->element['class'] . '"' : ' class="checkboxes"';
- $checkedOptions = explode(',', (string) $this->element['checked']);
// Start the checkbox field output.
$html[] = '<fieldset id="' . $this->id . '"' . $class . '>';
@@ -64,15 +63,7 @@ protected function getInput()
foreach ($options as $i => $option)
{
// Initialize some option attributes.
- if (!isset($this->value) || empty($this->value))
- {
- $checked = (in_array((string) $option->value, (array) $checkedOptions) ? ' checked="checked"' : '');
- }
- else
- {
- $value = !is_array($this->value) ? explode(',', $this->value) : $this->value;
- $checked = (in_array((string) $option->value, $value) ? ' checked="checked"' : '');
- }
+ $checked = (in_array((string) $option->value, (array) $this->value) ? ' checked="checked"' : '');
$class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
$disabled = !empty($option->disable) ? ' disabled="disabled"' : '';
@@ -135,4 +126,57 @@ protected function getOptions()
return $options;
}
+
+ /**
+ * Method to get the field default value.
+ *
+ * @return mixed The field default value.
+ *
+ * @since 12.2
+ */
+ protected function getDefault()
+ {
+ $default = parent::getDefault();
+ if ($default == '')
+ {
+ // Initialize variables.
+ $default = array();
+
+ foreach ($this->element->xpath('option[@default="true"]') as $option)
+ {
+ $default[] = (string) $option['value'];
+ }
+ }
+ return $default;
+ }
+
+ /**
+ * Method to get the field preset value.
+ *
+ * @return mixed The field default value.
+ *
+ * @since 12.2
+ */
+ protected function getPreset()
+ {
+ $preset = parent::getPreset();
+ if ($preset == '')
+ {
+ if (isset($this->element['checked']))
+ {
+ $preset = explode(',', $this->element['checked']);
+ }
+ else
+ {
+ // Initialize variables.
+ $preset = array();
+
+ foreach ($this->element->xpath('option[@checked="true"]') as $option)
+ {
+ $preset[] = (string) $option['value'];
+ }
+ }
+ }
+ return $preset;
+ }
}
@@ -178,4 +178,57 @@ protected function getInput()
return implode($html);
}
+
+ /**
+ * Method to get the field default value.
+ *
+ * @return mixed The field default value.
+ *
+ * @since 12.2
+ */
+ protected function getDefault()
+ {
+ $default = parent::getDefault();
+ if ($this->multiple && $default == '')
+ {
+ // Initialize variables.
+ $default = array();
+
+ foreach ($this->element->xpath('//option[@default="true"]') as $option)
+ {
+ $default[] = (string) $option['value'];
+ }
+ }
+ return $default;
+ }
+
+ /**
+ * Method to get the field preset value.
+ *
+ * @return mixed The field default value.
+ *
+ * @since 12.2
+ */
+ protected function getPreset()
+ {
+ $preset = parent::getPreset();
+ if ($this->multiple && $preset == '')
+ {
+ if (isset($this->element['selected']))
+ {
+ $preset = explode(',', $this->element['selected']);
+ }
+ else
+ {
+ // Initialize variables.
+ $preset = array();
+
+ foreach ($this->element->xpath('//option[@selected="true"]') as $option)
+ {
+ $preset[] = (string) $option['value'];
+ }
+ }
+ }
+ return $preset;
+ }
}
@@ -59,16 +59,22 @@ protected function getInput()
// Get the field options.
$options = (array) $this->getOptions();
+ // Get the value
+ $values = (array) $this->value;
+
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
- $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
- $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
+ $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
+ foreach ($values as $value)
+ {
+ $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $value . '"/>';
+ }
}
// Create a regular list.
else
{
- $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
+ $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $values, $this->id);
}
return implode($html);
@@ -116,4 +122,57 @@ protected function getOptions()
return $options;
}
+
+ /**
+ * Method to get the field default value.
+ *
+ * @return mixed The field default value.
+ *
+ * @since 12.2
+ */
+ protected function getDefault()
+ {
+ $default = parent::getDefault();
+ if ($this->multiple && $default == '')
+ {
+ // Initialize variables.
+ $default = array();
+
+ foreach ($this->element->xpath('option[@default="true"]') as $option)
+ {
+ $default[] = (string) $option['value'];
+ }
+ }
+ return $default;
+ }
+
+ /**
+ * Method to get the field preset value.
+ *
+ * @return mixed The field default value.
+ *
+ * @since 12.2
+ */
+ protected function getPreset()
+ {
+ $preset = parent::getPreset();
+ if ($this->multiple && $preset == '')
+ {
+ if (isset($this->element['selected']))
+ {
+ $preset = explode(',', $this->element['selected']);
+ }
+ else
+ {
+ // Initialize variables.
+ $preset = array();
+
+ foreach ($this->element->xpath('option[@selected="true"]') as $option)
+ {
+ $preset[] = (string) $option['value'];
+ }
+ }
+ }
+ return $preset;
+ }
}
Oops, something went wrong.

0 comments on commit c2faf11

Please sign in to comment.