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

Language PHP Lines 196
MD5 Hash 4353d7f35eea3b89a773a17c6c6a3b05 Estimated Cost $3,197 (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
<?php
/**
 * File: framework/system/form.php
 * 
 * Defines a class used by ORMs to generate HTML forms.
 */
 
class Form {
	
	private $model;
	private $action;
	private $submit_value = 'Submit';
	private $attributes = array();
	private $inputs = array();
	
	/**
	 * Accepts a reference to an ORM model to get the form fields from.
	 */
	function __construct(&$orm = NULL) {
		if(!is_null($orm)) {
			$this->model = $orm;
		}
	}
	
	/**
	 * Validates form. Returns TRUE on success and FALSE on failure, or
	 * NULL if there was no POST data.
	 */
	public function validate() {
		if(Framework::post()) {
			$valid = TRUE;
			
			if(Framework::config('logging', 'form') != 'none') {
				Framework::log('--- Begin Form Input ---',2);
			}
			
			foreach($this->get_inputs() as $input) {
				$input->set_input(Framework::post($input->get_name()));
				if($input->filter() === FALSE) {
					// do some logging
					if(Framework::config('logging', 'form') == 'all' || Framework::config('logging', 'form') == 'invalid') {
						Framework::log('Field: '.$input->get_name(), 2);
						Framework::log('Raw Input:'.print_r($input->get_input(), true), 3);
						Framework::log('Error: '.$input->get_error(), 3);
					}
					if(Framework::config('debugging', 'form')) {
						Framework::record(
							array(
								'field'=>$input->get_name(),
								'raw input'=>$input->get_input(),
								'error'=>$input->get_error()
							)
						);
					}
					// the filter returned false, so the input is not valid
					$valid = FALSE;
				} else {
					// do some more logging
					if(Framework::config('logging','form') == 'all' || Framework::config('logging', 'form') == 'valid') {
						Framework::log('Field: '.$input->get_name(), 2);
						Framework::log('Raw Input: '.print_r($input->get_input(), true), 3);
						Framework::log('Filtered Input: '.print_r($input->get_value(), true), 3);
					}
					if(Framework::config('debugging', 'form')) {
						Framework::record(
							array(
								'field'=>$input->get_name(),
								'raw input'=>$input->get_input(),
								'filtered input'=>$input->get_value()
							)
						);
					}
				}
			}
			if(Framework::config('logging', 'form') != 'none') {
				Framework::log('--- End Form Input ---',2);
			}
			
			return $valid;
		}
	}
	
	/**
	 * Returns the input values as an object.
	 */
	public function data() {
		$obj = new stdClass();
		foreach($this->get_inputs() as $input) {
			$name = $input->get_name();
			$value = $input->get_value();
			if(!empty($name)) {
				$obj->$name = $value;
			}
		}
		return $obj;
	}
	
	/**
	 * Generates the HTML form.
	 */
	public function __toString() {
		$string = '<form method="post" action="'.$this->action.'"';
		if(!empty($this->attributes)) {
			foreach($this->attributes as $name=>$value) {
				$string .= $name.'="'.$value.'" ';
			}
			$string = rtrim($string);
		}
		$string .= '>';
		
		foreach($this->get_inputs() as $input) {
			$string .= $input;
		}
		$string .= '<input type="submit" value="'.$this->submit_value.'" />';
		$string .= '</form>';
		
		return $string;
	}
	
	/**
	 * Sets the action member variable.
	 */
	public function action($value) {
		$this->action = $value;
	}
	
	/**
	 * Sets the submit_value member variable.
	 */
	public function submit_value($value) {
		$this->submit_value = $value;
	}
	
	/**
	 * 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;
		}
	}
	
	/**
	 * Returns the list of inputs.
	 */
	public function get_inputs() {
		/* Populate fields list from the model when empty. */
		if(empty($this->inputs) && isset($this->model)) {
			foreach($this->model->get_fields() as $field) {
				$this->inputs[] = &$field->get_input();
			}
		}
		return $this->inputs;
	}
	
	
	/**
	 * Appends a field's input from the model member variable and
	 * returns the reference.
	 */
	public function use_input($name) {
		foreach($this->model->get_fields() as $field) {
			if($field->get_name() == $name) {
				$index = count($this->inputs);
				$this->inputs[$index] = $field->get_input();
				return $this->inputs[$index];
			}
		}
		Framework::error('Input '.$name.' does not exist.');
	}
	
	/**
	 * Appends a new field input to the model member variable and
	 * returns the reference.
	 */
	public function new_input($type) {
		require_once(FRAMEWORK_PATH.'system/input.php');
		$dirs = array(INPUT_PATH, FRAMEWORK_PATH.'inputs/');
		foreach($dirs as $dir) {
			$file = $dir.strtolower($type).'input.php';
			if(file_exists($file)) {
				require_once($file);
				$class = $type.'Input';
				
				$index = count($this->inputs);
				$this->inputs[$index] = new $class($this);
				return $this->inputs[$index];
			}
		}
	}
}

?>
Back to Top