Fixes vim mode line.
[banana.git] / banana / page.inc.php
index 620e3c5..fe6d56a 100644 (file)
@@ -13,19 +13,23 @@ if (@!include_once('Smarty.class.php')) {
 
 class BananaPage extends Smarty
 {
-    private $error = array();
-    private $page  = null;
+    protected $error = array();
+    protected $page  = null;
 
-    private $pages   = array();
-    private $killed  = array();
-    private $actions = array();
+    protected $pages   = array();
+    protected $killed  = array();
+    protected $actions = array();
+
+    protected $mode;
+    protected $json_params = array();
 
     public $css = '';
 
-    public function __construct()
+    public function __construct($mode = null)
     {
-        $this->Smarty();
+        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,11 +67,22 @@ 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)
      * @return true if success
-     */ 
+     */
     public function registerAction($action_code, array $pages = null)
     {
         $this->actions[] = array('text' => $action_code, 'pages' => $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') {
@@ -133,11 +158,14 @@ class BananaPage extends Smarty
 
         return 'banana-base.tpl';
     }
-    
+
     /** Generate XHTML code
      */
     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'));
@@ -182,6 +210,7 @@ class BananaPage extends Smarty
         $this->assign('withtabs'   , Banana::$withtabs);
         $this->assign('feed_format', Banana::$feed_format);
         $this->assign('feed_active', Banana::$feed_active);
+        $this->assign('with_javascript', Banana::$msgshow_javascript);
 
         $this->register_function('url',     array($this, 'makeUrl'));
         $this->register_function('link',    array($this, 'makeLink'));
@@ -214,7 +243,7 @@ class BananaPage extends Smarty
      * @param params ARRAY location datas
      * @param smarty OBJECT Smarty instance associated (null if none)
      * @return URL of the page associated with the given parameters
-     * 
+     *
      * Usual parameters are :
      * - group : the box name
      * - artid : the current message id (index of message-id)
@@ -225,16 +254,16 @@ 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)) {
             return $res;
-        }   
+        }
         $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
         $host  = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
         $file  = $_SERVER['PHP_SELF'];
-    
+
         if (count($params) != 0) {
             $get = '?';
             foreach ($params as $key=>$value) {
@@ -245,7 +274,7 @@ class BananaPage extends Smarty
             }
         } else {
             $get = '';
-        }     
+        }
         return $proto . $host . $file . $get;
     }
 
@@ -263,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;
         }
@@ -307,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) {
@@ -333,7 +362,7 @@ class BananaPage extends Smarty
 
         return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
     }
-    
+
     /** Build a link to one of the banana built-in javascript
      * @param src STRING javascript name
      * @return Javascript tag
@@ -355,7 +384,7 @@ class BananaPage extends Smarty
 
         return '<script type="text/javascript" src="' . $url . '"/></script>';
     }
-    
+
     /** Build a link with an image as text
      * @param params ARRAY image and location data
      * @param smarty OBJECT Smarty instance associated (null if none)
@@ -366,11 +395,11 @@ 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'];
-        }    
+        }
         $img = $this->makeImg($params, $smarty);
         if (isset($params['text'])) {
             $img .= ' ' . $params['text'];
@@ -395,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');
 
@@ -425,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:
 ?>