123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * Checkbox element for use on forms.
- *
- * @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_CheckboxElement extends Staple_Form_Element
- {
- private $changed = false;
-
- private $checked = false;
- /**
- *
- * Override the default field value
- * @var unknown_type
- */
- protected $value = 1;
-
- public function setChecked($bool = true)
- {
- $bool = (bool)$bool;
- $this->checked = $bool;
- return $this;
- }
-
- /**
- * Returns a boolean whether the checkbox is checked or not.
- * @return boolean
- */
- public function isChecked()
- {
- if($this->checked === true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Sets the starting value for the checkbox
- * @param boolean $val
- * @return Staple_Form_CheckboxElement
- */
- public function setValue($val)
- {
- $this->setChecked($val);
- $this->changed = true;
- return $this;
- }
-
- /**
- * Returns the starting value for the checkbox
- * @return boolean
- */
- public function getValue()
- {
- return (bool)$this->isChecked();
- }
-
- /* (non-PHPdoc)
- * @see Staple_Form_Element::field()
- */
- public function field()
- {
- $checked = '';
- if($this->isChecked())
- {
- $checked = ' checked';
- }
- return ' <input type="checkbox" id="'.$this->escape($this->id).'" name="'.$this->escape($this->name).'" value="'.$this->escape($this->value).'"'.$checked.$this->getAttribString().'>';
- }
- /* (non-PHPdoc)
- * @see Staple_Form_Element::label()
- */
- public function label()
- {
- return ' <label for="'.$this->escape($this->id).'"'.$this->getClassString().'>'.$this->label.'</label>';
- }
- public function build()
- {
- $buf = '';
- $view = FORMS_ROOT.'/fields/CheckboxElement.phtml';
- if(file_exists($view))
- {
- ob_start();
- include $view;
- $buf = ob_get_contents();
- ob_end_clean();
- }
- else
- {
- $this->addClass('form_element');
- $this->addClass('element_checkbox');
- $classes = $this->getClassString();
- $buf .= "<div$classes id=\"".$this->escape($this->id)."_element\">\n";
- $buf .= $this->field();
- $buf .= $this->label();
- $buf .= '</div>';
- }
- return $buf;
- }
- }
|