PasswordElement.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Password element for use on forms. This class differs from other elements by not allowing the value to be
  4. * stored in the session. It also will not output the value of the field to the html source in it's standard
  5. * build function.
  6. *
  7. * @author Ironpilot
  8. * @copyright Copywrite (c) 2011, STAPLE CODE
  9. *
  10. * This file is part of the STAPLE Framework.
  11. *
  12. * The STAPLE Framework is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as published by the
  14. * Free Software Foundation, either version 3 of the License, or (at your option)
  15. * any later version.
  16. *
  17. * The STAPLE Framework is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  20. * more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. class Staple_Form_PasswordElement extends Staple_Form_Element
  26. {
  27. private $releaseValue = false;
  28. /**
  29. * Size of the text field.
  30. * @var int
  31. */
  32. protected $size;
  33. /**
  34. * Maxlength of the textfield.
  35. * @var int
  36. */
  37. protected $max;
  38. public function releaseValue($bool = true)
  39. {
  40. $this->releaseValue = (bool)$bool;
  41. return $this;
  42. }
  43. /**
  44. * @return the $size
  45. */
  46. public function getSize()
  47. {
  48. return $this->size;
  49. }
  50. /**
  51. * @return the $max
  52. */
  53. public function getMax()
  54. {
  55. return $this->max;
  56. }
  57. /**
  58. * @param int $size
  59. */
  60. public function setSize($size)
  61. {
  62. $this->size = (int)$size;
  63. return $this;
  64. }
  65. /**
  66. * @param int $max
  67. */
  68. public function setMax($max)
  69. {
  70. $this->max = (int)$max;
  71. return $this;
  72. }
  73. public function __sleep()
  74. {
  75. return array('name','label','classes','id','instructions','attrib','required','readOnly','validators','errors','size','max');
  76. }
  77. /**
  78. * Build the field label.
  79. * @see Staple_Form_Element::label()
  80. * @return string
  81. */
  82. public function label()
  83. {
  84. return ' <label for="'.$this->escape($this->id).'"'.$this->getClassString().'>'.$this->label."</label>\n";
  85. }
  86. /**
  87. * Build the field itself.
  88. * @see Staple_Form_Element::field()
  89. * @return string
  90. */
  91. public function field()
  92. {
  93. $size = '';
  94. $max = '';
  95. if(isset($this->size))
  96. {
  97. $size = ' size="'.$this->size.'"';
  98. }
  99. if(isset($this->max))
  100. {
  101. $max = ' maxlength="'.$this->max.'"';
  102. }
  103. $value = '';
  104. if($this->releaseValue === true)
  105. {
  106. $value = $this->value;
  107. }
  108. return ' <input type="password" id="'.$this->escape($this->id).'" name="'.$this->escape($this->name).'" value="'.$this->escape($value).'"'.$size.$max.$this->getAttribString().'>'."\n";
  109. }
  110. /**
  111. * Build the form field.
  112. * @see Staple_Form_Element::build()
  113. * @return string
  114. */
  115. public function build()
  116. {
  117. $buf = '';
  118. $view = FORMS_ROOT.'/fields/TextElement.phtml';
  119. if(file_exists($view))
  120. {
  121. ob_start();
  122. include $view;
  123. $buf = ob_get_contents();
  124. ob_end_clean();
  125. }
  126. else
  127. {
  128. $this->addClass('form_element');
  129. $this->addClass('element_password');
  130. $classes = $this->getClassString();
  131. $buf .= "<div$classes id=\"".$this->escape($this->id)."_element\">\n";
  132. $buf .= $this->label();
  133. $buf .= $this->field();
  134. $buf .= $this->instructions();
  135. $buf .= "</div>\n";
  136. }
  137. return $buf;
  138. }
  139. }