. */ class Staple_Form_FoundationCheckboxGroup extends Staple_Form_Element { /** * An array that holds the Checkbox elements. * @var array[Staple_Form_CheckboxElement] */ protected $boxes = array(); public function addCheckbox(Staple_Form_FoundationCheckboxElement $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_FoundationCheckboxElement) { $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_FoundationCheckboxElement $a, Staple_Form_FoundationCheckboxElement $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_FoundationCheckboxElement $a, Staple_Form_FoundationCheckboxElement $b) { return strcmp($a->getLabel(), $b->getLabel()); } //-------------------------------------------------BUILDERS------------------------------------------------- /** * * @see Staple_Form_Element::field() */ public function field() { $buff = '
'; foreach ($this->boxes as $box) { $buff .= $box->build(); } $buff .= '
'; return $buff; } public function errors() { $errors = implode(" ",$this->getErrors()); return $errors; } /** * * @see Staple_Form_Element::label() */ public function label() { return "getClassString().">".$this->escape($this->getLabel()).""; } /** * * @see Staple_Form_Element::build() */ public function build() { $buf = ''; $view = FORMS_ROOT.'/fields/CheckboxGroup.phtml'; if(file_exists($view)) { ob_start(); include $view; $buf = ob_get_contents(); ob_end_clean(); } else { $buf .= "
\n"; //Row Start $buf .= "
\n"; //Label Start $buf .= $this->label(); $buf .= "
\n"; //Label End $buf .= "
\n"; //Field Start if(count($this->errors) != 0) { $buf .= ""; $buf .= ""; foreach($this->errors as $error) { foreach($error as $message) { $buf .= "- $message
\n"; } } $buf .= "
"; } $buf .= "
\n"; //Field End $buf .= "
\n"; //Row end return $buf; } } } ?>