← Back to team overview

anewt-developers team mailing list archive

[Branch ~sander-sinaasappel/anewt/anewt.new.cxs] Rev 1488: [form] Added ability to render a specific choice control form option

 

------------------------------------------------------------
revno: 1488
committer: Sander van Schouwenburg <sander@xxxxxxxxxxxxx>
branch nick: anewt.new.cxs
timestamp: Mon 2010-01-04 14:03:15 +0100
message:
  [form] Added ability to render a specific choice control form option
  
  This option can be rendered using AnewtFormControlChoice::build_option($option_name).
  This will only work when the options have unique names (otherwise the LAST
  option with the name will be rendered). This is useful for custom renderers
  where the checkboxes or radio buttons are spread out over the form.
  
  As a side effect, AnewtFormControlChoice::get_composite() now works properly
  for special cases.
  
  
  (from anewt.new.svn:97)
modified:
  form/controls/choice.lib.php


--
lp:~sander-sinaasappel/anewt/anewt.new.cxs
https://code.launchpad.net/~sander-sinaasappel/anewt/anewt.new.cxs

Your team Anewt developers is subscribed to branch lp:~sander-sinaasappel/anewt/anewt.new.cxs.
To unsubscribe from this branch go to https://code.launchpad.net/~sander-sinaasappel/anewt/anewt.new.cxs/+edit-subscription.
=== modified file 'form/controls/choice.lib.php'
--- form/controls/choice.lib.php	2010-01-04 13:01:54 +0000
+++ form/controls/choice.lib.php	2010-01-04 13:03:15 +0000
@@ -157,6 +157,16 @@
 	/**
 	 * \private
 	 *
+	 * Associative array of options by value.
+	 *
+	 * Will only contain instances of AnewtFormOption, but with multiple
+	 * options sharing the same name, newer ones will overwrite older ones.
+	 */
+	private $_options_by_value = array();
+
+	/**
+	 * \private
+	 *
 	 * Whether this control has at least one selected item. This flag is for
 	 * internal use only and is only valid when called directly after setting
 	 * values.
@@ -318,7 +328,23 @@
 	 */
 	function get_composite()
 	{
-		return count($this->_options) <= $this->_get('threshold');
+		$threshold = $this->_get('threshold');
+		$num_options = count($this->_options);
+
+		/* Decide how to render. The values 0 and -1 are special, and if there
+		 * are no options, always render a single element. */
+		if ($num_options === 0)
+			$render_many_elements = false;
+		elseif ($threshold === -1)
+			$render_many_elements = true;
+		elseif ($threshold === 0)
+			$render_many_elements = false;
+		elseif ($num_options <= $threshold)
+			$render_many_elements = true;
+		else
+			$render_many_elements = false;
+
+		return $render_many_elements;
 	}
 
 	/**
@@ -376,6 +402,10 @@
 			$this->at_least_one_selected = $option->_ensure_selection();
 
 		$this->_options[] = $option;
+
+		if ($option->_isset('value')) {
+			$this->_options_by_value[$option->_get('value')] = $option;
+		}
 	}
 
 	/**
@@ -464,28 +494,32 @@
 	 * \name Rendering methods
 	 */
 
+	function build_option($option_name) {
+		assert('array_has_key($this->_options_by_value, $option_name); // option '.$option_name.' must exist');
+		$option = $this->_options_by_value[$option_name];
+
+		if ($this->get('composite'))
+		{
+			if ($this->_get('multiple'))
+			{
+				return $option->_build_checkbox();
+			} else
+			{
+				return $option->_build_radiobutton();
+			}
+		} else
+		{
+			return $option->_build_option();
+		}
+	}
+
 	function build_widget()
 	{
 		$name = $this->get('name');
 		$id = $this->get('id');
 		$multiple = $this->_get('multiple');
-		$threshold = $this->_get('threshold');
-		$num_options = count($this->_options);
-
-		/* Decide how to render. The values 0 and -1 are special, and if there
-		 * are no options, always render a single element. */
-		if ($num_options === 0)
-			$render_many_elements = false;
-		elseif ($threshold === -1)
-			$render_many_elements = true;
-		elseif ($threshold === 0)
-			$render_many_elements = false;
-		elseif ($num_options <= $threshold)
-			$render_many_elements = true;
-		else
-			$render_many_elements = false;
-
-		if ($render_many_elements)
+
+		if ($this->get('composite'))
 		{
 			/* Set the id property on the first child (this is the radio button
 			 * or checkbox) of the first option. */