9113ba1ae0d21faa05324998cf733e4e9ca9f709
[diogenes.git] / include / diogenes / diogenes.core.page.inc.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
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.
10 *
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.
15 *
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
19 */
20
21
22 require_once 'Smarty.class.php';
23
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.
27 */
28 class DiogenesCorePage extends Smarty {
29 /** The constructor.
30 */
31 function DiogenesCorePage() {
32 global $globals;
33
34 // smarty definitions
35 $this->Smarty();
36 $this->template_dir = $globals->root."/templates";
37 $this->config_dir = $globals->root."/configs";
38 $this->compile_dir = $globals->spoolroot."/templates_c";
39
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;
49
50 // smarty assignments
51 $this->assign('script_self',$this->script_self());
52 }
53
54
55 /** Check that $_SESSION['session'] is usable
56 */
57 function checkSession()
58 {
59 return isset($_SESSION['session']) && is_object($_SESSION['session']);
60 }
61
62
63 /** Return the current script location.
64 */
65 function script_self() {
66 $url = explode("?",$this->script_uri());
67 return $url[0];
68 }
69
70
71 /** Return the current URI.
72 */
73 function script_uri() {
74 return $_SERVER['REQUEST_URI'];
75 }
76
77
78 /** Returns the URL to a page relative to current location.
79 *
80 * @param rel
81 */
82 function url($rel) {
83 global $globals;
84
85 return $globals->rooturl.$rel;
86 }
87
88 }
89
90
91 /** Displays an anchor tag.
92 *
93 * Parameters:
94 * +lnk an array containing([href],text,icon)
95 * +class
96 *
97 * @param params the function input
98 */
99 function diogenes_func_a($params)
100 {
101 extract($params);
102 if (empty($lnk))
103 return;
104
105 if (is_array($lnk)) {
106 $text = $lnk[0];
107 $href = $lnk[1];
108 $icon = isset($lnk[2]) ? $lnk[2] : '';
109 } else {
110 $text = $lnk;
111 }
112
113 if (empty($href) && empty($class))
114 $class = "empty";
115
116 // we have either an href or a class
117 return "<a"
118 .( empty($class) ? "" : " class=\"$class\"")
119 .( empty($href) ? "" : " href=\"$href\"")
120 .">"
121 .( empty($icon) ? $text : "<img src=\"$icon\" alt=\"$text\" title=\"$text\" />" )
122 ."</a>";
123 }
124
125
126 /** Displays a checkbox.
127 *
128 * @param params the function input
129 */
130 function diogenes_func_checkbox($params)
131 {
132 extract($params);
133
134 if (empty($name)) $name = "";
135 if (empty($value)) $value = 1;
136 $checked = (!empty($checked));
137
138 return "<input type=\"checkbox\"".
139 ($name ? " name=\"$name\"" : "").
140 ($checked ? " checked=\"checked\"" : "").
141 " value=\"$value\" />";
142 }
143
144
145 /** Format some diff lines for output.
146 *
147 * @param params
148 */
149 function diogenes_func_diff($params)
150 {
151 extract($params);
152
153 if (empty($block) || empty($op))
154 return;
155 $lines=explode("\n",$block);
156 $out=$out2="";
157 foreach($lines as $line) {
158 switch(substr($line,0,2)) {
159 case "> ":
160 if ($op == "a")
161 $class = "add";
162 else
163 $class = "change";
164 break;
165 case "< ":
166 if ($op == "d")
167 $class = "delete";
168 else
169 $class = "change";
170 break;
171 default:
172 $class = "other";
173 }
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);
179
180 if (isset($old)) {
181 if ($old != $class)
182 $out .= "</div>";
183 else
184 $out .= "<br />\n";
185 }
186 if ($line) {
187 if (!isset($old) || ($old != $class))
188 $out .= "<div class=\"$class\">";
189 $out .= htmlentities($line, ENT_COMPAT | ENT_HTML401, "ISO-8859-1");
190 }
191 $old = $class;
192 }
193 return $out;
194 }
195
196
197 /** Displays a set of external values from a database
198 *
199 * @params a set of options read from a database
200 */
201 function diogenes_func_extval($params) {
202 global $globals,$diogenes_db_cache;
203 if(empty($diogenes_db_cache)) $diogenes_db_cache = Array();
204
205 extract($params);
206 if(empty($table) | empty($field) | empty($vtable) | empty($vjoinid) | empty($vfield))
207 return;
208
209 $cache_id = "$vtable,$vjoinid,$vfield";
210
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;
216 }
217
218 $html_out = "";
219 // if we have a name, display opening select tag
220 if (isset($value))
221 return $diogenes_db_cache[$cache_id][$value];
222
223 if(empty($name))
224 return;
225
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";
232
233 return $html_out;
234 }
235
236 /** Displays a set of options read from a database.
237 *
238 * @param params the function input
239 */
240 function diogenes_func_flags($params)
241 {
242 global $globals;
243 extract($params);
244
245 if (empty($table) | empty($field))
246 return;
247 if (empty($selected))
248 $selected = "";
249
250 $res = $globals->db->query("show columns from $table like '$field'");
251 $set = mysql_fetch_row($res);
252 $set = $set[1];
253
254 // examine the type of field
255 if (substr($set,0,5)=="enum(") {
256 $multi = false;
257 $set = substr($set,5);
258 } else if (substr($set,0,4) == "set(") {
259 $multi = true;
260 $set = substr($set,4);
261 } else {
262 return "field neither set nor enum";
263 }
264
265 $html_out = "";
266 // if we have a name, display opening select tag
267 if (!empty($name))
268 $html_out .= "<select name=\"$name".($multi ? "[]\" multiple=\"multiple\"" : "\"").">\n";
269
270 $set = ereg_replace('\)$', '', $set);
271 $set = explode(',', $set);
272 for ($vals = explode(',', $selected); list(, $k) = each($vals);) {
273 $vset[$k] = 1;
274 }
275
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))."</option>\n";
284 }
285
286 // if we have a name, display closing select tag
287 if (!empty($name))
288 $html_out .= "</select>\n";
289
290 return $html_out;
291 }
292
293
294 /** Displays a menu item.
295 *
296 * Parameters:
297 * +item a menu item, that is an array (item_level, item_link, item_text)
298 *
299 * @param params the function input
300 */
301 function diogenes_func_menu_item($params)
302 {
303 extract($params);
304 if (empty($item))
305 return;
306
307 $level = array_shift($item);
308 if ($level == 0)
309 $class = "top";
310 else
311 $class = ($level % 2) ? "odd" : "even";
312
313 // process link
314 $lnk = array( $item[0] );
315 if ( isset($item[1]) )
316 array_push($lnk, $item[1]);
317
318 $margin = $level * 20;
319 return "<div class=\"item\" style=\"margin-left: {$margin}px\">".
320 diogenes_func_a(array("lnk"=>$lnk,"class"=>$class))."</div>";
321
322 }
323
324
325 /** Displays a generic XHTML tag.
326 *
327 * Parameters
328 * +tag : the type of tag (required)
329 * +props : the tag's properties (optional)
330 * +content : the tag's contents (optional)
331 *
332 * OR
333 *
334 * +item : associative array containing (tag, props, content)
335 */
336 function diogenes_func_tag($params)
337 {
338 extract($params);
339
340 if (isset($item) && is_array($item))
341 extract($item);
342
343 if (empty($tag))
344 return;
345
346 $out = "<$tag";
347
348 if (is_array($props)) {
349 foreach($props as $key=>$val)
350 $out .= " $key=\"$val\"";
351 }
352
353 $out .= empty($content) ? " />" : ">$content</$tag>";
354
355 return $out;
356 }
357
358
359 /** Displays a toolbar from a collection of links.
360 *
361 * Parameters:
362 * +lnk a link or an array of links
363 * +class the CSS class for the links
364 *
365 * @param params the function input
366 */
367 function diogenes_func_toolbar($params)
368 {
369 extract($params);
370 if (empty($lnk))
371 return;
372
373 if (!is_array($lnk))
374 $lnk = array($lnk);
375
376 // the separator
377 $sep = "&nbsp;|&nbsp;";
378 $out = $sep;
379 foreach($lnk as $mylnk) {
380 if (empty($class))
381 $out .= diogenes_func_a(array("lnk"=>$mylnk));
382 else
383 $out .= diogenes_func_a(array("lnk"=>$mylnk,"class"=>$class));
384 $out .= $sep;
385 }
386 return $out;
387 }
388
389 ?>