-
Notifications
You must be signed in to change notification settings - Fork 2
/
easy_admin.module
349 lines (295 loc) · 9.36 KB
/
easy_admin.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* @file
* Display a dropdown menu at the top of the window.
*/
/**
* Implements hook_menu().
*/
function easy_admin_menu() {
// Config page.
$items['admin/config/easy_admin'] = array(
'title' => 'easy admin configuration',
'description' => 'Configure easy admin',
'page callback' => 'drupal_get_form',
'page arguments' => array('easy_admin_config_form'),
'access arguments' => array('administer easy admin'),
'file' => 'inc/easy_admin.config_page.inc',
);
return $items;
}
/**
* Create a user field as soon as this module is turned on
*/
function easy_admin_enable() {
// Increase the module weight, so easy_admin catches any alterations made by
// other modules in hook_menu_alter().
db_update('system')
->fields(array('weight' => 100))
->condition('type', 'module')
->condition('name', 'easy_admin')
->execute();
// Tell user that the module was installed and how to configure it.
$t = get_t();
$link = l($t('Manager Menu'), 'admin/structure/menu/manage/manager');
$text = $t("Easy admin was installed. It can be configured here: " . $link);
drupal_set_message(filter_xss_admin($text));
}
/**
* Determines if user has access to easy admin
*/
function easy_admin_enabled() {
global $user;
// Determine if the "easy_admin_show_root" setting grants access
if((int)$user->uid === 1 && (bool)variable_get('easy_admin_show_root', 1)) {
return TRUE;
}
// Determine if the "easy_admin_show_roles" setting grants access
// This is done by intersecting the roles that are enabled in easy admin
// settings with the roles that the current user has (plus anonymous).
$enabled_roles = variable_get('easy_admin_roles', array());
$user_roles = array_keys($user->roles);
// Add anonymous (role id: 1)
array_push($user_roles, 1);
$matched_roles = array_intersect($enabled_roles, $user_roles);
return (!empty($matched_roles));
}
function easy_admin_preprocess_html(&$vars) {
// Add a body class if easy admin is present
if (easy_admin_enabled()) {
$vars['classes_array'][] = 'easy-admin-menu';
}
}
/**
* Implements hook_page_alter().
*/
function easy_admin_page_alter(&$page) {
// Do not show menu, if user hasn't access to it
if (!easy_admin_enabled()) {
return '';
}
// Do not display with core overlay, this creates duplicates menus, one in the
// main page and one in the overlay iframe
if (function_exists('overlay_get_mode')) {
if (overlay_get_mode() == 'child') {
return FALSE;
}
}
// Do not show menu on dialog page in the "media" module.
if (!isset($page['#theme'])) {
return '';
}
// Hide menu if exo is present
if (function_exists('exo_frame_view')) {
return '';
}
// Do not show menu on dialog created by references dialog module (https://drupal.org/project/references_dialog)
if ($page['#theme'] == 'references_dialog_page') {
return '';
}
$page['page_top']['easy_admin'] = array(
'#markup' => easy_admin_output(),
'#weight' => -50,
);
}
/**
* Helper to create a data structure representing a menu item in the dropdown.
*
* @param string $title
* The title of the menu item.
* @param string $href
* The link of the menu item.
* @param array $classes
* CSS classes that will be put on the menu item (with default theming, these
* are put on the <li> tag).
*/
function easy_admin_create_menu_item($title, $href, $classes = array(), $localized_options = array()) {
return array(
'title' => $title,
'href' => $href,
'classes' => $classes,
'localized_options' => $localized_options,
'children' => NULL,
);
}
/**
* Create the easy admin.
*
*/
function easy_admin_output() {
$menu_name = 'manager';
$tree = easy_admin_get_tree($menu_name);
return theme('easy_admin_toolbar', array('tree' => $tree));
}
/**
* Render the toolbar.
*
* @param array $variables
* - tree: A hierarical data structure suitable for
* theme_easy_admin_tree().
*
* @ingroup themeable
*/
function theme_easy_admin_toolbar($variables) {
drupal_add_js(drupal_get_path('module', 'easy_admin') . '/assets/js/easy_admin.js');
drupal_add_css(drupal_get_path('module', 'easy_admin') . '/assets/css/easy_admin.css');
$tree = $variables['tree'];
global $user;
$user = user_load($user->uid);
$style = 'square_profile_pic';
$info = field_info_instance('user', 'field_profile_picture', 'user');
$default_fid = $info['settings']['default_image'];
$default_img = file_load($default_fid);
// if this field exists create a variable that points to the file uri
if(!empty($user->field_profile_picture)) {
$profile_pic = $user->field_profile_picture['und']['0'];
$profile_pic_uri = $profile_pic['uri'];
}
// if a custom img is set assign that to a varaible or
// assign the default image to the variable.
if (!empty($default_img) && !empty($profile_pic_uri)) {
$img_url = $profile_pic_uri;
}elseif (!empty($default_img) && empty($profile_pic_uri)) {
$img_url = $default_img->uri;
}else {
$img_url = NULL;
}
//Build the html structure of the easy admin
$html = '<div class="easy-admin"><div class="firstLevel">';
$html .= '<ul class="current-user"><li>';
if ($img_url != NULL) {
$html .= '<a href="/user/' . $user->uid . '/edit" title="Edit My Profile">';
$html .= '<img src="' . image_style_url($style, $img_url) . '" alt="profile" />';
$html .= '</a>';
}
$html .= '<span>Welcome</span>';
$html .= '<a href="/user/' . $user->uid . '/edit" title="Edit My Profile">';
$html .= $user->name;
$html .= '</a>';
$html .= '</li></ul>';
$html .= theme('easy_admin_tree', array('tree' => $tree));
// User Logout button
$html .= '<ul class="logout"><li><a href="/user/logout" title="User Logout"> <i></i> <span>Logout</span></a></li></ul>';
$html .= '</div></div>';
return $html;
}
/**
* Render a menu tree.
*
* @param array $variables
* An associative array containing:
* - tree: An array of menu items. Each menu item is
* suitable for being rendered by theme_easy_admin_subtree
*
* @ingroup themeable
*/
function theme_easy_admin_tree($variables) {
$menu_items = $variables['tree'];
// Build the admin menu unordered list from 'manager-menu'
$html = '<ul class="manager-menu">';
foreach ($menu_items as $menu_item) {
$html .= theme('easy_admin_item', array('menu_item' => $menu_item));
}
$html .= '</ul>';
// Return all of it
return $html;
}
/**
* Render a menu item, including its children.
*
* @param array $variables
* An associative array containing:
* - menu_item: An associative array containing:
* - title: The title of the menu link
* - href: The link
* - classes: An array of classes
* - children: An array of menu items. Suitable for being rendered by
* theme_easy_admin_tree.
*
* @ingroup themeable
*/
function theme_easy_admin_item($variables) {
$menu_item = $variables['menu_item'];
if (isset($menu_item['localized_options']['attributes']['class'][0])) {
$menu_item_class = $menu_item['localized_options']['attributes']['class'][0];
$html = '<li class="' . $menu_item_class . '">';
} else {
$html = '<li class="placeholder">';
}
$html .= l($menu_item['title'], $menu_item['href'], $menu_item['localized_options']);
if (isset($menu_item['children'])) {
$html .= '<div class="subMenu">' . theme('easy_admin_tree', array('tree' => $menu_item['children'])) . '</div>';
}
$html .= '</li>';
return $html;
}
/**
* Get a tree.
*
* @param string $menu_name
* The name of the menu.
*
* @return array
* A data structure suitable for theme_easy_admin_tree().
*/
function easy_admin_get_tree($menu_name) {
$tree = menu_tree_all_data($menu_name);
// Allow i18n module to translate strings where available.
if (module_exists('i18n_menu')) {
$tree = i18n_menu_localize_tree($tree);
}
$menu_items = easy_admin_create_datastructure_from_tree($tree);
return $menu_items;
}
/**
* Build a datastructure suitable for theme_easy_admin_tree.
*
* @param array $tree
* A tree structure like the returned by menu_tree_all_data.
*
* @return array
* A data structure suitable for theme_easy_admin_tree().
*/
function easy_admin_create_datastructure_from_tree($tree) {
$new_menu_list = array();
foreach ($tree as $element) {
// Skip disabled links.
if ($element['link']['hidden'] == 1) {
continue;
}
$new_menu_item = easy_admin_create_menu_item($element['link']['title'], $element['link']['href'], array(), $element['link']['localized_options']);
if (isset($element['below']) && count($element['below']) > 0) {
$new_menu_item['classes'][] = 'expanded';
$new_menu_item['children'] = easy_admin_create_datastructure_from_tree($element['below']);
}
$new_menu_list[] = $new_menu_item;
}
return $new_menu_list;
}
/**
* Implements hook_permission().
*/
function easy_admin_permission() {
return array(
'administer easy admin' => array(
'title' => t('Administer easy admin'),
'description' => t('Configure easy admin'),
),
);
}
/**
* Implements hook_theme().
*/
function easy_admin_theme() {
return array(
'easy_admin_toolbar' => array(
'variables' => array('tree' => array()),
),
'easy_admin_tree' => array(
'variables' => array('tree' => array()),
),
'easy_admin_item' => array(
'variables' => array('menu_item' => array()),
),
);
}