PageRenderTime 35ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/drool/resources/theme/drool2/template.php

https://github.com/blundeln/drool
PHP | 388 lines | 185 code | 45 blank | 158 comment | 44 complexity | 1ae3a9d46e7e5bd32e5ce90842ec96eb MD5 | raw file
  1. <?php
  2. // $Id: template.php,v 1.12.2.8 2007/02/16 01:32:18 jjeff Exp $
  3. /**
  4. * @file
  5. * File which contains theme overrides for the Zen theme.
  6. */
  7. /*
  8. * ABOUT
  9. *
  10. * The template.php file is one of the most useful files when creating or modifying Drupal themes.
  11. * You can add new regions for block content, modify or override Drupal's theme functions,
  12. * intercept or make additional variables available to your theme, and create custom PHP logic.
  13. * For more information, please visit the Theme Developer's Guide on Drupal.org:
  14. * http://drupal.org/node/509
  15. */
  16. /*
  17. * MODIFYING OR CREATING REGIONS
  18. *
  19. * Regions are areas in your theme where you can place blocks.
  20. * The default regions used in themes are "left sidebar", "right sidebar", "header", and "footer", although you can create
  21. * as many regions as you want. Once declared, they are made available to the page.tpl.php file as a variable.
  22. * For instance, use <?php print $header ?> for the placement of the "header" region in page.tpl.php.
  23. *
  24. * By going to the administer > site building > blocks page you can choose which regions various blocks should be placed.
  25. * New regions you define here will automatically show up in the drop-down list by their human readable name.
  26. */
  27. /*
  28. * Declare the available regions implemented by this engine.
  29. *
  30. * @return
  31. * An array of regions. The first array element will be used as the default region for themes.
  32. * Each array element takes the format: variable_name => t('human readable name')
  33. */
  34. function zen_regions() {
  35. return array(
  36. 'left' => t('left sidebar'),
  37. 'right' => t('right sidebar'),
  38. 'content_top' => t('content top'),
  39. 'content_bottom' => t('content bottom'),
  40. 'header' => t('header'),
  41. 'footer' => t('footer')
  42. );
  43. }
  44. /*
  45. * OVERRIDING THEME FUNCTIONS
  46. *
  47. * The Drupal theme system uses special theme functions to generate HTML output automatically.
  48. * Often we wish to customize this HTML output. To do this, we have to override the theme function.
  49. * You have to first find the theme function that generates the output, and then "catch" it and modify it here.
  50. * The easiest way to do it is to copy the original function in its entirety and paste it here, changing
  51. * the prefix from theme_ to zen_. For example:
  52. *
  53. * original: theme_breadcrumb()
  54. * theme override: zen_breadcrumb()
  55. *
  56. * See the following example. In this theme, we want to change all of the breadcrumb separator links from >> to ::
  57. *
  58. */
  59. /**
  60. * Return a themed breadcrumb trail.
  61. *
  62. * @param $breadcrumb
  63. * An array containing the breadcrumb links.
  64. * @return a string containing the breadcrumb output.
  65. */
  66. function zen_breadcrumb($breadcrumb) {
  67. if (!empty($breadcrumb)) {
  68. return '<div class="breadcrumb">'. implode(' :: ', $breadcrumb) .'</div>';
  69. }
  70. }
  71. /*
  72. * CREATE OR MODIFY VARIABLES FOR YOUR THEME
  73. *
  74. * The most powerful function available to themers is the _phptemplate_variables() function. It allows you
  75. * to pass newly created variables to different template (tpl.php) files in your theme. Or even unset ones you don't want
  76. * to use.
  77. *
  78. * It works by switching on the hook, or name of the theme function, such as:
  79. * - page
  80. * - node
  81. * - comment
  82. * - block
  83. *
  84. * By switching on this hook you can send different variables to page.tpl.php file, node.tpl.php
  85. * (and any other derivative node template file, like node-forum.tpl.php), comment.tpl.php, and block.tpl.php
  86. *
  87. */
  88. /**
  89. * Intercept template variables
  90. *
  91. * @param $hook
  92. * The name of the theme function being executed
  93. * @param $vars
  94. * A sequential array of variables passed to the theme function.
  95. */
  96. function _phptemplate_variables($hook, $vars = array()) {
  97. // get the currently logged in user
  98. global $user;
  99. // set a new $is_admin variable
  100. // this is determined by looking at the currently logged in user and seeing if they are in the role 'admin'
  101. // the 'admin' will need to have been created manually for this to work
  102. // this variable is available to all templates
  103. $vars['is_admin'] = in_array('admin', $user->roles);
  104. switch ($hook) {
  105. // Send a new variable, $logged_in, to page.tpl.php to tell us if the current user is logged in or out.
  106. case 'page':
  107. global $theme, $theme_key;
  108. // if we're in the main theme
  109. if ($theme == $theme_key) {
  110. // These next lines add additional CSS files and redefine
  111. // the $css and $styles variables available to your page template
  112. // We had previously used @import declarations in the css files,
  113. // but these are incompatible with the CSS caching in Drupal 5
  114. drupal_add_css($vars['directory'] .'/layout.css', 'theme', 'all');
  115. drupal_add_css($vars['directory'] .'/icons.css', 'theme', 'all');
  116. drupal_add_css($vars['directory'] .'/main-style.css', 'theme', 'all');
  117. drupal_add_css($vars['directory'] .'/skin/skin.css', 'theme', 'all');
  118. $vars['css'] = drupal_add_css($vars['directory'] .'/print.css', 'theme', 'print');
  119. $vars['styles'] = drupal_get_css();
  120. // Nick
  121. drupal_add_js($vars['directory'] .'/js/scripts.js');
  122. $vars['scripts'] = drupal_get_js();
  123. }
  124. // An anonymous user has a user id of zero.
  125. if ($user->uid > 0) {
  126. // The user is logged in.
  127. $vars['logged_in'] = TRUE;
  128. }
  129. else {
  130. // The user has logged out.
  131. $vars['logged_in'] = FALSE;
  132. }
  133. $body_classes = array();
  134. // classes for body element
  135. // allows advanced theming based on context (home page, node of certain type, etc.)
  136. $body_classes[] = ($vars['is_front']) ? 'front' : 'not-front';
  137. $body_classes[] = ($vars['logged_in']) ? 'logged-in' : 'not-logged-in';
  138. if ($vars['node']->type) {
  139. // if on an individual node page, put the node type in the body classes
  140. $body_classes[] = 'ntype-'. zen_id_safe($vars['node']->type);
  141. }
  142. /* XXX Nick Blundell
  143. switch (TRUE) {
  144. case $vars['sidebar_left'] && $vars['sidebar_right'] :
  145. $body_classes[] = 'both-sidebars';
  146. break;
  147. case $vars['sidebar_left'] :
  148. $body_classes[] = 'sidebar-left';
  149. break;
  150. case $vars['sidebar_right'] :
  151. $body_classes[] = 'sidebar-right';
  152. break;
  153. }*/
  154. // implode with spaces
  155. $vars['body_classes'] = implode(' ', $body_classes);
  156. break;
  157. case 'node':
  158. if ($vars['submitted']) {
  159. // we redefine the format for submitted
  160. // adding macrotags and
  161. $vars['submitted'] =
  162. t('Posted <abbr class="created" title="!microdate">@date</abbr> by !username',
  163. array(
  164. '!username' => theme('username', $vars['node']),
  165. '@date' => format_date($vars['node']->created,'custom', "F jS, Y"),
  166. '!microdate' => format_date($vars['node']->created,'custom', "Y-m-d\TH:i:sO")
  167. )
  168. );
  169. }
  170. // special classes for nodes
  171. $node_classes = array('node');
  172. if ($vars['sticky']) {
  173. $node_classes[] = 'sticky';
  174. }
  175. if (!$vars['node']->status) {
  176. $node_classes[] = 'node-unpublished';
  177. }
  178. if ($vars['node']->uid && $vars['node']->uid == $user->uid) {
  179. // node is authored by current user
  180. $node_classes[] = 'node-mine';
  181. }
  182. // class for node type: "ntype-page", "ntype-story", "ntype-my-custom-type", etc.
  183. $node_classes[] = 'ntype-'. zen_id_safe($vars['node']->type);
  184. // implode with spaces
  185. $vars['node_classes'] = implode(' ', $node_classes);
  186. break;
  187. case 'comment':
  188. // we load the node object that the current comment is attached to
  189. $node = node_load($vars['comment']->nid);
  190. // if the author of this comment is equal to the author of the node, we set a variable
  191. // then in our theme we can theme this comment differently to stand out
  192. $vars['author_comment'] = $vars['comment']->uid == $node->uid ? TRUE : FALSE;
  193. $comment_classes = array('comment');
  194. // odd/even handling
  195. static $comment_odd = TRUE;
  196. $comment_classes[] = $comment_odd ? 'odd' : 'even';
  197. $comment_odd = !$comment_odd;
  198. if ($vars['comment']->status == COMMENT_NOT_PUBLISHED) {
  199. $comment_classes[] = 'comment-unpublished';
  200. }
  201. if ($vars['author_comment']) {
  202. // comment is by the node author
  203. $comment_classes[] = 'comment-by-author';
  204. }
  205. if ($vars['comment']->uid == 0) {
  206. // comment is by an anonymous user
  207. $comment_classes[] = 'comment-by-anon';
  208. }
  209. if ($user->uid && $vars['comment']->uid == $user->uid) {
  210. // comment was posted by current user
  211. $comment_classes[] = 'comment-mine';
  212. }
  213. $vars['comment_classes'] = implode(' ', $comment_classes);
  214. // if comment subjects are disabled, don't display 'em
  215. if (variable_get('comment_subject_field', 1) == 0) {
  216. $vars['title'] = '';
  217. }
  218. break;
  219. }
  220. // allow subtheme to add/alter variables
  221. if (function_exists('zen_variables')) {
  222. $vars = zen_variables($hook, $vars);
  223. }
  224. return $vars;
  225. }
  226. /**
  227. * Converts a string to a suitable html ID attribute.
  228. * - Preceeds initial numeric with 'n' character.
  229. * - Replaces space and underscore with dash.
  230. * - Converts entire string to lowercase.
  231. * - Works for classes too!
  232. *
  233. * @param string $string
  234. * the string
  235. * @return
  236. * the converted string
  237. */
  238. function zen_id_safe($string) {
  239. if (is_numeric($string{0})) {
  240. // if the first character is numeric, add 'n' in front
  241. $string = 'n'. $string;
  242. }
  243. return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
  244. }
  245. /**
  246. * This bit allows the subtheme to have its own template.php
  247. */
  248. if (path_to_subtheme()) {
  249. // I'm being careful not to create variables in the global scope
  250. if (file_exists(path_to_subtheme() .'/template.php')) {
  251. include_once(path_to_subtheme() .'/template.php');
  252. }
  253. }
  254. /**
  255. * These next functions allow subthemes to have their own
  256. * page.tpl.php, node.tpl.php, node-type.tpl.php, etc.
  257. */
  258. function _phptemplate_node($vars, $suggestions) {
  259. array_unshift($suggestions, 'node'); // not quite sure why I need to do this...
  260. return _zen_default('node', $vars, $suggestions);
  261. }
  262. function _phptemplate_comment($vars, $suggestions) {
  263. array_unshift($suggestions, 'comment'); // not quite sure why I need to do this...
  264. return _zen_default('comment', $vars, $suggestions);
  265. }
  266. function _phptemplate_page($vars, $suggestions) {
  267. return _zen_default('page', $vars, $suggestions);
  268. }
  269. function _phptemplate_block($vars, $suggestions) {
  270. return _zen_default('block', $vars, $suggestions);
  271. }
  272. function _phptemplate_box($vars, $suggestions) {
  273. return _zen_default('box', $vars, $suggestions);
  274. }
  275. /**
  276. * return path to the subtheme directory
  277. * or FALSE if there is no subtheme
  278. */
  279. function path_to_subtheme() {
  280. global $theme, $theme_key;
  281. static $theme_path;
  282. if (!isset($theme_path)) {
  283. if ($theme != $theme_key) {
  284. $themes = list_themes();
  285. $theme_path = dirname($themes[$theme_key]->filename);
  286. }
  287. else {
  288. $theme_path = FALSE;
  289. }
  290. }
  291. return $theme_path;
  292. }
  293. /**
  294. * This is an exact copy of _phptemplate_default() with the
  295. * addition of the $theme_path and $parent_theme_path
  296. */
  297. function _zen_default($hook, $variables, $suggestions = array(), $extension = '.tpl.php') {
  298. global $theme_engine;
  299. global $theme;
  300. global $theme_key;
  301. if ($theme_path = path_to_subtheme()) {
  302. $parent_theme_path = path_to_theme();
  303. }
  304. else {
  305. $theme_path = path_to_theme();
  306. }
  307. // Loop through any suggestions in FIFO order.
  308. $suggestions = array_reverse($suggestions);
  309. foreach ($suggestions as $suggestion) {
  310. if (!empty($suggestion) && file_exists($theme_path .'/'. $suggestion . $extension)) {
  311. $file = $theme_path .'/'. $suggestion . $extension;
  312. break;
  313. }
  314. elseif (isset($parent_theme_path) && !empty($suggestion) && file_exists($parent_theme_path .'/'. $suggestion . $extension)) {
  315. $file = $parent_theme_path .'/'. $suggestion . $extension;
  316. break;
  317. }
  318. }
  319. if (!isset($file)) {
  320. if (file_exists($theme_path ."/$hook$extension")) {
  321. $file = $theme_path ."/$hook$extension";
  322. }
  323. else {
  324. if (in_array($hook, array('node', 'block', 'box', 'comment'))) {
  325. $file = "themes/engines/$theme_engine/$hook$extension";
  326. }
  327. else {
  328. $variables['hook'] = $hook;
  329. watchdog('error', t('%engine.engine was instructed to override the %name theme function, but no valid template file was found.', array('%engine' => $theme_engine, '%name' => $hook)));
  330. $file = "themes/engines/$theme_engine/default$extension";
  331. }
  332. }
  333. }
  334. if (isset($file)) {
  335. return call_user_func('_'. $theme_engine .'_render', $file, $variables);
  336. }
  337. }
  338. function phptemplate_links($links, $attributes = array('class' => 'links'), $skin=False) {
  339. /**
  340. * catches the theme_links function and calls back a link.tpl.php file to determine the layout
  341. */
  342. return _phptemplate_callback('links', array('links' => $links, 'attributes' => $attributes, 'skin' => $skin));
  343. }