-
Notifications
You must be signed in to change notification settings - Fork 8
/
extras.php
executable file
·614 lines (536 loc) · 19.7 KB
/
extras.php
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<?php
/** PAGE.TPL.PHP PREPROCESS VARIABLES
---------------------------------------------------------- */
function megatron_preprocess_page(&$variables) {
// Secondary nav
$variables['secondary_nav'] = FALSE;
if ($variables['secondary_menu']) {
$secondary_menu = menu_load(variable_get('menu_secondary_links_source', 'user-menu'));
// Build links
$tree = menu_tree_page_data($secondary_menu['menu_name']);
$variables['secondary_menu'] = megatron_menu_navigation_links($tree);
// Build list
$variables['secondary_nav'] = theme('megatron_btn_dropdown', array(
'links' => $variables['secondary_menu'],
'label' => $secondary_menu['title'],
'type' => 'success',
'attributes' => array(
'id' => 'user-menu',
'class' => array('pull-right'),
),
'heading' => array(
'text' => t('Secondary menu'),
'level' => 'h2',
'class' => array('element-invisible'),
),
));
}
// Replace tabs with dropdown version
$variables['tabs']['#primary'] = _megatron_local_tasks($variables['tabs']['#primary']);
}
/** GENERAL
A wrapper function for megatron_theme_get_settings().
*
* @param $name
* The name of the setting that you want to retrieve.
* @param $default (optional)
* The name (key) of the theme that you want to fetch the
* setting for. Defaults to NULL.
* @param $theme (optional)
* The key (machine-readable name) of a theme. Defaults to the key of the
* current theme if not defined.
*
* @see megatron_theme_get_setting().
*/
/*
function megatron_theme_get_setting($name, $theme = NULL) {
switch($name) {
case 'exclude':
$setting = megatron_theme_get_info($name, $theme);
break;
default:
$setting = theme_get_setting($name, $theme);
break;
}
return isset($setting) ? $setting : NULL;
}
function megatron_get_settings($theme = NULL) {
if (!isset($theme)) {
$theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : '';
}
if ($theme) {
$themes = list_themes();
$theme_object = $themes[$theme];
}
return $theme_object->info['settings'];
}*/
/** theme_megatron_progress_bar
---------------------------------------------------------- */
function megatron_megatron_progress_bar($variables) {
$variables['attributes']['class'][] = 'progress';
return '<div' . drupal_attributes($variables['attributes']) . '>
<div class="bar" style="width: ' . $variables['percent'] . '%;"></div>
</div>';
}
/** MENUS
Returns HTML for primary and secondary local tasks.
---------------------------------------------------------- */
function megatron_menu_local_tasks(&$variables) {
$output = '';
if ( !empty($variables['primary']) ) {
$variables['primary']['#prefix'] = '<ul class="nav nav-tabs">';
$variables['primary']['#suffix'] = '</ul>';
$output .= drupal_render($variables['primary']);
}
if ( !empty($variables['secondary']) ) {
$variables['secondary']['#prefix'] = '<ul class="nav nav-pills">';
$variables['secondary']['#suffix'] = '</ul>';
$output .= drupal_render($variables['secondary']);
}
return $output;
}
/** Returns HTML for primary and secondary local tasks.
---------------------------------------------------------- */
function megatron_menu_local_task($variables) {
$link = $variables['element']['#link'];
$link_text = $link['title'];
$classes = array();
if (!empty($variables['element']['#active'])) {
// Add text to indicate active tab for non-visual users.
$active = '<span class="element-invisible">' . t('(active tab)') . '</span>';
// If the link does not contain HTML already, check_plain() it now.
// After we set 'html'=TRUE the link will not be sanitized by l().
if (empty($link['localized_options']['html'])) {
$link['title'] = check_plain($link['title']);
}
$link['localized_options']['html'] = TRUE;
$link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));
$classes[] = 'active';
}
// Render child tasks if available.
$children = '';
if (element_children($variables['element'])) {
$link['localized_options']['attributes']['class'][] = 'dropdown-toggle';
$link['localized_options']['attributes']['data-toggle'][] = 'dropdown';
$classes[] = 'dropdown';
$children = drupal_render_children($variables['element']);
$children = '</b><ul class="secondary-tabs dropdown-menu">' . $children . "</ul>";
return '<li class="' . implode(' ', $classes) . '"><a href="#"' . drupal_attributes($link['localized_options']['attributes']) .'>' . $link_text . '<div class="ubc7-arrow down-arrow"></div>' . $children . "</li>\n";
}
else {
return '<li class="' . implode(' ', $classes) . '">' . l($link_text, $link['href'], $link['localized_options']) . $children . "</li>\n";
}
}
/** Get all primary tasks including subsets
---------------------------------------------------------- */
function _megatron_local_tasks($tabs = FALSE) {
if ($tabs == '')
return $tabs;
if (!$tabs)
$tabs = menu_primary_local_tasks();
foreach($tabs as $key => $element) {
$result = db_select('menu_router', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('menu_router')
->condition('tab_parent', $element['#link']['path'])
->condition('context', MENU_CONTEXT_INLINE, '<>')
->condition('type', array(MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_TASK), 'IN')
->orderBy('weight')
->orderBy('title')
->execute();
$router_item = menu_get_item($element['#link']['path']);
$map = $router_item['original_map'];
$i = 0;
foreach ($result as $item) {
_menu_translate($item, $map, TRUE);
//only add items that we have access to
if ($item['tab_parent'] && $item['access']) {
//set path to that of parent for the first item
if ($i === 0) {
$item['href'] = $item['tab_parent'];
}
if (current_path() == $item['href']) {
$tabs[$key][] = array(
'#theme' => 'menu_local_task',
'#link' => $item,
'#active' => TRUE,
);
}
else {
$tabs[$key][] = array(
'#theme' => 'menu_local_task',
'#link' => $item,
);
}
//only count items we have access to.
$i++;
}
}
}
return $tabs;
}
/** FORMS
Implements hook_form_alter().
---------------------------------------------------------- */
function megatron_form_alter(&$form, &$form_state, $form_id) {
// Id's of forms that should be ignored
// Make this configurable?
$form_ids = array(
'node_form',
'system_site_information_settings',
'user_profile_form',
);
// Only wrap in container for certain form
if (isset($form['#form_id']) && !in_array($form['#form_id'], $form_ids) && !isset($form['#node_edit_form'])) {
$form['actions']['#theme_wrappers'] = array();
}
}
/** Returns HTML for a form element.
---------------------------------------------------------- */
function megatron_form_element(&$variables) {
$element = &$variables['element'];
// This is also used in the installer, pre-database setup.
$t = get_t();
// This function is invoked as theme wrapper, but the rendered form element
// may not necessarily have been processed by form_builder().
$element += array(
'#title_display' => 'before',
);
// Add element #id for #type 'item'.
if (isset($element['#markup']) && !empty($element['#id'])) {
$attributes['id'] = $element['#id'];
}
// Add bootstrap class
$attributes['class'] = array('control-group');
// Check for errors and set correct error class
if (isset($element['#parents']) && form_get_error($element)) {
$attributes['class'][] = 'error';
}
if (!empty($element['#type'])) {
$attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
}
if (!empty($element['#name'])) {
$attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
}
// Add a class for disabled elements to facilitate cross-browser styling.
if (!empty($element['#attributes']['disabled'])) {
$attributes['class'][] = 'form-disabled';
}
$output = '<div' . drupal_attributes($attributes) . '>' . "\n";
// If #title is not set, we don't display any label or required marker.
if (!isset($element['#title'])) {
$element['#title_display'] = 'none';
}
$prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
$suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
switch ($element['#title_display']) {
case 'before':
case 'invisible':
$output .= ' ' . theme('form_element_label', $variables);
$output .= '<div class="controls">';
$output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
break;
case 'after':
$output .= '<div class="controls">';
$variables['#children'] = ' ' . $prefix . $element['#children'] . $suffix;
$output .= ' ' . theme('form_element_label', $variables) . "\n";
break;
case 'none':
case 'attribute':
// Output no label and no required marker, only the children.
$output .= '<div class="controls">';
$output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
break;
}
if ( !empty($element['#description']) ) {
$output .= '<p class="help-block">' . $element['#description'] . "</p>\n";
}
$output .= "</div></div>\n";
return $output;
}
/** Returns HTML for a form element label and required marker.
---------------------------------------------------------- */
function megatron_form_element_label(&$variables) {
$element = $variables['element'];
// This is also used in the installer, pre-database setup.
$t = get_t();
// If title and required marker are both empty, output no label.
if ((!isset($element['#title']) || $element['#title'] === '') && empty($element['#required'])) {
return '';
}
// If the element is required, a required marker is appended to the label.
$required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '';
$title = filter_xss_admin($element['#title']);
$attributes = array();
// Style the label as class option to display inline with the element.
if ($element['#title_display'] == 'after') {
$attributes['class'][] = 'option';
$attributes['class'][] = $element['#type'];
}
// Show label only to screen readers to avoid disruption in visual flows.
elseif ($element['#title_display'] == 'invisible') {
$attributes['class'][] = 'element-invisible';
}
if (!empty($element['#id'])) {
$attributes['for'] = $element['#id'];
}
// @Bootstrap: Add Bootstrap control-label class.
$attributes['class'][] = 'control-label';
// @Bootstrap: Insert radio and checkboxes inside label elements.
$output = '';
if ( isset($variables['#children']) ) {
$output .= $variables['#children'];
}
// @Bootstrap: Append label
$output .= $t('!title !required', array('!title' => $title, '!required' => $required));
// The leading whitespace helps visually separate fields from inline labels.
return ' <label' . drupal_attributes($attributes) . '>' . $output . "</label>\n";
}
/** Preprocessor for theme('button').
---------------------------------------------------------- */
function megatron_preprocess_button(&$variables) {
$variables['element']['#attributes']['class'][] = 'btn';
if (isset($variables['element']['#value'])) {
$classes = array(
// Specifics.
t('Save and add') => 'btn-info',
t('Add another item') => 'btn-info',
t('Add effect') => 'btn-primary',
t('Add and configure') => 'btn-primary',
t('Update style') => 'btn-primary',
t('Download feature') => 'btn-primary',
// Generals.
t('Save') => 'btn-primary',
t('Apply') => 'btn-primary',
t('Create') => 'btn-primary',
t('Confirm') => 'btn-primary',
t('Submit') => 'btn-primary',
t('Export') => 'btn-primary',
t('Import') => 'btn-primary',
t('Restore') => 'btn-primary',
t('Rebuild') => 'btn-primary',
t('Search') => 'btn-primary',
t('Add') => 'btn-info',
t('Update') => 'btn-info',
t('Delete') => 'btn-danger',
t('Remove') => 'btn-danger',
);
foreach ($classes as $search => $class) {
if (strpos($variables['element']['#value'], $search) !== FALSE) {
$variables['element']['#attributes']['class'][] = $class;
break;
}
}
}
}
/** Returns HTML for a button form element.
---------------------------------------------------------- */
function megatron_button($variables) {
$element = $variables['element'];
$label = check_plain($element['#value']);
element_set_attributes($element, array('id', 'name', 'value', 'type'));
$element['#attributes']['class'][] = 'form-' . $element['#button_type'];
if (!empty($element['#attributes']['disabled'])) {
$element['#attributes']['class'][] = 'form-button-disabled';
}
// The ending line break adds inherent margin between multiple buttons.
return '<button' . drupal_attributes($element['#attributes']) . '>'. $label .'</button>' . "\n";
}
/** PAGER */
/* Returns HTML for a query pager.
*
* Menu callbacks that display paged query results should call theme('pager') to
* retrieve a pager control so that users can view other results. Format a list
* of nearby pages with additional query results.
*
* @param $variables
* An associative array containing:
* - tags: An array of labels for the controls in the pager.
* - element: An optional integer to distinguish between multiple pagers on
* one page.
* - parameters: An associative array of query string parameters to append to
* the pager links.
* - quantity: The number of pages in the list.
*
* @ingroup themeable
*/
function megatron_pager($variables) {
$output = "";
$tags = $variables['tags'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$quantity = $variables['quantity'];
global $pager_page_array, $pager_total;
// Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2);
// current is the page we are currently paged to
$pager_current = $pager_page_array[$element] + 1;
// first is the first page listed by this pager piece (re quantity)
$pager_first = $pager_current - $pager_middle + 1;
// last is the last page listed by this pager piece (re quantity)
$pager_last = $pager_current + $quantity - $pager_middle;
// max is the maximum page number
$pager_max = $pager_total[$element];
// End of marker calculations.
// Prepare for generation loop.
$i = $pager_first;
if ($pager_last > $pager_max) {
// Adjust "center" if at end of query.
$i = $i + ($pager_max - $pager_last);
$pager_last = $pager_max;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
// End of generation loop preparation.
$li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('first')), 'element' => $element, 'parameters' => $parameters));
$li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
$li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
$li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last')), 'element' => $element, 'parameters' => $parameters));
if ($pager_total[$element] > 1) {
/*
if ($li_first) {
$items[] = array(
'class' => array('pager-first'),
'data' => $li_first,
);
}
*/
if ($li_previous) {
$items[] = array(
'class' => array('prev'),
'data' => $li_previous,
);
}
// When there is more than one page, create the pager list.
if ($i != $pager_max) {
if ($i > 1) {
$items[] = array(
'class' => array('pager-ellipsis'),
'data' => '…',
);
}
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
$items[] = array(
// 'class' => array('pager-item'),
'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => array('active'), // Add the active class
'data' => l($i, '#', array('fragment' => '', 'external' => TRUE)),
);
}
if ($i > $pager_current) {
$items[] = array(
//'class' => array('pager-item'),
'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
);
}
}
if ($i < $pager_max) {
$items[] = array(
'class' => array('pager-ellipsis'),
'data' => '…',
);
}
}
// End generation.
if ($li_next) {
$items[] = array(
'class' => array('next'),
'data' => $li_next,
);
}
/*
if ($li_last) {
$items[] = array(
'class' => array('pager-last'),
'data' => $li_last,
);
}
*/
return '<div class="pagination">'. theme('item_list', array(
'items' => $items,
//'attributes' => array('class' => array('pager')),
)) . '</div>';
}
return $output;
}
/**
* Implements theme_item_list().
*/
function megatron_item_list($variables) {
$items = $variables['items'];
$title = $variables['title'];
$type = $variables['type'];
$attributes = $variables['attributes'];
$output = '';
if (isset($title)) {
$output .= '<h3>' . $title . '</h3>';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
$num_items = count($items);
foreach ($items as $i => $item) {
$attributes = array();
$children = array();
$data = '';
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
// Render nested list.
$data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes));
}
if ($i == 0) {
$attributes['class'][] = 'first';
}
if ($i == $num_items - 1) {
$attributes['class'][] = 'last';
}
$output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
}
$output .= "</$type>";
}
return $output;
}
/** Add Bootstrap table class to tables added by Drupal
---------------------------------------------------------- */
function megatron_preprocess_table(&$variables) {
if (!isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = array('table', 'table-striped');
}
else {
$variables['attributes']['class'][] = 'table';
$variables['attributes']['class'][] = 'table-striped';
}
}
/** VIEWS
Provides views theme override functions for Bootstrap themes.
Add Bootstrap table class to views tables.
---------------------------------------------------------- */
function megatron_preprocess_views_view_table(&$variables) {
$variables['classes_array'][] = 'table';
}
function megatron_preprocess_views_view_grid(&$variables) {
$variables['class'] .= ' table';
}