From 951b553a9887df92d93ecc42e7e83ca568e26aae Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 9 Nov 2003 23:27:22 +0000 Subject: - Committed stage 2 of the theme system improvements! Patch by CodeMonkeyX. --- includes/common.inc | 3 +- includes/pager.inc | 10 +- includes/theme.inc | 356 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 243 insertions(+), 126 deletions(-) (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index 9df7cc320..dc8a942b8 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1186,6 +1186,5 @@ module_init(); $locale = locale_init(); // initialize theme: -$theme = theme_init(); - +$theme = init_theme(); ?> diff --git a/includes/pager.inc b/includes/pager.inc index a567499b2..602f6e78a 100644 --- a/includes/pager.inc +++ b/includes/pager.inc @@ -28,7 +28,7 @@ function pager_display($tags = "", $limit = 10, $element = 0, $type = "default", * * @see pager_display */ -function pager_display_default($tags = "", $limit = 10, $element = 0, $attributes = array()) { +function theme_pager_display_default($tags = "", $limit = 10, $element = 0, $attributes = array()) { global $pager_total; if ($pager_total[$element] > $limit) { $output .= "
"; @@ -51,13 +51,13 @@ function pager_display_default($tags = "", $limit = 10, $element = 0, $attribute * * @see pager_display */ -function pager_display_simple($tags = "", $limit = 10, $element = 0, $attributes = array()) { +function theme_pager_display_simple($tags = "", $limit = 10, $element = 0, $attributes = array()) { /* ** It's left as an exercise to theme writers to create an alternative ** pager for pager_display_simple(). if your theme does not offer a ** replacement, the theme.inc pager_display_default() is used. */ - return pager_display_default($tags, $limit, $element, $attributes); + return theme_pager_display_default($tags, $limit, $element, $attributes); } /** @@ -68,13 +68,13 @@ function pager_display_simple($tags = "", $limit = 10, $element = 0, $attributes * * @see pager_display */ -function pager_display_admin($tags = "", $limit = 10, $element = 0, $attributes = array()) { +function theme_pager_display_admin($tags = "", $limit = 10, $element = 0, $attributes = array()) { /* ** It's left as an exercise to theme writers to create an alternative ** pager for pager_display_admin(). if your theme does not offer a ** replacement, the pager.inc pager_display_default() is used. */ - return pager_display_default($tags, $limit, $element, $attributes); + return theme_pager_display_default($tags, $limit, $element, $attributes); } /* ******************************************************************* diff --git a/includes/theme.inc b/includes/theme.inc index 7df69fa2d..eadafe75a 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1,29 +1,25 @@ "; $output .= "". $title ? $title : variable_get(site_name, "drupal") .""; $output .= theme_head($main); - $output .= ""; + $output .= ""; $output .= "
"; - print $output; - theme("box", t("Navigation"), @implode("
", link_page())); - theme_blocks("all", $this); - print "
"; + $output .= theme("box", t("Navigation"), @implode("
", link_page())); + $output .= render_blocks("all"); + $output .= "
"; + + return $output; } +/** + 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. +**/ function theme_links($links, $delimiter = " | ") { return implode($delimiter, $links); } +/** + Returns themed image. + + @param $name the \a name of the image file. + + @return a string contraining the \a image output. +**/ function theme_image($name) { return "misc/$name"; } +/** + Returns a themed breadcrumb menu. + + @param $breadcrumb an array containing the breadcrumb links. + + @return a string contraining the \a breadcrumb output. +**/ function theme_breadcrumb($breadcrumb) { - print "
". implode($breadcrumb, " » ") ."
"; + return "
". implode($breadcrumb, " » ") ."
"; } +/** + 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. +**/ function theme_node($node, $main) { if (module_exist("taxonomy")) { $terms = taxonomy_link("taxonomy terms", $node); @@ -60,7 +98,7 @@ function theme_node($node, $main) { $output = "

$node->title

by ". format_name($node); if (count($terms)) { - $output .= " (". theme("links", $terms) .")
"; + $output .= " (". print theme("links", $terms) .")
"; } if ($main && $node->teaser) { @@ -71,58 +109,77 @@ function theme_node($node, $main) { } if ($links = link_node($node, $main)) { - $output .= "
[ ". theme("links", $links) ." ]"; + $output .= "
[ ". print theme("links", $links) ." ]"; } $output .= "
"; - print $output; + return $output; } +/** + 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. +**/ function theme_box($subject, $content, $region = "main") { $output = "

$subject

$content

"; - print $output; + return $output; } /** - * Render a block. - * - * You can style your blocks by defining .block (all blocks), - * .block-module (all blocks of module module), - * and \#block-module-delta (specific block of - * module module with delta delta) 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 module_block("view") - * ($block->subject, $block->content, ...). - */ + Returns a themed block. + + You can style your blocks by defining .block (all blocks), + .block-module (all blocks of module module), + and \#block-module-delta (specific block of + module module with delta delta) 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 module_block("view") ($block->subject, $block->content, ...). + + @return a string contraining the \a box output. +**/ function theme_block($block) { $output = "
module\" id=\"block-$block->module-$block->delta\">"; $output .= "

$block->subject

"; $output .= "
$block->content
"; $output .= "
"; - print $output; + return $output; } +/** + Returns themed page footer. + + @return a string contraining the \a footer output. +**/ function theme_footer() { $output = "
"; $output .= theme_closure(); $output .= ""; - print $output; + return $output; } /** - * Return a marker. Used to indicate new comments or required form - * fields. - */ + Returns themed marker, useful for marking new comments or required form elements. + + @return a string contraining the \a mark output. +**/ function theme_mark() { return "*"; } /** - * Return a formatted array of items. - */ + 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. +**/ function theme_item_list($items = array(), $title = NULL) { $output .= "
"; if (isset($title)) { @@ -141,32 +198,23 @@ function theme_item_list($items = array(), $title = NULL) { } /** - * Return an error message. - */ + Returns themed error message. + + @param $message the error message to be themed. + + @return a string contraining the \a error output. +**/ function theme_error($message) { return "
$message
"; } -function theme_list($refresh = 0) { - static $list; +/** + Execute hook _head which is run at the start of the page, and output should be in the head tags. - if ($refresh) { - unset($list); - } - - 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; - } - } - } - - return $list; -} + @param $main (optional) + @return a string contraining the \a error output. +**/ function theme_head($main = 0) { global $base_url; $output .= ""; @@ -180,95 +228,165 @@ function theme_head($main = 0) { } /** - * Execute hook _footer() which is run at the end of the page right - * before the tag - */ + Execute hook _footer() which is run at the end of the page right + before the tag. + + @param $main (optional) + + @return a string contraining the \a cloasure output. +**/ function theme_closure($main = 0) { $footer = module_invoke_all("footer", $main); return implode($footer, "\n"); } -function theme_init() { - global $user; +/** + 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. - $themes = theme_list(); - $name = $user->theme ? $user->theme : variable_get("theme_default", 0); + @param $theme_onloads (optional) addition onload directives. - if (is_object($themes[$name])) { - include_once($themes[$name]->filename); - $class = "Theme_$name"; - $instance =& new $class(); - $instance->path = dirname($themes[$name]->filename); + @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); } - else { - $instance =& new BaseTheme; + // 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) . "\""; } - - $instance->theme = $name; - - return $instance; + return ""; } /** - * Render blocks available for (global) $user and $region calling $theme->block($block). - * - * @param $region main|left|right - */ -function theme_blocks($region) { + 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) { global $user; $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); + $output = ""; 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); + $output .= theme('block', (object)$block); } } } + return $output; } -function theme() { - global $theme; +/** + Hook Help - returns theme specific help and information. - $args = func_get_args(); + @param section defines the \a section of the help to be returned. - $function = array_shift($args); - - $name = $theme->theme; + @return a string contraining the help output. +**/ +function theme_help($section) { + $ouptout = ""; - if (function_exists($name ."_". $function)) { - return call_user_func_array($name ."_". $function, $args); + switch ($section) { + case 'admin/system/themes#description': + $output = t("The base theme"); + break; } - else if (method_exists($theme, $function)) { - return call_user_method_array($function, $theme, $args); - // temporary fall-back; can be removed as soon the $theme-object is no more + 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); } - else if (function_exists("theme_". $function)) { - return call_user_func_array("theme_". $function, $args); + + 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; + } + } } - else { - return call_user_func_array($function, $args); - // temporary fall-back; can be removed as soon the $theme-object is no more + + 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; } + + return $theme; } /** - * 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); + 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); } - // 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) . "\""; + elseif (function_exists("theme_". $function)){ + return call_user_func_array("theme_". $function, $args); } - return; } +/** @} End of defgroup theme_system **/ ?> -- cgit v1.2.3