TextElement.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Text 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_TextElement extends Staple_Form_Element
  24. {
  25. /**
  26. * Size of the text field.
  27. * @var int
  28. */
  29. protected $size;
  30. /**
  31. * Maxlength of the textfield.
  32. * @var int
  33. */
  34. protected $max;
  35. /**
  36. * @return the $size
  37. */
  38. public function getSize()
  39. {
  40. return $this->size;
  41. }
  42. /**
  43. * @return the $max
  44. */
  45. public function getMax()
  46. {
  47. return $this->max;
  48. }
  49. /**
  50. * @param int $size
  51. */
  52. public function setSize($size)
  53. {
  54. $this->size = (int)$size;
  55. return $this;
  56. }
  57. /**
  58. * @param int $max
  59. */
  60. public function setMax($max)
  61. {
  62. $this->max = (int)$max;
  63. return $this;
  64. }
  65. /**
  66. * Build the field label.
  67. * @see Staple_Form_Element::label()
  68. * @return string
  69. */
  70. public function label()
  71. {
  72. return ' <label for="'.$this->escape($this->id).'"'.$this->getClassString().'>'.$this->label."</label>\n";
  73. }
  74. /**
  75. * Build the field itself.
  76. * @see Staple_Form_Element::field()
  77. * @return string
  78. */
  79. public function field()
  80. {
  81. $size = '';
  82. $max = '';
  83. if(isset($this->size))
  84. {
  85. $size = ' size="'.((int)$this->size).'"';
  86. }
  87. if(isset($this->max))
  88. {
  89. $max = ' maxlength="'.((int)$this->max).'"';
  90. }
  91. return ' <input type="text" id="'.$this->escape($this->id).'" name="'.$this->escape($this->name).'" value="'.$this->escape($this->value).'"'.$size.$max.$this->getAttribString().'>'."\n";
  92. }
  93. /**
  94. * Build the form field.
  95. * @see Staple_Form_Element::build()
  96. * @return string
  97. */
  98. public function build()
  99. {
  100. $buf = '';
  101. $view = FORMS_ROOT.'/fields/TextElement.phtml';
  102. if(file_exists($view))
  103. {
  104. ob_start();
  105. include $view;
  106. $buf = ob_get_contents();
  107. ob_end_clean();
  108. }
  109. else
  110. {
  111. $this->addClass('form_element');
  112. $this->addClass('element_text');
  113. $classes = $this->getClassString();
  114. $buf .= "<div$classes id=\"".$this->escape($this->id)."_element\">\n";
  115. $buf .= $this->label();
  116. $buf .= $this->field();
  117. $buf .= $this->instructions();
  118. $buf .= "</div>\n";
  119. }
  120. return $buf;
  121. }
  122. }