4 * Various utility functions used by Nitobe.
7 /** The cache variable to store the header list in. */
8 define("NITOBE_CACHE_HDR", "nitobe.headers.list");
10 /** The path to the headers in the current theme, falling back to Nitobe if
11 * the headers directory is not present in the sub-theme. */
12 define("NITOBE_HEADER_PATH",
13 (is_dir(path_to_theme() .
"/headers") ?
14 path_to_theme() : drupal_get_path("theme", "nitobe")) .
"/headers");
16 /** The mask to use when listing the headers directory. */
17 define("NITOBE_HEADER_IMG_MASK",
18 "/\\.jpg$|\\.JPG$|\\.jpeg*|\\.JPEG*|\\.gif$|\\.GIF$|\\.png$|\\.PNG$/");
22 * Contextually adds 960 Grid System classes.
24 * The first parameter passed is the *default class*. All other parameters must
25 * be set in pairs like so: "$variable, 3". The variable can be anything
26 * available within a template file and the integer is the width set for the
27 * adjacent box containing that variable.
30 * class="<?php print ns('grid-16', $var_a, 6); ?>"
33 * If $var_a contains data, the next parameter (integer) will be subtracted from
34 * the default class. See the README.txt file.
36 function nitobe_ns() {
37 $args = func_get_args();
38 $default = array_shift($args);
40 // -- Get the type of class, i.e., 'grid', 'pull', 'push', etc.
41 // -- Also get the default unit for the type to be procesed and returned.
42 list($type, $return_unit) = explode('-', $default);
44 // -- Process the conditions.
45 $flip_states = array('var' => 'int', 'int' => 'var');
49 foreach ($args as
$arg) {
50 if ($state == 'var') {
51 $var_state = !empty($arg);
54 $return_unit = $return_unit - $arg;
56 $state = $flip_states[$state];
60 // -- Anything below a value of 1 is not needed.
61 if ($return_unit > 0) {
62 $output = $type .
'-' .
$return_unit;
69 * Gets the push/pull classes for the various layout options.
72 * A nested array of layout and class information. The array's values are
73 * accesed with $push_pull[$placement][$layout][$region] such that:
74 * - $placement is one of left or center and indicates where the
75 * sidebars are placed relative to the content. A 'right' placement is
76 * not provided since this is the natural layout.
77 * - $layout is one of left, right, or both and indicates which sidebars
79 * - $region is one of content, sidebar_first, sidebar_second and indicates
80 * which region push/pull classes are wanted for.
82 function _nitobe_get_push_pull() {
86 "content" => "push-4",
87 "sidebar_first" => "pull-12",
90 "content" => "push-4",
91 "sidebar_first" => "pull-8",
96 "content" => "push-4",
97 "sidebar_first" => "pull-12",
100 "content" => "push-4",
101 "sidebar_second" => "pull-12",
104 "content" => "push-8",
105 "sidebar_first" => "pull-8",
106 "sidebar_second" => "pull-8",
116 * Copies selected items from one array to another array by key.
118 * For each item in $keys, if that key exists in $source, copy the key-value
119 * pair from $source to &$destination. In the event of duplicate keys, the
120 * value of a key in $source will replace the value in &$destination.
122 * @param array $source
123 * A keyed array of values to copy from.
124 * @param array &$destination
125 * The array to copy to.
127 * An array of keys to copy from $source to &$destination.
129 function _nitobe_copy_if_exists($source, &$destination, $keys) {
130 foreach ($keys as
$key) {
131 if (array_key_exists($key, $source)) {
132 $destination[$key] = $source[$key];
139 * Retrieves a list of header images.
141 * Scans the headers directory and generate a "pretty" name for each. Pretty
142 * names are generated from the image's path within the headers directory using
144 * -# '/' is replaced with ' / '
145 * -# '_' is replaced with ' '.
146 * -# '.***' extension is removed.
148 * @param boolean $refresh
149 * If TRUE, reload the image list and flush the cached version.
152 * A mapping of the headers' pretty names to their actual names.
154 function _nitobe_get_header_list($refresh = FALSE
) {
155 // -- If caching is disabled, force a refresh.
156 if (!$refresh && (variable_get('cache', 0) == 0)) {
160 $cached = cache_get(NITOBE_CACHE_HDR
);
161 $files = (!empty($cached)) ?
$cached->data
: NULL
;
163 if (($files == NULL
) OR ($refresh == TRUE
)) {
164 $files = file_scan_directory(NITOBE_HEADER_PATH
,
165 NITOBE_HEADER_IMG_MASK
,
166 array('.', '..', 'CVS', '.svn'));
167 foreach ($files as
$filename => $data) {
168 $name = substr($filename, strlen(NITOBE_HEADER_PATH
) + 1);
169 $name = preg_replace('/\//', ' / ', $name);
170 $name = preg_replace('/_/', ' ', $name);
171 $name = preg_replace('/\.(\w{3,4}$)/', '', $name);
173 $data->pretty_name
= $name;
176 // -- Cache the list for a week.
177 cache_set(NITOBE_CACHE_HDR
, $files, 'cache', time() + 604800);