aboutsummaryrefslogtreecommitdiffstats
path: root/include/comanche.php
blob: cca7cb0f6bea10d37339edfda0808764d86c4b12 (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
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 */

require_once('include/security.php');
require_once('include/menu.php');
require_once('include/widgets.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 = '';

	$sql_extra = item_permissions_sql($uid);

	$r = q("select item_id.*, mid from item_id left join item on iid = item.id where item_id.uid = %d and item_id.uid = item.uid and service = 'PDL' $sql_extra order by sid asc",
		intval($uid)
	);

	$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['sid']}</option>";
	}

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



function comanche_parser(&$a, $s, $pass = 0) {
	$matches = array();

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

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

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

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

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

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

		$cnt = preg_match_all("/\[webpage\](.*?)\[\/webpage\]/ism", $s, $matches, PREG_SET_ORDER);
		if($cnt) {
			// only the last webpage definition is used if there is more than one
			foreach($matches as $mtch) {
				$a->layout['webpage'] = comanche_webpage($a,$mtch[1]);
			}
		}

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

	}

}


function comanche_menu($s, $class = '') {

	$channel_id = comanche_get_channel_id();
	$name = $s;

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

	if($channel_id) {
		$m = menu_fetch($name,$channel_id, get_observer_hash());
		return menu_render($m, $class, $edit = false, $var);
	}
}

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

/**
 * @brief Returns the channel_id of the profile owner of the page.
 *
 * Returns the channel_id of the profile owner of the page, or the local_channel
 * if there is no profile owner. Otherwise returns 0.
 *
 * @return channel_id
 */
function comanche_get_channel_id() {
	$channel_id = ((is_array(get_app()->profile)) ? get_app()->profile['profile_uid'] : 0);

	if ((! $channel_id) && (local_channel()))
		$channel_id = local_channel();

	return $channel_id;
}

function comanche_block($s, $class = '') {
	$var = array();
	$matches = array();
	$name = $s;
	$class = (($class) ? $class : 'bblock widget');

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

	$o = '';
	$channel_id = comanche_get_channel_id();

	if($channel_id) {
		$r = q("select * from item inner join item_id on iid = item.id and item_id.uid = item.uid and item.uid = %d and service = 'BUILDBLOCK' and sid = '%s' limit 1",
			intval($channel_id),
			dbesc($name)
		);

		if($r) {
			$o .= (($var['wrap'] == 'none') ? '' : '<div class="' . $class . '">');

			if($r[0]['title'] && trim($r[0]['title']) != '$content') {
				$o .= '<h3>' . $r[0]['title'] . '</h3>';
			}

			if($r[0]['title'] && trim($r[0]['title']) === '$content') {
				$o .= '<h3>' . get_app()->data['webpage'][0]['title'] . '</h3>';
			}


			if(trim($r[0]['body']) === '$content') {
				$o .= prepare_text(get_app()->data['webpage'][0]['body'], get_app()->data['webpage'][0]['mimetype']);
			}
			else {
				$o .= prepare_text($r[0]['body'], $r[0]['mimetype']);
			}

			$o .= (($var['wrap'] == 'none') ? '' : '</div>');
		}
	}

	return $o;
}

function comanche_js($s) {

	switch($s) {
		case 'jquery':
			$path = 'view/js/jquery.js';
			break;
		case 'bootstrap':
			$path = 'library/bootstrap/js/bootstrap.min.js';
			break;
	}

	return '<script src="' . z_root() . '/' . $path . '" ></script>';

}

function comanche_css($s) {

	switch($s) {
		case 'bootstrap':
			$path = 'library/bootstrap/css/bootstrap.min.css';
			break;
	}

	return '<link rel="stylesheet" href="' . z_root() . '/' . $path . '" type="text/css" media="screen">';

}

// This doesn't really belong in Comanche, but it could also be argued that it is the perfect place.
// We need to be able to select what kind of template and decoration to use for the webpage at the heart of our content.
// For now we'll allow an '[authored]' element which defaults to name and date, or 'none' to remove these, and perhaps
// 'full' to provide a social network style profile photo.
// But leave it open to have richer templating options and perhaps ultimately discard this one, once we have a better idea
// of what template and webpage options we might desire. 

function comanche_webpage(&$a,$s) {
	$ret = array();
	$matches = array();

	$cnt = preg_match_all("/\[authored\](.*?)\[\/authored\]/ism", $s, $matches, PREG_SET_ORDER);
	if($cnt) {
		foreach($matches as $mtch) {
			$ret['authored'] = $mtch[1];
		}
	}
	return $ret;
}


/**
 * Widgets will have to get any operational arguments from the session, the
 * global app environment, or config storage until we implement argument passing
 *
 * @param string $name
 * @param string $text
 */
function comanche_widget($name, $text) {
	$vars = array();
	$matches = array();

	$cnt = preg_match_all("/\[var=(.*?)\](.*?)\[\/var\]/ism", $text, $matches, PREG_SET_ORDER);
	if ($cnt) {
		foreach ($matches as $mtch) {
			$vars[$mtch[1]] = $mtch[2];
		}
	}

	if(file_exists('widget/' . trim($name) . '.php'))
		require_once('widget/' . trim($name) . '.php');

	$func = 'widget_' . trim($name);
	if (function_exists($func))
		return $func($vars);
}


function comanche_region(&$a, $s) {
	$matches = array();

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

	// menu class e.g. [menu=horizontal]my_menu[/menu] or [menu=tabbed]my_menu[/menu]
	// allows different menu renderings to be applied

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

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

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

	$cnt = preg_match_all("/\[css\](.*?)\[\/css\]/ism", $s, $matches, PREG_SET_ORDER);
	if($cnt) {
		foreach($matches as $mtch) {
			$s = str_replace($mtch[0],comanche_css(trim($mtch[1])),$s);
		}
	}
	// need to modify this to accept parameters

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

	return $s;
}


/*
 * @function register_page_template($arr)
 *   Registers a page template/variant for use by Comanche selectors
 * @param array $arr
 *    'template' => template name
 *    'variant' => array(
 *           'name' => variant name
 *           'desc' => text description
 *           'regions' => array(
 *               'name' => name
 *               'desc' => text description
 *           )
 *    )
 */


function register_page_template($arr) {
	get_app()->page_layouts[$arr['template']] = array($arr['variant']);
	return;
}