Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

views/default/page/components/tabs.php (1 issue)

1
<?php
2
/**
3
 * Tabbed module component
4
 * Provides support for inline and ajax tabbing
5
 *
6
 * @uses $vars['id']      Optional ID of the module
7
 * @uses $vars['class']   Additional classes
8
 * @uses $vars['module']  Module name
9
 *                        Defaults to 'tabs' (.elgg-module-tabs)
10
 * @uses $vars['tabs']    An array of tabs suitable for 'navigation/tabs' view
11
 *                        Each tab should specify either:
12
 *                        - a 'href' parameter to load content via AJAX, or
13
 *                        - a 'content' parameter to display content inline
14
 *                        If the tab uses a 'href' attribute, it should specify
15
 *                        whether the contents should be reloaded on a subsequent
16
 *                        click via 'data-ajax-reload' parameter; by default,
17
 *                        all tabs will be reloading on subsequent clicks.
18
 *                        You can pass additional data to the ajax view via
19
 *                        data-ajax-query attribute (json encoded string).
20
 *                        You can also set the data-ajax-href parameter of the tab,
21
 *                        which will override the href parameter, in case you want
22
 *                        to ensure the tab is clickable even before the JS is bootstrapped.
23
 *                        <code>
24
 *                        [
25
 *                           ['text' => 'Tab 1', 'href' => '/dynamic', 'data-ajax-reload' => true],
26
 *                           ['text' => 'Tab 2', 'href' => '/static', 'data-ajax-reload' => false],
27
 *                           ['text' => 'Tab 3', 'href' => '/static', 'data-ajax-reload' => false, 'data-ajax-query' => json_encode($data)],
28
 *                           ['text' => 'Tab 3', 'href' => '/page', 'data-ajax-href' => '/ajax/page'],
29
 *                           ['text' => 'Tab 4', 'href' => 'Tab content'],
30
 *                        ]
31
 *                        </code>
32
 */
33
$id = elgg_extract('id', $vars);
34
if (!isset($vars['id'])) {
35
	$id = "elgg-tabs-" . base_convert(mt_rand(), 10, 36);
0 ignored issues
show
The call to mt_rand() has too few arguments starting with min. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
	$id = "elgg-tabs-" . base_convert(/** @scrutinizer ignore-call */ mt_rand(), 10, 36);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36
}
37
$vars['id'] = $id;
38
39
$vars['class'] = elgg_extract_class($vars, 'elgg-tabs-component');
40
41
$tabs = (array) elgg_extract('tabs', $vars, []);
42
unset($vars['tabs']);
43
if (empty($tabs)) {
44
	return;
45
}
46
47
$content = '';
48
foreach ($tabs as $index => $tab) {
49
	if (!isset($tab['href']) && !isset($tab['content'])) {
50
		elgg_log('Tab configuration in "page/components/tabs" requires either a "href" or "content" parameter', 'NOTICE');
51
		continue;
52
	}
53
54
	$selected = elgg_extract('selected', $tab);
55
56
	if (isset($tab['content'])) {
57
		$class = ['elgg-content'];
58
		$class[] = $selected ? 'elgg-state-active' : 'hidden';
59
60
		$content .= elgg_format_element('div', [
61
			'class' => $class,
62
			'id' => "{$id}-{$index}",
63
		], $tab['content']);
64
		unset($tab['content']);
65
66
		$tab['href'] = "#{$id}-{$index}";
67
	} else {
68
		if (!isset($tab['data-ajax-reload'])) {
69
			$tab['data-ajax-reload'] = true;
70
		}
71
	}
72
	
73
	$tabs[$index] = $tab;
74
}
75
76
$tabs = elgg_view('navigation/tabs', [
77
	'tabs' => $tabs,
78
]);
79
80
$content = elgg_format_element('div', [
81
	'class' => 'elgg-tabs-content',
82
], $content);
83
84
$module = elgg_extract('module', $vars, 'tabs');
85
unset($vars['module']);
86
$vars['header'] = $tabs;
87
echo elgg_view_module($module, null, $content, $vars);
88
89
elgg_require_js('page/components/tabs');
90