PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ekkl/tanora
PHP | 244 lines | 138 code | 26 blank | 80 comment | 9 complexity | 4aaf0c872b2be1d761d915dc11827a47 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * File: framework/system/input.php
  4. *
  5. * Defines the base class for Input objects used in Field objects.
  6. */
  7. abstract class Input {
  8. protected $name;
  9. protected $label;
  10. protected $description;
  11. protected $wrapper;
  12. protected $input;
  13. protected $value;
  14. protected $filters = array();
  15. protected $html;
  16. protected $error;
  17. protected $attributes = array();
  18. protected $options = array();
  19. /**
  20. * Appends a filter to the filters member variable and returns the
  21. * reference.
  22. */
  23. public function add_filter($type) {
  24. // array of files in order of priority they should be checked
  25. $paths = array(DOCUMENT_ROOT.'filters/', FRAMEWORK_PATH.'filters/');
  26. require_once(FRAMEWORK_PATH.'system/filter.php');
  27. foreach($paths as $dir) {
  28. $file = $dir.strtolower($type).'filter.php';
  29. if(file_exists($file)) {
  30. require_once($file);
  31. $class = $type.'Filter';
  32. $this->filters[$type] = new $class();
  33. return $this->filters[$type];
  34. }
  35. }
  36. Framework:: error('Cannot load filter: '.$type);
  37. }
  38. /**
  39. * Prepends a filter to the filters member variable and returns the
  40. * reference.
  41. */
  42. public function push_filter($type) {
  43. // array of files in order of priority they should be checked
  44. $paths = array(DOCUMENT_ROOT.'filters/', FRAMEWORK_PATH.'filters/');
  45. require_once(FRAMEWORK_PATH.'system/filter.php');
  46. foreach($paths as $dir) {
  47. $file = $dir.strtolower($type).'filter.php';
  48. if(file_exists($file)) {
  49. require_once($file);
  50. $class = $type.'Filter';
  51. $tmp = array();
  52. $tmp[$type] = new $class();
  53. $this->filters = array_merge($tmp, $this->filters);
  54. return $this->filters[$type];
  55. }
  56. }
  57. Framework:: error('Cannot load filter: '.$type);
  58. }
  59. /**
  60. * Runs the filters on the input member variable and sets the
  61. * cleaned input as the value member variable.
  62. */
  63. public function filter() {
  64. // create a copy of the input member variable
  65. $input = $this->get_input();
  66. foreach($this->filters as $filter) {
  67. if($filter->run($input) === FALSE) {
  68. $this->set_error($filter->get_error_message());
  69. return FALSE;
  70. }
  71. }
  72. // sets the value member variable to the cleaned input
  73. $this->value = $input;
  74. return TRUE;
  75. }
  76. /**
  77. * Gets the name member variable
  78. */
  79. public function get_name() {
  80. return $this->name;
  81. }
  82. /**
  83. * Sets the name member variable.
  84. */
  85. public function set_name($value) {
  86. $this->name = $value;
  87. }
  88. /**
  89. * Sets the label member variable.
  90. */
  91. public function set_label($value) {
  92. $this->label = $value;
  93. }
  94. /**
  95. * Sets the error message.
  96. */
  97. public function set_error($value) {
  98. $this->error = $value;
  99. }
  100. /**
  101. * Gets the label member variable.
  102. */
  103. public function get_label() {
  104. if(is_null($this->label)) {
  105. return ucfirst(str_replace('_',' ',$this->get_name()));
  106. }
  107. return $this->label;
  108. }
  109. /**
  110. * Sets the description member variable.
  111. */
  112. public function set_description($text) {
  113. $this->description = $text;
  114. }
  115. /**
  116. * Gets the descritpiotn member variable.
  117. */
  118. public function get_description() {
  119. return $this->description;
  120. }
  121. /**
  122. * Sets the input member variable.
  123. */
  124. public function set_input($value) {
  125. $this->input = $value;
  126. }
  127. /**
  128. * Gets the input member variable.
  129. */
  130. public function get_input() {
  131. return $this->input;
  132. }
  133. /**
  134. * Gets the value member variable.
  135. */
  136. public function get_value() {
  137. return $this->value;
  138. }
  139. /**
  140. * Returns a reference to a filter with the specified key.
  141. */
  142. public function &get_filter($key) {
  143. return $this->filters[$key];
  144. }
  145. /**
  146. * Gets the error member variable.
  147. */
  148. public function get_error() {
  149. return $this->error;
  150. }
  151. /**
  152. * Appends an attribute to the attributes member variable.
  153. */
  154. public function attribute($name, $value = NULL) {
  155. if(is_array($name)) {
  156. $this->attributes = array_merge($this->attributes, $name);
  157. } elseif(!is_null($value)) {
  158. $this->attributes[$name] = $value;
  159. }
  160. }
  161. /**
  162. * Gets all of the attributes as a formatted HTML string.
  163. */
  164. public function get_attributes() {
  165. $string = '';
  166. foreach($this->attributes as $name=>$value) {
  167. $string .= $name.'="'.$value.'" ';
  168. }
  169. return rtrim($string);
  170. }
  171. /**
  172. * Appends values to the options array.
  173. */
  174. public function set_options($options) {
  175. /* Use this instead of array_merge to avoid it remapping
  176. * numeric keys. */
  177. foreach((array) $options as $key=>$option) {
  178. $this->options[$key] = $option;
  179. }
  180. }
  181. /**
  182. * Gets values from the options array.
  183. */
  184. public function get_options() {
  185. return $this->options;
  186. }
  187. /**
  188. * Formats the error message and label.
  189. */
  190. public function get_wrapper() {
  191. if(!isset($this->wrapper)) {
  192. $view = Framework::load_view('inputs/default');
  193. } else {
  194. $view = Framework::load_view($this->wrapper);
  195. }
  196. $view->hide();
  197. if(!is_null($this->get_error())) {
  198. $view->error = $this->get_error();
  199. }
  200. $view->label = $this->get_label();
  201. $view->input = '%s';
  202. if(!is_null($this->get_description())) {
  203. $view->description = $this->get_description();
  204. }
  205. return (string) $view;
  206. }
  207. /**
  208. * Sets the wrapper.
  209. */
  210. public function set_wrapper($view) {
  211. $this->wrapper = $view;
  212. }
  213. /**
  214. * Must be overwritten in subclasses.
  215. */
  216. abstract public function __toString();
  217. }
  218. ?>