Fixes vim mode line.
[banana.git] / banana / page.inc.php
index 40fd970..fe6d56a 100644 (file)
@@ -20,12 +20,16 @@ class BananaPage extends Smarty
     protected $killed  = array();
     protected $actions = array();
 
+    protected $mode;
+    protected $json_params = array();
+
     public $css = '';
 
-    public function __construct()
+    public function __construct($mode = null)
     {
         parent::Smarty();
 
+        $this->mode          = strtolower($mode);
         $this->compile_check = Banana::$debug_smarty;
         $this->template_dir  = dirname(__FILE__) . '/templates/';
         $this->compile_dir   = Banana::$spool_root . '/templates_c/';
@@ -63,6 +67,17 @@ class BananaPage extends Smarty
         return true;
     }
 
+    /** Assign a variable in the page.
+     */
+    public function assign($var, $value)
+    {
+        if ($this->mode === 'json') {
+            $this->json_params[$var] = $value;
+        } else {
+            parent::assign($var, $value);
+        }
+    }
+
     /** Register an action to show on banana page
      * @param action_code HTML code of the action
      * @param pages ARRAY pages where to show the action (null == every pages)
@@ -148,6 +163,9 @@ class BananaPage extends Smarty
      */
     public function run()
     {
+        if ($this->mode === 'json') {
+            return json_encode($this->json_params);
+        }
         $tpl = $this->prepare();
         if (!isset($this->pages[$this->page])) {
             $this->trig(_b_('La page demandée n\'existe pas'));
@@ -236,7 +254,7 @@ class BananaPage extends Smarty
      *
      * smarty funciton : {url param1=... param2=...}
      */
-    public function makeUrl(array $params, &$smarty = null)
+    public function makeUrl(array $params, $smarty = null)
     {
         if (function_exists('hook_makeLink')
                 && $res = hook_makeLink($params)) {
@@ -274,14 +292,14 @@ class BananaPage extends Smarty
      *
      * Smarty function : {link param1=... param2=...}
      */
-    public function makeLink(array $params, &$smarty = null)
+    public function makeLink(array $params, $smarty = null)
     {
         $catch = array('text', 'popup', 'class', 'accesskey', 'style');
         foreach ($catch as $key) {
             ${$key} = isset($params[$key]) ? $params[$key] : null;
             unset($params[$key]);
         }
-        $link = $this->makeUrl($params, &$smarty);
+        $link = $this->makeUrl($params, $smarty);
         if (is_null($text)) {
             $text = $link;
         }
@@ -318,7 +336,7 @@ class BananaPage extends Smarty
      *
      * Smarty function: {img img=... alt=... [height=...] [width=...]}
      */
-    public function makeImg(array $params, &$smarty = null)
+    public function makeImg(array $params, $smarty = null)
     {
         $catch = array('img', 'alt', 'height', 'width');
         foreach ($catch as $key) {
@@ -377,7 +395,7 @@ class BananaPage extends Smarty
      *
      * Smarty function : {imglink img=... alt=... [param1=...]}
      */
-    public function makeImgLink(array $params, &$smarty = null)
+    public function makeImgLink(array $params, $smarty = null)
     {
         if (!isset($params['popup'])) {
             $params['popup'] = @$params['alt'];
@@ -406,7 +424,7 @@ class BananaPage extends Smarty
 
 // {{{  function banana_trimwhitespace
 
-function banana_trimwhitespace($source, &$smarty)
+function banana_trimwhitespace($source, $smarty)
 {
     $tags = array('script', 'pre', 'textarea');
 
@@ -436,5 +454,5 @@ function rss_date($t)
 
 // }}}
 
-// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
+// vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
 ?>