home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / AJAX / Helper.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.9 KB  |  187 lines

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