3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 require_once 'Smarty.class.php';
24 /** Base class for all of Diogenes' pages. This class is purposefully
25 * kept very generic as it is part of the Diogenes library which is
26 * also used by Lycee-vanGogh.net.
28 class DiogenesCorePage
extends Smarty
{
31 function DiogenesCorePage() {
36 $this->template_dir
= $globals->root
."/templates";
37 $this->config_dir
= $globals->root
."/configs";
38 $this->compile_dir
= $globals->spoolroot
."/templates_c";
40 $this->register_function("extval","diogenes_func_extval");
41 $this->register_function("flags","diogenes_func_flags");
42 $this->register_function("a","diogenes_func_a");
43 $this->register_function("checkbox","diogenes_func_checkbox");
44 $this->register_function("diff","diogenes_func_diff");
45 $this->register_function("menu_item","diogenes_func_menu_item");
46 $this->register_function("tag","diogenes_func_tag");
47 $this->register_function("toolbar","diogenes_func_toolbar");
48 $this->debugging_ctrl
= true
;
51 $this->assign('script_self',$this->script_self());
55 /** Check that $_SESSION['session'] is usable
57 function checkSession()
59 return isset($_SESSION['session']) && is_object($_SESSION['session']);
63 /** Return the current script location.
65 function script_self() {
66 $url = explode("?",$this->script_uri());
71 /** Return the current URI.
73 function script_uri() {
74 return $_SERVER['REQUEST_URI'];
78 /** Returns the URL to a page relative to current location.
85 return $globals->rooturl
.$rel;
91 /** Displays an anchor tag.
94 * +lnk an array containing([href],text,icon)
97 * @param params the function input
99 function diogenes_func_a($params)
105 if (is_array($lnk)) {
108 $icon = isset($lnk[2]) ?
$lnk[2] : '';
113 if (empty($href) && empty($class))
116 // we have either an href or a class
118 .( empty($class) ?
"" : " class=\"$class\"")
119 .( empty($href) ?
"" : " href=\"$href\"")
121 .( empty($icon) ?
$text : "<img src=\"$icon\" alt=\"$text\" title=\"$text\" />" )
126 /** Displays a checkbox.
128 * @param params the function input
130 function diogenes_func_checkbox($params)
134 if (empty($name)) $name = "";
135 if (empty($value)) $value = 1;
136 $checked = (!empty($checked));
138 return "<input type=\"checkbox\"".
139 ($name ?
" name=\"$name\"" : "").
140 ($checked ?
" checked=\"checked\"" : "").
141 " value=\"$value\" />";
145 /** Format some diff lines for output.
149 function diogenes_func_diff($params)
153 if (empty($block) ||
empty($op))
155 $lines=explode("\n",$block);
157 foreach($lines as $line) {
158 switch(substr($line,0,2)) {
174 // strip 2 leading chars
175 $line = substr($line,2);
176 // if necessary, drop trailing newline char
177 if (substr($line,-1) == "\n")
178 $line = substr($line,0,-1);
187 if (!isset($old) ||
($old != $class))
188 $out .= "<div class=\"$class\">";
189 $out .= htmlentities($line, ENT_COMPAT | ENT_HTML401
, "ISO-8859-1");
197 /** Displays a set of external values from a database
199 * @params a set of options read from a database
201 function diogenes_func_extval($params) {
202 global $globals,$diogenes_db_cache;
203 if(empty($diogenes_db_cache)) $diogenes_db_cache = Array();
206 if(empty($table) |
empty($field) |
empty($vtable) |
empty($vjoinid) |
empty($vfield))
209 $cache_id = "$vtable,$vjoinid,$vfield";
211 if(empty($diogenes_db_cache[$cache_id])) {
212 $res = $globals->db
->query("select $vjoinid,$vfield from $vtable order by $vfield");
213 $diogenes_db_cache[$cache_id] = Array();
214 while(list($id,$val) = mysql_fetch_row($res))
215 $diogenes_db_cache[$cache_id][$id] = $val;
219 // if we have a name, display opening select tag
221 return $diogenes_db_cache[$cache_id][$value];
226 $html_out .= "<select name=\"$name\">\n";
227 foreach($diogenes_db_cache[$cache_id] as $id=>$val)
228 $html_out .= " <option value=\"$id\"".
229 ($selected==$id ?
" selected=\"selected\"":"")
230 .">".htmlentities($val, ENT_COMPAT | ENT_HTML401
, "ISO-8859-1")."</option>\n";
231 $html_out .= "</select>\n";
236 /** Displays a set of options read from a database.
238 * @param params the function input
240 function diogenes_func_flags($params)
245 if (empty($table) |
empty($field))
247 if (empty($selected))
250 $res = $globals->db
->query("show columns from $table like '$field'");
251 $set = mysql_fetch_row($res);
254 // examine the type of field
255 if (substr($set,0,5)=="enum(") {
257 $set = substr($set,5);
258 } else if (substr($set,0,4) == "set(") {
260 $set = substr($set,4);
262 return "field neither set nor enum";
266 // if we have a name, display opening select tag
268 $html_out .= "<select name=\"$name".($multi ?
"[]\" multiple=\"multiple\"" : "\"").">\n";
270 $set = ereg_replace('\)$', '', $set);
271 $set = explode(',', $set);
272 for ($vals = explode(',', $selected); list(, $k) = each($vals);) {
276 $countset = count($set);
277 for($j=0; $j < $countset; $j++
) {
278 $subset = substr($set[$j], 1, -1);
279 // Removes automatic MySQL escape format
280 $subset = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $subset));
281 $html_out .= "<option value=\"$subset\""
282 . ((isset($vset[$subset]) && $vset[$subset]) ?
" selected=\"selected\"" : "")
283 . ">".(isset($trans) ?
$trans[$subset] : htmlspecialchars($subset, ENT_COMPAT | ENT_HTML401
, "ISO-8859-1"))."</option>\n";
286 // if we have a name, display closing select tag
288 $html_out .= "</select>\n";
294 /** Displays a menu item.
297 * +item a menu item, that is an array (item_level, item_link, item_text)
299 * @param params the function input
301 function diogenes_func_menu_item($params)
307 $level = array_shift($item);
311 $class = ($level %
2) ?
"odd" : "even";
314 $lnk = array( $item[0] );
315 if ( isset($item[1]) )
316 array_push($lnk, $item[1]);
318 $margin = $level * 20;
319 return "<div class=\"item\" style=\"margin-left: {$margin}px\">".
320 diogenes_func_a(array("lnk"=>$lnk,"class"=>$class))."</div>";
325 /** Displays a generic XHTML tag.
328 * +tag : the type of tag (required)
329 * +props : the tag's properties (optional)
330 * +content : the tag's contents (optional)
334 * +item : associative array containing (tag, props, content)
336 function diogenes_func_tag($params)
340 if (isset($item) && is_array($item))
348 if (is_array($props)) {
349 foreach($props as $key=>$val)
350 $out .= " $key=\"$val\"";
353 $out .= empty($content) ?
" />" : ">$content</$tag>";
359 /** Displays a toolbar from a collection of links.
362 * +lnk a link or an array of links
363 * +class the CSS class for the links
365 * @param params the function input
367 function diogenes_func_toolbar($params)
377 $sep = " | ";
379 foreach($lnk as $mylnk) {
381 $out .= diogenes_func_a(array("lnk"=>$mylnk));
383 $out .= diogenes_func_a(array("lnk"=>$mylnk,"class"=>$class));