AJAX.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Group                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: The Horde Team <http://www.horde.org>                       |
00020 // |          Jason Rust <jrust@codejanitor.com>                          |
00021 // |          Dan Allen <dan@mojavelinux.com>                             |
00022 // +----------------------------------------------------------------------+
00023 
00024 // }}}
00025 // {{{ class FF_Output_AJAX
00026 
00036 // }}}
00037 class FF_Output_AJAX extends FF_Output {
00038     // {{{ properties
00039 
00044     var $_xml = '';
00045 
00050     var $_nodes = array();
00051 
00052     // }}}
00053     // {{{ display()
00054 
00061     function display()
00062     {
00063         $this->_renderMessages();
00064         foreach ($this->_nodes as $s_key => $val) {
00065             if (is_array($val) && count($val) == 1) {
00066                 $this->_xml .= $this->arrayToXML(current($val), $s_key, key($val));
00067             }
00068             else {
00069                 $this->_xml .= $this->arrayToXML(array($s_key => $val));
00070             }
00071         }
00072 
00073         header('Content-Type: text/xml');
00074         print '<?xml version="1.0" encoding="ISO-8859-1"?>';
00075         print '<ajax>';
00076         print $this->_xml;
00077         print '</ajax>';
00078     }
00079 
00080     // }}}
00081     // {{{ addNode()
00082 
00094     function addNode($in_key, $in_value, $in_nodeName = null)
00095     {
00096         if (is_null($in_nodeName)) {
00097             $this->_nodes[$in_key] = $in_value;
00098         }
00099         else {
00100             $this->_nodes[$in_key] = array($in_nodeName => $in_value);
00101         }
00102     }
00103 
00104     // }}}
00105     // {{{ arrayToXML()
00106 
00116     function arrayToXML($in_array, $in_topNode = null, $in_nodeName = 'el', $in_level = 1) 
00117     {
00118         $s_xml = '';
00119         if ($in_level == 1 && !is_null($in_topNode)) {
00120             $s_xml .= "<$in_topNode>";
00121         }
00122 
00123         foreach ($in_array as $key => $value) {
00124             if (is_array($value)) {
00125                 // Here we handle integer keys
00126                 // NOTE: We don't handle mixed int/string arrays
00127                 if (isset($value[0])) {
00128                     $s_xml .= $this->arrayToXML($value, $in_topNode, $key, $in_level);
00129                 }
00130                 else {
00131                     if (is_int($key)) {
00132                         $key = $in_nodeName;
00133                     }
00134 
00135                     $s_xml .= "<$key>";
00136                     $s_xml .= $this->arrayToXML($value, $key, $in_nodeName, $in_level + 1);
00137                     $s_xml .= "</$key>";
00138                 }
00139             }
00140             else {
00141                 // Remove control characters -- breaks XML
00142                 $value = preg_replace('/[[:cntrl:]]/', '', $value);
00143                 if (trim($value) == '') {
00144                     $s_xml .= "<$key />";
00145                 }
00146                 else if (htmlspecialchars($value) != $value) {
00147                     $s_xml .= "<$key><![CDATA[$value]]></$key>";
00148                 } 
00149                 else {
00150                     $s_xml .= "<$key>$value</$key>";
00151                 }
00152             } 
00153         }
00154 
00155         if ($in_level == 1 && !is_null($in_topNode)) {
00156             $s_xml .= "</$in_topNode>";
00157         }
00158 
00159         return $s_xml;
00160     }
00161 
00162     // }}}
00163     // {{{ setMessage()
00164 
00177     function setMessage($in_message, $in_mode = FASTFRAME_NORMAL_MESSAGE, $in_top = false)
00178     {
00179         parent::setMessage($in_message, $in_mode, $in_top);
00180         // If it's an error then we assume the operation failed, so send out the error code.
00181         if ($in_mode == FASTFRAME_ERROR_MESSAGE) {
00182             header('HTTP/1.0 500 Unsuccessful Action');
00183         }
00184     }
00185 
00186     // }}}
00187     // {{{ _renderMessages()
00188 
00195     function _renderMessages()
00196     {
00197         $a_messages = array();
00198         foreach ($this->messages as $a_message) {
00199             $a_messages[] = array('txt' => $a_message[0], 'type' => $a_message[1]);
00200         }
00201 
00202         $this->addNode('msgs', $a_messages, 'msg');
00203     }
00204 
00205     // }}}
00206 }
00207 ?>

Generated on Fri Jun 23 11:38:14 2006 for FastFrame by  doxygen 1.4.4