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

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

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

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

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

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

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

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

Dries's avatar
 
Dries 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's avatar
 
Dries committed
30
  $output .= "</head><body style=\"background-color: #fff; color: #000;\"". theme("onload_attribute"). "\">";
Dries's avatar
 
Dries committed
31 32
  $output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\"><tr><td style=\"vertical-align: top; width: 170px;\">";

Dries's avatar
 
Dries 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's avatar
 
Dries committed
38
}
Dries's avatar
 
Dries committed
39

Dries's avatar
 
Dries 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's avatar
 
Dries committed
48 49 50
function theme_links($links, $delimiter = " | ") {
  return implode($delimiter, $links);
}
Dries's avatar
 
Dries committed
51

Dries's avatar
 
Dries 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's avatar
 
Dries committed
59 60 61
function theme_image($name) {
  return "misc/$name";
}
Dries's avatar
 
Dries committed
62

Dries's avatar
 
Dries 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's avatar
 
Dries committed
70
function theme_breadcrumb($breadcrumb) {
Dries's avatar
 
Dries committed
71
  return "<div class=\"breadcrumb\">". implode($breadcrumb, " &raquo; ") ."</div>";
Dries's avatar
 
Dries committed
72
}
Dries's avatar
 
Dries committed
73

Dries's avatar
 
Dries 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's avatar
 
Dries committed
93 94 95
function theme_node($node, $main) {
  if (module_exist("taxonomy")) {
    $terms = taxonomy_link("taxonomy terms", $node);
Dries's avatar
 
Dries committed
96 97
  }

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

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

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

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

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

Dries's avatar
 
Dries 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's avatar
 
Dries committed
128 129
function theme_box($subject, $content, $region = "main") {
  $output = "<h2>$subject</h2><p>$content</p>";
Dries's avatar
 
Dries committed
130
  return $output;
Dries's avatar
 
Dries committed
131 132 133
}

/**
Dries's avatar
 
Dries 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's avatar
 
Dries 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's avatar
 
Dries committed
151
  return $output;
Dries's avatar
 
Dries committed
152
}
Dries's avatar
 
Dries committed
153

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

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

Dries's avatar
 
Dries committed
166
/**
Dries's avatar
 
Dries 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's avatar
 
Dries committed
171
function theme_mark() {
Dries's avatar
 
Dries committed
172
  return "<span class=\"marker\">*</span>";
Dries's avatar
 
Dries committed
173 174
}

Dries's avatar
 
Dries committed
175
/**
Dries's avatar
 
Dries 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's avatar
 
Dries committed
183
function theme_item_list($items = array(), $title = NULL) {
Dries's avatar
 
Dries committed
184
  $output .= "<div class=\"item-list\">";
Dries's avatar
 
Dries committed
185
  if (isset($title)) {
Dries's avatar
 
Dries committed
186
    $output .= "<h3>$title</h3>";
Dries's avatar
 
Dries committed
187 188
  }

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

Dries's avatar
 
Dries committed
200
/**
Dries's avatar
 
Dries 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's avatar
 
Dries committed
207
function theme_error($message) {
Dries's avatar
 
Dries committed
208
  return "<div class=\"error\">$message</div>";
Dries's avatar
 
Dries committed
209 210
}

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

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

Dries's avatar
 
Dries committed
216 217
  @return a string contraining the \a error output.
**/
Dries's avatar
 
Dries committed
218
function theme_head($main = 0) {
Dries's avatar
 
Dries committed
219
  global $base_url;
Dries's avatar
 
Dries committed
220
  $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
Dries's avatar
 
Dries 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's avatar
 
Dries committed
225
  $head = module_invoke_all("head", $main);
Dries's avatar
 
Dries committed
226 227
  $output .= implode($head, "\n");
  return $output;
Dries's avatar
 
Dries committed
228 229
}

Dries's avatar
 
Dries committed
230
/**
Dries's avatar
 
Dries 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's avatar
 
Dries committed
238
function theme_closure($main = 0) {
Dries's avatar
 
Dries committed
239 240 241 242
  $footer = module_invoke_all("footer", $main);
  return implode($footer, "\n");
}

Dries's avatar
 
Dries 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's avatar
 
Dries committed
247
  @param $theme_onloads (optional) addition onload directives.
Dries's avatar
 
Dries committed
248

Dries's avatar
 
Dries 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's avatar
 
Dries committed
254
  }
Dries's avatar
 
Dries 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's avatar
 
Dries committed
260
  }
Dries's avatar
 
Dries committed
261
  return "";
Dries's avatar
 
Dries committed
262 263
}

Dries's avatar
 
Dries committed
264
/**
Dries's avatar
 
Dries 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's avatar
 
Dries committed
272
  global $user;
Dries's avatar
 
Dries committed
273

Dries's avatar
 
Dries 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's avatar
 
Dries committed
275

Dries's avatar
 
Dries committed
276
  $output = "";
Dries's avatar
 
Dries 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's avatar
 
Dries committed
281
        $output .= theme('block', (object)$block);
282
      }
Dries's avatar
 
Dries committed
283 284
    }
  }
Dries's avatar
 
Dries committed
285
  return $output;
Dries's avatar
 
Dries committed
286
}
287

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

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

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

Dries's avatar
 
Dries committed
298 299 300 301
  switch ($section) {
    case 'admin/system/themes#description':
      $output = t("The base theme");
      break;
Dries's avatar
 
Dries committed
302
  }
Dries's avatar
 
Dries 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's avatar
 
Dries committed
318
  }
Dries's avatar
 
Dries 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's avatar
 
Dries committed
328
  }
Dries's avatar
 
Dries 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's avatar
 
Dries committed
351
  }
Dries's avatar
 
Dries committed
352 353

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

Dries's avatar
 
Dries committed
356
/**
Dries's avatar
 
Dries 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's avatar
 
Dries committed
385
  }
Dries's avatar
 
Dries committed
386 387
  elseif (function_exists("theme_". $function)){
    return call_user_func_array("theme_". $function, $args);
Dries's avatar
 
Dries committed
388 389 390
  }
}

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