drupal/themes/engines/phptemplate/phptemplate.engine

338 lines
12 KiB
Plaintext
Raw Normal View History

<?php
// $Id$
/**
* @file
* Handles integration of templates written in pure php with the Drupal theme system.
*/
function phptemplate_init($template) {
$file = dirname($template->filename) . '/template.php';
if (file_exists($file)) {
include_once($file);
}
}
function phptemplate_templates($directory = 'themes') {
return system_listing('^page\.tpl\.php$', $directory, 'filename');
}
/**
* Execute a template engine call.
*
* Each call to the template engine has two parts. Namely preparing
* the variables, and then doing something with them.
*
* The first step is done by all template engines / themes, the second
* step is dependent on the engine used.
*
* @param $hook
* The name of the theme function being executed.
* @param $variables
* A sequential array of variables passed to the theme function.
* @param $file
* A suggested template file to use. If the file is not found, the default $hook.tpl.php will be used.
* @return
* The HTML generated by the template system.
*/
function _phptemplate_callback($hook, $variables = array(), $file = null) {
$variables = array_merge($variables, _phptemplate_default_variables($hook, $variables));
// Allow specified variables to be overridden
if (function_exists('_phptemplate_variables')) {
$variables = array_merge($variables, _phptemplate_variables($hook, $variables));
}
if ($variables['template_file']) {
$file = $variables['template_file'];
}
if (function_exists('_phptemplate_' . $hook)) {
return call_user_func('_phptemplate_' . $hook, $variables, $file);
}
elseif (function_exists('_phptemplate_default')) {
return call_user_func('_phptemplate_default', $hook, $variables, $file);
}
}
/**
* Adds additional helper variables to all templates.
*
* Counts how many times certain hooks have been called. Sidebar left / right are special cases.
*
* @param $hook
* The name of the theme function being executed.
* @param $variables
* A sequential array of variables passed to the theme function.
*/
function _phptemplate_default_variables($hook, $variables) {
static $count = array();
$count[$hook] = is_int($count[$hook]) ? $count[$hook] : 1;
$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
$variables['id'] = $count[$hook]++;
global $sidebar_indicator;
if ($hook == 'block') {
$count['block_counter'][$sidebar_indicator] = is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1;
$variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even';
$variables['block_id'] = $count['block_counter'][$sidebar_indicator]++;
}
// Tell all templates where they are located.
$variables['directory'] = path_to_theme();
if (drupal_get_path_alias($_GET['q']) == variable_get('site_frontpage', 'node')) {
$variables['is_front'] = true;
}
return $variables;
}
function phptemplate_features() {
return array(
'logo',
'toggle_name',
'toggle_search',
'toggle_slogan',
'toggle_mission',
'toggle_primary_links',
'toggle_secondary_links',
'toggle_node_user_picture',
'toggle_comment_user_picture');
}
/**
* Prepare the values passed to the theme_page function to be passed
* into a pluggable template engine.
*/
function phptemplate_page($content) {
/* Set title and breadcrumb to declared values */
if (file_exists(path_to_theme() . '/favicon.ico')) {
drupal_set_html_head("<link rel=\"shortcut icon\" href=\"" . path_to_theme() . "/favicon.ico\" />\n");
}
if ($_GET['q'] == variable_get('site_frontpage', 'node')) {
$mission = theme_get_setting('mission');
$frontpage = true;
}
$links['primary'] = array();
$links['secondary'] = array();
foreach (array('primary', 'secondary') as $type) {
//Get the data to populate the textfields, if the variable is not an array .. try to parse the old-style link format.
$value = theme_get_setting($type . '_links');
//Get the amount of links to show, possibly expanding if there are more links defined than the count specifies.
$count = variable_get($type . '_link_count', 5);
$count = ($count > sizeof($value['link'])) ? $count : sizeof($value['link']);
if (theme_get_setting('toggle_' . $type . '_links')) {
for ($i =0; $i < $count; $i++) {
unset($attributes);
if (!empty($value['text'][$i])) {
if (!empty($value['description'][$i])) {
$attributes['title'] = $value['description'][$i];
}
$text = $value['text'][$i];
$link = $value['link'][$i];
if (substr($link, 0, 4) == 'http') {
$links[$type][] = '<a href="'. $link .'"'. drupal_attributes($attributes) .'>'. $text .'</a>';
}
else {
$links[$type][] = l($text, $link, $attributes);
}
}
}
}
}
/**
* Populate sidebars.
*/
$layout = "none";
global $sidebar_indicator;
/**
* Sidebar_indicator tells the block counting code to count sidebars seperately.
*/
$sidebar_indicator = 'left';
$sidebar_left = theme("blocks", "left");
if ($sidebar_left != "") {
$layout = "left";
}
$sidebar_indicator = 'right';
$sidebar_right = theme("blocks", "right");
if ($sidebar_right != "") {
$layout = ($layout == "left") ? "both" : "right";
}
$sidebar_indicator = null;
$variables = array(
'head_title' => (drupal_get_title() ? strip_tags(drupal_get_title()) .' | '. variable_get('site_name', 'drupal') : variable_get('site_name', 'drupal') .' | '. variable_get('site_slogan', '')),
'language' => $GLOBALS['locale'],
'site' => variable_get('site_name', 'drupal'),
'head' => drupal_get_html_head(),
'onload_attributes' => theme('onload_attribute'),
'logo' => theme_get_setting('logo'),
'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''),
'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''),
'search_box' => theme_get_setting('toggle_search'),
'search_url' => url('search'),
'search_button_text' => t('search'),
'search_description' => t('Enter the terms you wish to search for.'),
'title' => drupal_get_title(),
'primary_links' => $links['primary'],
'secondary_links' => $links['secondary'],
'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()),
'tabs' => theme('menu_local_tasks'),
'messages' => theme('status_messages'),
'layout' => $layout,
'help' => theme('help'),
'styles' => theme_get_styles(),
'mission' => $mission,
'sidebar_left' => $sidebar_left,
'content' => '<!-- begin content -->' . $content . '<!-- end content -->',
'sidebar_right' => $sidebar_right,
'footer_message' => variable_get('site_footer', FALSE),
'closure' => theme('closure')
);
if ((arg(0) == 'node') && is_int(arg(1))) {
$variables['node'] = node_load(array('nid' => arg(1)));
}
return _phptemplate_callback('page', $variables);
}
/**
* Prepare the values passed to the theme_node function to be passed
* into a pluggable template engine.
*/
function phptemplate_node($node, $main = 0, $page = 0) {
if (module_exist('taxonomy')) {
$taxonomy = taxonomy_link('taxonomy terms', $node);
}
else {
$taxonomy = array();
}
$variables = array(
'title' => check_plain($node->title),
'node_url' => url('node/' . $node->nid),
'terms' => theme('links',$taxonomy),
'name' => format_name($node),
'date' => format_date($node->created),
'sticky' => $node->sticky,
'picture' => theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '',
'content' => ($main && $node->teaser) ? $node->teaser : $node->body,
'links' => $node->links ? theme('links', $node->links) : '',
'mission' => $mission,
'page' => $page,
'taxonomy' => $taxonomy,
/* Lastly , pass the actual node to allow more customization */
'node' => $node,
'main' => $main,
'page' => $page
);
// Display info only on certain node types.
if (theme_get_setting('toggle_node_info_' . $node->type)) {
$variables['submitted'] = t('Submitted by %a on %b.', array('%a' => format_name($node), '%b' => format_date($node->created)));
}
return _phptemplate_callback('node', $variables, 'node-' . $node->type);
}
/**
* Prepare the values passed to the theme_comment function to be passed
* into a pluggable template engine.
*/
function phptemplate_comment($comment, $links = 0) {
return _phptemplate_callback('comment', array(
'new' => $comment->new ? t('new') : '',
'comment' => $comment,
'submitted' => t('Submitted by %a on %b.',
array('%a' => format_name($comment),
'%b' => format_date($comment->timestamp))),
'title' => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid"),
'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '',
'links' => $links,
'content' => $comment->comment,
'author' => format_name($comment),
'date' => format_date($comment->timestamp)
));
}
/**
* Prepare the values passed to the theme_block function to be passed
* into a pluggable template engine.
*/
function phptemplate_block($block) {
return _phptemplate_callback('block', array('block' => $block));
}
/**
* Prepare the values passed to the theme_box function to be passed
* into a pluggable template engine.
*/
function phptemplate_box($title, $content, $region = 'main') {
return _phptemplate_callback('box', array(
'title' => $title,
'content' => $content,
'region' => $region
));
}
/**
* Default callback for PHPTemplate.
*
* Load a template file, and pass the variable array to it.
* If the suggested file is not found, PHPTemplate will attempt to use
* a $hook.tpl.php file in the template directory, and failing that a
* $hook.tpl.php in the PHPTemplate directory.
*
* @param $hook
* The name of the theme function being executed.
* @param $variables
* A sequential array of variables passed to the theme function.
* @param $file
* A suggested template file to use.
*/
function _phptemplate_default($hook, $variables, $file = null) {
if ($file && file_exists(path_to_theme() . "/$file.tpl.php")) {
$file = path_to_theme() . "/$file.tpl.php";
}
else {
if (file_exists(path_to_theme() . "/$hook.tpl.php")) {
$file = path_to_theme() . "/$hook.tpl.php";
}
else {
if (in_array($hook, array('node', 'block', 'box', 'comment'))) {
$file = "themes/engines/phptemplate/$hook.tpl.php";
}
else {
$variables['hook'] = $hook;
watchdog('error', 'PHPTemplate was instructed to override the ' . $hook . ' theme function, but no valid template file was found.');
$file = "themes/engines/phptemplate/default.tpl.php";
}
}
}
if ($file) {
extract($variables); // Extract the vars to local namespace
ob_start(); // Start output buffering
include($file); // Include the file
$contents = ob_get_contents(); // Get the contents of the buffer
ob_end_clean(); // End buffering and discard
return $contents; // Return the contents
}
}
?>