FoundationCheckboxElement.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Checkbox element for use on forms.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Form_FoundationCheckboxElement extends Staple_Form_Element
  24. {
  25. private $changed = false;
  26. private $checked = false;
  27. /**
  28. *
  29. * Override the default field value
  30. * @var unknown_type
  31. */
  32. protected $value = 1;
  33. public function setChecked($bool = true)
  34. {
  35. $bool = (bool)$bool;
  36. $this->checked = $bool;
  37. return $this;
  38. }
  39. /**
  40. * Returns a boolean whether the checkbox is checked or not.
  41. * @return boolean
  42. */
  43. public function isChecked()
  44. {
  45. if($this->checked === true)
  46. {
  47. return true;
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. }
  54. /**
  55. * Sets the starting value for the checkbox
  56. * @param boolean $val
  57. * @return Staple_Form_CheckboxElement
  58. */
  59. public function setValue($val)
  60. {
  61. $this->setChecked($val);
  62. $this->changed = true;
  63. return $this;
  64. }
  65. /**
  66. * Returns the starting value for the checkbox
  67. * @return boolean
  68. */
  69. public function getValue()
  70. {
  71. return (bool)$this->isChecked();
  72. }
  73. /* (non-PHPdoc)
  74. * @see Staple_Form_Element::field()
  75. */
  76. public function field()
  77. {
  78. $checked = '';
  79. if($this->isChecked())
  80. {
  81. $checked = ' checked';
  82. }
  83. return ' <input type="checkbox" id="'.$this->escape($this->id).'" name="'.$this->escape($this->name).'" value="'.$this->escape($this->value).'"'.$checked.$this->getAttribString().'>';
  84. }
  85. /* (non-PHPdoc)
  86. * @see Staple_Form_Element::label()
  87. */
  88. public function label()
  89. {
  90. return ' <label for="'.$this->escape($this->id).'"'.$this->getClassString().'>'.$this->label.'</label>';
  91. }
  92. public function build()
  93. {
  94. $buf = '';
  95. $view = FORMS_ROOT.'/fields/CheckboxElement.phtml';
  96. if(file_exists($view))
  97. {
  98. ob_start();
  99. include $view;
  100. $buf = ob_get_contents();
  101. ob_end_clean();
  102. }
  103. else
  104. {
  105. $buf .= "<div class=\"row\">\n"; //Row Start
  106. $buf .= "<div class=\"small-12 columns\">\n"; //Label Start
  107. $buf .= $this->field();
  108. $buf .= $this->label();
  109. $buf .= "</div>\n"; //Field End
  110. $buf .= "</div>\n"; //Row end
  111. }
  112. return $buf;
  113. }
  114. }