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

4 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;

Kjartan's avatar
Kjartan committed
24
    $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n";
Dries's avatar
 
Dries committed
25
    $output .= "<html><head><title>". $title ? $title : variable_get(site_name, "drupal") ."</title>";
Dries's avatar
 
Dries committed
26
    $output .= "<base href=\"$base_url/\" />";
Dries's avatar
 
Dries committed
27
    $output .= theme_head($main);
Dries's avatar
 
Dries committed
28
    $output .= "</head><body bgcolor=\"$this->background\" text=\"$this->foreground". theme_onload_attribute(). "\">";
Dries's avatar
 
Dries committed
29 30 31
    $output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\"><tr><td valign=\"top\" width=\"170\">";

    print $output;
32
    $this->box(t("Navigation"), @implode("<br />", link_page())); theme_blocks("all", $this);
Dries's avatar
 
Dries committed
33
    print "</td><td valign=\"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
  function node($node, $main) {
Dries's avatar
 
Dries committed
46 47
    if (module_exist("taxonomy")) {
      $terms = taxonomy_link("taxonomy terms", $node);
48
    }
Dries's avatar
 
Dries committed
49

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

52 53 54
    if (count($terms)) {
      $output .= "<small>(". $this->links($terms) .")</small><br />";
    }
Dries's avatar
 
Dries committed
55

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

    print $output;
Dries's avatar
 
Dries committed
68 69
  }

Dries's avatar
 
Dries committed
70
  function box($subject, $content, $region = "main") {
Dries's avatar
 
Dries committed
71
    $output = "<p><b>$subject</b><br />$content</p>";
Dries's avatar
 
Dries committed
72 73 74
    print $output;
  }

Dries's avatar
 
Dries committed
75 76 77 78 79 80
  function block($subject, $content, $region = "main") {
    global $theme;

    $theme->box($subject, $content, $region);
  }

Dries's avatar
 
Dries committed
81
  function footer() {
Kjartan's avatar
Kjartan committed
82
    $output = "</td></tr></table>";
Dries's avatar
 
Dries committed
83
    $output .= theme_footer();
Dries's avatar
 
Dries committed
84 85 86 87
    $output .= "</body></html>";
    print $output;
  }

Kjartan's avatar
Changes  
Kjartan committed
88 89
}

Dries's avatar
 
Dries committed
90 91 92 93 94 95 96 97
function theme_mark() {
  /*
  ** Return a marker.  Used to indicate new comments or required form
  ** fields.
  */
  return "<span style=\"color: red;\">*</span>";
}

Dries's avatar
 
Dries committed
98
function theme_item_list($items = array(), $title = NULL) {
Dries's avatar
 
Dries committed
99 100 101 102
  /*
  ** Return a formatted array of items.
  */

Dries's avatar
 
Dries committed
103
  if (isset($title)) {
Dries's avatar
 
Dries committed
104 105 106
    $output .= "<b>$title</b><br />";
  }

Dries's avatar
 
Dries committed
107 108 109 110
  if (isset($items)) {
    foreach ($items as $item) {
      $output .= "- $item<br />";
    }
Dries's avatar
 
Dries committed
111 112 113 114 115
  }

  return $output;
}

Dries's avatar
 
Dries committed
116 117 118 119 120 121 122
function theme_error($message) {
  /*
  ** Return an error message.
  */
  return "<div style=\"color: red;\">$message</div>";
}

Dries's avatar
 
Dries committed
123
function theme_list($refresh = 0) {
124
  static $list;
Dries's avatar
 
Dries committed
125

Dries's avatar
 
Dries committed
126 127 128 129
  if ($refresh) {
    unset($list);
  }

130 131 132 133
  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)) {
Dries's avatar
 
Dries committed
134 135 136
      if (file_exists($theme->filename)) {
        $list[$theme->name] = $theme;
      }
137
    }
Dries's avatar
 
Dries committed
138
  }
139 140 141 142

  return $list;
}

Dries's avatar
 
Dries committed
143 144
function theme_head($main = 0) {
  $head = module_invoke_all("head", $main);
Dries's avatar
 
Dries committed
145
  return implode($head, "\n");
Dries's avatar
 
Dries committed
146 147
}

Dries's avatar
 
Dries committed
148 149 150 151 152 153 154 155 156
/*
 * Execute hook _footer() which is run at the end of the page right before
 * the </body> tag
 */
function theme_footer($main = 0) {
  $footer = module_invoke_all("footer", $main);
  return implode($footer, "\n");
}

157 158 159 160 161
function theme_init() {
  global $user;

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

163 164
  if (is_object($themes[$name])) {
    include_once($themes[$name]->filename);
Dries's avatar
 
Dries committed
165 166 167 168 169 170
    $class = "Theme_$name";
    $instance =& new $class();
    $instance->path = dirname($themes[$name]->filename);
  }
  else {
    $instance =& new BaseTheme;
Dries's avatar
 
Dries committed
171
  }
172

Dries's avatar
 
Dries committed
173
  return $instance;
Dries's avatar
 
Dries committed
174 175
}

Dries's avatar
 
Dries committed
176 177 178 179 180 181
/**
 * Render blocks available for $user and $region calling $theme->block($region).
 *
 * @param   string  $region   main|left|right
 */
function theme_blocks($region) {
Dries's avatar
 
Dries committed
182
  global $user, $REQUEST_URI;
Dries's avatar
 
Dries committed
183

Dries's avatar
 
Dries committed
184
  $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
185

186
  while ($result && ($block = db_fetch_object($result))) {
Dries's avatar
 
Dries committed
187
    if ((($block->status && (!$user->uid || !$block->custom)) || ($block->custom && $user->block[$block->module][$block->delta])) && (!$block->path || preg_match('/'. str_replace('/', '\/', $block->path) .'/', request_uri()))) {
Dries's avatar
 
Dries committed
188 189
      $block_data = module_invoke($block->module, "block", "view", $block->delta);
      if ($block_data["content"]) {
190
        theme("block", $block_data["subject"], $block_data["content"], $region);
191
      }
Dries's avatar
 
Dries committed
192 193 194
    }
  }
}
195

196
function theme() {
Dries's avatar
 
Dries committed
197 198
  global $theme;
  $args = func_get_args();
Dries's avatar
 
Dries committed
199

Dries's avatar
 
Dries committed
200 201 202 203
  $function = array_shift($args);

  if (method_exists($theme, $function)) {
    return call_user_method_array($function, $theme, $args);
Dries's avatar
 
Dries committed
204
  }
Dries's avatar
 
Dries committed
205 206 207 208 209
  else {
    return call_user_func_array($function, $args);
  }
}

Dries's avatar
 
Dries committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/*
 * 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
227
?>