| 1 |
<?php
|
| 2 |
// $Id: nitobe_utils.php,v 1.7 2009/02/28 18:26:20 shannonlucas Exp $
|
| 3 |
/**
|
| 4 |
* @file Utility functions for the Nitobe theme.
|
| 5 |
*/
|
| 6 |
|
| 7 |
define('NITOBE_CACHE_HDR', 'nitobe.headers.list');
|
| 8 |
define('NITOBE_HEADER_PATH', (is_dir(path_to_theme() . '/headers') ? path_to_theme() : drupal_get_path('theme', 'nitobe')) . '/headers');
|
| 9 |
define('NITOBE_HEADER_IMG_MASK',
|
| 10 |
'/\.jpg$|\.JPG$|\.jpeg*|\.JPEG*|\.gif$|\.GIF$|\.png$|\.PNG$/');
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Retrieve a list of the images in the headers directory and provide each
|
| 14 |
* with a pretty name. Pretty names are generated from the image's path within
|
| 15 |
* the headers directory using these rules:
|
| 16 |
* 1. '/' is replaced with ' / '
|
| 17 |
* 2. '_' is replaced with ' '.
|
| 18 |
* 3. '.***' extension is removed.
|
| 19 |
*
|
| 20 |
* @param $refresh bool If TRUE, reload the image list and flush the cached
|
| 21 |
* version.
|
| 22 |
*
|
| 23 |
* @return array
|
| 24 |
*/
|
| 25 |
function _nitobe_get_header_list($refresh = FALSE) {
|
| 26 |
$cached = cache_get(NITOBE_CACHE_HDR);
|
| 27 |
$files = (!empty($cached)) ? $cached->data : NULL;
|
| 28 |
|
| 29 |
if (($files == NULL) OR ($refresh == TRUE)) {
|
| 30 |
$options = array(
|
| 31 |
'recurse' => TRUE,
|
| 32 |
);
|
| 33 |
|
| 34 |
$files = file_scan_directory(NITOBE_HEADER_PATH,
|
| 35 |
NITOBE_HEADER_IMG_MASK,
|
| 36 |
$options);
|
| 37 |
foreach ($files as $filename => $data) {
|
| 38 |
$name = substr($filename, strlen(NITOBE_HEADER_PATH) + 1);
|
| 39 |
$name = preg_replace('/\//', ' / ', $name);
|
| 40 |
$name = preg_replace('/_/', ' ', $name);
|
| 41 |
$name = preg_replace('/\.(\w{3,4}$)/', '', $name);
|
| 42 |
|
| 43 |
$data->pretty_name = $name;
|
| 44 |
}
|
| 45 |
|
| 46 |
// Cache the list for a week.
|
| 47 |
cache_set(NITOBE_CACHE_HDR, $files, 'cache', time() + 604800);
|
| 48 |
}
|
| 49 |
|
| 50 |
return $files;
|
| 51 |
}
|
| 52 |
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Generate the JavaScript for rotating the header image.
|
| 56 |
*
|
| 57 |
* @return string The JavaScript for rotating the header.
|
| 58 |
*/
|
| 59 |
function _nitobe_random_header_js() {
|
| 60 |
global $base_url;
|
| 61 |
|
| 62 |
$files = _nitobe_get_header_list();
|
| 63 |
$names = array();
|
| 64 |
|
| 65 |
foreach ($files as $file => $data) {
|
| 66 |
$names[] = $base_url . '/' . $file;
|
| 67 |
}
|
| 68 |
|
| 69 |
$name_js = drupal_to_js($names);
|
| 70 |
|
| 71 |
$js = <<<EOJS
|
| 72 |
<script type="text/javascript">
|
| 73 |
jQuery(document).ready(function() {
|
| 74 |
var names = {$name_js};
|
| 75 |
jQuery('#masthead').css('background-image', 'url(' + names[Math.floor(Math.random() * names.length)] + ')');
|
| 76 |
});
|
| 77 |
</script><noscript></noscript>
|
| 78 |
EOJS;
|
| 79 |
|
| 80 |
return $js;
|
| 81 |
}
|
| 82 |
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Return the CSS to place inline to choose the header background image.
|
| 86 |
*
|
| 87 |
* @param string $filename The filename relative to the theme,
|
| 88 |
*
|
| 89 |
* @return string The CSS to add to the header.
|
| 90 |
*/
|
| 91 |
function _nitobe_fixed_header_css($filename) {
|
| 92 |
global $base_url;
|
| 93 |
|
| 94 |
$url = $base_url . '/' . $filename;
|
| 95 |
$output = '<style type="text/css">#masthead{background-image:url(%s);}</style>';
|
| 96 |
|
| 97 |
return sprintf($output, $url);
|
| 98 |
}
|