Add basic support for a json output.
[banana.git] / banana / page.inc.php
index 2d8abf6..1d62313 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)
     {
         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)
@@ -112,7 +127,17 @@ class BananaPage extends Smarty
         if (!is_null(Banana::$group)) {
             $this->registerPage('thread', Banana::$group, null);
             if (!is_null(Banana::$artid)) {
-                $this->registerPage('message', _b_('Message'), null);
+                if (Banana::$spool) {
+                    $subject = Banana::$spool->overview[Banana::$artid]->subject;
+                } else if (Banana::$message) {
+                    $subject = Banana::$message->getHeaderValue('subject');
+                } else {
+                    $subject = _b_('Message');
+                }
+                if (strlen($subject) > 30) {
+                    $subject = substr($subject, 0, 30) . '…';
+                }
+                $this->registerPage('message', $subject, null);
                 if ($this->page == 'cancel') {
                     $this->registerPage('cancel', _b_('Annulation'), null);
                 } elseif ($this->page == 'new') {
@@ -138,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'));
@@ -226,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)) {
@@ -264,7 +292,7 @@ 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) {
@@ -308,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) {
@@ -367,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'];
@@ -396,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');