| Commit | Line | Data |
|---|---|---|
| cc27397e J |
1 | <?php |
| 2 | // $Id$ | |
| 3 | ||
| b623318e J |
4 | /* |
| 5 | * Allow the sub-theme to have its own template.php. | |
| 6 | */ | |
| 7 | if (path_to_subtheme()) { | |
| 8 | // Be careful not to create variables in the global scope | |
| 9 | if (file_exists(path_to_subtheme() .'/template.php')) { | |
| 10 | include_once path_to_subtheme() .'/template.php'; | |
| 11 | } | |
| 12 | } | |
| 13 | ||
| 14 | ||
| cc27397e J |
15 | /** |
| 16 | * Return the path to the sub-theme directory or FALSE if there is no sub-theme. | |
| 17 | * | |
| 18 | * This function also fixes sub-themes that are mistakenly seen by PHPTemplate | |
| 19 | * as separate from their base theme. | |
| 20 | */ | |
| 21 | function path_to_subtheme() { | |
| 22 | $base_theme = 'zen'; | |
| 23 | ||
| 24 | global $theme, $theme_key; | |
| 25 | static $theme_path; | |
| b623318e | 26 | |
| cc27397e J |
27 | if (!isset($theme_path)) { |
| 28 | if ($theme_key == $base_theme) { | |
| 29 | // This is not a sub-theme. | |
| 30 | $theme_path = FALSE; | |
| 31 | } | |
| 32 | else { | |
| 33 | // Extract current files from database. | |
| 34 | $themes = list_themes(); | |
| 35 | ||
| 36 | // Sub-themes with their own page.tpl.php files are seen by PHPTemplate as | |
| 37 | // their own theme (seperate from Zen). So we need to re-connect those | |
| 38 | // sub-themes with the base Zen theme. | |
| 39 | if ($theme == $theme_key) { | |
| 40 | // Update database | |
| 41 | $parent_path = $themes[$base_theme]->filename; | |
| 42 | $subtheme_path = str_replace('page.tpl.php', 'style.css', $themes[$theme]->filename); | |
| 43 | db_query("UPDATE {system} SET description='%s', filename='%s' WHERE name='%s'", $parent_path, $subtheme_path, $theme); | |
| 44 | ||
| 45 | // Refresh Drupal internals. | |
| 46 | $theme = $base_theme; | |
| 47 | $themes = list_themes(TRUE); | |
| 48 | } | |
| 49 | ||
| 50 | $theme_path = dirname($themes[$theme_key]->filename); | |
| 51 | } | |
| 52 | } | |
| 53 | return $theme_path; | |
| 54 | } | |
| 55 | ||
| cc27397e | 56 | /** |
| b623318e J |
57 | * Allow sub-themes to have their own .tpl.php template files. |
| 58 | * | |
| cc27397e J |
59 | * This is an exact copy of _phptemplate_default() with the addition of the |
| 60 | * $theme_path and $parent_theme_path | |
| 61 | */ | |
| 62 | function _zen_default($hook, $variables, $suggestions = array(), $extension = '.tpl.php') { | |
| 63 | global $theme_engine; | |
| cc27397e J |
64 | |
| 65 | if ($theme_path = path_to_subtheme()) { | |
| 66 | $parent_theme_path = path_to_theme(); | |
| 67 | } | |
| 68 | else { | |
| 69 | $theme_path = path_to_theme(); | |
| 70 | } | |
| 71 | ||
| 72 | // Loop through any suggestions in FIFO order. | |
| 73 | $suggestions = array_reverse($suggestions); | |
| 74 | foreach ($suggestions as $suggestion) { | |
| a8a1366d J |
75 | if (!empty($suggestion)) { |
| 76 | if (file_exists($theme_path .'/'. $suggestion . $extension)) { | |
| 77 | $file = $theme_path .'/'. $suggestion . $extension; | |
| 78 | break; | |
| 79 | } | |
| 80 | elseif (!empty($parent_theme_path) && file_exists($parent_theme_path .'/'. $suggestion . $extension)) { | |
| 81 | $file = $parent_theme_path .'/'. $suggestion . $extension; | |
| 82 | break; | |
| 83 | } | |
| cc27397e J |
84 | } |
| 85 | } | |
| 86 | ||
| 87 | if (!isset($file)) { | |
| 88 | if (file_exists($theme_path ."/$hook$extension")) { | |
| 89 | $file = $theme_path ."/$hook$extension"; | |
| 90 | } | |
| a8a1366d J |
91 | elseif (!empty($parent_theme_path) && file_exists($parent_theme_path ."/$hook$extension")) { |
| 92 | $file = $parent_theme_path ."/$hook$extension"; | |
| 93 | } | |
| cc27397e J |
94 | else { |
| 95 | if (in_array($hook, array('node', 'block', 'box', 'comment'))) { | |
| b623318e | 96 | $file = path_to_engine() .'/'. $hook . $extension; |
| cc27397e J |
97 | } |
| 98 | else { | |
| 99 | $variables['hook'] = $hook; | |
| 100 | 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))); | |
| b623318e | 101 | $file = path_to_engine() .'/default'. $extension; |
| cc27397e J |
102 | } |
| 103 | } | |
| 104 | } | |
| b623318e | 105 | |
| cc27397e J |
106 | if (isset($file)) { |
| 107 | return call_user_func('_'. $theme_engine .'_render', $file, $variables); | |
| 108 | } | |
| 109 | } | |
| 608fe8f0 | 110 | |
| 027e9d2a J |
111 | /** |
| 112 | * To allow themes to have their own template files, we have to override | |
| 113 | * _phptemplate_default() by dynamically defining _phptemplate_HOOK(). | |
| 114 | */ | |
| 115 | function _zen_hook($hook) { | |
| 116 | if (!function_exists("_phptemplate_$hook")) { | |
| 117 | // Only create a function if $hook contains letters and underscores. | |
| 118 | if (!preg_match('/\W/', $hook)) { | |
| 119 | $declaration = <<<EOD | |
| 120 | function _phptemplate_$hook(\$vars, \$suggestions = array()) { | |
| 121 | return _zen_default('$hook', \$vars, \$suggestions); | |
| 122 | } | |
| 123 | EOD; | |
| 124 | eval($declaration); | |
| 125 | } | |
| 126 | else { | |
| 127 | // Since we can't create an override function, we simply add a template | |
| 128 | // suggestion that contains the sub-theme directory. | |
| 129 | $vars['template_file'] = "$theme_key/$hook"; | |
| 130 | } | |
| 131 | } | |
| 132 | } | |
| 133 | ||
| 608fe8f0 | 134 | /* |
| 027e9d2a J |
135 | * Rather than let _zen_hook() dynamically, and slowly, define override |
| 136 | * functions for functions we will definitely need, we define them explicitly. | |
| 608fe8f0 J |
137 | */ |
| 138 | function _phptemplate_page($vars, $suggestions) { | |
| 139 | return _zen_default('page', $vars, $suggestions); | |
| 140 | } | |
| 141 | ||
| 142 | function _phptemplate_node($vars, $suggestions) { | |
| 143 | return _zen_default('node', $vars, $suggestions); | |
| 144 | } | |
| 145 | ||
| 146 | function _phptemplate_comment($vars, $suggestions) { | |
| 147 | return _zen_default('comment', $vars, $suggestions); | |
| 148 | } | |
| 149 | ||
| 150 | function _phptemplate_block($vars, $suggestions) { | |
| 151 | return _zen_default('block', $vars, $suggestions); | |
| 152 | } | |
| 153 | ||
| 154 | function _phptemplate_box($vars, $suggestions) { | |
| 155 | return _zen_default('box', $vars, $suggestions); | |
| 156 | } |