[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * UrlHelper. 13 * 14 * @package symfony 15 * @subpackage helper 16 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 17 * @version SVN: $Id: UrlHelper.php 3380 2007-02-01 06:57:47Z fabien $ 18 */ 19 20 21 /** 22 * Returns a routed URL based on the module/action passed as argument 23 * and the routing configuration. 24 * 25 * <b>Examples:</b> 26 * <code> 27 * echo url_for('my_module/my_action'); 28 * => /path/to/my/action 29 * echo url_for('@my_rule'); 30 * => /path/to/my/action 31 * echo url_for('@my_rule', true); 32 * => http://myapp.example.com/path/to/my/action 33 * </code> 34 * 35 * @param string 'module/action' or '@rule' of the action 36 * @param bool return absolute path? 37 * @return string routed URL 38 */ 39 function url_for($internal_uri, $absolute = false) 40 { 41 static $controller; 42 43 if (!isset($controller)) 44 { 45 $controller = sfContext::getInstance()->getController(); 46 } 47 48 return $controller->genUrl($internal_uri, $absolute); 49 } 50 51 /** 52 * Creates a <a> link tag of the given name using a routed URL 53 * based on the module/action passed as argument and the routing configuration. 54 * It's also possible to pass a string instead of a module/action pair to 55 * get a link tag that just points without consideration. 56 * If null is passed as a name, the link itself will become the name. 57 * If an object is passed as a name, the object string representation is used. 58 * One of the options serves for for creating javascript confirm alerts where 59 * if you pass 'confirm' => 'Are you sure?', the link will be guarded 60 * with a JS popup asking that question. If the user accepts, the link is processed, 61 * otherwise not. 62 * 63 * <b>Options:</b> 64 * - 'absolute' - if set to true, the helper outputs an absolute URL 65 * - 'query_string' - to append a query string (starting by ?) to the routed url 66 * - 'confirm' - displays a javascript confirmation alert when the link is clicked 67 * - 'popup' - if set to true, the link opens a new browser window 68 * - 'post' - if set to true, the link submits a POST request instead of GET (caution: do not use inside a form) 69 * 70 * <b>Note:</b> The 'popup' and 'post' options are not compatible with each other. 71 * 72 * <b>Examples:</b> 73 * <code> 74 * echo link_to('Delete this page', 'my_module/my_action'); 75 * => <a href="/path/to/my/action">Delete this page</a> 76 * echo link_to('Visit Hoogle', 'http://www.hoogle.com'); 77 * => <a href="http://www.hoogle.com">Visit Hoogle</a> 78 * echo link_to('Delete this page', 'my_module/my_action', array('id' => 'myid', 'confirm' => 'Are you sure?', 'absolute' => true)); 79 * => <a href="http://myapp.example.com/path/to/my/action" id="myid" onclick="return confirm('Are you sure?');">Delete this page</a> 80 * </code> 81 * 82 * @param string name of the link, i.e. string to appear between the <a> tags 83 * @param string 'module/action' or '@rule' of the action 84 * @param array additional HTML compliant <a> tag parameters 85 * @return string XHTML compliant <a href> tag 86 * @see url_for 87 */ 88 function link_to($name = '', $internal_uri = '', $options = array()) 89 { 90 $html_options = _parse_attributes($options); 91 92 $html_options = _convert_options_to_javascript($html_options); 93 94 $absolute = false; 95 if (isset($html_options['absolute_url'])) 96 { 97 $html_options['absolute'] = $html_options['absolute_url']; 98 unset($html_options['absolute_url']); 99 } 100 if (isset($html_options['absolute'])) 101 { 102 $absolute = (boolean) $html_options['absolute']; 103 unset($html_options['absolute']); 104 } 105 106 $html_options['href'] = url_for($internal_uri, $absolute); 107 108 if (isset($html_options['query_string'])) 109 { 110 $html_options['href'] .= '?'.$html_options['query_string']; 111 unset($html_options['query_string']); 112 } 113 114 if (is_object($name)) 115 { 116 if (method_exists($name, '__toString')) 117 { 118 $name = $name->__toString(); 119 } 120 else 121 { 122 throw new sfException(sprintf('Object of class "%s" cannot be converted to string (Please create a __toString() method)', get_class($name))); 123 } 124 } 125 126 if (!strlen($name)) 127 { 128 $name = $html_options['href']; 129 } 130 131 return content_tag('a', $name, $html_options); 132 } 133 134 /** 135 * If the condition passed as first argument is true, 136 * creates a <a> link tag of the given name using a routed URL 137 * based on the module/action passed as argument and the routing configuration. 138 * If the condition is false, the given name is returned between <span> tags 139 * 140 * <b>Options:</b> 141 * - 'tag' - the HTML tag that must enclose the name if the condition is false, defaults to <span> 142 * - 'absolute' - if set to true, the helper outputs an absolute URL 143 * - 'query_string' - to append a query string (starting by ?) to the routed url 144 * - 'confirm' - displays a javascript confirmation alert when the link is clicked 145 * - 'popup' - if set to true, the link opens a new browser window 146 * - 'post' - if set to true, the link submits a POST request instead of GET (caution: do not use inside a form) 147 * 148 * <b>Examples:</b> 149 * <code> 150 * echo link_to_if($user->isAdministrator(), 'Delete this page', 'my_module/my_action'); 151 * => <a href="/path/to/my/action">Delete this page</a> 152 * echo link_to_if(!$user->isAdministrator(), 'Delete this page', 'my_module/my_action'); 153 * => <span>Delete this page</span> 154 * </code> 155 * 156 * @param bool condition 157 * @param string name of the link, i.e. string to appear between the <a> tags 158 * @param string 'module/action' or '@rule' of the action 159 * @param array additional HTML compliant <a> tag parameters 160 * @return string XHTML compliant <a href> tag or name 161 * @see link_to 162 */ 163 function link_to_if($condition, $name = '', $internal_uri = '', $options = array()) 164 { 165 if ($condition) 166 { 167 return link_to($name, $internal_uri, $options); 168 } 169 else 170 { 171 $html_options = _parse_attributes($options); 172 $tag = _get_option($html_options, 'tag', 'span'); 173 174 return content_tag($tag, $name, $html_options); 175 } 176 } 177 178 /** 179 * If the condition passed as first argument is false, 180 * creates a <a> link tag of the given name using a routed URL 181 * based on the module/action passed as argument and the routing configuration. 182 * If the condition is true, the given name is returned between <span> tags 183 * 184 * <b>Options:</b> 185 * - 'tag' - the HTML tag that must enclose the name if the condition is true, defaults to <span> 186 * - 'absolute' - if set to true, the helper outputs an absolute URL 187 * - 'query_string' - to append a query string (starting by ?) to the routed url 188 * - 'confirm' - displays a javascript confirmation alert when the link is clicked 189 * - 'popup' - if set to true, the link opens a new browser window 190 * - 'post' - if set to true, the link submits a POST request instead of GET (caution: do not use inside a form) 191 * 192 * <b>Examples:</b> 193 * <code> 194 * echo link_to_unless($user->isAdministrator(), 'Delete this page', 'my_module/my_action'); 195 * => <span>Delete this page</span> 196 * echo link_to_unless(!$user->isAdministrator(), 'Delete this page', 'my_module/my_action'); 197 * => <a href="/path/to/my/action">Delete this page</a> 198 * </code> 199 * 200 * @param bool condition 201 * @param string name of the link, i.e. string to appear between the <a> tags 202 * @param string 'module/action' or '@rule' of the action 203 * @param array additional HTML compliant <a> tag parameters 204 * @return string XHTML compliant <a href> tag or name 205 * @see link_to 206 */ 207 function link_to_unless($condition, $name = '', $url = '', $options = array()) 208 { 209 return link_to_if(!$condition, $name, $url, $options); 210 } 211 212 /** 213 * Creates an <input> button tag of the given name pointing to a routed URL 214 * based on the module/action passed as argument and the routing configuration. 215 * The syntax is similar to the one of link_to. 216 * 217 * <b>Options:</b> 218 * - 'absolute' - if set to true, the helper outputs an absolute URL 219 * - 'query_string' - to append a query string (starting by ?) to the routed url 220 * - 'confirm' - displays a javascript confirmation alert when the button is clicked 221 * - 'popup' - if set to true, the button opens a new browser window 222 * - 'post' - if set to true, the button submits a POST request instead of GET (caution: do not use inside a form) 223 * 224 * <b>Examples:</b> 225 * <code> 226 * echo button_to('Delete this page', 'my_module/my_action'); 227 * => <input value="Delete this page" type="button" onclick="document.location.href='/path/to/my/action';" /> 228 * </code> 229 * 230 * @param string name of the button 231 * @param string 'module/action' or '@rule' of the action 232 * @param array additional HTML compliant <input> tag parameters 233 * @return string XHTML compliant <input> tag 234 * @see url_for, link_to 235 */ 236 function button_to($name, $internal_uri, $options = array()) 237 { 238 $html_options = _convert_options($options); 239 $html_options['value'] = $name; 240 241 if (isset($html_options['post']) && $html_options['post']) 242 { 243 if (isset($html_options['popup'])) 244 { 245 throw new sfConfigurationException('You can\'t use "popup" and "post" together'); 246 } 247 $html_options['type'] = 'submit'; 248 unset($html_options['post']); 249 $html_options = _convert_options_to_javascript($html_options); 250 251 return form_tag($internal_uri, array('method' => 'post', 'class' => 'button_to')).tag('input', $html_options).'</form>'; 252 } 253 else if (isset($html_options['popup'])) 254 { 255 $html_options['type'] = 'button'; 256 $html_options = _convert_options_to_javascript($html_options, $internal_uri); 257 258 return tag('input', $html_options); 259 } 260 else 261 { 262 $html_options['type'] = 'button'; 263 $html_options['onclick'] = "document.location.href='".url_for($internal_uri)."';"; 264 $html_options = _convert_options_to_javascript($html_options); 265 266 return tag('input', $html_options); 267 } 268 } 269 270 /** 271 * Creates a <a> link tag to the given email (with href="mailto:..."). 272 * If null is passed as a name, the email itself will become the name. 273 * 274 * <b>Options:</b> 275 * - 'encode' - if set to true, the email address appears with various random encoding for each letter. 276 * The mail link still works when encoded, but the address doesn't appear in clear 277 * in the source. Use it to prevent spam (efficiency not guaranteed). 278 * 279 * <b>Examples:</b> 280 * <code> 281 * echo mail_to('webmaster@example.com'); 282 * => <a href="mailto:webmaster@example.com">webmaster@example.com</a> 283 * echo mail_to('webmaster@example.com', 'send us an email'); 284 * => <a href="mailto:webmaster@example.com">send us an email</a> 285 * echo mail_to('webmaster@example.com', 'send us an email', array('encode' => true)); 286 * => <a href="mailto:webmaster@example.com">send us an email</a> 287 * </code> 288 * 289 * @param string target email 290 * @param string name of the link, i.e. string to appear between the <a> tags 291 * @param array additional HTML compliant <a> tag parameters 292 * @return string XHTML compliant <a href> tag 293 * @see link_to 294 */ 295 function mail_to($email, $name = '', $options = array(), $default_value = array()) 296 { 297 $html_options = _parse_attributes($options); 298 299 $html_options = _convert_options_to_javascript($html_options); 300 301 $default_tmp = _parse_attributes($default_value); 302 $default = array(); 303 foreach ($default_tmp as $key => $value) 304 { 305 $default[] = urlencode($key).'='.urlencode($value); 306 } 307 $options = count($default) ? '?'.implode('&', $default) : ''; 308 309 if (isset($html_options['encode']) && $html_options['encode']) 310 { 311 unset($html_options['encode']); 312 $html_options['href'] = _encodeText('mailto:'.$email.$options); 313 if (!$name) 314 { 315 $name = _encodeText($email); 316 } 317 } 318 else 319 { 320 $html_options['href'] = 'mailto:'.$email.$options; 321 if (!$name) 322 { 323 $name = $email; 324 } 325 } 326 327 return content_tag('a', $name, $html_options); 328 } 329 330 function _convert_options_to_javascript($html_options, $internal_uri = '') 331 { 332 // confirm 333 $confirm = isset($html_options['confirm']) ? $html_options['confirm'] : ''; 334 unset($html_options['confirm']); 335 336 // popup 337 $popup = isset($html_options['popup']) ? $html_options['popup'] : ''; 338 unset($html_options['popup']); 339 340 // post 341 $post = isset($html_options['post']) ? $html_options['post'] : ''; 342 unset($html_options['post']); 343 344 $onclick = isset($html_options['onclick']) ? $html_options['onclick'] : ''; 345 346 if ($popup && $post) 347 { 348 throw new sfConfigurationException('You can\'t use "popup" and "post" in the same link'); 349 } 350 else if ($confirm && $popup) 351 { 352 $html_options['onclick'] = $onclick.'if ('._confirm_javascript_function($confirm).') { '._popup_javascript_function($popup, $internal_uri).' };return false;'; 353 } 354 else if ($confirm && $post) 355 { 356 $html_options['onclick'] = $onclick.'if ('._confirm_javascript_function($confirm).') { '._post_javascript_function().' };return false;'; 357 } 358 else if ($confirm) 359 { 360 if ($onclick) 361 { 362 $html_options['onclick'] = 'if ('._confirm_javascript_function($confirm).') {'.$onclick.'}'; 363 } 364 else 365 { 366 $html_options['onclick'] = 'return '._confirm_javascript_function($confirm).';'; 367 } 368 } 369 else if ($post) 370 { 371 $html_options['onclick'] = $onclick._post_javascript_function().'return false;'; 372 } 373 else if ($popup) 374 { 375 $html_options['onclick'] = $onclick._popup_javascript_function($popup, $internal_uri).'return false;'; 376 } 377 378 return $html_options; 379 } 380 381 function _confirm_javascript_function($confirm) 382 { 383 return "confirm('".escape_javascript($confirm)."')"; 384 } 385 386 function _popup_javascript_function($popup, $internal_uri = '') 387 { 388 $url = $internal_uri == '' ? 'this.href' : "'".url_for($internal_uri)."'"; 389 390 if (is_array($popup)) 391 { 392 if (isset($popup[1])) 393 { 394 return "var w=window.open(".$url.",'".$popup[0]."','".$popup[1]."');w.focus();"; 395 } 396 else 397 { 398 return "var w=window.open(".$url.",'".$popup[0]."');w.focus();"; 399 } 400 } 401 else 402 { 403 return "var w=window.open(".$url.");w.focus();"; 404 } 405 } 406 407 function _post_javascript_function() 408 { 409 return "f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();"; 410 } 411 412 function _encodeText($text) 413 { 414 $encoded_text = ''; 415 416 for ($i = 0; $i < strlen($text); $i++) 417 { 418 $char = $text{$i}; 419 $r = rand(0, 100); 420 421 # roughly 10% raw, 45% hex, 45% dec 422 # '@' *must* be encoded. I insist. 423 if ($r > 90 && $char != '@') 424 { 425 $encoded_text .= $char; 426 } 427 else if ($r < 45) 428 { 429 $encoded_text .= '&#x'.dechex(ord($char)).';'; 430 } 431 else 432 { 433 $encoded_text .= '&#'.ord($char).';'; 434 } 435 } 436 437 return $encoded_text; 438 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |