[ Index ]
 

Code source de PHPonTrax 2.6.6-svn

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/vendor/trax/action_view/helpers/ -> url_helper.php (source)

   1  <?php
   2  /**
   3   *  File containing the UrlHelper class and support functions
   4   *
   5   *  (PHP 5)
   6   *
   7   *  @package PHPonTrax
   8   *  @version $Id: url_helper.php 240 2006-08-02 09:21:48Z john $
   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          } else {
 170              if(isset($html_options["width"])) {
 171                  $image_options["width"] = $html_options["width"];
 172                  unset($html_options["width"]);            
 173              } elseif(isset($html_options["height"])) {
 174                  $image_options["height"] = $html_options["height"];
 175                  unset($html_options["height"]);            
 176              }
 177          } 
 178          
 179          if (isset($html_options["border"])) {
 180              $image_options["border"] = $html_options["border"];
 181              unset($html_options["border"]);
 182          } else {
 183              $image_options["border"] = 0;        
 184          }
 185  
 186          if (isset($html_options["align"])) {
 187              $image_options["align"] = $html_options["align"];
 188              unset($html_options["align"]);
 189          }
 190  
 191          return $this->link_to($this->tag("img", $image_options), $options, $html_options);
 192      }
 193  
 194      /**
 195       *  Generate URL based on current URL and optional arguments
 196       *
 197       *  Output a URL with controller and optional action and id.
 198       *  The output URL has the same method, host and
 199       *  <samp>TRAX_URL_PREFIX</samp> as 
 200       *  the current URL.  Controller is either the current controller
 201       *  or a controller specified in $options.  Action and ID are
 202       *  optionally specified in $options, or omitted.  The
 203       *  <samp>':id'</samp> option will be ignored if the <samp>':action'</samp>
 204       *  option is omitted.
 205       *  @param mixed[]
 206       *  <ul>
 207       *    <li><b>string:</b><br />
 208       *      The string value is returned immediately with no
 209       *      substitutions.</li>
 210       *    <li><b>array:</b>
 211       *     <ul>
 212       *       <li><samp>':controller'=></samp><i>controller value</i></li>
 213       *       <li><samp>':action'=></samp><i>action value</i></li>
 214       *       <li><samp>':id'=></samp><i>id value</i></li>
 215       *     </ul>
 216       *  </ul>
 217       *  @return string
 218       *  @uses controller_path
 219       */
 220      function url_for($options = array()) {
 221          $url_base = null;
 222          $url = array();
 223          $extra_params = array();
 224          if(is_string($options)) {
 225  
 226              //  Argument is a string, just return it
 227              return $options;
 228  
 229          } elseif(is_array($options)) {
 230  
 231              //  Argument is a (possibly empty) array
 232              //  Start forming URL with this host
 233              $url_base = $_SERVER['HTTP_HOST'];
 234              if(substr($url_base, -1) == "/") {
 235                  # remove the ending slash
 236                  $url_base = substr($url_base, 0, -1);
 237              }           
 238  
 239              //  Method is same as was used by the current URL
 240              if($_SERVER['SERVER_PORT'] == 443) {
 241                  $url_base = "https://".$url_base;
 242              } else {
 243                  $url_base = "http://".$url_base;
 244              }
 245              //  Insert value of Trax::$url_prefix
 246              if(!is_null(Trax::$url_prefix)) {
 247                  $prefix = Trax::$url_prefix;
 248                  if($prefix{0} != "/") {
 249                      $prefix = "/$prefix";
 250                  }
 251                  $url_base .= $prefix;
 252              }
 253              
 254              //  Get controller from $options or $controller_path
 255              if(array_key_exists(":controller", $options)) {
 256                  if($controller = $options[":controller"]) {
 257                      $url[] = $controller; 
 258                  }
 259              } else {
 260                  $controller = $this->controller_path;
 261                  if(substr($controller, 0, 1) == "/") {
 262                      # remove the beginning slash
 263                      $controller = substr($controller, 1);        
 264                  }
 265                  $url[] = $controller;
 266              }
 267  
 268              //  If controller found, get action from $options
 269              if(count($url)) {
 270                  if(array_key_exists(":action", $options)) {
 271                      if($action = $options[":action"]) {
 272                          $url[] = $action;
 273                      }
 274                  } 
 275              }
 276  
 277              //  If controller and action found, get id from $actions
 278              if(count($url) > 1) {
 279                  if(array_key_exists(":id", $options)) {
 280                      if(is_object($options[":id"])) {
 281                          if($id = $options[":id"]->id) {
 282                              $url[] = $id;
 283                          }
 284                      } else {
 285                          if($id = $options[":id"]) {
 286                              $url[] = $id;
 287                          }
 288                      }
 289                  }
 290              }
 291              
 292              if(count($options)) {
 293                  foreach($options as $key => $value) {
 294                      if(!strstr($key, ":")) {
 295                          $extra_params[$key] = $value; 
 296                      }       
 297                  }    
 298              }
 299          }
 300          
 301          if(count($url) && substr($url_base,-1) != "/") {
 302              $url_base .= "/";    
 303          } 
 304          return $url_base . implode("/", $url)
 305              . (count($extra_params)
 306                 ? "?".http_build_query($extra_params) : null);
 307      }    
 308  
 309  }
 310  
 311  /**
 312   *  Make a new UrlHelper object and call its link_to() method
 313   *  @uses UrlHelper::link_to()
 314   */
 315  function link_to($name, $options = array(), $html_options = array()) {
 316      $url_helper = new UrlHelper();
 317      return $url_helper->link_to($name, $options, $html_options);
 318  }
 319  
 320  /**
 321   *  Make a new UrlHelper object and call its link_image_to() method
 322   *  @uses UrlHelper::link_image_to()
 323   */
 324  function link_image_to($src, $options = array(), $html_options = array()) {
 325      $url_helper = new UrlHelper();
 326      return $url_helper->link_image_to($src, $options, $html_options);        
 327  }
 328  
 329  /**
 330   *  Make a new UrlHelper object and call its button_to() method
 331   *  @uses UrlHelper::button_to()
 332   */
 333  function button_to($name, $options = array(), $html_options = null) {
 334      $url_helper = new UrlHelper();
 335      return $url_helper->button_to($name, $options, $html_options);    
 336  }
 337  
 338  /**
 339   *  Make a new UrlHelper object and call its url_for() method
 340   *  @uses UrlHelper::url_for()
 341   */
 342  function url_for($options = array()) {
 343      $url_helper = new UrlHelper();
 344      return $url_helper->url_for($options);
 345  }
 346  
 347  // -- set Emacs parameters --
 348  // Local variables:
 349  // tab-width: 4
 350  // c-basic-offset: 4
 351  // c-hanging-comment-ender-p: nil
 352  // indent-tabs-mode: nil
 353  // End:
 354  ?>


Généré le : Sun Feb 25 20:04:38 2007 par Balluche grâce à PHPXref 0.7