2 /********************************************************************************
3 * banana/page.inc.php : class for group lists
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once 'smarty/libs/Smarty.class.php';
12 class BananaPage
extends Smarty
14 private $error = array();
17 private $pages = array();
18 private $actions = array();
20 public function __construct()
24 $this->compile_check
= Banana
::$debug_smarty;
25 $this->template_dir
= dirname(__FILE__
) . '/templates/';
26 $this->compile_dir
= dirname(dirname(__FILE__
)) . '/spool/templates_c/';
27 $this->register_prefilter('banana_trimwhitespace');
31 /** Add an error message
32 * @param message STRING html code of the error to display
34 public function trig($message)
36 $this->error
[] = $message;
39 /** Kill the current page (generate an error message and skip page generation)
40 * @param message STRING html code of the error message to display
41 @ @return XHTML code of the page
43 public function kill($message)
45 $this->trig($message);
46 $this->assign('killed', true
);
50 /** Set the current page
51 * @param page STRING page name
53 public function setPage($page)
58 /** Register an action to show on banana page
59 * @param action_code HTML code of the action
60 * @param pages ARRAY pages where to show the action (null == every pages)
61 * @return true if success
63 public function registerAction($action_code, array $pages = null
)
65 $this->actions
[] = array('text' => $action_code, 'pages' => $pages);
69 /** Register a new page
70 * @param name Name of the page
71 * @param text Text for the tab of the page
72 * @param template Template path for the page if null, the page is not handled by banana
73 * @return true if success
75 public function registerPage($name, $text, $template = null
)
77 $this->pages
[$name] = array('text' => $text, 'template' => $template);
81 /** Preparte the page generation
82 * @return template to use
84 protected function prepare()
86 $this->registerPage('subscribe', _b_('Abonnements'), null
);
87 $this->registerPage('forums', _b_('Les forums'), null
);
88 if (!is_null(Banana
::$group)) {
89 $this->registerPage('thread', Banana
::$group, null
);
90 if (!is_null(Banana
::$artid)) {
91 $this->registerPage('message', _b_('Message'), null
);
92 if ($this->page
== 'cancel') {
93 $this->registerPage('cancel', _b_('Annulation'), null
);
94 } elseif ($this->page
== 'new') {
95 $this->registerPage('new', _b_('RĂ©pondre'), null
);
97 } elseif ($this->page
== 'new') {
98 $this->registerPage('new', _b_('Nouveau'), null
);
101 foreach ($this->actions
as $key=>&$action) {
102 if (!is_null($action['pages']) && !in_array($this->page
, $action['pages'])) {
103 unset($this->actions
[$key]);
107 return 'banana-base.tpl';
110 /** Generate XHTML code
112 public function run()
114 $tpl = $this->prepare();
116 $this->assign('group', Banana
::$group);
117 $this->assign('artid', Banana
::$artid);
118 $this->assign('part', Banana
::$part);
119 $this->assign('first', Banana
::$first);
120 $this->assign('action', Banana
::$action);
121 $this->assign('profile', Banana
::$profile);
122 $this->assign('spool', Banana
::$spool);
123 $this->assign('protocole', Banana
::$protocole);
125 $this->register_function('url', array($this, 'makeUrl'));
126 $this->register_function('link', array($this, 'makeLink'));
127 $this->register_function('imglink', array($this, 'makeImgLink'));
128 $this->register_function('img', array($this, 'makeImg'));
130 $this->assign('errors', $this->error
);
131 $this->assign('page', $this->page
);
132 $this->assign('pages', $this->pages
);
133 $this->assign('actions', $this->actions
);
135 if (!Banana
::$debug_smarty) {
136 $error_level = error_reporting(0);
138 $text = $this->fetch($tpl);
139 $text = banana_utf8entities($text);
140 if (!Banana
::$debug_smarty) {
141 error_reporting($error_level);
146 /** Build a URL in Banana
147 * @param params ARRAY location datas
148 * @param smarty OBJECT Smarty instance associated (null if none)
149 * @return URL of the page associated with the given parameters
151 * Usual parameters are :
152 * - group : the box name
153 * - artid : the current message id (index of message-id)
154 * - part : part id to show (may be a content-id, xface or a mime-type for text)
155 * - first : first linear-index to show in spool view
156 * - action: like subscribe, cancel, new
157 * - all others params are allowed, but not parsed by the base implementation of banana
159 * smarty funciton : {url param1=... param2=...}
161 public function makeUrl(array $params, &$smarty = null
)
163 if (function_exists('hook_makeLink')
164 && $res = hook_makeLink($params)) {
167 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
168 $host = $_SERVER['HTTP_HOST'];
169 $file = $_SERVER['PHP_SELF'];
171 if (count($params) != 0) {
173 foreach ($params as $key=>$value) {
174 if (strlen($get) != 1) {
177 $get .= $key . '=' . $value;
182 return $proto . $host . $file . $get;
185 /** Build a link to a Banana page
186 * @param params ARRAY location datas
187 * @param smarty OBJECT Smarty instance associated (null if none)
188 * @return Link to the page associated with the given parameters
190 * Support all @ref makeURL parameters, but catch the following:
191 * - text : if set, defined the text of the link (if not set, the URL is used
192 * - popup : title of the link (showed as a tooltip on most browsers)
193 * - class : specific style class for the markup
194 * - accesskey: keyboard key to trigger the link
195 * None of this parameters is needed
197 * Smarty function : {link param1=... param2=...}
199 public function makeLink(array $params, &$smarty = null
)
201 $catch = array('text', 'popup', 'class', 'accesskey');
202 foreach ($catch as $key) {
203 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
204 unset($params[$key]);
206 $link = $this->makeUrl($params, &$smarty);
207 if (is_null($text)) {
210 if (!is_null($accesskey)) {
211 $popup .= ' (raccourci : ' . $accesskey . ')';
213 if (!is_null($popup)) {
214 $popup = ' title="' . banana_htmlentities($popup) . '"';
216 if (!is_null($class)) {
217 $class = ' class="' . $class . '"';
219 if (!is_null($accesskey)) {
220 $accesskey = ' accesskey="' . $accesskey . '"';
222 return '<a href="' . banana_htmlentities($link) . '"'
223 . $popup . $class . $accesskey
224 . '>' . $text . '</a>';
227 /** Build a link to one of the banana built-in images
228 * @param params ARRAY image datas
229 * @param smarty OBJECT Smarty instance associated (null if none)
232 * Supported parameters are
233 * - img : name of the image (without its extension)
234 * - alt : alternative text
235 * - height and width : dimensions of the images
236 * img and alt are needed
238 * Smarty function: {img img=... alt=... [height=...] [width=...]}
240 public function makeImg(array $params, &$smarty = null
)
242 $catch = array('img', 'alt', 'height', 'width');
243 foreach ($catch as $key) {
244 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
247 if (function_exists('hook_makeImg')
248 && $res = hook_makeImg($img, $alt, $height, $width)) {
252 if (!is_null($width)) {
253 $width = ' width="' . $width . '"';
255 if (!is_null($height)) {
256 $height = ' height="' . $height . '"';
259 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
260 $host = $_SERVER['HTTP_HOST'];
261 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
262 $url = $proto . $host . $file;
264 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
267 /** Build a link with an image as text
268 * @param params ARRAY image and location data
269 * @param smarty OBJECT Smarty instance associated (null if none)
270 * @return an image within an link
272 * All @ref makeImg and @ref makeLink parameters are supported
273 * if text is set, the text will be appended after the image in the link
275 * Smarty function : {imglink img=... alt=... [param1=...]}
277 public function makeImgLink(array $params, &$smarty = null
)
279 $params['alt'] = _b_($params['alt']);
280 if (!isset($params['popup'])) {
281 $params['popup'] = $params['alt'];
283 $img = $this->makeImg($params, $smarty);
284 if (isset($params['text'])) {
285 $img .= ' ' . $params['text'];
287 $params['text'] = $img;
288 return $this->makeLink($params, $smarty);
291 /** Redirect to the page with the given parameter
294 public function redirect(array $params = array())
296 header('Location: ' . $this->makeUrl($params));
300 // {{{ function banana_trimwhitespace
302 function banana_trimwhitespace($source, &$smarty)
304 $tags = array('script', 'pre', 'textarea');
306 foreach ($tags as $tag) {
307 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
308 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
311 // remove all leading spaces, tabs and carriage returns NOT
312 // preceeded by a php close tag.
313 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
315 foreach ($tags as $tag) {
316 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
325 // vim:set et sw=4 sts=4 ts=4: