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

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

?>
Back to Top