aboutsummaryrefslogtreecommitdiffstats
path: root/include/comanche.php
blob: cf7ecd7c575655949d420d5c8a24f99333c6fcfc (plain) (blame)
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 /** @file */

require_once('include/security.php');

// When editing a webpage - a dropdown is needed to select a page layout
// On submit, the pdl_select value (which is the mid of an item with item_restrict = ITEM_PDL) is stored in 
// the webpage's resource_id, with resource_type 'pdl'.

// Then when displaying a webpage, we can see if it has a pdl attached. If not we'll 
// use the default site/page layout.

// If it has a pdl we'll load it as we know the mid and pass the body through comanche_parser() which will generate the 
// page layout from the given description


function pdl_selector($uid,$current="") {

	$o = '';

	// You can use anybody's Comanche layouts on this site that haven't been protected in some way

	$sql_extra = item_permissions_sql($uid);

	// By default order by title (therefore at this time pdl's need a unique title across this system), 
	// though future work may allow categorisation
	// based on taxonomy terms

	$r = q("select title, mid from item where (item_restrict & %d) $sql_extra order by title",
		intval(ITEM_PDL)
	);

	$arr = array('channel_id' => $uid, 'current' => $current, 'entries' => $r);
	call_hooks('pdl_selector',$arr);

	$entries = $arr['entries'];
	$current = $arr['current'];		
 
	$o .= "<select name=\"pdl_select\" id=\"pdl_select\" size=\"1\" >";
	$entries[] = array('title' => t('Default'), 'mid' => '');
	foreach($entries as $selection) {
		$selected = (($selection == $current) ? ' selected="selected" ' : '');
		$o .= "<option value=\"{$selection['mid']}\" $selected >{$selection['title']}</option>";
	}

	$o .= '</select>';
	return $o;
}	



function comanche_parser(&$a,$s) {


	$cnt = preg_match("/\[layout\](.*?)\[\/layout\]/ism", $matches, $s);
	if($cnt)
		$a->page['template'] = trim($matches[1]);

	$cnt = preg_match("/\[theme\](.*?)\[\/theme\]/ism", $matches, $s);
	if($cnt)
		$a->layout['theme'] = trim($matches[1]);

	$cnt = preg_match_all("/\[region=(.*?)\](.*?)\[\/region\]/ism", $matches, $s, PREG_SET_ORDER);
	if($cnt) {
		foreach($matches as $mtch) {
			$a->layout['region_' . $mtch[1]] = comanche_region($a,$mtch[2]);
		}
	}

}


function comanche_menu($name) {
	$a = get_app();
	$m = menu_fetch($name,$a->profile['profile_uid'],get_observer_hash());
	return render_menu($m);
}

function comanche_replace_region($match) {
	$a = get_app();
	if(array_key_exists($match[1],$a->page))
		return $a->page[$match[1]];
}

// Widgets will have to get any operational arguments from the session,
// the global app environment, or config storage until we implement argument passing


function comanche_widget($name,$args = null) {
	$a = get_app();
	$func = 'widget_' . trim($name);
	if(function_exists($func))
		return $func($args);
}


function comanche_region(&$a,$s) {


	$cnt = preg_match_all("/\[menu\](.*?)\[\/menu\]/ism", $matches, $s, PREG_SET_ORDER);
	if($cnt) {
		foreach($matches as $mtch) {
			$s = str_replace($mtch[0],comanche_menu(trim($mtch[1])),$s);
		}
	}

	// need to modify this to accept parameters

	$cnt = preg_match_all("/\[widget\](.*?)\[\/widget\]/ism", $matches, $s, PREG_SET_ORDER);
	if($cnt) {
		foreach($matches as $mtch) {
			$s = str_replace($mtch[0],comanche_widget(trim($mtch[1])),$s);
		}
	}

	return $s;
}


function widget_profile($args) {
	$a = get_app();
	$block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
	return profile_sidebar($a->profile, $block, true);
}