theme.inc 10.3 KB
Newer Older
Dries Buytaert's avatar
   
Dries Buytaert committed
1
<?php
2
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
3
  Theme System - controls the output of Drupal.
4

Dries Buytaert's avatar
   
Dries Buytaert committed
5
6
  The theme system allows for nearly all output of the Drupal system to be
  customized by user themes.
Dries Buytaert's avatar
   
Dries Buytaert committed
7

Dries Buytaert's avatar
   
Dries Buytaert committed
8
  @package theme_system
Dries Buytaert's avatar
   
Dries Buytaert committed
9

Dries Buytaert's avatar
   
Dries Buytaert committed
10
11
12
  @defgroup theme_system
  @{
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
13

Dries Buytaert's avatar
   
Dries Buytaert committed
14
/* $Id$ */
Dries Buytaert's avatar
   
Dries Buytaert committed
15

Dries Buytaert's avatar
   
Dries Buytaert committed
16
17
/**
  Returns the theme header.
Dries Buytaert's avatar
   
Dries Buytaert committed
18

Dries Buytaert's avatar
   
Dries Buytaert committed
19
  @param $title (optional) override the page title.
Dries Buytaert's avatar
   
Dries Buytaert committed
20

Dries Buytaert's avatar
   
Dries Buytaert committed
21
22
  @return a string contraining the \a header output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
23
24
function theme_header($title = "") {
  global $base_url;
Dries Buytaert's avatar
   
Dries Buytaert committed
25

Dries Buytaert's avatar
   
Dries Buytaert committed
26
27
28
29
  $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  $output .= "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
  $output .= "<head><title>". $title ? $title : variable_get(site_name, "drupal") ."</title>";
  $output .= theme_head($main);
Dries Buytaert's avatar
   
Dries Buytaert committed
30
  $output .= "</head><body style=\"background-color: #fff; color: #000;\"". theme("onload_attribute"). "\">";
Dries Buytaert's avatar
   
Dries Buytaert committed
31
32
  $output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\"><tr><td style=\"vertical-align: top; width: 170px;\">";

Dries Buytaert's avatar
   
Dries Buytaert committed
33
34
35
36
37
  $output .= theme("box", t("Navigation"), @implode("<br />", link_page()));
  $output .= render_blocks("all");
  $output .= "</td><td style=\"vertical-align: top;\">";

  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
38
}
Dries Buytaert's avatar
   
Dries Buytaert committed
39

Dries Buytaert's avatar
   
Dries Buytaert committed
40
41
42
43
44
45
46
47
/**
  Returns themed set of links.

  @param $links an array of \a links to be themed.
  @param $delimiter (optional) \a delimiter used to seperate the links.

  @return a string contraining the \a links output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
48
49
50
function theme_links($links, $delimiter = " | ") {
  return implode($delimiter, $links);
}
Dries Buytaert's avatar
   
Dries Buytaert committed
51

Dries Buytaert's avatar
   
Dries Buytaert committed
52
53
54
55
56
57
58
/**
  Returns themed image.

  @param $name the \a name of the image file.

  @return a string contraining the \a image output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
59
60
61
function theme_image($name) {
  return "misc/$name";
}
Dries Buytaert's avatar
   
Dries Buytaert committed
62

Dries Buytaert's avatar
   
Dries Buytaert committed
63
64
65
66
67
68
69
/**
  Returns a themed breadcrumb menu.

  @param $breadcrumb an array containing the breadcrumb links.

  @return a string contraining the \a breadcrumb output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
70
function theme_breadcrumb($breadcrumb) {
Dries Buytaert's avatar
   
Dries Buytaert committed
71
  return "<div class=\"breadcrumb\">". implode($breadcrumb, " &raquo; ") ."</div>";
Dries Buytaert's avatar
   
Dries Buytaert committed
72
}
Dries Buytaert's avatar
   
Dries Buytaert committed
73

Dries Buytaert's avatar
   
Dries Buytaert committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
  Returns themed node.

  The passed $node object provides a all relevant information for displaying a node:
  \li \c $node->nid
  \li \c $node->type i.e. story, blog, forum.
  \li \c $node->title
  \li \c $node->created a unix timestamp.
  \li \c $node->teaser
  \li \c $node->body
  \li \c $node->changed a unix timestamp.
  \li \c $node->uid the id of the poster.
  \li \c $node->username the username of the poster.

  @param $node the \a node to be themed.
  @param $main

  @return a string contraining the \a node output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
93
94
95
function theme_node($node, $main) {
  if (module_exist("taxonomy")) {
    $terms = taxonomy_link("taxonomy terms", $node);
Dries Buytaert's avatar
   
Dries Buytaert committed
96
97
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
98
99
100
  $output = "<h2>$node->title</h2> by ". format_name($node);

  if (count($terms)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
101
    $output .= " <small>(". print theme("links", $terms) .")</small><br />";
Dries Buytaert's avatar
   
Dries Buytaert committed
102
103
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
104
105
106
107
108
  if ($main && $node->teaser) {
    $output .= $node->teaser;
  }
  else {
    $output .= $node->body;
Dries Buytaert's avatar
   
Dries Buytaert committed
109
110
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
111
  if ($links = link_node($node, $main)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
112
    $output .= "<br />[ ". print theme("links", $links) ." ]";
Dries Buytaert's avatar
   
Dries Buytaert committed
113
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
114
115
  $output .= "<hr />";

Dries Buytaert's avatar
   
Dries Buytaert committed
116
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
117
118
}

Dries Buytaert's avatar
   
Dries Buytaert committed
119
120
121
122
123
124
125
126
127
/**
  Returns themed box.

  @param $subject the \a subject of the box.
  @param $content the \a content of the box.
  @param $requion the \a region of the box.

  @return a string contraining the \a box output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
128
129
function theme_box($subject, $content, $region = "main") {
  $output = "<h2>$subject</h2><p>$content</p>";
Dries Buytaert's avatar
   
Dries Buytaert committed
130
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
131
132
133
}

/**
Dries Buytaert's avatar
   
Dries Buytaert committed
134
135
136
137
138
139
140
141
142
143
144
145
  Returns a themed block.

  You can style your blocks by defining .block (all blocks),
  .block-<i>module</i> (all blocks of module <i>module</i>),
  and \#block-<i>module</i>-<i>delta</i> (specific block of
  module <i>module</i> with delta <i>delta</i>) in your
  theme's CSS.

  @param $block object "indexed with" fields from database table 'blocks' ($block->module, $block->delta, $block->region, ...) and fields returned by <i>module</i>_block("view") ($block->subject, $block->content, ...).

  @return a string contraining the \a box output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
146
147
148
149
150
function theme_block($block) {
  $output  = "<div class=\"block block-$block->module\" id=\"block-$block->module-$block->delta\">";
  $output .= " <h3>$block->subject</h3>";
  $output .= " <div class=\"content\">$block->content</div>";
  $output .= "</div>";
Dries Buytaert's avatar
   
Dries Buytaert committed
151
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
152
}
Dries Buytaert's avatar
   
Dries Buytaert committed
153

Dries Buytaert's avatar
   
Dries Buytaert committed
154
155
156
157
158
/**
  Returns themed page footer.

  @return a string contraining the \a footer output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
159
160
function theme_footer() {
  $output = "</td></tr></table>";
Dries Buytaert's avatar
   
Dries Buytaert committed
161
  $output .= theme_closure();
Dries Buytaert's avatar
   
Dries Buytaert committed
162
  $output .= "</body></html>";
Dries Buytaert's avatar
   
Dries Buytaert committed
163
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
164
}
Kjartan Mannes's avatar
Changes    
Kjartan Mannes committed
165

Dries Buytaert's avatar
   
Dries Buytaert committed
166
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
167
168
169
170
  Returns themed marker, useful for marking new comments or required form elements.

  @return a string contraining the \a mark output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
171
function theme_mark() {
Dries Buytaert's avatar
   
Dries Buytaert committed
172
  return "<span class=\"marker\">*</span>";
Dries Buytaert's avatar
   
Dries Buytaert committed
173
174
}

Dries Buytaert's avatar
   
Dries Buytaert committed
175
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
176
177
178
179
180
181
182
  Returns themed list of items.

  @param $items (optional) an array of the items to be displayed in a list.
  @param $title (optional) the title of the list.

  @return a string contraining the \a list output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
183
function theme_item_list($items = array(), $title = NULL) {
Dries Buytaert's avatar
   
Dries Buytaert committed
184
  $output .= "<div class=\"item-list\">";
Dries Buytaert's avatar
   
Dries Buytaert committed
185
  if (isset($title)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
186
    $output .= "<h3>$title</h3>";
Dries Buytaert's avatar
   
Dries Buytaert committed
187
188
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
189
  if (isset($items)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
190
    $output .= "<ul>";
Dries Buytaert's avatar
   
Dries Buytaert committed
191
    foreach ($items as $item) {
Dries Buytaert's avatar
   
Dries Buytaert committed
192
      $output .= "<li>$item</li>";
Dries Buytaert's avatar
   
Dries Buytaert committed
193
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
194
    $output .= "</ul>";
Dries Buytaert's avatar
   
Dries Buytaert committed
195
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
196
  $output .= "</div>";
Dries Buytaert's avatar
   
Dries Buytaert committed
197
198
199
  return $output;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
200
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
201
202
203
204
205
206
  Returns themed error message.

  @param $message the error message to be themed.

  @return a string contraining the \a error output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
207
function theme_error($message) {
Dries Buytaert's avatar
   
Dries Buytaert committed
208
  return "<div class=\"error\">$message</div>";
Dries Buytaert's avatar
   
Dries Buytaert committed
209
210
}

Dries Buytaert's avatar
   
Dries Buytaert committed
211
212
/**
  Execute hook _head which is run at the start of the page, and output should be in the head tags.
Dries Buytaert's avatar
   
Dries Buytaert committed
213

Dries Buytaert's avatar
   
Dries Buytaert committed
214
  @param $main (optional)
215

Dries Buytaert's avatar
   
Dries Buytaert committed
216
217
  @return a string contraining the \a error output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
218
function theme_head($main = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
219
  global $base_url;
Dries Buytaert's avatar
   
Dries Buytaert committed
220
  $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
Dries Buytaert's avatar
   
Dries Buytaert committed
221
222
223
224
  $output .= "<base href=\"$base_url/\" />\n";
  $output .= "<style type=\"text/css\">\n";
  $output .= "@import url(misc/drupal.css);\n";
  $output .= "</style>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
225
  $head = module_invoke_all("head", $main);
Dries Buytaert's avatar
   
Dries Buytaert committed
226
227
  $output .= implode($head, "\n");
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
228
229
}

Dries Buytaert's avatar
   
Dries Buytaert committed
230
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
231
232
233
234
235
236
237
  Execute hook _footer() which is run at the end of the page right
  before the </body> tag.

  @param $main (optional)

  @return a string contraining the \a cloasure output.
**/
Dries Buytaert's avatar
   
Dries Buytaert committed
238
function theme_closure($main = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
239
240
241
242
  $footer = module_invoke_all("footer", $main);
  return implode($footer, "\n");
}

Dries Buytaert's avatar
   
Dries Buytaert committed
243
244
245
/**
  Call _onload hook in all modules to enable modules to insert javascript
  that will get run once the page has been loaded by the browser.
246

Dries Buytaert's avatar
   
Dries Buytaert committed
247
  @param $theme_onloads (optional) addition onload directives.
Dries Buytaert's avatar
   
Dries Buytaert committed
248

Dries Buytaert's avatar
   
Dries Buytaert committed
249
250
251
252
253
  @return a string contraining the \a onload output.
**/
function theme_onload_attribute($theme_onloads = array()) {
  if (!is_array($theme_onloads)) {
    $theme_onloads = array($theme_onloads);
Dries Buytaert's avatar
   
Dries Buytaert committed
254
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
255
256
257
258
259
  // Merge theme onloads (javascript rollovers, image preloads, etc.)
  // with module onloads (htmlarea, etc.)
  $onloads = array_merge(module_invoke_all("onload"), $theme_onloads);
  if (count($onloads)) {
    return " onload=\"" . implode("; ", $onloads) . "\"";
Dries Buytaert's avatar
   
Dries Buytaert committed
260
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
261
  return "";
Dries Buytaert's avatar
   
Dries Buytaert committed
262
263
}

Dries Buytaert's avatar
   
Dries Buytaert committed
264
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
265
266
267
268
269
270
271
  Render blocks available for (global) $user and $region calling $theme->block($block).

  @param   $region   main|left|right

  @return a string contraining the \a blocks output.
 **/
function render_blocks($region) {
Dries Buytaert's avatar
   
Dries Buytaert committed
272
  global $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
273

Dries Buytaert's avatar
   
Dries Buytaert committed
274
  $result = db_query("SELECT * FROM {blocks} WHERE (status = '1' OR custom = '1') ". ($region != "all" ? "AND region = %d " : "") ."ORDER BY weight, module", $region == "left" ? 0 : 1);
Dries Buytaert's avatar
   
Dries Buytaert committed
275

Dries Buytaert's avatar
   
Dries Buytaert committed
276
  $output = "";
Dries Buytaert's avatar
   
Dries Buytaert committed
277
278
279
280
  while ($result && ($block = db_fetch_array($result))) {
    if ((($block['status'] && (!$user->uid || !$block['custom'])) || ($block['custom'] && $user->block[$block['module']][$block['delta']])) && (!$block['path'] || preg_match($block['path'], str_replace("?q=", "", request_uri())))) {
      $block = array_merge($block, module_invoke($block['module'], 'block', 'view', $block['delta']));
      if ($block['content']) {
Dries Buytaert's avatar
   
Dries Buytaert committed
281
        $output .= theme('block', (object)$block);
282
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
283
284
    }
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
285
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
286
}
287

Dries Buytaert's avatar
   
Dries Buytaert committed
288
289
/**
  Hook Help - returns theme specific help and information.
Dries Buytaert's avatar
   
Dries Buytaert committed
290

Dries Buytaert's avatar
   
Dries Buytaert committed
291
  @param section defines the \a section of the help to be returned.
Dries Buytaert's avatar
   
Dries Buytaert committed
292

Dries Buytaert's avatar
   
Dries Buytaert committed
293
294
295
296
  @return a string contraining the help output.
**/
function theme_help($section) {
  $ouptout = "";
Dries Buytaert's avatar
   
Dries Buytaert committed
297

Dries Buytaert's avatar
   
Dries Buytaert committed
298
299
300
301
  switch ($section) {
    case 'admin/system/themes#description':
      $output = t("The base theme");
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
302
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  return $output;
}

/**
  Provides a list of currently avalible themes.

  @param $refresh

  @return an array of the currently avalible themes.
**/
function list_themes($refresh = 0) {
  static $list;

  if ($refresh) {
    unset($list);
Dries Buytaert's avatar
   
Dries Buytaert committed
318
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
319
320
321
322
323
324
325
326
327

  if (!$list) {
    $list = array();
    $result = db_query("SELECT * FROM {system} where type = 'theme' AND status = '1' ORDER BY name");
    while ($theme = db_fetch_object($result)) {
      if (file_exists($theme->filename)) {
        $list[$theme->name] = $theme;
      }
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
328
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

  return $list;
}

/**
  Initialized the theme system.

  @return the name of the currently selected theme.
**/
function init_theme() {
  global $user;

  $themes = list_themes();
  $name = $user->theme ? $user->theme : variable_get("theme_default", 0);

  $theme->path = "";
  $theme->name = "";

  if (is_object($themes[$name])) {
    include_once($themes[$name]->filename);
    $theme->path = dirname($themes[$name]->filename);
    $theme->name = $name;
Dries Buytaert's avatar
   
Dries Buytaert committed
351
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
352
353

  return $theme;
Dries Buytaert's avatar
   
Dries Buytaert committed
354
355
}

Dries Buytaert's avatar
   
Dries Buytaert committed
356
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  Returns the path to the currently selected theme.

  @return the path to the the currently selected theme.
**/
function path_to_theme() {
  global $theme;
  return $theme->path;
}

/**
  External interface of the theme system to all other modules, and core files.

  All requests for themed functions must go through this function. It examines
  the request and routes it to the appropriate theme function. If the current
  theme does not implement the requested function, then the base theme function
  is called.
  Example: \verbatim $header_text = theme("header"); \endverbatim

  @return the path to the the currently selected theme.
**/
function theme() {
  global $theme;

  $args = func_get_args();
  $function = array_shift($args);

  if (($theme->name != "") && (function_exists($theme->name ."_". $function))) {
    return call_user_func_array($theme->name ."_". $function, $args);
Dries Buytaert's avatar
   
Dries Buytaert committed
385
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
386
387
  elseif (function_exists("theme_". $function)){
    return call_user_func_array("theme_". $function, $args);
Dries Buytaert's avatar
   
Dries Buytaert committed
388
389
390
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
391
/**  @} End of defgroup theme_system **/
Dries Buytaert's avatar
   
Dries Buytaert committed
392
?>