Simpletest Coverage - modules/toolbar/toolbar.module

1 <?php
2 // $Id: toolbar.module,v 1.5 2009/08/14 15:30:59 webchick Exp $
3
4 /**
5 * @file
6 * Administration toolbar for quick access to top level administration items.
7 */
8
9 /**
10 * Implementation of hook_permission().
11 */
12 function toolbar_permission() {
13 return array(
14 'access toolbar' => array(
15 'title' => t('Access administration toolbar'),
16 'description' => t('Access the persistent administration toolbar displayed on all pages.'),
17 ),
18 );
19 }
20
21 /**
22 * Implement hook_theme().
23 */
24 function toolbar_theme($existing, $type, $theme, $path) {
25 $items['toolbar'] = array(
26 'arguments' => array('toolbar' => array()),
27 'template' => 'toolbar',
28 'path' => drupal_get_path('module', 'toolbar'),
29 );
30 return $items;
31 }
32
33 /**
34 * Implement hook_page_alter().
35 *
36 * Add admin toolbar to the page_top region automatically.
37 */
38 function toolbar_page_alter(&$page) {
39 if (user_access('access toolbar')) {
40 $page['page_top']['toolbar'] = toolbar_build();
41 }
42 }
43
44 /**
45 * Implement hook_preprocess_page().
46 *
47 * Add some page classes, so global page theming can adjust to the toolbar.
48 */
49 function toolbar_preprocess_page(&$vars) {
50 if (user_access('access toolbar')) {
51 $vars['classes_array'][] = 'toolbar toolbar-shortcuts';
52 }
53 }
54
55 /**
56 * Build the admin menu as a structured array ready for drupal_render().
57 */
58 function toolbar_build() {
59 global $user;
60
61 $module_path = drupal_get_path('module', 'toolbar');
62 $build = array(
63 '#theme' => 'toolbar',
64 '#attached_js' => array(
65 $module_path . '/toolbar.js',
66 array('data' => 'misc/jquery.cookie.js', 'weight' => JS_LIBRARY + 2),
67 ),
68 '#attached_css' => array(
69 $module_path . '/toolbar.css',
70 ),
71 );
72
73 // Retrieve the admin menu from the database.
74 $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
75 $build['toolbar_menu'] = array(
76 '#theme' => 'links',
77 '#links' => $links,
78 '#attributes' => array('id' => 'toolbar-menu'),
79 );
80
81 // Add logout & user account links
82 $build['toolbar_user'] = array(
83 '#theme' => 'links',
84 '#links' => array(
85 'account' => array(
86 'title' => t('Hello <strong>@username</strong>', array('@username' => $user->name)),
87 'href' => 'user',
88 'html' => TRUE,
89 ),
90 'logout' => array(
91 'title' => t('Logout'),
92 'href' => 'user/logout',
93 ),
94 ),
95 '#attributes' => array('id' => 'toolbar-user'),
96 );
97
98 // Add convenience shortcut links.
99 $shortcuts = menu_tree_all_data('admin_shortcuts');
100 $shortcuts = toolbar_menu_navigation_links($shortcuts);
101 $build['toolbar_shortcuts'] = array(
102 '#theme' => 'links',
103 '#links' => $shortcuts,
104 '#attributes' => array('id' => 'toolbar-shortcuts'),
105 );
106
107 return $build;
108 }
109
110 /**
111 * Get only the top level items below the 'admin' path.
112 */
113 function toolbar_get_menu_tree() {
114 $tree = array();
115 $admin_link = db_query("SELECT * FROM {menu_links} WHERE menu_name = 'management' AND module = 'system' AND link_path = 'admin'")->fetchAssoc();
116 if ($admin_link) {
117 $tree = menu_tree_all_data('management', $admin_link);
118 // The tree will be a sub-tree with the admin link as a single root item.
119 $admin_link = array_pop($tree);
120 $tree = $admin_link['below'] ? $admin_link['below'] : array();
121 foreach ($tree as $key => $item) {
122 // Get rid of subitems to have a leaner data structure.
123 unset($tree[$key]['below']);
124 }
125 }
126 return $tree;
127 }
128
129 /**
130 * Generate a links array from a menu tree array.
131 *
132 * Based on menu_navigation_links(). Adds in path based IDs, icon placeholders
133 * and overlay classes for the links.
134 */
135 function toolbar_menu_navigation_links($tree) {
136 $links = array();
137 foreach ($tree as $item) {
138 if (!$item['link']['hidden'] && $item['link']['access']) {
139 $class = '';
140 // Make sure we have a path specific ID in place, so we can attach icons
141 // and behaviors to the items.
142 $id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['href']);
143
144 $link = $item['link']['localized_options'];
145 $link['href'] = $item['link']['href'];
146 // Add icon placeholder.
147 $link['title'] = '<span class="icon"></span>' . $item['link']['title'];
148 // Add admin link ID and to-overlay class for the overlay.
149 $link['attributes'] = array('id' => 'toolbar-link-' . $id, 'class' => 'to-overlay');
150 $link['html'] = TRUE;
151
152 $class = ' path-' . $id;
153 if (toolbar_in_active_trail($item['link']['href'])) {
154 $class .= ' active-trail';
155 }
156 $links['menu-' . $item['link']['mlid'] . $class] = $link;
157 }
158 }
159 return $links;
160 }
161
162 /**
163 * Checks whether an item is in the active trail.
164 *
165 * Useful when using a menu generated by menu_tree_all_data() which does
166 * not set the 'in_active_trail' flag on items.
167 *
168 * @todo
169 * Look at migrating to a menu system level function.
170 */
171 function toolbar_in_active_trail($path) {
172 $active_paths = &drupal_static(__FUNCTION__);
173
174 // Gather active paths.
175 if (!isset($active_paths)) {
176 $active_paths = array();
177 $trail = menu_get_active_trail();
178 foreach ($trail as $item) {
179 if (!empty($item['href'])) {
180 $active_paths[] = $item['href'];
181 }
182 }
183 }
184 return in_array($path, $active_paths);
185 }
186

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.