/tanora.org/www/framework/system/input.php
PHP | 244 lines | 138 code | 26 blank | 80 comment | 9 complexity | 4aaf0c872b2be1d761d915dc11827a47 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?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();
-
- }
- ?>