. */ class Staple_Form_CheckboxGroup extends Staple_Form_Element { /** * An array that holds the Checkbox elements. * @var array[Staple_Form_CheckboxElement] */ protected $boxes = array(); public function addCheckbox(Staple_Form_CheckboxElement $box) { $this->boxes[] = $box; return $this; } /** * Adds multiple checkboxes to the array of checkboxes; * @param array $boxes */ public function addCheckboxArray(array $boxes) { foreach($boxes as $box) { if($box instanceof Staple_Form_CheckboxElement) { $this->addCheckbox($box); } } return $this; } /** * Return all the checkbox objects * @return array[Staple_Form_CheckboxElement] */ public function getBoxes() { return $this->boxes; } /** * Sorts the boxes by Label */ public function sortBoxesByLabel() { usort($this->boxes, array($this, 'sortCmpLabel')); return $this; } /** * Sorts the boxes by Name */ public function sortBoxesByName() { usort($this->boxes, array($this, 'sortCmpName')); return $this; } /** * This simple function resets the $boxes array. */ public function clearBoxes() { $this->boxes = array(); return $this; } /** * Returns an associative array of * @return array */ public function getValue() { $values = array(); foreach($this->boxes as $key=>$value) { $values[$value->getName()] = $value->getValue(); } return $values; } /** * This function requires an associative array composed of the form field names, followed by their values. * @return Staple_Form_CheckboxGroup */ public function setValue(array $inserts) { foreach($this->boxes as $key=>$value) { if(array_key_exists($value->getName(), $inserts)) { $this->boxes[$key]->setValue($inserts[$value->getName()]); } } return $this; } /** * Comparison function for sorting by names * @param Staple_Form_CheckboxElement $a * @param Staple_Form_CheckboxElement $b */ private function sortCmpName(Staple_Form_CheckboxElement $a, Staple_Form_CheckboxElement $b) { return strcmp($a->getName(), $b->getName()); } /** * Comparison function for sorting by labels * @param Staple_Form_CheckboxElement $a * @param Staple_Form_CheckboxElement $b */ private function sortCmpLabel(Staple_Form_CheckboxElement $a, Staple_Form_CheckboxElement $b) { return strcmp($a->getLabel(), $b->getLabel()); } //-------------------------------------------------BUILDERS------------------------------------------------- /** * * @see Staple_Form_Element::field() */ public function field() { $buff = '