PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file url_helper.php

Documentation is available at url_helper.php

  1. <?php
  2. /**
  3. * File containing the UrlHelper class and support functions
  4. *
  5. * (PHP 5)
  6. *
  7. * @package PHPonTrax
  8. * @version $Id$
  9. * @copyright (c) 2005 John Peterson
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30.  
  31. /**
  32. * @todo Document this class
  33. */
  34. class UrlHelper extends Helpers {
  35.  
  36. /**
  37. * Creates a link tag of the given +name+ using an URL created by
  38. * the set of +options+.
  39. * It's also possible to pass a string instead of an options hash
  40. * to get a link tag that just points without consideration. If
  41. * null is passed as a name, the link itself will become the
  42. * name.
  43. * The $html_options have a special feature for creating
  44. * javascript confirm alerts where if you pass ":confirm" => 'Are
  45. * you sure?',
  46. * the link will be guarded with a JS popup asking that
  47. * question. If the user accepts, the link is processed, otherwise
  48. * not.
  49. *
  50. * Example:
  51. * link_to("Delete this page", array(":action" => "delete",
  52. * ":id" => $page->id), array(":confirm" => "Are you sure?"))
  53. * @return string
  54. * @uses content_tag()
  55. * @uses convert_confirm_option_to_javascript()
  56. * @uses url_for()
  57. */
  58. function link_to($name, $options = array(), $html_options = array()) {
  59. $html_options =
  60. $this->convert_confirm_option_to_javascript($html_options);
  61. if(is_string($options)) {
  62. $href = array("href" => $options);
  63. if(count($html_options) > 0) {
  64. $html_options = array_merge($html_options, $href);
  65. } else {
  66. $html_options = $href;
  67. }
  68. if(!$name) {
  69. $name = $options;
  70. }
  71. $html = $this->content_tag("a", $name, $html_options);
  72. } else {
  73. $url = $this->url_for($options);
  74. if(!$name) {
  75. $name = $url;
  76. }
  77. $href = array("href" => $url);
  78. if(count($html_options) > 0) {
  79. $html_options = array_merge($html_options, $href);
  80. } else {
  81. $html_options = $href;
  82. }
  83. $html = $this->content_tag("a", $name, $html_options);
  84. }
  85. return $html;
  86. }
  87.  
  88. /**
  89. * @todo Document this method
  90. * @param string[] Options
  91. * @return string
  92. */
  93. function convert_confirm_option_to_javascript($html_options) {
  94. if(array_key_exists('confirm', $html_options)) {
  95. $html_options['onclick'] =
  96. "return confirm('".addslashes($html_options['confirm'])."');";
  97. unset($html_options['confirm']);
  98. }
  99. return $html_options;
  100. }
  101.  
  102. /**
  103. * @todo Document this method
  104. * @param mixed[]
  105. * @param mixed[]
  106. * @return mixed[]
  107. */
  108. function convert_boolean_attributes(&$html_options, $bool_attrs) {
  109. foreach($bool_attrs as $x) {
  110. if(@array_key_exists($x, $html_options)) {
  111. $html_options[$x] = $x;
  112. }
  113. }
  114. return $html_options;
  115. }
  116.  
  117. /**
  118. * @todo Document this method
  119. * @param string
  120. * @param mixed[]
  121. * @param mixed[]
  122. * @return string
  123. * @uses convert_boolean_attributes()
  124. * @uses convert_confirm_option_to_javascript()
  125. * @uses url_for()
  126. */
  127. function button_to($name, $options = array(), $html_options = null) {
  128. $html_options = (!is_null($html_options) ? $html_options : array());
  129. $this->convert_boolean_attributes($html_options, array('disabled'));
  130. $this->convert_confirm_option_to_javascript($html_options);
  131. if (is_string($options)) {
  132. $url = $options;
  133. $name = (!is_null($name) ? $name : $options);
  134. } else {
  135. $url = url_for($options);
  136. $name = (!is_null($name) ? $name : url_for($options));
  137. }
  138.  
  139. $html_options = array_merge($html_options,
  140. array("type" => "submit", "value" => $name));
  141. return "<form method=\"post\" action=\"" . htmlspecialchars($url)
  142. . "\" class=\"button-to\"><div>"
  143. . $this->tag("input", $html_options) . "</div></form>";
  144. }
  145.  
  146. /**
  147. * This tag is deprecated. Combine the link_to and
  148. * AssetTagHelper::image_tag yourself instead, like:
  149. * link_to(image_tag("rss", array("size" => "30x45"),
  150. * array("border" => 0)), "http://www.example.com")
  151. * @todo Document this method
  152. */
  153. function link_image_to($src, $options = array(),
  154. $html_options = array()) {
  155. $image_options = array("src" => (ereg("/", $src) ? $src : "/images/$src"));
  156. if (!ereg(".", $image_options["src"])) $image_options["src"] .= ".png";
  157.  
  158. if (isset($html_options["alt"])) {
  159. $image_options["alt"] = $html_options["alt"];
  160. unset($html_options["alt"]);
  161. } else {
  162. $image_options["alt"] = ucfirst(end(explode("/", $src)));
  163. }
  164.  
  165. if (isset($html_options["size"])) {
  166. $image_options["width"] = current(explode("x", $html_options["size"]));
  167. $image_options["height"] = end(explode("x", $html_options["size"]));
  168. unset($html_options["size"]);
  169. }
  170.  
  171. if (isset($html_options["border"])) {
  172. $image_options["border"] = $html_options["border"];
  173. unset($html_options["border"]);
  174. }
  175.  
  176. if (isset($html_options["align"])) {
  177. $image_options["align"] = $html_options["align"];
  178. unset($html_options["align"]);
  179. }
  180.  
  181. return $this->link_to($this->tag("img", $image_options), $options, $html_options);
  182. }
  183.  
  184. /**
  185. * Generate URL based on current URL and optional arguments
  186. *
  187. * Output a URL with controller and optional action and id.
  188. * The output URL has the same method, host and
  189. * <samp>TRAX_URL_PREFIX</samp> as
  190. * the current URL. Controller is either the current controller
  191. * or a controller specified in $options. Action and ID are
  192. * optionally specified in $options, or omitted. The
  193. * <samp>':id'</samp> option will be ignored if the <samp>':action'</samp>
  194. * option is omitted.
  195. * @param mixed[]
  196. * <ul>
  197. * <li><b>string:</b><br />
  198. * The string value is returned immediately with no
  199. * substitutions.</li>
  200. * <li><b>array:</b>
  201. * <ul>
  202. * <li><samp>':controller'=></samp><i>controller value</i></li>
  203. * <li><samp>':action'=></samp><i>action value</i></li>
  204. * <li><samp>':id'=></samp><i>id value</i></li>
  205. * </ul>
  206. * </ul>
  207. * @return string
  208. * @uses controller_path
  209. */
  210. function url_for($options = array()) {
  211. $url_base = null;
  212. $url = array();
  213. $extra_params = array();
  214. if(is_string($options)) {
  215.  
  216. // Argument is a string, just return it
  217. return $options;
  218.  
  219. } elseif(is_array($options)) {
  220.  
  221. // Argument is a (possibly empty) array
  222. // Start forming URL with this host
  223. $url_base = $_SERVER['HTTP_HOST'];
  224. if(substr($url_base, -1) == "/") {
  225. # remove the ending slash
  226. $url_base = substr($url_base, 0, -1);
  227. }
  228.  
  229. // Method is same as was used by the current URL
  230. if($_SERVER['SERVER_PORT'] == 443) {
  231. $url_base = "https://".$url_base;
  232. } else {
  233. $url_base = "http://".$url_base;
  234. }
  235. // Insert value of TRAX_URL_PREFIX (must start with /)
  236. if(!is_null(TRAX_URL_PREFIX)) {
  237. $url_base .= TRAX_URL_PREFIX;
  238. }
  239. // Get controller from $options or $controller_path
  240. if(array_key_exists(":controller", $options)) {
  241. if($controller = $options[":controller"]) {
  242. $url[] = $controller;
  243. }
  244. } else {
  245. $controller = $this->controller_path;
  246. if(substr($controller, 0, 1) == "/") {
  247. # remove the beginning slash
  248. $controller = substr($controller, 1);
  249. }
  250. $url[] = $controller;
  251. }
  252.  
  253. // If controller found, get action from $options
  254. if(count($url)) {
  255. if(array_key_exists(":action", $options)) {
  256. if($action = $options[":action"]) {
  257. $url[] = $action;
  258. }
  259. }
  260. }
  261.  
  262. // If controller and action found, get id from $actions
  263. if(count($url) > 1) {
  264. if(array_key_exists(":id", $options)) {
  265. if(is_object($options[":id"])) {
  266. if($id = $options[":id"]->id) {
  267. $url[] = $id;
  268. }
  269. } else {
  270. if($id = $options[":id"]) {
  271. $url[] = $id;
  272. }
  273. }
  274. }
  275. }
  276. #if(count($options)) {
  277. # foreach($options as $key => $value) {
  278. # if(!strstr($key, ":")) {
  279. # $extra_params[$key] = $value;
  280. # }
  281. # }
  282. #}
  283. }
  284. if(count($url) && substr($url_base,-1) != "/") {
  285. $url_base .= "/";
  286. }
  287. return $url_base . implode("/", $url)
  288. . (count($extra_params)
  289. ? "?".http_build_query($extra_params) : null);
  290. }
  291.  
  292. }
  293.  
  294. /**
  295. * Make a new UrlHelper object and call its link_to() method
  296. * @uses UrlHelper::link_to()
  297. */
  298. function link_to($name, $options = array(), $html_options = array()) {
  299. $url_helper = new UrlHelper();
  300. return $url_helper->link_to($name, $options, $html_options);
  301. }
  302.  
  303. /**
  304. * Make a new UrlHelper object and call its link_image_to() method
  305. * @uses UrlHelper::link_image_to()
  306. */
  307. function link_image_to($src, $options = array(), $html_options = array()) {
  308. $url_helper = new UrlHelper();
  309. return $url_helper->link_image_to($src, $options, $html_options);
  310. }
  311.  
  312. /**
  313. * Make a new UrlHelper object and call its button_to() method
  314. * @uses UrlHelper::button_to()
  315. */
  316. function button_to($name, $options = array(), $html_options = null) {
  317. $url_helper = new UrlHelper();
  318. return $url_helper->button_to($name, $options, $html_options);
  319. }
  320.  
  321. /**
  322. * Make a new UrlHelper object and call its url_for() method
  323. * @uses UrlHelper::url_for()
  324. */
  325. function url_for($options = array()) {
  326. $url_helper = new UrlHelper();
  327. return $url_helper->url_for($options);
  328. }
  329.  
  330. // -- set Emacs parameters --
  331. // Local variables:
  332. // tab-width: 4
  333. // c-basic-offset: 4
  334. // c-hanging-comment-ender-p: nil
  335. // indent-tabs-mode: nil
  336. // End:
  337. ?>

Documentation generated on Thu, 04 May 2006 19:48:05 -0600 by phpDocumentor 1.3.0RC4