| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class sfFillInForm |
|---|
| 19 |
{ |
|---|
| 20 |
protected |
|---|
| 21 |
$converters = array(), |
|---|
| 22 |
$skipFields = array(), |
|---|
| 23 |
$types = array('text', 'checkbox', 'radio', 'hidden', 'password'); |
|---|
| 24 |
|
|---|
| 25 |
public function addConverter($callable, $fields) |
|---|
| 26 |
{ |
|---|
| 27 |
foreach ((array) $fields as $field) |
|---|
| 28 |
{ |
|---|
| 29 |
$this->converters[$field][] = $callable; |
|---|
| 30 |
} |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
public function setSkipFields($fields) |
|---|
| 34 |
{ |
|---|
| 35 |
$this->skipFields = $fields; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
public function setTypes($types) |
|---|
| 39 |
{ |
|---|
| 40 |
$this->types = $types; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* fills in the values and returns HTML. This is a non validating tolerant mode. |
|---|
| 45 |
* |
|---|
| 46 |
* @return HTML with values filled in |
|---|
| 47 |
*/ |
|---|
| 48 |
public function fillInHtml($html, $formName, $formId, $values) |
|---|
| 49 |
{ |
|---|
| 50 |
$dom = new DomDocument('1.0', sfConfig::get('sf_charset', 'UTF-8')); |
|---|
| 51 |
|
|---|
| 52 |
$noHead = strpos($html,'<head') === false; |
|---|
| 53 |
if ($noHead){ |
|---|
| 54 |
|
|---|
| 55 |
$html = '<meta http-equiv="Content-Type" content="text/html; charset='.sfConfig::get('sf_charset').'"/>'.$html; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
@$dom->loadHTML($html); |
|---|
| 59 |
$dom = $this->fillInDom($dom, $formName, $formId, $values); |
|---|
| 60 |
|
|---|
| 61 |
if($noHead){ |
|---|
| 62 |
|
|---|
| 63 |
$headElement = $dom->getElementsByTagName('head')->item(0); |
|---|
| 64 |
if ($headElement) |
|---|
| 65 |
{ |
|---|
| 66 |
$dom->getElementsByTagName('html')->item(0)->removeChild($headElement); |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
return $dom->saveHTML(); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
* fills in the values and returns XHTML. This is same as XML but stripts the XML Prolog. |
|---|
| 74 |
* |
|---|
| 75 |
* @return XHTML without prolog with values filled in |
|---|
| 76 |
*/ |
|---|
| 77 |
public function fillInXhtml($xml, $formName, $formId, $values) |
|---|
| 78 |
{ |
|---|
| 79 |
$xhtml = $this->fillInXml($xml, $formName, $formId, $values); |
|---|
| 80 |
$prolog_regexp = '/^' . preg_quote('<?xml version="1.0"?>') . '\s*/'; |
|---|
| 81 |
return preg_replace($prolog_regexp, '', $xhtml); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* fills in the values and returns XHTML. It can only work correctly on validating XHTML. |
|---|
| 86 |
* |
|---|
| 87 |
* @return XHTML including XML prolog with values filled in |
|---|
| 88 |
*/ |
|---|
| 89 |
public function fillInXml($xml, $formName, $formId, $values) |
|---|
| 90 |
{ |
|---|
| 91 |
$dom = new DomDocument('1.0', sfConfig::get('sf_charset', 'UTF-8')); |
|---|
| 92 |
if (strpos($xml,'<!DOCTYPE') === false) |
|---|
| 93 |
{ |
|---|
| 94 |
$xml = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '. |
|---|
| 95 |
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'. |
|---|
| 96 |
$xml; |
|---|
| 97 |
} |
|---|
| 98 |
@$dom->loadXML($xml); |
|---|
| 99 |
|
|---|
| 100 |
$dom = $this->fillInDom($dom, $formName, $formId, $values); |
|---|
| 101 |
|
|---|
| 102 |
return $dom->saveXML(); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
public function fillInDom($dom, $formName, $formId, $values) |
|---|
| 106 |
{ |
|---|
| 107 |
$xpath = new DomXPath($dom); |
|---|
| 108 |
if ($dom->documentElement && $dom->documentElement->namespaceURI) |
|---|
| 109 |
{ |
|---|
| 110 |
$xpath->registerNamespace('xhtml', $dom->documentElement->namespaceURI); |
|---|
| 111 |
$ns = 'xhtml:'; |
|---|
| 112 |
} |
|---|
| 113 |
else |
|---|
| 114 |
{ |
|---|
| 115 |
$ns = ''; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
if ($formName) |
|---|
| 120 |
{ |
|---|
| 121 |
$xpath_query = '//'.$ns.'form[@name="'.$formName.'"]'; |
|---|
| 122 |
} |
|---|
| 123 |
elseif ($formId) |
|---|
| 124 |
{ |
|---|
| 125 |
$xpath_query = '//'.$ns.'form[@id="'.$formId.'"]'; |
|---|
| 126 |
} |
|---|
| 127 |
else |
|---|
| 128 |
{ |
|---|
| 129 |
$xpath_query = '//'.$ns.'form'; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
$form = $xpath->query($xpath_query)->item(0); |
|---|
| 133 |
if (!$form) |
|---|
| 134 |
{ |
|---|
| 135 |
if (!$formName && !$formId) |
|---|
| 136 |
{ |
|---|
| 137 |
throw new sfException('No form found in this page'); |
|---|
| 138 |
} |
|---|
| 139 |
else |
|---|
| 140 |
{ |
|---|
| 141 |
throw new sfException(sprintf('The form "%s" cannot be found', $formName ? $formName : $formId)); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
$query = 'descendant::'.$ns.'input[@name and (not(@type)'; |
|---|
| 146 |
foreach ($this->types as $type) |
|---|
| 147 |
{ |
|---|
| 148 |
$query .= ' or @type="'.$type.'"'; |
|---|
| 149 |
} |
|---|
| 150 |
$query .= ')] | descendant::'.$ns.'textarea[@name] | descendant::'.$ns.'select[@name]'; |
|---|
| 151 |
|
|---|
| 152 |
foreach ($xpath->query($query, $form) as $element) |
|---|
| 153 |
{ |
|---|
| 154 |
$name = (string) $element->getAttribute('name'); |
|---|
| 155 |
$value = (string) $element->getAttribute('value'); |
|---|
| 156 |
$type = (string) $element->getAttribute('type'); |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
if (!$this->hasValue($values, $name) || in_array($name, $this->skipFields)) |
|---|
| 160 |
{ |
|---|
| 161 |
continue; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
if ($element->nodeName == 'input') |
|---|
| 165 |
{ |
|---|
| 166 |
if ($type == 'checkbox' || $type == 'radio') |
|---|
| 167 |
{ |
|---|
| 168 |
|
|---|
| 169 |
$element->removeAttribute('checked'); |
|---|
| 170 |
if (is_array($this->getValue($values, $name)) && ($this->hasValue($values, $name) || !$element->hasAttribute('value'))) |
|---|
| 171 |
{ |
|---|
| 172 |
if (in_array($value, $this->getValue($values, $name))) |
|---|
| 173 |
{ |
|---|
| 174 |
$element->setAttribute('checked', 'checked'); |
|---|
| 175 |
} |
|---|
| 176 |
} |
|---|
| 177 |
else if ($this->hasValue($values, $name) && ($this->getValue($values, $name) == $value || !$element->hasAttribute('value'))) |
|---|
| 178 |
{ |
|---|
| 179 |
$element->setAttribute('checked', 'checked'); |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
else |
|---|
| 183 |
{ |
|---|
| 184 |
|
|---|
| 185 |
$element->removeAttribute('value'); |
|---|
| 186 |
$element->setAttribute('value', $this->escapeValue($this->getValue($values, $name, true), $name)); |
|---|
| 187 |
} |
|---|
| 188 |
} |
|---|
| 189 |
else if ($element->nodeName == 'textarea') |
|---|
| 190 |
{ |
|---|
| 191 |
$el = $element->cloneNode(false); |
|---|
| 192 |
$el->appendChild($dom->createTextNode($this->escapeValue($this->getValue($values, $name, true), $name))); |
|---|
| 193 |
$element->parentNode->replaceChild($el, $element); |
|---|
| 194 |
} |
|---|
| 195 |
else if ($element->nodeName == 'select') |
|---|
| 196 |
{ |
|---|
| 197 |
|
|---|
| 198 |
$value = $this->getValue($values, $name, strpos($name,'[]') !== false); |
|---|
| 199 |
$multiple = $element->hasAttribute('multiple'); |
|---|
| 200 |
foreach ($xpath->query('descendant::'.$ns.'option', $element) as $option) |
|---|
| 201 |
{ |
|---|
| 202 |
$option->removeAttribute('selected'); |
|---|
| 203 |
if ($multiple && is_array($value)) |
|---|
| 204 |
{ |
|---|
| 205 |
if (in_array($option->getAttribute('value'), $value)) |
|---|
| 206 |
{ |
|---|
| 207 |
$option->setAttribute('selected', 'selected'); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
else if ($value == $option->getAttribute('value')) |
|---|
| 211 |
{ |
|---|
| 212 |
$option->setAttribute('selected', 'selected'); |
|---|
| 213 |
} |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
return $dom; |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
protected function hasValue($values, $name) |
|---|
| 222 |
{ |
|---|
| 223 |
if (array_key_exists($name, $values)) |
|---|
| 224 |
{ |
|---|
| 225 |
return true; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
return null !== sfToolkit::getArrayValueForPath($values, $name); |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
protected function getValue(&$values, $name, $shiftArray = false) |
|---|
| 233 |
{ |
|---|
| 234 |
if (array_key_exists($name, $values)) |
|---|
| 235 |
{ |
|---|
| 236 |
$return = &$values[$name]; |
|---|
| 237 |
} else { |
|---|
| 238 |
$return = &sfToolkit::getArrayValueForPathByRef($values, $name); |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
if ($shiftArray && is_array($return)) |
|---|
| 242 |
{ |
|---|
| 243 |
|
|---|
| 244 |
return array_shift($return); |
|---|
| 245 |
} |
|---|
| 246 |
return $return; |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
protected function escapeValue($value, $name) |
|---|
| 250 |
{ |
|---|
| 251 |
if (function_exists('iconv') && strtolower(sfConfig::get('sf_charset')) != 'utf-8') |
|---|
| 252 |
{ |
|---|
| 253 |
$new_value = iconv(sfConfig::get('sf_charset'), 'UTF-8', $value); |
|---|
| 254 |
if (false !== $new_value) |
|---|
| 255 |
{ |
|---|
| 256 |
$value = $new_value; |
|---|
| 257 |
} |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
if (isset($this->converters[$name])) |
|---|
| 261 |
{ |
|---|
| 262 |
foreach ($this->converters[$name] as $callable) |
|---|
| 263 |
{ |
|---|
| 264 |
$value = call_user_func($callable, $value); |
|---|
| 265 |
} |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
return $value; |
|---|
| 269 |
} |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|