theme.inc 6.89 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
 */
Kjartan's avatar
Changes  
Kjartan committed
9
class BaseTheme {
10 11
  var $background = "#ffffff";
  var $foreground = "#000000";
Dries's avatar
 
Dries committed
12

13
  function system($field) {
14 15 16
    $system["name"] = "Basic theme";
    $system["author"] = "Drupal";
    $system["description"] = "Basic theme. Lynx friendly";
17 18 19 20

    return $system[$field];
  }

21
  function header($title = "") {
Dries's avatar
 
Dries committed
22 23
    global $base_url;

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

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

  }

Dries's avatar
 
Dries committed
37
  function links($links, $delimiter = " | ") {
38
    return implode($delimiter, $links);
Dries's avatar
 
Dries committed
39
  }
Dries's avatar
 
Dries committed
40

Dries's avatar
 
Dries committed
41 42
  function image($name) {
    return "misc/$name";
Dries's avatar
 
Dries committed
43
  }
Dries's avatar
 
Dries committed
44

Dries's avatar
 
Dries committed
45 46
  function breadcrumb($breadcrumb) {
    print "<div class=\"breadcrumb\">". implode($breadcrumb, " &raquo; ") ."</div>";
Dries's avatar
 
Dries committed
47 48
  }

Dries's avatar
 
Dries committed
49
  function node($node, $main) {
Dries's avatar
 
Dries committed
50 51
    if (module_exist("taxonomy")) {
      $terms = taxonomy_link("taxonomy terms", $node);
52
    }
Dries's avatar
 
Dries committed
53

Dries's avatar
 
Dries committed
54
    $output = "<b>$node->title</b> by ". format_name($node) ."<br />";
Dries's avatar
 
Dries committed
55

56 57 58
    if (count($terms)) {
      $output .= "<small>(". $this->links($terms) .")</small><br />";
    }
Dries's avatar
 
Dries committed
59

Dries's avatar
 
Dries committed
60
    if ($main && $node->teaser) {
Dries's avatar
 
Dries committed
61
      $output .= $node->teaser;
Dries's avatar
 
Dries committed
62 63
    }
    else {
Dries's avatar
 
Dries committed
64
      $output .= $node->body;
Dries's avatar
 
Dries committed
65
    }
66 67
    if ($links = link_node($node, $main)) {
      $output .= "<br />[ ". $this->links($links) ." ]";
Dries's avatar
 
Dries committed
68 69 70 71
    }
    $output .= "<hr />";

    print $output;
Dries's avatar
 
Dries committed
72 73
  }

Dries's avatar
 
Dries committed
74
  function box($subject, $content, $region = "main") {
Dries's avatar
 
Dries committed
75
    $output = "<p><b>$subject</b><br />$content</p>";
Dries's avatar
 
Dries committed
76 77 78
    print $output;
  }

Dries's avatar
 
Dries committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  /**
   * 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 block($block) {
    $output  = "<div class=\"block block-$block->module\" id=\"block-$block->module-$block->delta\">";
    $output .= " <div class=\"subject\">$block->subject</div>";
    $output .= " <div class=\"content\">$block->content</div>";
    $output .= "</div>";
    print $output;
Dries's avatar
 
Dries committed
99 100
  }

Dries's avatar
 
Dries committed
101
  function footer() {
Kjartan's avatar
Kjartan committed
102
    $output = "</td></tr></table>";
Dries's avatar
 
Dries committed
103
    $output .= theme_footer();
Dries's avatar
 
Dries committed
104 105 106 107
    $output .= "</body></html>";
    print $output;
  }

Dries's avatar
 
Dries committed
108
}  // End of BaseTheme class //
Kjartan's avatar
Changes  
Kjartan committed
109

Dries's avatar
 
Dries committed
110 111 112 113
/**
 * Return a marker.  Used to indicate new comments or required form
 * fields.
 */
Dries's avatar
 
Dries committed
114
function theme_mark() {
Dries's avatar
 
Dries committed
115
  return "<span class=\"marker\">*</span>";
Dries's avatar
 
Dries committed
116 117
}

Dries's avatar
 
Dries committed
118 119 120
/**
 * Return a formatted array of items.
 */
Dries's avatar
 
Dries committed
121
function theme_item_list($items = array(), $title = NULL) {
Dries's avatar
 
Dries committed
122
  $output .= "<div class=\"item-list\">";
Dries's avatar
 
Dries committed
123
  if (isset($title)) {
Dries's avatar
 
Dries committed
124
    $output .= "<div class=\"title\">$title</div>";
Dries's avatar
 
Dries committed
125 126
  }

Dries's avatar
 
Dries committed
127
  if (isset($items)) {
Dries's avatar
 
Dries committed
128
    $output .= "<ul>";
Dries's avatar
 
Dries committed
129
    foreach ($items as $item) {
Dries's avatar
 
Dries committed
130
      $output .= "<li>$item</li>";
Dries's avatar
 
Dries committed
131
    }
Dries's avatar
 
Dries committed
132
    $output .= "</ul>";
Dries's avatar
 
Dries committed
133
  }
Dries's avatar
 
Dries committed
134
  $output .= "</div>";
Dries's avatar
 
Dries committed
135 136 137
  return $output;
}

Dries's avatar
 
Dries committed
138 139 140
/**
 * Return an error message.
 */
Dries's avatar
 
Dries committed
141
function theme_error($message) {
Dries's avatar
 
Dries committed
142
  return "<div class=\"error\">$message</div>";
Dries's avatar
 
Dries committed
143 144
}

Dries's avatar
 
Dries committed
145
function theme_list($refresh = 0) {
146
  static $list;
Dries's avatar
 
Dries committed
147

Dries's avatar
 
Dries committed
148 149 150 151
  if ($refresh) {
    unset($list);
  }

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

  return $list;
}

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

Dries's avatar
 
Dries committed
177 178 179
/**
 * Execute hook _footer() which is run at the end of the page right
 * before the </body> tag
Dries's avatar
 
Dries committed
180 181 182 183 184 185
 */
function theme_footer($main = 0) {
  $footer = module_invoke_all("footer", $main);
  return implode($footer, "\n");
}

186 187 188 189 190
function theme_init() {
  global $user;

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

192 193
  if (is_object($themes[$name])) {
    include_once($themes[$name]->filename);
Dries's avatar
 
Dries committed
194 195 196 197 198 199
    $class = "Theme_$name";
    $instance =& new $class();
    $instance->path = dirname($themes[$name]->filename);
  }
  else {
    $instance =& new BaseTheme;
Dries's avatar
 
Dries committed
200
  }
201

Dries's avatar
 
Dries committed
202
  return $instance;
Dries's avatar
 
Dries committed
203 204
}

Dries's avatar
 
Dries committed
205
/**
Dries's avatar
 
Dries committed
206
 * Render blocks available for (global) $user and $region calling $theme->block($block).
Dries's avatar
 
Dries committed
207
 *
Dries's avatar
 
Dries committed
208
 * @param   $region   main|left|right
Dries's avatar
 
Dries committed
209 210
 */
function theme_blocks($region) {
Dries's avatar
 
Dries committed
211
  global $user;
Dries's avatar
 
Dries committed
212

Dries's avatar
 
Dries committed
213
  $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
214

Dries's avatar
 
Dries committed
215 216 217 218 219
  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);
220
      }
Dries's avatar
 
Dries committed
221 222 223
    }
  }
}
224

225
function theme() {
Dries's avatar
 
Dries committed
226 227
  global $theme;
  $args = func_get_args();
Dries's avatar
 
Dries committed
228

Dries's avatar
 
Dries committed
229 230 231 232
  $function = array_shift($args);

  if (method_exists($theme, $function)) {
    return call_user_method_array($function, $theme, $args);
Dries's avatar
 
Dries committed
233
  }
Dries's avatar
 
Dries committed
234 235 236 237 238
  else {
    return call_user_func_array($function, $args);
  }
}

Dries's avatar
 
Dries committed
239
/**
Dries's avatar
 
Dries committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
 * 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
256
?>