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: 0.5.4
  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: 0.5.4
  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.      *  Combine jsLibraries into a single require and remove duplicates
  44.      */
  45.     var $combineJsIncludes = false;
  46.  
  47.     /**
  48.      * Include all needed libraries, stubs, and set defaultServer
  49.      *
  50.      * @return    string 
  51.      */
  52.     function setupAJAX(
  53.     {
  54.         $libs = array(0=>array());
  55.         $combinedLibs = array();
  56.  
  57.         $this->jsLibraries = array_unique($this->jsLibraries);
  58.         foreach($this->jsLibraries as $library{
  59.             if (is_array($library)) {
  60.                 $library array_unique($library);
  61.                 $combinedLibs array_merge($combinedLibs,$library);
  62.                 $libs[implode(',',$library);
  63.             }
  64.             else {
  65.                 $libs[0][$library;
  66.             }
  67.         }
  68.         $libs[0implode(',',$libs[0]);
  69.  
  70.         $sep '?';
  71.         if (strstr($this->serverUrl,'?')) {
  72.             $sep '&';
  73.         }
  74.  
  75.         $ret '';
  76.         if ($this->combineJsIncludes == true{
  77.             $list implode(',',$combinedLibs);
  78.             $ret .= "<script type='text/javascript' src='{$this->serverUrl}{$sep}client={$list}'></script>\n";
  79.         } 
  80.         else {
  81.             foreach($libs as $list) {
  82.                 $ret .= "<script type='text/javascript' src='{$this->serverUrl}{$sep}client={$list}'></script>\n";
  83.             }
  84.         }
  85.  
  86.         if (count($this->stubs> 0{
  87.             $stubs = implode(',',$this->stubs);
  88.             $ret .= "<script type='text/javascript' src='{$this->serverUrl}{$sep}stub={$stubs}'></script>\n";
  89.         }
  90.         $ret .= $this->encloseInScript('HTML_AJAX.defaultServerUrl = '.$this->escape($this->serverUrl));
  91.         return $ret;
  92.     }
  93.  
  94.     /**
  95.      * Create a custom Loading message
  96.      *
  97.      * @param string    $body    HTML body of the loading div
  98.      * @param string    $class    CSS class of the div
  99.      * @param string    $style    style tag of the loading div
  100.      */
  101.     function loadingMessage($body, $class = 'HTML_AJAX_Loading', 
  102.             $style = 'position: absolute; top: 0; right: 0; background-color: red; width: 80px; padding: 4px; display: none') 
  103.     {
  104.         return "<div id='HTML_AJAX_LOADING' class='{$class}' style=\"{$style}\">{$body}</div>\n";
  105.     }
  106.  
  107.     /**
  108.      * Update the contents of an element using ajax
  109.      *
  110.      * @param string    $id    id of the element to update
  111.      * @param string|array    $update    Either a url to update with or a array like array('class','method')
  112.      * @param string    $type    replace or append
  113.      * @param boolean    $enclose
  114.      */
  115.     function updateElement($id, $update, $type, $enclose = false) {
  116.         if (is_array($update)) {
  117.             $updateStr = "";
  118.             $comma = '';
  119.             foreach($update as $item) {
  120.                 $updateStr .= $comma.$this->escape($item);
  121.                 $comma ',';
  122.             }
  123.         }
  124.         else {
  125.             $updateStr = $this->escape($update);
  126.         }
  127.  
  128.         $ret = "HTML_AJAX.{$type}(".$this->escape($id).",{$updateStr});\n";
  129.         if ($enclose) {
  130.             $ret = $this->encloseInScript($ret);
  131.         }
  132.         return $ret;
  133.     }
  134.  
  135.     /**
  136.      * Escape a string and add quotes allowing it to be a javascript paramater
  137.      *
  138.      * @param string    $input
  139.      * @return string
  140.      * @todo do something here besides a quick hack
  141.      */
  142.     function escape($input) {
  143.         return "'".addslashes($input)."'";
  144.     }
  145.  
  146.     /**
  147.      * Enclose a string in a script block
  148.      *
  149.      * @param string    $input
  150.      * @return string
  151.      */
  152.     function encloseInScript($input) {
  153.         return '<script type="text/javascript">'.$input."</script>\n";
  154.     }
  155.  
  156.     /**
  157.      * Generate a JSON String
  158.      *
  159.      * @param string    $input
  160.      * @return string
  161.      */
  162.     function jsonEncode($input) {
  163.         require_once 'HTML/AJAX/Serializer/JSON.php';
  164.  
  165.         $s = new HTML_AJAX_Serializer_JSON();
  166.         return $s->serialize($input);
  167.     }
  168.  
  169.     /**
  170.      * Check the request headers to see if this is an AJAX request
  171.      *
  172.      * @return boolean
  173.      */
  174.     function isAJAX() {
  175.         if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  176.             return true;
  177.         }
  178.         return false;
  179.     }
  180. }
  181. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

Documentation generated on Fri, 04 Apr 2008 18:30:16 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.