/[drupal]/contributions/themes/nitobe/template.php
ViewVC logotype

Contents of /contributions/themes/nitobe/template.php

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


Revision 1.21 - (show annotations) (download) (as text)
Sun Mar 1 13:05:03 2009 UTC (8 months, 4 weeks ago) by shannonlucas
Branch: MAIN
CVS Tags: DRUPAL-6--3-2, DRUPAL-7--1-1, HEAD
Branch point for: DRUPAL-6--4
Changes since 1.20: +14 -33 lines
File MIME type: text/x-php
Fixes for [#387256]
1 <?php
2 // $Id: template.php,v 1.20 2009/02/28 16:20:09 shannonlucas Exp $
3 /**
4 * @file The Nitobe theme.
5 */
6
7 require_once path_to_nitobetheme() . '/nitobe_utils.php';
8
9
10 /**
11 * Return the path to the main nitobe theme directory.
12 */
13 function path_to_nitobetheme() {
14 static $theme_path;
15
16 if (!isset($theme_path)) {
17 global $theme;
18 if ($theme == 'nitobe') {
19 $theme_path = path_to_theme();
20 }
21 else {
22 $theme_path = drupal_get_path('theme', 'nitobe');
23 }
24 }
25 return $theme_path;
26 }
27
28
29 /**
30 * Implementation of hook_theme().
31 *
32 * @param array $existing
33 * @param string $type
34 * @param string $theme
35 * @param string $path
36 *
37 * @return array
38 */
39 function nitobe_theme($existing, $type, $theme, $path) {
40 $funcs = array(
41 'nitobe_username' => array(
42 'arguments' => $existing['theme_username'],
43 ),
44 'nitobe_preprocess_page' => array(
45 'arguments' => $existing['phptemplate_preprocess_page'],
46 ),
47 'nitobe_preprocess' => array(
48 'arguments' => $existing['template_preprocess'],
49 ),
50 'nitobe_preprocess_comment' => array(
51 'arguments' => $existing['phptemplate_preprocess_comment'],
52 ),
53 'nitobe_preprocess_user_picture' => array(
54 'arguments' => $existing['phptemplate_preprocess_user_picture'],
55 ),
56 );
57
58 nitobe_settings_init($theme);
59
60 return $funcs;
61 }
62
63
64 /**
65 * Display the list of available node types for node creation.
66 *
67 * @param $content array
68 *
69 * @return string The rendered HTML.
70 */
71 function nitobe_node_add_list($content) {
72 $output = '';
73
74 if ($content) {
75 $output = '<dl class="node-type-list">';
76 $class = 'odd';
77 foreach ($content as $item) {
78 $output .= '<dt class="' . $class . '">'. l($item['title'], $item['href'], $item['options']) .'</dt>';
79 $output .= '<dd class="' . $class . '">'. filter_xss_admin($item['description']) .'</dd>';
80
81 $class = ($class == 'odd') ? 'even' : 'odd';
82 }
83 $output .= '</dl>';
84 }
85 return $output;
86 }
87
88
89 /**
90 * Decorates theme_username().
91 *
92 * @param object $object An instance of a node, comment, etc.
93 *
94 * @return string The decorated output from theme_username().
95 */
96 function nitobe_username($object) {
97 $output = theme_username($object);
98
99 if ((boolean)theme_get_setting('nitobe_remove_not_verified')) {
100 $to_strip = ' ('. t('not verified') .')';
101 $output = str_replace($to_strip, '', $output);
102 }
103
104 return $output;
105 }
106
107
108 /**
109 * Prepare the user pictures for rendering.
110 *
111 * @param &$variables array The associative array of template arguments.
112 */
113 function nitobe_preprocess_user_picture(&$variables) {
114 $variables['picture'] = '';
115
116 if (variable_get('user_pictures', 0)) {
117 $account = $variables['account'];
118 if (!empty($account->picture) && file_exists($account->picture)) {
119 $picture = file_create_url($account->picture);
120 }
121 else if (variable_get('user_picture_default', '')) {
122 $picture = variable_get('user_picture_default', '');
123 }
124 else {
125 $picture = path_to_nitobetheme() . '/user-icon.jpg';
126 }
127
128 if (isset($picture)) {
129 $name = $account->name ? $account->name : variable_get('anonymous', t('Anonymous'));
130 $alt = t("@user's picture", array('@user' => $name));
131 $attr = array('class' => 'user-picture');
132 $variables['picture'] = theme('image', $picture, $alt, $alt, $attr, FALSE);
133
134 // Link the picture if allowed.
135 if (!empty($account->uid) && user_access('access user profiles')) {
136 $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
137 $variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes);
138 }
139 }
140 }
141 }
142
143
144 /**
145 * Determine whether to show the date stamp for the given node.
146 *
147 * @param $type string The machine readable name of the type to check.
148 *
149 * @return boolean TRUE if the node is of a type that should show the date
150 * stamp, FALSE if not.
151 */
152 function nitobe_show_datestamp($type) {
153 $default = drupal_map_assoc(array('blog', 'forum', 'poll', 'story'));
154 $valid_types = theme_get_setting('nitobe_show_datestamp');
155 $valid_types = (!empty($valid_types)) ? $valid_types : $default;
156
157 return (array_key_exists($type, $valid_types) && ($valid_types[$type] === $type));
158 }
159
160
161 /**
162 * Removes the spaces between words in the given string and returns an HTML
163 * string with every other word wrapped in a span with the class "alt-color".
164 *
165 * @param $text string The text to render.
166 *
167 * @return string The rendered HTML.
168 */
169 function nitobe_alt_word_text($text = '') {
170 $words = explode(' ', $text);
171 $result = '';
172
173 if (is_array($words)) {
174 $alt = FALSE;
175 foreach ($words as $word) {
176 if ($alt) {
177 $result .= '<span class="alt-color">' . $word . '</span>';
178 }
179 else {
180 $result .= $word;
181 }
182
183 $alt = !$alt;
184 }
185 }
186
187 return $result;
188 }
189
190
191 /**
192 * Render the node terms with a text prefix and join them with a comma.
193 *
194 * @param $node object The node to render term links for.
195 * @param $prefix string The text to show before the list of terms. By
196 * defaults the localized text 'Tags:' is used.
197 * @param $separator string The character(s) to place between the terms. By
198 * default a comma is used.
199 */
200 function nitobe_render_terms($node, $prefix = NULL, $separator = ',') {
201 $prefix = ($prefix == NULL) ? t('Tags:') : $prefix;
202 $output = '';
203
204 if (module_exists('taxonomy')) {
205 $terms = taxonomy_link('taxonomy terms', $node);
206 }
207 else {
208 $terms = array();
209 }
210
211 if (count($terms) > 0) {
212 $output .= $prefix . ' <ul class="links inline">';
213 $rendered = nitobe_list_of_links($terms);
214
215 $i = 1;
216 foreach ($rendered as $term) {
217 $output .= '<li class="' . $term[1] . '">' . $term[0];
218
219 if ($i < count($terms)) {
220 $output .= $separator . ' ';
221 }
222
223 $output .= '</li>';
224
225 $i++;
226 }
227
228 $output .= '</ul>';
229 }
230
231 return $output;
232 }
233
234
235 /**
236 * Returns an array of rendered lists without any wrapping elements such as
237 * <ul> and <li>.
238 *
239 * @param $links array A keyed array of links to be themed.
240 *
241 * @return array An array of arrays. The first element of each inner array
242 * will be the rendered link. The second element will be the CSS
243 * class that should be applied to any wrapping element of that
244 * link.
245 */
246 function nitobe_list_of_links($links) {
247 $output = array();
248
249 if (count($links) > 0) {
250 $num_links = count($links);
251 $i = 1;
252
253 foreach ($links as $key => $link) {
254 $class = $key;
255
256 // Add first, last and active classes to the list of links to help
257 // out themers.
258 if ($i == 1) {
259 $class .= ' first';
260 }
261 if ($i == $num_links) {
262 $class .= ' last';
263 }
264 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
265 $class .= ' active';
266 }
267
268 $output[] = array(nitobe_single_link($link), $class);
269
270 $i++;
271 }
272 }
273
274 return $output;
275 }
276
277
278 /**
279 * Render a single link instance.
280 *
281 * @param $link array The array of options to use when rendering the link.
282 *
283 * @return string The rendered HTML link or span element if no href was
284 * provided in the given link.
285 */
286 function nitobe_single_link($link) {
287 if (isset($link['href']) || isset($link['fragment'])) {
288 // Pass in $link as $options, they share the same keys.
289 return l($link['title'], $link['href'], $link);
290 }
291 else if (!empty($link['title'])) {
292 // Some links are actually not links, but we wrap these in <span> for
293 // adding title and class attributes
294 if (empty($link['html'])) {
295 $link['title'] = check_plain($link['title']);
296 }
297
298 $span_attributes = '';
299 if (isset($link['attributes'])) {
300 $span_attributes = drupal_attributes($link['attributes']);
301 }
302
303 $span = '<span'. $span_attributes .'>'. $link['title'] .'</span>';
304
305 return $span;
306 }
307 }
308
309
310 /**
311 * Return the comment link to display for a node rendered as a teaser.
312 *
313 * @param $node object The node to render the comment link for.
314 * @param $teaser boolean Whether or not this call is in the context of
315 * rendering a teaser.
316 *
317 * @return string The rendered comment link.
318 */
319 function nitobe_comment_link($node, $teaser = FALSE) {
320 $separate = (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE);
321 $link = NULL;
322
323 if (!empty($node->links)) {
324 $links = $node->links;
325
326 if (!empty($links['comment_forbidden'])) {
327 $link = $links['comment_forbidden'];
328 }
329 else if (!empty($links['comment_new_comments'])) {
330 $link = $links['comment_new_comments'];
331 $link['title'] = format_plural($node->comment_count,
332 '1 New Comment', '@count New Comments');
333
334 if ($node->comment == COMMENT_NODE_READ_WRITE) {
335 $link['attributes']['title'] = t('Read new comments or comment on @title',
336 array('@title' => $node->title));
337 }
338 else {
339 $link['attributes']['title'] = t('Read new comments for @title',
340 array('@title' => $node->title));
341 }
342 }
343 else if (!empty($links['comment_comments'])) {
344 $link = $links['comment_comments'];
345 $link['title'] = format_plural($node->comment_count,
346 '1 Comment', '@count Comments');
347
348 if ($node->comment == COMMENT_NODE_READ_WRITE) {
349 $link['attributes']['title'] = t('Read comments or comment on @title',
350 array('@title' => $node->title));
351 }
352 else {
353 $link['attributes']['title'] = t('Read comments for @title',
354 array('@title' => $node->title));
355 }
356
357 if (!$separate) {
358 $link['href'] = "node/{$node->nid}";
359 }
360 }
361 else if (!empty($links['comment_add'])) {
362 $link = $links['comment_add'];
363 $link['title'] = t('Add a Comment');
364 $link['attributes']['title'] = t('Comment on @title', array('@title' => $node->title));;
365
366 if (!$separate) {
367 $link['href'] = "node/{$node->nid}";
368 }
369 }
370 }
371 else if ($node->comment == COMMENT_NODE_READ_WRITE) {
372 $url = ($separate == TRUE) ? "comment/reply/{$node->nid}" : "node/{$node->nid}";
373
374 $link = array(
375 'title' => t('Add a Comment'),
376 'href' => $url,
377 'fragment' => 'comment-form',
378 'attributes' => array('title' => t('Comment on @title',
379 array('@title' => $node->title))),
380 );
381 }
382
383 return ($link != NULL) ? nitobe_single_link($link) : '';
384 }
385
386
387 /**
388 * Create the 'Continue reading' link for the bottom of posts.
389 *
390 * @param $node object The node to add the continue reading link to.
391 *
392 * @return string The link HTML.
393 */
394 function nitobe_read_more_link($node) {
395 if ($node != NULL) {
396 $link_text = t('Continue reading...');
397 $link_title = t('Continue reading !title.', array('!title' => $node->title));
398 $options = array(
399 'attributes' => array('title' => $link_title),
400 'html' => TRUE,
401 );
402 return l($link_text, 'node/' . $node->nid, $options);
403 }
404
405 return '';
406 }
407
408
409 /**
410 * Return a themed breadcrumb trail.
411 *
412 * @param $breadcrumb
413 * An array containing the breadcrumb links.
414 * @return a string containing the breadcrumb output.
415 */
416 function phptemplate_breadcrumb($breadcrumb) {
417 $show_single = (boolean)theme_get_setting('nitobe_show_single_crumb');
418
419 if ((count($breadcrumb) == 1) && !$show_single) {
420 return '';
421 }
422
423 return theme_breadcrumb($breadcrumb);
424 }
425
426
427 /**
428 * Allow themable wrapping of all comments.
429 *
430 * @param $content string The comments to wrap.
431 * @param $node object The node the comments belong to.
432 *
433 * @return string The rendered HTML.
434 */
435 function phptemplate_comment_wrapper($content, $node) {
436 if (!$content || $node->type == 'forum') {
437 return '<div id="comments">'. $content .'</div>';
438 }
439 else {
440 return '<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
441 }
442 }
443
444
445 /**
446 * Insert variables at the beginning of the theming process.
447 */
448 function nitobe_preprocess(&$vars, $hook) {
449 static $has_sidebar;
450
451 if (!isset($has_sidebar)) {
452 $left = count(block_list('left'));
453 $right = count(block_list('right'));
454 $has_sidebar = (($left + $right) > 0);
455 }
456
457 $vars['nitobe_has_sidebar'] = $has_sidebar;
458 }
459
460
461 /**
462 * Override or insert PHPTemplate variables into the templates.
463 */
464 function nitobe_preprocess_page(&$vars) {
465 $vars['tabs2'] = menu_secondary_local_tasks();
466
467 // Determine if the masthead image should be displayed.
468 $force_header = theme_get_setting('nitobe_header_always_show');
469 $masthead = $vars['masthead'];
470
471 if ($force_header || empty($masthead)) {
472 // Determine the header image if it is set, or add the JavaScript for
473 // random header images.
474 $header_img = theme_get_setting('nitobe_header_image');
475 $header_img = empty($header_img) ? '<random>' : $header_img;
476
477 if ($header_img == '<random>') {
478 $vars['closure'] .= _nitobe_random_header_js();
479
480 // Add css for a random image for browsers without js enabled.
481 // Patch supplied by Jonathan Hedstrom ( http://drupal.org/user/208732 )
482 $image = array_rand(_nitobe_get_header_list());
483 $vars['styles'] .= _nitobe_fixed_header_css($image) . "\n";
484 }
485 else {
486 $vars['styles'] .= _nitobe_fixed_header_css($header_img) . "\n";
487 }
488 }
489 }
490
491
492 /**
493 * Overrides template_preprocess_comment(). Allows for the removal of the
494 * 'reply' link.
495 *
496 * @param array $variables
497 */
498 function nitobe_preprocess_comment(&$variables) {
499 $comment = $variables['comment'];
500 $node = $variables['node'];
501
502 $variables['author'] = theme('username', $comment);
503 $variables['content'] = $comment->comment;
504
505 $params = array(
506 '@date' => format_date($comment->timestamp, 'custom', 'M jS, Y'),
507 '@time' => format_date($comment->timestamp, 'custom', 'g:i a'),
508 );
509 $variables['date'] = t('@date at @time', $params);
510
511 //--------------------------------------------------------------------------
512 // In order to remove the 'reply' link, it's necessary to get them again as
513 // an array. By the time this function is called, they have already been
514 // rendered as HTML.
515 $linkage = module_invoke_all('link', 'comment', $comment);
516 $suppress_reply = (boolean)theme_get_setting('nitobe_suppress_comment_reply');
517
518 if ($suppress_reply) {
519 unset($linkage['comment_reply']);
520 }
521
522 $links = theme('links', $linkage);
523 $links = preg_replace('!</li>\n<li!', ' | </li><li', $links);
524 $variables['links'] = $links;
525 }
526
527
528 /**
529 * Returns the rendered local tasks. The default implementation renders
530 * them as tabs. Overridden to split the secondary tasks.
531 *
532 * @ingroup themeable
533 */
534 function phptemplate_menu_local_tasks() {
535 return menu_primary_local_tasks();
536 }
537
538 function phptemplate_comment_submitted($comment) {
539 return t('!datetime — !username',
540 array(
541 '!username' => theme('username', $comment),
542 '!datetime' => format_date($comment->timestamp)
543 ));
544 }
545
546 function phptemplate_node_submitted($node) {
547 return t('!datetime — !username',
548 array(
549 '!username' => theme('username', $node),
550 '!datetime' => format_date($node->created),
551 ));
552 }
553
554
555 /**
556 * Read the theme settings' default values from the .info and save them into
557 * the database.
558 *
559 * @param string $theme The actual name of theme that is being being checked.
560 */
561 function nitobe_settings_init($theme) {
562 $themes = list_themes();
563
564 // Get the default values from the .info file.
565 $defaults = $themes[$theme]->info['settings'];
566
567 // Get the theme settings saved in the database.
568 $settings = theme_get_settings($theme);
569
570 // Don't save the toggle_node_info_ variables.
571 if (module_exists('node')) {
572 foreach (node_get_types() as $type => $name) {
573 unset($settings['toggle_node_info_' . $type]);
574 }
575 }
576
577 // Save default theme settings.
578 variable_set(
579 str_replace('/', '_', 'theme_' . $theme . '_settings'),
580 array_merge($defaults, $settings)
581 );
582
583 // Force refresh of Drupal internals.
584 theme_get_setting('', TRUE);
585 }
586
587
588

  ViewVC Help
Powered by ViewVC 1.1.2