first view of the results of a survey
[platal.git] / classes / platalpage.php
index 0fe8ec6..2550d62 100644 (file)
 
 require_once 'smarty/libs/Smarty.class.php';
 
-class PlatalPage extends Smarty
+abstract class PlatalPage extends Smarty
 {
     private $_page_type;
     private $_tpl;
     private $_errors;
     private $_failure;
+    private $_jsonVars;
 
     // {{{ function PlatalPage()
 
@@ -46,14 +47,12 @@ class PlatalPage extends Smarty
 
         $this->compile_check = !empty($globals->debug);
 
-        $this->_page_type = $type;
-        $this->_tpl       = $tpl;
+        $this->changeTpl($tpl, $type);
         $this->_errors    = array();
+        $this->_jsonVars  = array();
         $this->_failure   = false;
 
         $this->register_prefilter('at_to_globals');
-        $this->register_prefilter('trimwhitespace');
-        $this->register_prefilter('form_force_encodings');
         $this->addJsLink('xorg.js');
     }
 
@@ -68,6 +67,16 @@ class PlatalPage extends Smarty
     }
 
     // }}}
+    // {{{ function raw()
+
+    public function raw()
+    {
+        global $globals;
+        $this->assign('globals', $globals);
+        return $this->fetch($this->_tpl);
+    }
+
+    // }}}
     // {{{ function _run()
 
     protected function _run($skin)
@@ -76,10 +85,16 @@ class PlatalPage extends Smarty
 
         session_write_close();
 
+        $this->register_prefilter('trimwhitespace');
+        $this->register_prefilter('form_force_encodings');
         $this->assign('xorg_errors', $this->_errors);
         $this->assign('xorg_failure', $this->_failure);
         $this->assign('globals', $globals);
 
+        if (Env::has('json') && count($this->_jsonVars)) {
+            return $this->jsonDisplay();
+        }
+        
         if (Env::v('display') == 'light') {
             $this->_page_type = SIMPLE;
         } elseif (Env::v('display') == 'raw') {
@@ -104,6 +119,9 @@ class PlatalPage extends Smarty
         $this->register_outputfilter('hide_emails');
         $this->addJsLink('wiki.js');
         header("Accept-Charset: utf-8");
+        if (Env::v('forceXml')) {
+            header("Content-Type: text/xml; charset=utf-8");
+        }
 
         if (!$globals->debug) {
             error_reporting(0);
@@ -145,6 +163,8 @@ class PlatalPage extends Smarty
         exit;
     }
 
+    abstract public function run();
+
     // }}}
     // {{{ function nb_errs()
 
@@ -209,6 +229,42 @@ class PlatalPage extends Smarty
     }
 
     // }}}
+    // {{{ function jsonDisplay
+    protected function jsonDisplay()
+    {
+        header("Content-type: text/javascript; charset=utf-8");
+        array_walk_recursive($this->_jsonVars, "escape_xorgDB");
+        $jsonbegin = Env::v('jsonBegin');
+        $jsonend = Env::v('jsonEnd');
+        if (Env::has('jsonVar')) {
+            $jsonbegin = Env::v('jsonVar').' = ';
+            $jsonend = ';';
+        } elseif (Env::has('jsonFunc')) {
+            $jsonbegin = Env::v('jsonFunc').'(';
+            $jsonend = ');';
+        }
+        echo $jsonbegin, json_encode($this->_jsonVars), $jsonend;
+        exit;
+    }
+    // }}}
+    // {{{ function jsonAssign
+    public function jsonAssign($var, $value)
+    {
+        $this->_jsonVars[$var] = $value;
+    }
+
+    // }}}
+}
+
+function escape_xorgDB(&$item, $key)
+{
+    if (is_a($item, 'XOrgDBIterator')) {
+        $expanded = array();
+        while ($a = $item->next()) {
+            $expanded[] = $a;
+        }
+        $item = $expanded;
+    }
 }
 
 // {{{ function escape_html ()
@@ -224,10 +280,10 @@ class PlatalPage extends Smarty
 function escape_html($string)
 {
     if (is_string($string)) {
-       $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
-       return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/u", "&amp;", strtr(pl_entities($string), $transtbl));
+       $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
+           return strtr($string, $transtbl);
     } else {
-       return $string;
+           return $string;
     }
 }