summaryrefslogtreecommitdiff
path: root/plugins/menu.tikiwiki.php
blob: a0c071d5c3a9e9e2372a18d04e0ebdc4f6f2560b (plain)
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
<?php
/**
 * Plugin for Nexus creating a tikiwiki style menu with the difference of multiple levels being available
 *
 * @abstract creates a javascript expandable menu
 * @author   xing@synapse.plus.com
 * @version  $Revision$
 * @package  nexus
 * @subpackage plugins
 */
global $gNexusSystem;

/**
* definitions
*/
// GUID should be a maximum of 16 chars
define( 'NEXUS_PLUGIN_GUID_TIKIWIKI', 'tikiwiki' );

$pluginParams = array(
	'auto_activate'        => true,
	'write_cache_function' => 'write_tikiwiki_cache',
	'title'                => 'TikiWiki menus',
	'description'          => 'expandable menu reminiscent of the tikiwiki menu',
	'web_link'             => '<a class="external" href = "http://www.tikiwiki.org">TikiWiki</a>',
	'browser_requirements' => 'Most browsers that support javascript should support these menus.',
	'edit_label'           => 'TikiWiki menus',
	'plugin_type'          => 'nexus_plugin',
	'menu_types'           => array(
		'heo' => array( 'label' => 'head expands - open',               'note' => 'Head item serves merely as container and clicking on it will expand the underlying items (initial setting is open).' ),
		'iho' => array( 'label' => 'head expands (with icon) - open',   'note' => 'Head item serves merely as container and clicking on it will expand the underlying items (initial setting is open). Displays an icon along with it.' ),
		'hec' => array( 'label' => 'head expands - closed',             'note' => 'Initial setting is closed.' ),
		'ihc' => array( 'label' => 'head expands (with icon) - closed', 'note' => 'Initial setting is closed. Displays an icon along with it.' ),
		'ieo' => array( 'label' => 'icon expands - open',               'note' => 'Menu head item serves as link and there is an icon to expand the menu (initial setting is open).' ),
		'iec' => array( 'label' => 'icon expands - closed',             'note' => 'Initial setting is closed.' ),
	),
);

$gNexusSystem->registerPlugin( NEXUS_PLUGIN_GUID_TIKIWIKI, $pluginParams );

/**
* exports tikiwiki style menu
* @param $pMenuHash full menu hash
* @return full menu string ready for printing (key serves as cache file path)
*/
function write_tikiwiki_cache( $pMenuHash ) {
	global $gBitSmarty;
	$menu_name = preg_replace( "/ +/", "_", trim( $pMenuHash->mInfo['title'] ) );
	$menu_name = strtolower( $menu_name );
	$menu_file = $pMenuHash->mInfo['cache']['file'];
	$data = '{bitmodule title="'.$pMenuHash->mInfo['title'].'" name="'.$menu_name.'"}';
	$data .= '<div class="tikiwiki menu">';
	// if a permission has been set, we need to work out when to close the {if} clause
	$permCloseIds = [];
	$perm_close = false;
	$perm_cycle = false;
	$type = $pMenuHash->mInfo['menu_type'];
	foreach( $pMenuHash->mInfo['tree'] as $key => $item ) {
		if( $item['first'] ) {
			$data .= '<div id="togid'.$item['item_id'].'">';
		} else {
			// close permission clauses
			if( $perm_cycle ) {
				$data .= '{/if}';
				$perm_cycle = false;
			}
			if( in_array( $item['item_id'], $permCloseIds ) ) {
				$perm_cycle = true;
			}
			if( $perm_close ) {
				$data .= '{/if}';
				$perm_close = false;
			}
		}
		if( $item['last'] ) {
			$data .= '</div>';
		} else {
			if( !empty( $item['perm'] ) ) {
				// open permission if clause
				$data .= '{if $gBitUser->hasPermission("'.$item['perm'].'")}';
				if( !$item['head'] ) {
					$perm_close = true;
				} else {
					$permCloseIds[] = $item['item_id'];
				}
			}
			if( $item['head'] ) {
				$tog_next = 'togid'.$pMenuHash->mInfo['tree'][$key+1]['item_id'];
				if( $type == 'heo' || $type == 'hec' ) {
					$item['display_url'] = "javascript:toggle('".$tog_next."');";
				} else {
					$item['expand_url'] = "javascript:flipIcon('".$tog_next."');";
				}
				$gBitSmarty->assign( 'tog_next', $tog_next );
				$gBitSmarty->assign( 'togid', $tog_next );
			}
			$gBitSmarty->assign( 'item', $item );
			$gBitSmarty->assign( 'type', $type );
			$data .= $gBitSmarty->fetch( NEXUS_PKG_PATH.'templates/'.NEXUS_PLUGIN_GUID_TIKIWIKI.'/item.tpl' );
		}
	}
	$data .= '</div><!-- end .menu -->';

	// apply state of expandable menus
	$data .= '<script>';
	foreach( $pMenuHash->mInfo['tree'] as $key => $item ) {
		if( $item['first'] ) {
			$togid = 'togid'.$item['item_id'];
			$data .= '$(\''.$togid.'\').style.display = \'{if $smarty.cookies.'.$togid.' eq \'c\'}none{elseif $smarty.cookies.'.$togid.' eq \'o\'}block{else}';
			if( $key != 0 && preg_match( "/c$/", $type ) ) {
				$data .= 'none';
			} else {
				$data .= 'block';
			}
			$data .= '{/if}\';';
		}
	}
	$data .= '</script>';

	$data .= '{/bitmodule}';
	$ret[$menu_file] = $data;
	return $ret;
}
?>