theme.inc 7.09 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
3

4
/**
Dries's avatar
 
Dries committed
5 6 7 8
 * Basic theme
 *
 * @package theme system
 */
9

Dries's avatar
 
Dries committed
10

Dries's avatar
 
Dries committed
11
function theme_help($section) {
Dries's avatar
 
Dries committed
12

Dries's avatar
 
Dries committed
13
  $ouptout = "";
Dries's avatar
 
Dries committed
14

Dries's avatar
 
Dries committed
15 16 17 18
  switch ($section) {
    case 'admin/system/themes#description':
      $output = t("The base theme");
      break;
Dries's avatar
 
Dries committed
19 20
  }

Dries's avatar
 
Dries committed
21 22
  return $output;
}
Dries's avatar
 
Dries committed
23

Dries's avatar
 
Dries committed
24 25
class BaseTheme {
}
Dries's avatar
 
Dries committed
26

Dries's avatar
 
Dries committed
27 28
function theme_header($title = "") {
  global $base_url;
Dries's avatar
 
Dries committed
29

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

  print $output;
  theme("box", t("Navigation"), @implode("<br />", link_page()));
  theme_blocks("all", $this);
  print "</td><td style=\"vertical-align: top;\">";
}
Dries's avatar
 
Dries committed
42

Dries's avatar
 
Dries committed
43 44 45
function theme_links($links, $delimiter = " | ") {
  return implode($delimiter, $links);
}
Dries's avatar
 
Dries committed
46

Dries's avatar
 
Dries committed
47 48 49
function theme_image($name) {
  return "misc/$name";
}
Dries's avatar
 
Dries committed
50

Dries's avatar
 
Dries committed
51 52 53
function theme_breadcrumb($breadcrumb) {
  print "<div class=\"breadcrumb\">". implode($breadcrumb, " &raquo; ") ."</div>";
}
Dries's avatar
 
Dries committed
54

Dries's avatar
 
Dries committed
55 56 57
function theme_node($node, $main) {
  if (module_exist("taxonomy")) {
    $terms = taxonomy_link("taxonomy terms", $node);
Dries's avatar
 
Dries committed
58 59
  }

Dries's avatar
 
Dries committed
60 61 62 63
  $output = "<h2>$node->title</h2> by ". format_name($node);

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

Dries's avatar
 
Dries committed
66 67 68 69 70
  if ($main && $node->teaser) {
    $output .= $node->teaser;
  }
  else {
    $output .= $node->body;
Dries's avatar
 
Dries committed
71 72
  }

Dries's avatar
 
Dries committed
73 74
  if ($links = link_node($node, $main)) {
    $output .= "<br />[ ". theme("links", $links) ." ]";
Dries's avatar
 
Dries committed
75
  }
Dries's avatar
 
Dries committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  $output .= "<hr />";

  print $output;
}

function theme_box($subject, $content, $region = "main") {
  $output = "<h2>$subject</h2><p>$content</p>";
  print $output;
}

/**
 * Render a 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, ...).
 */
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>";
  print $output;
}
Dries's avatar
 
Dries committed
107

Dries's avatar
 
Dries committed
108 109 110 111 112 113
function theme_footer() {
  $output = "</td></tr></table>";
  $output .= theme_footer();
  $output .= "</body></html>";
  print $output;
}
Kjartan's avatar
Changes  
Kjartan committed
114

Dries's avatar
 
Dries committed
115 116 117 118
/**
 * Return a marker.  Used to indicate new comments or required form
 * fields.
 */
Dries's avatar
 
Dries committed
119
function theme_mark() {
Dries's avatar
 
Dries committed
120
  return "<span class=\"marker\">*</span>";
Dries's avatar
 
Dries committed
121 122
}

Dries's avatar
 
Dries committed
123 124 125
/**
 * Return a formatted array of items.
 */
Dries's avatar
 
Dries committed
126
function theme_item_list($items = array(), $title = NULL) {
Dries's avatar
 
Dries committed
127
  $output .= "<div class=\"item-list\">";
Dries's avatar
 
Dries committed
128
  if (isset($title)) {
Dries's avatar
 
Dries committed
129
    $output .= "<h3>$title</h3>";
Dries's avatar
 
Dries committed
130 131
  }

Dries's avatar
 
Dries committed
132
  if (isset($items)) {
Dries's avatar
 
Dries committed
133
    $output .= "<ul>";
Dries's avatar
 
Dries committed
134
    foreach ($items as $item) {
Dries's avatar
 
Dries committed
135
      $output .= "<li>$item</li>";
Dries's avatar
 
Dries committed
136
    }
Dries's avatar
 
Dries committed
137
    $output .= "</ul>";
Dries's avatar
 
Dries committed
138
  }
Dries's avatar
 
Dries committed
139
  $output .= "</div>";
Dries's avatar
 
Dries committed
140 141 142
  return $output;
}

Dries's avatar
 
Dries committed
143 144 145
/**
 * Return an error message.
 */
Dries's avatar
 
Dries committed
146
function theme_error($message) {
Dries's avatar
 
Dries committed
147
  return "<div class=\"error\">$message</div>";
Dries's avatar
 
Dries committed
148 149
}

Dries's avatar
 
Dries committed
150
function theme_list($refresh = 0) {
151
  static $list;
Dries's avatar
 
Dries committed
152

Dries's avatar
 
Dries committed
153 154 155 156
  if ($refresh) {
    unset($list);
  }

157 158
  if (!$list) {
    $list = array();
Dries's avatar
 
Dries committed
159
    $result = db_query("SELECT * FROM {system} where type = 'theme' AND status = '1' ORDER BY name");
160
    while ($theme = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
161 162 163
      if (file_exists($theme->filename)) {
        $list[$theme->name] = $theme;
      }
164
    }
Dries's avatar
 
Dries committed
165
  }
166 167 168 169

  return $list;
}

Dries's avatar
 
Dries committed
170
function theme_head($main = 0) {
Dries's avatar
 
Dries committed
171
  global $base_url;
Dries's avatar
 
Dries committed
172
  $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
Dries's avatar
 
Dries committed
173 174 175 176
  $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
177
  $head = module_invoke_all("head", $main);
Dries's avatar
 
Dries committed
178 179
  $output .= implode($head, "\n");
  return $output;
Dries's avatar
 
Dries committed
180 181
}

Dries's avatar
 
Dries committed
182 183 184
/**
 * Execute hook _footer() which is run at the end of the page right
 * before the </body> tag
Dries's avatar
 
Dries committed
185
 */
Dries's avatar
 
Dries committed
186
function theme_closure($main = 0) {
Dries's avatar
 
Dries committed
187 188 189 190
  $footer = module_invoke_all("footer", $main);
  return implode($footer, "\n");
}

191 192 193 194 195
function theme_init() {
  global $user;

  $themes = theme_list();
  $name = $user->theme ? $user->theme : variable_get("theme_default", 0);
Dries's avatar
 
Dries committed
196

197 198
  if (is_object($themes[$name])) {
    include_once($themes[$name]->filename);
Dries's avatar
 
Dries committed
199 200 201 202 203 204
    $class = "Theme_$name";
    $instance =& new $class();
    $instance->path = dirname($themes[$name]->filename);
  }
  else {
    $instance =& new BaseTheme;
Dries's avatar
 
Dries committed
205
  }
206

Dries's avatar
 
Dries committed
207 208
  $instance->theme = $name;

Dries's avatar
 
Dries committed
209
  return $instance;
Dries's avatar
 
Dries committed
210 211
}

Dries's avatar
 
Dries committed
212
/**
Dries's avatar
 
Dries committed
213
 * Render blocks available for (global) $user and $region calling $theme->block($block).
Dries's avatar
 
Dries committed
214
 *
Dries's avatar
 
Dries committed
215
 * @param   $region   main|left|right
Dries's avatar
 
Dries committed
216 217
 */
function theme_blocks($region) {
Dries's avatar
 
Dries committed
218
  global $user;
Dries's avatar
 
Dries committed
219

Dries's avatar
 
Dries committed
220
  $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
221

Dries's avatar
 
Dries committed
222 223 224 225 226
  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']) {
        theme('block', (object) $block);
227
      }
Dries's avatar
 
Dries committed
228 229 230
    }
  }
}
231

232
function theme() {
Dries's avatar
 
Dries committed
233
  global $theme;
Dries's avatar
 
Dries committed
234

Dries's avatar
 
Dries committed
235
  $args = func_get_args();
Dries's avatar
 
Dries committed
236

Dries's avatar
 
Dries committed
237 238
  $function = array_shift($args);

Dries's avatar
 
Dries committed
239 240 241 242 243 244
  $name = $theme->theme;

  if (function_exists($name ."_". $function)) {
    return call_user_func_array($name ."_". $function, $args);
  }
  else if (method_exists($theme, $function)) {
Dries's avatar
 
Dries committed
245
    return call_user_method_array($function, $theme, $args);
Dries's avatar
 
Dries committed
246 247 248 249
      // temporary fall-back; can be removed as soon the $theme-object is no more
  }
  else if (function_exists("theme_". $function)) {
    return call_user_func_array("theme_". $function, $args);
Dries's avatar
 
Dries committed
250
  }
Dries's avatar
 
Dries committed
251 252
  else {
    return call_user_func_array($function, $args);
Dries's avatar
 
Dries committed
253
      // temporary fall-back; can be removed as soon the $theme-object is no more
Dries's avatar
 
Dries committed
254 255 256
  }
}

Dries's avatar
 
Dries committed
257
/**
Dries's avatar
 
Dries committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
 * 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
 */
function theme_onload_attribute($theme_onloads = array()) {
  if (!is_array($theme_onloads)) {
    $theme_onloads = array($theme_onloads);
  }
  // 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) . "\"";
  }
  return;
}

Dries's avatar
 
Dries committed
274
?>