contributions/db.textfields/model/base/fields/dbfield.text.url.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * A field to hold an URL 00004 * 00005 * Value gets validated and prefixed with http://, if it hasn't a scheme already. 00006 * 00007 * Field in DB should be defined as VARCHAR(255) 00008 * 00009 * URLs of course can be much longer than 255 chars, up to 4000 e.g. is Apache default. This however 00010 * hardly makes sense. 00011 * 00012 * @since 0.5.1 00013 * 00014 * @author Gerd Riesselmann 00015 * @ingroup TextFields 00016 */ 00017 class DBFieldTextUrl extends DBFieldText { 00018 public function __construct($name, $default_value = null, $policy = self::NOT_NULL) { 00019 parent::__construct($name, 255, $default_value, $policy); 00020 } 00021 00022 /** 00023 * Returns true, if the value passed fits the fields restrictions 00024 * 00025 * @param string $value 00026 * @return Status 00027 */ 00028 public function validate($value) { 00029 $ret = parent::validate($value); 00030 if ($ret->is_ok() && Cast::string($value) !== '') { 00031 if (!Validation::is_url($value)) { 00032 $ret->append(tr( 00033 '%field must be an URL', 00034 'textfields', 00035 array( 00036 '%field' => tr($this->get_field_name()), 00037 ) 00038 )); 00039 } 00040 } 00041 return $ret; 00042 } 00043 00044 /** 00045 * Reformat passed value to DB format 00046 * 00047 * @param mixed $value 00048 * @return string 00049 */ 00050 public function format($value) { 00051 if (Cast::string($value) === '') { 00052 return 'NULL'; 00053 } 00054 else { 00055 return $this->quote(Url::create($value)->build(Url::ABSOLUTE, Url::NO_ENCODE_PARAMS)); 00056 } 00057 } 00058 }