/[drupal]/contributions/theme-engines/phptemplate/phptemplate.engine
ViewVC logotype

Contents of /contributions/theme-engines/phptemplate/phptemplate.engine

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.27 - (show annotations) (download) (as text)
Sat Apr 30 18:42:47 2005 UTC (4 years, 6 months ago) by tdobes
Branch: MAIN
CVS Tags: HEAD
Changes since 1.26: +2 -2 lines
File MIME type: text/x-php
update for #21517
1 <?php
2 // $Id: phptemplate.engine,v 1.26 2005/04/11 00:07:54 adrian Exp $
3
4 function phptemplate_init($template) {
5 require_once(realpath('themes/engines/phptemplate/template.php'));
6 $engine_code = variable_get('phptemplate_extra_logic', array());
7 $file = dirname($template->filename) . '/template.php';
8 if (file_exists($file)) {
9 include_once($file);
10 }
11 }
12
13 function phptemplate_templates($directory = 'themes') {
14 return system_listing('^page\.tpl\.php$', $directory, 'filename');
15 }
16
17
18 /**
19 * Execute a template engine call.
20 *
21 * Each call to the template engine has two parts. Namely preparing
22 * the variables, and then doing something with them.
23 *
24 * The first step is done by all template engines / themes, the second
25 * step is dependent on the engine used.
26 *
27 * @param $hook
28 * The name of the theme function being executed
29 * @param $vars
30 * A sequential array of variables passed to the theme function.
31 * @param $file
32 * A suggested template file to use.
33 * @return
34 * The HTML generated by the template system.
35 */
36 function _phptemplate_callback($hook, $vars = array(), $file = null) {
37
38 $vars = array_merge($vars, _phptemplate_default_variables($hook, $vars));
39
40 // Allow specified variables to be overridden
41 if (function_exists('_phptemplate_variables')) {
42 $vars = array_merge($vars, _phptemplate_variables($hook, $vars));
43 }
44
45 if ($vars['template_file']) {
46 $file = $vars['template_file'];
47 }
48
49 if (function_exists('_phptemplate_' . $hook)) {
50 return call_user_func('_phptemplate_' . $hook, $vars, $file);
51 }
52 elseif (function_exists('_phptemplate_default')) {
53 return call_user_func('_phptemplate_default', $hook, $vars, $file);
54 }
55
56 }
57
58 /**
59 * Counts how many times certain hooks have been called
60 *
61 * Sidebar left / right are special cases.
62 */
63 function _phptemplate_default_variables($hook, $vars) {
64 static $count = array();
65 $count[$hook] = is_int($count[$hook]) ? $count[$hook] : 1;
66 $vars['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
67 $vars['seqid'] = $count[$hook]++;
68
69 global $sidebar_indicator;
70 if ($hook == 'block') {
71 $count['block_counter'][$sidebar_indicator] = is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1;
72 $vars['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even';
73 $vars['block_seqid'] = $count['block_counter'][$sidebar_indicator]++;
74 }
75
76 // Tell all templates where they are located.
77 $vars['directory'] = path_to_theme();
78
79 if (drupal_get_path_alias($_GET['q']) == variable_get('site_frontpage', 'node')) {
80 $vars['is_front'] = true;
81 }
82
83 return $vars;
84 }
85
86 function phptemplate_features() {
87 return array(
88 'logo',
89 'toggle_name',
90 'toggle_search',
91 'toggle_slogan',
92 'toggle_mission',
93 'toggle_primary_links',
94 'toggle_secondary_links',
95 'toggle_node_user_picture',
96 'toggle_comment_user_picture');
97 }
98
99 /**
100 * PHPTemplate specific theme settings
101 *
102 * Due to differences in the handling of primary and secondary links, these settings need to be seperately specified for phptemplate based themes.
103 *
104 * @returns
105 * HTML code for the phptemplate engine specific form segment on theme configuration pages.
106 */
107 function phptemplate_settings() {
108 $header = array(t('link text'), t('url'), t('description'));
109 $form = t('<p>Due to incompatibilies in the handling of primary, and secondary links in PHPTemplate, these links need to specified seperately using the form below. Even though this is on the theme-specific configuration page, these settings will take affect in all themes using PHPTemplate.</p>');
110
111 foreach (array('Primary', 'Secondary') as $utype) {
112 $rows = array();
113
114 //Use $utype field , and strtolower() it to get the type field.. to avoid issues with ucfirst() and unicode.
115 $type = strtolower($utype);
116
117 //Increment the link count, if the user has requested more links.
118 if (variable_get('phptemplate_' . $type . '_links_more', false)) {
119 variable_del('phptemplate_' . $type . '_links_more');
120 variable_set('phptemplate_' . $type . '_link_count', variable_get('phptemplate_' . $type . '_link_count', 5) + 5);
121 }
122
123 //Get the data to populate the textfields, if the variable is not an array .. try to parse the old-style link format.
124 $value = variable_get('phptemplate_' . $type . '_links', array());
125
126 //Get the amount of links to show, possibly expanding if there are more links defined than the count specifies.
127 $count = variable_get('phptemplate_' . $type . '_link_count', 5);
128 $count = ($count > sizeof($value['link'])) ? $count : sizeof($value['link']);
129 if (variable_get('phptemplate_' . $type . '_link_count', 5) != $count) {
130 variable_set('phptemplate_' . $type . '_link_count', $count);
131 }
132
133 for ($i = 0; $i < $count; $i++) {
134 $row = array();
135 foreach (array('text', 'link', 'description') as $field) {
136 $row[] = form_textfield('', 'phptemplate_' . $type . '_links][' . $field . '][' . $i, $value[$field][$i], 15, 90);
137 }
138 $rows[] = $row;
139 }
140 $form .= form_item(t("$utype links"), theme("table", $header, $rows), t('You can specify your _TYPE_ links here, one link per line.<br /> The link text field is the text you want to link.<br /> The url field is the location the link points to.<br /> The description field is an optional description of where the link points.', array('_TYPE_' => $type)));
141 $form .= form_checkbox(t('I need more _TYPE_ links.', array('_TYPE_' => $type)), 'phptemplate_' . $type . '_links_more', 1, FALSE, t('Checking this box will give you 5 additional _TYPE_ links.', array('_TYPE_' => $type)));
142
143 }
144 return $form;
145 }
146
147 /**
148 * Prepare the values passed to the theme_page function to be passed
149 * into a pluggable template engine.
150 */
151 function phptemplate_page($content) {
152 global $node_cache;
153 /* Set title and breadcrumb to declared values */
154
155 if (file_exists(path_to_theme() . '/favicon.ico')) {
156 drupal_set_html_head("<link rel=\"shortcut icon\" href=\"" . path_to_theme() . "/favicon.ico\" />\n");
157 }
158
159 if ($_GET['q'] == variable_get('site_frontpage', 'node')) {
160 $mission = theme_get_setting('mission');
161 $frontpage = true;
162 }
163
164 $links['primary'] = array();
165 $links['secondary'] = array();
166
167 foreach (array('primary', 'secondary') as $type) {
168 //Get the data to populate the textfields, if the variable is not an array .. try to parse the old-style link format.
169 $value = variable_get('phptemplate_' . $type . '_links', array());
170
171 //Get the amount of links to show, possibly expanding if there are more links defined than the count specifies.
172 $count = variable_get('phptemplate_' . $type . '_link_count', 5);
173 $count = ($count > sizeof($value['link'])) ? $count : sizeof($value['link']);
174
175 if (theme_get_setting('toggle_' . $type . '_links')) {
176 for ($i =0; $i < $count; $i++) {
177 unset($attributes);
178 if (!empty($value['text'][$i])) {
179 if (!empty($value['description'][$i])) {
180 $attributes['title'] = $value['description'][$i];
181 }
182 $text = $value['text'][$i];
183 $link = $value['link'][$i];
184 if (substr($link, 0, 4) == 'http') {
185 $links[$type][] = '<a href="'. $link .'"'. drupal_attributes($attributes) .'>'. $text .'</a>';
186 }
187 else {
188 $links[$type][] = l($text, $link, $attributes);
189 }
190 }
191 }
192 }
193 }
194
195 /**
196 * Populate sidebars.
197 */
198 $layout = "none";
199 global $sidebar_indicator;
200 /**
201 * Sidebar_indicator tells the block counting code to count sidebars seperately.
202 */
203 $sidebar_indicator = 'left';
204 $sidebar_left = theme("blocks", "left");
205 if ($sidebar_left != "") {
206 $layout = "left";
207 }
208
209 $sidebar_indicator = 'right';
210 $sidebar_right = theme("blocks", "right");
211 if ($sidebar_right != "") {
212 $layout = ($layout == "left") ? "both" : "right";
213 }
214 $sidebar_indicator = null;
215
216 $vars = array(
217 'head_title' => (drupal_get_title() ? strip_tags(drupal_get_title()) .' | '. variable_get('site_name', 'drupal') : variable_get('site_name', 'drupal') .' | '. variable_get('site_slogan', '')),
218 'language' => $GLOBALS['locale'],
219 'site' => variable_get('site_name', 'drupal'),
220 'head' => drupal_get_html_head(),
221 'onload_attributes' => theme('onload_attribute'),
222 'logo' => theme_get_setting('logo'),
223 'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''),
224 'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''),
225 'search_box' => theme_get_setting('toggle_search'),
226 'search_url' => url('search'),
227 'search_button_text' => t('search'),
228 'search_description' => t('Enter the terms you wish to search for.'),
229 'title' => drupal_get_title(),
230 'primary_links' => $links['primary'],
231 'secondary_links' => $links['secondary'],
232 'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()),
233 'tabs' => theme('menu_local_tasks'),
234 'messages' => theme('status_messages'),
235 'layout' => $layout,
236 'help' => theme('help'),
237 'styles' => theme_get_styles(),
238 'mission' => $mission,
239 'sidebar_left' => $sidebar_left,
240 'content' => '<!-- begin content -->' . $content . '<!-- end content -->',
241 'sidebar_right' => $sidebar_right,
242 'footer_message' => variable_get('site_footer', FALSE),
243 'closure' => theme('closure')
244 );
245
246 /**
247 * Pass the node object to the page template on node views.
248 */
249 if (sizeof($node_cache) == 1) {
250 $vars['node'] = $node_cache[0];
251 }
252
253 return _phptemplate_callback('page', $vars);
254 }
255
256 /**
257 * Prepare the values passed to the theme_node function to be passed
258 * into a pluggable template engine.
259 */
260 function phptemplate_node($node, $main = 0, $page = 0) {
261 global $node_cache;
262 $node_cache[] = $node;
263
264 if (module_exist('taxonomy')) {
265 $taxonomy = taxonomy_link('taxonomy terms', $node);
266 }
267 else {
268 $taxonomy = array();
269 }
270
271 $vars = array(
272 'title' => check_plain($node->title),
273 'node_url' => url('node/' . $node->nid),
274 'terms' => theme('links',$taxonomy),
275 'name' => format_name($node),
276 'date' => format_date($node->created),
277 'sticky' => $node->sticky,
278 'picture' => theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '',
279 'content' => ($main && $node->teaser) ? $node->teaser : $node->body,
280 'links' => $node->links ? theme('links', $node->links) : '',
281 'mission' => $mission,
282 'page' => $page,
283 'taxonomy' => $taxonomy,
284
285 /* Lastly , pass the actual node to allow more customization */
286 'node' => $node,
287 'main' => $main,
288 'page' => $page
289 );
290
291 // Display info only on certain node types.
292 if (theme_get_setting('toggle_node_info_' . $node->type)) {
293 $vars['submitted'] = t('Submitted by %a on %b.', array('%a' => format_name($node), '%b' => format_date($node->created)));
294 }
295
296 return _phptemplate_callback('node', $vars, 'node-' . $node->type);
297 }
298
299 /**
300 * Prepare the values passed to the theme_comment function to be passed
301 * into a pluggable template engine.
302 */
303 function phptemplate_comment($comment, $links = 0) {
304 return _phptemplate_callback('comment', array(
305 'new' => $comment->new ? t('new') : '',
306 'comment' => $comment,
307 'submitted' => t('Submitted by %a on %b.',
308 array('%a' => format_name($comment),
309 '%b' => format_date($comment->timestamp))),
310 'title' => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid"),
311
312 'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '',
313 'links' => $links,
314 'content' => $comment->comment,
315 'author' => format_name($comment),
316 'date' => format_date($comment->timestamp)
317 ));
318 }
319
320 /**
321 * Prepare the values passed to the theme_block function to be passed
322 * into a pluggable template engine.
323 */
324 function phptemplate_block($block) {
325 return _phptemplate_callback('block', array('block' => $block));
326 }
327
328 /**
329 * Prepare the values passed to the theme_box function to be passed
330 * into a pluggable template engine.
331 */
332 function phptemplate_box($title, $content, $region = 'main') {
333 return _phptemplate_callback('box', array(
334 'title' => $title,
335 'content' => $content,
336 'region' => $region
337 ));
338 }
339
340 /**
341 * Default callback for PHPTemplate.
342 */
343 function _phptemplate_default($hook, $vars, $file = null) {
344 if ($file && file_exists(path_to_theme() . "/$file.tpl.php")) {
345 $file = path_to_theme() . "/$file.tpl.php";
346 }
347 else {
348 if (file_exists(path_to_theme() . "/$hook.tpl.php")) {
349 $file = path_to_theme() . "/$hook.tpl.php";
350 }
351 else {
352 if (in_array($hook, array('node', 'block', 'box', 'comment'))) {
353 $file = "themes/engines/phptemplate/$hook.tpl.php";
354 }
355 else {
356 $vars['hook'] = $hook;
357 watchdog('error', 'PHPTemplate was instructed to override the ' . $hook . ' theme function, but no valid template file was found.');
358 $file = "themes/engines/phptemplate/default.tpl.php";
359 }
360 }
361 }
362
363 if ($file) {
364 $phptemplate = new Template(dirname($file));
365 $phptemplate->set_vars($vars);
366 return $phptemplate->fetch(basename($file));
367 }
368 }
369
370 ?>

  ViewVC Help
Powered by ViewVC 1.1.2