123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- /**
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- */
- 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 = '<div class="form_checkboxes">';
- foreach ($this->boxes as $box)
- {
- $buff .= $box->build();
- }
- $buff .= '</div>';
- return $buff;
- }
- public function errors()
- {
- $errors = implode(" ",$this->getErrors());
- return $errors;
- }
- /**
- *
- * @see Staple_Form_Element::label()
- */
- public function label()
- {
- return "<label".$this->getClassString().">".$this->escape($this->getLabel())."</label>";
- }
- /**
- *
- * @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 .= "<div class=\"row\">\n"; //Row Start
- $buf .= "<div class=\"small-12 columns\">\n"; //Label Start
- $buf .= $this->label();
- $buf .= "</div>\n"; //Label End
- $buf .= "<div class=\"small-12 columns\">\n"; //Field Start
- if(count($this->errors) != 0)
- {
- $buf .= "<label class=\"error\">";
- }
- $buf .= $this->field();
- if(count($this->errors) != 0)
- {
- $buf .= "</label>";
- $buf .= "<small class=\"error\">";
- foreach($this->errors as $error)
- {
- foreach($error as $message)
- {
- $buf .= "- $message<br>\n";
- }
- }
- $buf .= "</small>";
- }
- $buf .= "</div>\n"; //Field End
- $buf .= "</div>\n"; //Row end
- return $buf;
- }
- }
- }
- ?>
|