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

Source for file Helper.php

Documentation is available at Helper.php

  1. <?php
  2. /**
  3.  * HTML/JavaScript Generation Helper
  4.  *
  5.  * @category   HTML
  6.  * @package    AJAX
  7.  * @author     Joshua Eichorn <josh@bluga.net>
  8.  * @copyright  2005 Joshua Eichorn
  9.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  10.  * @version    Release: @package_version@
  11.  */
  12.  
  13. /**
  14.  * HTML/JavaScript Generation Helper
  15.  *
  16.  * @category   HTML
  17.  * @package    AJAX
  18.  * @author     Joshua Eichorn <josh@bluga.net>
  19.  * @copyright  2005 Joshua Eichorn
  20.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  21.  * @version    Release: @package_version@
  22.  * @link       http://pear.php.net/package/HTML_AJAX
  23.  */
  24. {
  25.     /**
  26.      * URL where an HTML_AJAX_Server instance is serving up clients and taking ajax requests
  27.      */
  28.     var $serverUrl = 'server.php';
  29.  
  30.     /**
  31.      * JS libraries to include
  32.      *
  33.      * @var    array 
  34.      */
  35.     var $jsLibraries = array('Util','Main','Request','HttpClient','Dispatcher','Behavior','Loading','JSON','iframe');
  36.  
  37.     /**
  38.      * Remote class stubs to include
  39.      */
  40.     var $stubs = array();
  41.  
  42.     /**
  43.      * Include all needed libraries, stubs, and set defaultServer
  44.      *
  45.      * @return    string 
  46.      */
  47.     function setupAJAX(
  48.     {
  49.         $libs = array(0=>array());
  50.         foreach($this->jsLibraries as $library{
  51.             if (is_array($library)) {
  52.                 $libs[implode(',',$library);
  53.             }
  54.             else {
  55.                 $libs[0][$library;
  56.             }
  57.         }
  58.         $libs[0implode(',',$libs[0]);
  59.  
  60.         $ret '';
  61.         foreach($libs as $list{
  62.             $ret .= "<script type='text/javascript' src='{$this->serverUrl}?client={$list}'></script>\n";
  63.         }
  64.  
  65.         if (count($this->stubs> 0{
  66.             $stubs = implode(',',$this->stubs);
  67.             $ret .= "<script type='text/javascript' src='{$this->serverUrl}?stub={$stubs}'></script>\n";
  68.         }
  69.         $ret .= $this->encloseInScript('HTML_AJAX.defaultServerUrl = '.$this->escape($this->serverUrl));
  70.         return $ret;
  71.     }
  72.  
  73.     /**
  74.      * Create a custom Loading message
  75.      *
  76.      * @param string    $body    HTML body of the loading div
  77.      * @param string    $class    CSS class of the div
  78.      * @param string    $style    style tag of the loading div
  79.      */
  80.     function loadingMessage($body, $class = 'HTML_AJAX_Loading', 
  81.             $style = 'position: absolute; top: 0; right: 0; background-color: red; width: 80px; padding: 4px; display: none') 
  82.     {
  83.         return "<div id='HTML_AJAX_LOADING' class='{$class}' style=\"{$style}\">{$body}</div>\n";
  84.     }
  85.  
  86.     /**
  87.      * Update the contents of an element using ajax
  88.      *
  89.      * @param string    $id    id of the element to update
  90.      * @param string|array    $update    Either a url to update with or a array like array('class','method')
  91.      * @param string    $type    replace or append
  92.      * @param boolean    $enclose
  93.      */
  94.     function updateElement($id, $update, $type, $enclose = false) {
  95.         if (is_array($update)) {
  96.             $updateStr = "";
  97.             $comma = '';
  98.             foreach($update as $item) {
  99.                 $updateStr .= $comma.$this->escape($item);
  100.                 $comma ',';
  101.             }
  102.         }
  103.         else {
  104.             $updateStr = $this->escape($update);
  105.         }
  106.  
  107.         $ret = "HTML_AJAX.{$type}(".$this->escape($id).",{$updateStr});\n";
  108.         if ($enclose) {
  109.             $ret = $this->encloseInScript($ret);
  110.         }
  111.         return $ret;
  112.     }
  113.  
  114.     /**
  115.      * Escape a string and add quotes allowing it to be a javascript paramater
  116.      *
  117.      * @param string    $input
  118.      * @return string
  119.      * @todo do something here besides a quick hack
  120.      */
  121.     function escape($input) {
  122.         return "'".addslashes($input)."'";
  123.     }
  124.  
  125.     /**
  126.      * Enclose a string in a script block
  127.      *
  128.      * @param string    $input
  129.      * @return string
  130.      */
  131.     function encloseInScript($input) {
  132.         return '<script type="text/javascript">'.$input."</script>\n";
  133.     }
  134.  
  135.     /**
  136.      * Generate a JSON String
  137.      *
  138.      * @param string    $input
  139.      * @return string
  140.      */
  141.     function jsonEncode($input) {
  142.         require_once 'HTML/AJAX/Serializer/JSON.php';
  143.  
  144.         $s = new HTML_AJAX_Serializer_JSON();
  145.         return $s->serialize($input);
  146.     }
  147.  
  148.     /**
  149.      * Check the request headers to see if this is an AJAX request
  150.      *
  151.      * @return boolean
  152.      */
  153.     function isAJAX() {
  154.         if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  155.             return true;
  156.         }
  157.         return false;
  158.     }
  159. }
  160. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

Documentation generated on Sat, 05 May 2007 18:00:13 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.