/tanora.org/www/framework/system/form.php
PHP | 195 lines | 133 code | 20 blank | 42 comment | 26 complexity | 4353d7f35eea3b89a773a17c6c6a3b05 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?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];
- }
- }
- }
- }
- ?>