00001 <?php 00002 require_once('field.inc.php'); 00003 00009 abstract class AbstractField implements Field 00010 { 00011 public $name = ''; 00012 protected $title = ''; 00013 protected $value = ''; 00014 protected $attribs = array(); 00015 protected $required = false; 00016 protected $regExp = ''; 00017 protected $errorMessage = ''; 00018 protected $hasError = false; 00019 protected $parentForm = ''; 00020 public $definition = ''; 00021 00022 00035 public function __construct($name, Form &$parentForm, array $attribs = array()) 00036 { 00037 $this->name = $name; 00038 $this->parentForm = $parentForm; 00039 00040 $this->required = false; 00041 $this->parseExtras($attribs); 00042 $this->title = array_key_exists('title', $attribs) ? $attribs['title'] : ucwords(str_replace('_', ' ', $name)); 00043 $this->errorMessage = "The $this->title field had an error."; 00044 00045 if(!isset($this->attribs['id'])) 00046 $this->attribs['id'] = $this->name; 00047 00048 $this->hasError = null; 00049 } 00050 00055 public function __get($name) 00056 { 00057 if(isset($this->$name) || is_null($this->$name)) 00058 return $this->$name; 00059 else if(array_key_exists($name, $this->attribs)) 00060 return $this->attribs[$name]; 00061 00062 return false; 00063 } 00064 00070 public function __set($name, $value) 00071 { 00072 if($name == 'value') 00073 $this->setData($value); 00074 else if(isset($this->$name)) 00075 $this->$name = $value; 00076 else 00077 $this->attribs[$name] = $value; 00078 00079 return true; 00080 } 00081 00085 public function validate() 00086 { 00087 $this->setDataFromRequest(); 00088 00089 $this->hasError = false; 00090 00091 if($this->required && $this->value == null) 00092 { 00093 $this->hasError = true; 00094 $this->errorMessage = "The $this->title field is required."; 00095 } 00096 00097 if($this->regExp && !$this->hasError) 00098 { 00099 list($this->regExp, $this->errorMessage) = 00100 array_values($this->convertSpecialRegex($this->regExp, $this->errorMessage)); 00101 00102 # Error if the string doesn't match the regex, or if the value is empty (required fields are handled above) 00103 $this->hasError = !((bool)preg_match($this->regExp, $this->value) || $this->value == null); 00104 } 00105 } 00106 00107 public function getTitle() 00108 { 00109 if ($this->definition == '') 00110 return $this->title; 00111 else 00112 return "<acronym title=\"$this->definition\">$this->title</acronym>"; 00113 } 00114 00119 public function hasError() 00120 { 00121 if($this->hasError === null) 00122 throw new NotValidatedException($this); 00123 00124 return (bool)$this->hasError; 00125 } 00126 00131 public function forceError() 00132 { 00133 $this->hasError = true; 00134 } 00135 00140 public function getSqlImpl() 00141 { 00142 return "$this->name = '$this->value'"; 00143 } 00144 00149 public function setData($value) 00150 { 00151 $this->value = $value; 00152 } 00153 00157 public function getData() 00158 { 00159 return $this->value; 00160 } 00161 00166 public function getJs() 00167 { 00168 return null; # No-Op 00169 } 00170 00174 public function getCss() 00175 { 00176 return null; # No-Op 00177 } 00178 00183 public function addAttributes(array $attr) 00184 { 00185 $this->attribs = array_merge($this->attribs, $attr); 00186 } 00187 00188 00193 protected function parseExtras(array $data) 00194 { 00195 $this->attribs = array(); # clear out any existing value 00196 00197 foreach($data as $key => $value) 00198 $this->__set($key, $value); 00199 } 00200 00201 00206 protected function extrasAsAttributes() 00207 { 00208 $ret = array(); 00209 00210 foreach(array_change_key_case($this->attribs, CASE_LOWER) as $key => $value) 00211 $ret[] = "$key=\"$value\""; 00212 return implode(' ', $ret); 00213 } 00214 00218 protected function setDataFromRequest() 00219 { 00220 if(isset($_REQUEST[$this->name])) 00221 $this->value = $_REQUEST[$this->name]; 00222 } 00223 00224 00232 protected function convertSpecialRegex($regExp, $defaultError) 00233 { 00234 $error = $defaultError; 00235 switch($regExp) 00236 { 00237 case 'number': 00238 $regExp = '/^-?[0-9]+$/'; 00239 $error = "The $this->title field must be a number."; 00240 break; 00241 00242 case 'float': 00243 $regExp = '/^-?([0-9]*\.)?[0-9]+$/'; 00244 $error = "The $this->title field must be a number."; 00245 break; 00246 00247 case 'alpha': 00248 $regExp = '/^[A-Za-z]*$/'; 00249 $error = "The $this->title field must consist of only alphabetic characters."; 00250 break; 00251 00252 case 'alphanumeric': 00253 $regExp = '/^[A-Za-z0-9]*$/'; 00254 $error = "The $this->title field must consist of only alpha numeric characters."; 00255 break; 00256 00257 case 'email': 00258 $regExp = '/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i'; 00259 $error = "The $this->title field must be a real email."; 00260 break; 00261 } 00262 00263 return array('regexp' => $regExp, 'message' => $error); 00264 } 00265 00270 protected static function assertAttributeIsArray(&$value) 00271 { 00272 if(!is_array($value)) 00273 throw new InvalidAttributeValueException($value, 'An Array'); 00274 } 00275 00281 protected static function assertAttributeIsObject($type, &$value) 00282 { 00283 if(!($value instanceof $type)) 00284 throw new InvalidAttributeValueException($value, $type); 00285 } 00286 } 00287 ?>