add selectivizr
[project/basic.git] / template.php
1 <?php
2
3 /**
4 * Here we override the default HTML output of drupal.
5 * refer to http://drupal.org/node/550722
6 */
7
8 // Auto-rebuild the theme registry during theme development.
9 if (theme_get_setting('clear_registry')) {
10 // Rebuild .info data.
11 system_rebuild_theme_data();
12 // Rebuild theme registry.
13 drupal_theme_rebuild();
14 }
15
16 // Add Zen Tabs styles
17 if (theme_get_setting('basic_tabs')) {
18 drupal_add_css( drupal_get_path('theme', 'basic') .'/css/tabs.css');
19 }
20
21 function basic_preprocess_html(&$vars) {
22 if (theme_get_setting('basic_ie_enabled')) {
23 $basic_ie_enabled_versions = theme_get_setting('basic_ie_enabled_versions');
24 if (in_array('ie8', $basic_ie_enabled_versions, TRUE)) {
25 drupal_add_css(path_to_theme() . '/css/ie8.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 8', '!IE' => FALSE), 'preprocess' => FALSE));
26 drupal_add_js(path_to_theme() . '/js/selectivizr.js');
27 }
28 if (in_array('ie9', $basic_ie_enabled_versions, TRUE)) {
29 drupal_add_css(path_to_theme() . '/css/ie9.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 9', '!IE' => FALSE), 'preprocess' => FALSE));
30 }
31 }
32 }
33
34 function basic_preprocess_page(&$vars, $hook) {
35 if (isset($vars['node_title'])) {
36 $vars['title'] = $vars['node_title'];
37 }
38 // Adding a class to #page in wireframe mode
39 if (theme_get_setting('wireframe_mode')) {
40 $vars['classes_array'][] = 'wireframe-mode';
41 }
42 // Adding classes wether #navigation is here or not
43 if (!empty($vars['main_menu']) or !empty($vars['sub_menu'])) {
44 $vars['classes_array'][] = 'with-navigation';
45 }
46 if (!empty($vars['secondary_menu'])) {
47 $vars['classes_array'][] = 'with-subnav';
48 }
49
50 // Add first/last classes to node listings about to be rendered.
51 if (isset($vars['page']['content']['system_main']['nodes'])) {
52 // All nids about to be loaded (without the #sorted attribute).
53 $nids = element_children($vars['page']['content']['system_main']['nodes']);
54 // Only add first/last classes if there is more than 1 node being rendered.
55 if (count($nids) > 1) {
56 $first_nid = reset($nids);
57 $last_nid = end($nids);
58 $first_node = $vars['page']['content']['system_main']['nodes'][$first_nid]['#node'];
59 $first_node->classes_array = array('first');
60 $last_node = $vars['page']['content']['system_main']['nodes'][$last_nid]['#node'];
61 $last_node->classes_array = array('last');
62 }
63 }
64 }
65
66 function basic_preprocess_node(&$vars) {
67 // Add a striping class.
68 $vars['classes_array'][] = 'node-' . $vars['zebra'];
69
70 // Add $unpublished variable.
71 $vars['unpublished'] = (!$vars['status']) ? TRUE : FALSE;
72
73 // Merge first/last class (from basic_preprocess_page) into classes array of current node object.
74 $node = $vars['node'];
75 if (!empty($node->classes_array)) {
76 $vars['classes_array'] = array_merge($vars['classes_array'], $node->classes_array);
77 }
78 }
79
80 function basic_preprocess_block(&$vars, $hook) {
81 // Add a striping class.
82 $vars['classes_array'][] = 'block-' . $vars['block_zebra'];
83
84 // Add first/last block classes
85 $first_last = "";
86 // If block id (count) is 1, it's first in region.
87 if ($vars['block_id'] == '1') {
88 $first_last = "first";
89 $vars['classes_array'][] = $first_last;
90 }
91 // Count amount of blocks about to be rendered in that region.
92 $block_count = count(block_list($vars['elements']['#block']->region));
93 if ($vars['block_id'] == $block_count) {
94 $first_last = "last";
95 $vars['classes_array'][] = $first_last;
96 }
97 }
98
99 /**
100 * Return a themed breadcrumb trail.
101 *
102 * @param $breadcrumb
103 * An array containing the breadcrumb links.
104 * @return
105 * A string containing the breadcrumb output.
106 */
107 function basic_breadcrumb($variables) {
108 $breadcrumb = $variables['breadcrumb'];
109 // Determine if we are to display the breadcrumb.
110 $show_breadcrumb = theme_get_setting('basic_breadcrumb');
111 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
112
113 // Optionally get rid of the homepage link.
114 $show_breadcrumb_home = theme_get_setting('basic_breadcrumb_home');
115 if (!$show_breadcrumb_home) {
116 array_shift($breadcrumb);
117 }
118
119 // Return the breadcrumb with separators.
120 if (!empty($breadcrumb)) {
121 $breadcrumb_separator = theme_get_setting('basic_breadcrumb_separator');
122 $trailing_separator = $title = '';
123 if (theme_get_setting('basic_breadcrumb_title')) {
124 $item = menu_get_item();
125 if (!empty($item['tab_parent'])) {
126 // If we are on a non-default tab, use the tab's title.
127 $title = check_plain($item['title']);
128 }
129 else {
130 $title = drupal_get_title();
131 }
132 if ($title) {
133 $trailing_separator = $breadcrumb_separator;
134 }
135 }
136 elseif (theme_get_setting('basic_breadcrumb_trailing')) {
137 $trailing_separator = $breadcrumb_separator;
138 }
139
140 // Provide a navigational heading to give context for breadcrumb links to
141 // screen-reader users. Make the heading invisible with .element-invisible.
142 $heading = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
143
144 return $heading . '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . $trailing_separator . $title . '</div>';
145 }
146 }
147 // Otherwise, return an empty string.
148 return '';
149 }
150
151 /**
152 * Converts a string to a suitable html ID attribute.
153 *
154 * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
155 * valid ID attribute in HTML. This function:
156 *
157 * - Ensure an ID starts with an alpha character by optionally adding an 'n'.
158 * - Replaces any character except A-Z, numbers, and underscores with dashes.
159 * - Converts entire string to lowercase.
160 *
161 * @param $string
162 * The string
163 * @return
164 * The converted string
165 */
166 function basic_id_safe($string) {
167 // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
168 $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
169 // If the first character is not a-z, add 'n' in front.
170 if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
171 $string = 'id'. $string;
172 }
173 return $string;
174 }
175
176 /**
177 * Generate the HTML output for a menu link and submenu.
178 *
179 * @param $variables
180 * An associative array containing:
181 * - element: Structured array data for a menu link.
182 *
183 * @return
184 * A themed HTML string.
185 *
186 * @ingroup themeable
187 *
188 */
189 function basic_menu_link(array $variables) {
190 $element = $variables['element'];
191 $sub_menu = '';
192
193 if ($element['#below']) {
194 $sub_menu = drupal_render($element['#below']);
195 }
196 $output = l($element['#title'], $element['#href'], $element['#localized_options']);
197 // Adding a class depending on the TITLE of the link (not constant)
198 $element['#attributes']['class'][] = basic_id_safe($element['#title']);
199 // Adding a class depending on the ID of the link (constant)
200 $element['#attributes']['class'][] = 'mid-' . $element['#original_link']['mlid'];
201 return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
202 }
203
204 /**
205 * Override or insert variables into theme_menu_local_task().
206 */
207 function basic_preprocess_menu_local_task(&$variables) {
208 $link =& $variables['element']['#link'];
209
210 // If the link does not contain HTML already, check_plain() it now.
211 // After we set 'html'=TRUE the link will not be sanitized by l().
212 if (empty($link['localized_options']['html'])) {
213 $link['title'] = check_plain($link['title']);
214 }
215 $link['localized_options']['html'] = TRUE;
216 $link['title'] = '<span class="tab">' . $link['title'] . '</span>';
217 }
218
219 /**
220 * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs.
221 */
222 function basic_menu_local_tasks(&$variables) {
223 $output = '';
224
225 if (!empty($variables['primary'])) {
226 $variables['primary']['#prefix'] = '<h2 class="element-invisible">' . t('Primary tabs') . '</h2>';
227 $variables['primary']['#prefix'] .= '<ul class="tabs primary clearfix">';
228 $variables['primary']['#suffix'] = '</ul>';
229 $output .= drupal_render($variables['primary']);
230 }
231 if (!empty($variables['secondary'])) {
232 $variables['secondary']['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2>';
233 $variables['secondary']['#prefix'] .= '<ul class="tabs secondary clearfix">';
234 $variables['secondary']['#suffix'] = '</ul>';
235 $output .= drupal_render($variables['secondary']);
236 }
237 return $output;
238 }