PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/smarty/plugins/modifier.add_url_param.php

https://github.com/intelliants/elitius
PHP | 94 lines | 27 code | 16 blank | 51 comment | 4 complexity | 9499460304a3f579ed04b6efd6760cec MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty add_url_param modifier plugin
  4. * ----------------------------------------
  5. *
  6. * This plugin adds parameters to existing URLs and is useful
  7. * when you want to append or update a parameter without
  8. * knowing anything about the contents of the existing
  9. * URL.
  10. *
  11. * If any of the parameters passed to the modifier
  12. * are already contained in the URL, their values
  13. * are updated in the URL, rather than appending another
  14. * parameter to the end.
  15. *
  16. * Examples:
  17. *
  18. * {$smarty.server.REQUEST_URI|add_url_param:'param=test&param2=test2'}
  19. * {$smarty.server.REQUEST_URI|add_url_param:$paramArray}
  20. * {$smarty.server.REQUEST_URI|add_url_param:'variable':$value}
  21. *
  22. *
  23. * @author Mark Mitchenall <mark@standingwave.co.uk>
  24. * @copyright Standingwave Ltd, 2005
  25. *
  26. * @param $url string URL to add the parameters to
  27. * @param $parameter mixed Assoc. Array with param names and
  28. * values or string contain the
  29. * additional parameter(s)
  30. * @param $paramValue string (optional) Parameter value when
  31. * $parameter contains just a
  32. * parameter name.
  33. * @return string
  34. */
  35. function smarty_modifier_add_url_param($url, $parameter, $paramValue = NULL)
  36. {
  37. if ($paramValue !== NULL) {
  38. // we were passed the parameter and value as
  39. // separate plug-in parameters, so just apply
  40. // them to the URL.
  41. $url = _addURLParameter ($url, $parameter, $paramValue) ;
  42. } elseif (is_array($parameter)) {
  43. // we were passed an assoc. array containing
  44. // parameter names and parameter values, so
  45. // apply them all to the URL.
  46. foreach ($parameter as $paramName => $paramValue) {
  47. $url = _addURLParameter ($url, $paramName, $paramValue) ;
  48. }
  49. } else {
  50. // was passed a string containing at least one parameter
  51. // so parse out those passed and apply them separately
  52. // to the URL.
  53. $numParams = preg_match_all('/([^=?&]+?)=([^&]*)/', $parameter, $matches, PREG_SET_ORDER) ;
  54. foreach ($matches as $match) {
  55. $url = _addURLParameter ($url, $match[1], $match[2]) ;
  56. }
  57. }
  58. return $url ;
  59. }
  60. function _addURLParameter ($url, $paramName, $paramValue) {
  61. // first check whether the parameter is already
  62. // defined in the URL so that we can just update
  63. // the value if that's the case.
  64. if (preg_match('/[?&]('.$paramName.')=[^&]*/', $url)) {
  65. // parameter is already defined in the URL, so
  66. // replace the parameter value, rather than
  67. // append it to the end.
  68. $url = preg_replace('/([?&]'.$paramName.')=[^&]*/', '$1='.$paramValue, $url) ;
  69. } else {
  70. // can simply append to the end of the URL, once
  71. // we know whether this is the only parameter in
  72. // there or not.
  73. $url .= strpos($url, '?') ? '&amp;' : '?';
  74. $url .= $paramName . '=' . $paramValue;
  75. }
  76. return $url ;
  77. }
  78. ?>