PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tanora.org/www/framework/system/form.php

https://bitbucket.org/ekkl/tanora
PHP | 195 lines | 133 code | 20 blank | 42 comment | 26 complexity | 4353d7f35eea3b89a773a17c6c6a3b05 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * File: framework/system/form.php
  4. *
  5. * Defines a class used by ORMs to generate HTML forms.
  6. */
  7. class Form {
  8. private $model;
  9. private $action;
  10. private $submit_value = 'Submit';
  11. private $attributes = array();
  12. private $inputs = array();
  13. /**
  14. * Accepts a reference to an ORM model to get the form fields from.
  15. */
  16. function __construct(&$orm = NULL) {
  17. if(!is_null($orm)) {
  18. $this->model = $orm;
  19. }
  20. }
  21. /**
  22. * Validates form. Returns TRUE on success and FALSE on failure, or
  23. * NULL if there was no POST data.
  24. */
  25. public function validate() {
  26. if(Framework::post()) {
  27. $valid = TRUE;
  28. if(Framework::config('logging', 'form') != 'none') {
  29. Framework::log('--- Begin Form Input ---',2);
  30. }
  31. foreach($this->get_inputs() as $input) {
  32. $input->set_input(Framework::post($input->get_name()));
  33. if($input->filter() === FALSE) {
  34. // do some logging
  35. if(Framework::config('logging', 'form') == 'all' || Framework::config('logging', 'form') == 'invalid') {
  36. Framework::log('Field: '.$input->get_name(), 2);
  37. Framework::log('Raw Input:'.print_r($input->get_input(), true), 3);
  38. Framework::log('Error: '.$input->get_error(), 3);
  39. }
  40. if(Framework::config('debugging', 'form')) {
  41. Framework::record(
  42. array(
  43. 'field'=>$input->get_name(),
  44. 'raw input'=>$input->get_input(),
  45. 'error'=>$input->get_error()
  46. )
  47. );
  48. }
  49. // the filter returned false, so the input is not valid
  50. $valid = FALSE;
  51. } else {
  52. // do some more logging
  53. if(Framework::config('logging','form') == 'all' || Framework::config('logging', 'form') == 'valid') {
  54. Framework::log('Field: '.$input->get_name(), 2);
  55. Framework::log('Raw Input: '.print_r($input->get_input(), true), 3);
  56. Framework::log('Filtered Input: '.print_r($input->get_value(), true), 3);
  57. }
  58. if(Framework::config('debugging', 'form')) {
  59. Framework::record(
  60. array(
  61. 'field'=>$input->get_name(),
  62. 'raw input'=>$input->get_input(),
  63. 'filtered input'=>$input->get_value()
  64. )
  65. );
  66. }
  67. }
  68. }
  69. if(Framework::config('logging', 'form') != 'none') {
  70. Framework::log('--- End Form Input ---',2);
  71. }
  72. return $valid;
  73. }
  74. }
  75. /**
  76. * Returns the input values as an object.
  77. */
  78. public function data() {
  79. $obj = new stdClass();
  80. foreach($this->get_inputs() as $input) {
  81. $name = $input->get_name();
  82. $value = $input->get_value();
  83. if(!empty($name)) {
  84. $obj->$name = $value;
  85. }
  86. }
  87. return $obj;
  88. }
  89. /**
  90. * Generates the HTML form.
  91. */
  92. public function __toString() {
  93. $string = '<form method="post" action="'.$this->action.'"';
  94. if(!empty($this->attributes)) {
  95. foreach($this->attributes as $name=>$value) {
  96. $string .= $name.'="'.$value.'" ';
  97. }
  98. $string = rtrim($string);
  99. }
  100. $string .= '>';
  101. foreach($this->get_inputs() as $input) {
  102. $string .= $input;
  103. }
  104. $string .= '<input type="submit" value="'.$this->submit_value.'" />';
  105. $string .= '</form>';
  106. return $string;
  107. }
  108. /**
  109. * Sets the action member variable.
  110. */
  111. public function action($value) {
  112. $this->action = $value;
  113. }
  114. /**
  115. * Sets the submit_value member variable.
  116. */
  117. public function submit_value($value) {
  118. $this->submit_value = $value;
  119. }
  120. /**
  121. * Appends an attribute to the attributes member variable.
  122. */
  123. public function attribute($name, $value = NULL) {
  124. if(is_array($name)) {
  125. $this->attributes = array_merge($this->attributes, $name);
  126. } elseif(!is_null($value)) {
  127. $this->attributes[$name] = $value;
  128. }
  129. }
  130. /**
  131. * Returns the list of inputs.
  132. */
  133. public function get_inputs() {
  134. /* Populate fields list from the model when empty. */
  135. if(empty($this->inputs) && isset($this->model)) {
  136. foreach($this->model->get_fields() as $field) {
  137. $this->inputs[] = &$field->get_input();
  138. }
  139. }
  140. return $this->inputs;
  141. }
  142. /**
  143. * Appends a field's input from the model member variable and
  144. * returns the reference.
  145. */
  146. public function use_input($name) {
  147. foreach($this->model->get_fields() as $field) {
  148. if($field->get_name() == $name) {
  149. $index = count($this->inputs);
  150. $this->inputs[$index] = $field->get_input();
  151. return $this->inputs[$index];
  152. }
  153. }
  154. Framework::error('Input '.$name.' does not exist.');
  155. }
  156. /**
  157. * Appends a new field input to the model member variable and
  158. * returns the reference.
  159. */
  160. public function new_input($type) {
  161. require_once(FRAMEWORK_PATH.'system/input.php');
  162. $dirs = array(INPUT_PATH, FRAMEWORK_PATH.'inputs/');
  163. foreach($dirs as $dir) {
  164. $file = $dir.strtolower($type).'input.php';
  165. if(file_exists($file)) {
  166. require_once($file);
  167. $class = $type.'Input';
  168. $index = count($this->inputs);
  169. $this->inputs[$index] = new $class($this);
  170. return $this->inputs[$index];
  171. }
  172. }
  173. }
  174. }
  175. ?>