menu.php 10.7 KB
Newer Older
1 2 3
<?php /** @file */

require_once('include/security.php');
4
require_once('include/bbcode.php');
5

6 7 8

function menu_fetch($name,$uid,$observer_xchan) {

redmatrix's avatar
redmatrix committed
9
	$sql_options = permissions_sql($uid,$observer_xchan);
10 11 12 13 14 15 16 17 18

	$r = q("select * from menu where menu_channel_id = %d and menu_name = '%s' limit 1",
		intval($uid),
		dbesc($name)
	);
	if($r) {
		$x = q("select * from menu_item where mitem_menu_id = %d and mitem_channel_id = %d
			$sql_options 
			order by mitem_order asc, mitem_desc asc",
friendica's avatar
friendica committed
19
			intval($r[0]['menu_id']),
20 21
			intval($uid)
		);
friendica's avatar
friendica committed
22
		return array('menu' => $r[0], 'items' => $x );
23
	}
friendica's avatar
friendica committed
24

25 26 27
	return null;
}
	
28
function menu_element($channel,$menu) {
29 30 31

	$arr = array();
	$arr['type'] = 'menu';
32 33 34 35
	$arr['pagetitle'] = $menu['menu']['menu_name'];
	$arr['desc'] = $menu['menu']['menu_desc'];
	$arr['created'] = $menu['menu']['menu_created'];
	$arr['edited'] = $menu['menu']['menu_edited'];
36

37
	$arr['baseurl'] = z_root();
redmatrix's avatar
redmatrix committed
38
	if($menu['menu']['menu_flags']) {
39
		$arr['flags'] = array();
redmatrix's avatar
redmatrix committed
40
		if($menu['menu']['menu_flags'] & MENU_BOOKMARK)
41
			$arr['flags'][] = 'bookmark';
redmatrix's avatar
redmatrix committed
42
		if($menu['menu']['menu_flags'] & MENU_SYSTEM)
43 44 45 46 47 48
			$arr['flags'][] = 'system';
	}
	if($menu['items']) {
		$arr['items'] = array();
		foreach($menu['items'] as $it) {
			$entry = array();
49 50 51

			$entry['link'] = str_replace(z_root() . '/channel/' . $channel['channel_address'],'[channelurl]',$it['mitem_link']);
			$entry['link'] = str_replace(z_root() . '/page/' . $channel['channel_address'],'[pageurl]',$it['mitem_link']);
redmatrix's avatar
redmatrix committed
52
			$entry['link'] = str_replace(z_root() . '/cloud/' . $channel['channel_address'],'[cloudurl]',$it['mitem_link']);
53
			$entry['link'] = str_replace(z_root(),'[baseurl]',$it['mitem_link']);
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
			$entry['desc'] = $it['mitem_desc'];
			$entry['order'] = $it['mitem_order'];
			if($it['mitem_flags']) {
				$entry['flags'] = array();
				if($it['mitem_flags'] & MENU_ITEM_ZID)
					$entry['flags'][] = 'zid';
				if($it['mitem_flags'] & MENU_ITEM_NEWWIN)
					$entry['flags'][] = 'new-window';
				if($it['mitem_flags'] & MENU_ITEM_CHATROOM)
					$entry['flags'][] = 'chatroom';
			}
			$arr['items'][] = $entry;
		}
	}	

	return $arr;
}



75
function menu_render($menu, $class='', $edit = false, $var = array()) {
76

77 78
	if(! $menu)
		return '';
friendica's avatar
friendica committed
79

redmatrix's avatar
redmatrix committed
80
	$channel_id = ((is_array(App::$profile)) ? App::$profile['profile_uid'] : 0);
81 82 83
	if ((! $channel_id) && (local_channel()))
		$channel_id = local_channel();

Zot's avatar
Zot committed
84 85 86 87
	$chan = channelx_by_n($channel_id);
	if(! $chan)
		return '';

88
	$menu_list = menu_list($channel_id);
89
	$menu_names = array();
90 91 92 93 94 95

	foreach($menu_list as $menus) {
		if($menus['menu_name'] != $menu['menu']['menu_name'])
			$menu_names[] = $menus['menu_name'];
	}

96
	for($x = 0; $x < count($menu['items']); $x ++) {
97
		if(in_array($menu['items'][$x]['mitem_link'], $menu_names)) {
98
			$m = menu_fetch($menu['items'][$x]['mitem_link'], $channel_id, get_observer_hash());
99
			$submenu = menu_render($m, 'dropdown-menu', $edit = false, array('wrap' => 'none'));
100 101 102
			$menu['items'][$x]['submenu'] = $submenu;
		}

friendica's avatar
friendica committed
103 104
		if($menu['items'][$x]['mitem_flags'] & MENU_ITEM_ZID)
			$menu['items'][$x]['mitem_link'] = zid($menu['items'][$x]['mitem_link']);
105

friendica's avatar
friendica committed
106 107
		if($menu['items'][$x]['mitem_flags'] & MENU_ITEM_NEWWIN)
			$menu['items'][$x]['newwin'] = '1';
108

109
		$menu['items'][$x]['mitem_desc'] = zidify_links(smilies(bbcode($menu['items'][$x]['mitem_desc'])));
110
	}
111

112 113
	$wrap = (($var['wrap'] === 'none') ? false : true);

114
	$ret = replace_macros(get_markup_template('usermenu.tpl'),array(
115
		'$menu' => $menu['menu'],
116
		'$class' => $class,
Zot's avatar
Zot committed
117
		'$nick' => $chan['channel_address'],
friendica's avatar
friendica committed
118
		'$edit' => (($edit) ? t("Edit") : ''),
119
		'$id' => $menu['menu']['menu_id'],
120 121
		'$items' => $menu['items'],
		'$wrap' => $wrap
122
	));
123 124

	return $ret;
125
}
126 127


128

129 130 131 132 133 134 135 136 137 138 139 140
function menu_fetch_id($menu_id,$channel_id) {

	$r = q("select * from menu where menu_id = %d and menu_channel_id = %d limit 1",
		intval($menu_id),
		intval($channel_id)
	);

	return (($r) ? $r[0] : false);
}



141 142 143
function menu_create($arr) {
	$menu_name = trim(escape_tags($arr['menu_name']));
	$menu_desc = trim(escape_tags($arr['menu_desc']));
144 145
	$menu_flags = intval($arr['menu_flags']);

146 147 148
	//allow menu_desc (title) to be empty
	//if(! $menu_desc)
	//	$menu_desc = $menu_name;
149 150 151 152

	if(! $menu_name)
		return false;

153 154 155
	if(! $menu_flags)
		$menu_flags = 0;

156 157 158

	$menu_channel_id = intval($arr['menu_channel_id']);

159
	$r = q("select * from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
160
		dbesc($menu_name),
friendica's avatar
friendica committed
161
		intval($menu_channel_id)
162 163 164 165 166
	);

	if($r)
		return false;

167 168
	$t = datetime_convert();

169 170
	$r = q("insert into menu ( menu_name, menu_desc, menu_flags, menu_channel_id, menu_created, menu_edited ) 
		values( '%s', '%s', %d, %d, '%s', '%s' )",
171 172
 		dbesc($menu_name),
		dbesc($menu_desc),
173
		intval($menu_flags),
174
		intval($menu_channel_id),
redmatrix's avatar
redmatrix committed
175 176
		dbesc(datetime_convert('UTC','UTC',(($arr['menu_created']) ? $arr['menu_created'] : $t))),
		dbesc(datetime_convert('UTC','UTC',(($arr['menu_edited']) ? $arr['menu_edited'] : $t)))
177 178 179 180 181 182 183 184 185 186 187 188 189 190
	);
	if(! $r)
		return false;

	$r = q("select menu_id from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
		dbesc($menu_name),
		intval($menu_channel_id)
	);
	if($r)
		return $r[0]['menu_id'];
	return false;

}

191 192 193 194 195 196
/**
 * If $flags is present, check that all the bits in $flags are set
 * so that MENU_SYSTEM|MENU_BOOKMARK will return entries with both
 * bits set. We will use this to find system generated bookmarks.
 */

friendica's avatar
friendica committed
197
function menu_list($channel_id, $name = '', $flags = 0) {
198

friendica's avatar
friendica committed
199
	$sel_options = '';
200
	$sel_options .= (($name) ? " and menu_name = '" . protect_sprintf(dbesc($name)) . "' " : '');
friendica's avatar
friendica committed
201
	$sel_options .= (($flags) ? " and menu_flags = " . intval($flags) . " " : '');
202

203
	$r = q("select * from menu where menu_channel_id = %d $sel_options order by menu_desc",
friendica's avatar
friendica committed
204 205 206 207 208
		intval($channel_id)
	);
	return $r;
}

209
function menu_list_count($channel_id, $name = '', $flags = 0) {
friendica's avatar
friendica committed
210

211 212 213 214 215 216 217 218 219
	$sel_options = '';
	$sel_options .= (($name) ? " and menu_name = '" . protect_sprintf(dbesc($name)) . "' " : '');
	$sel_options .= (($flags) ? " and menu_flags = " . intval($flags) . " " : '');

	$r = q("select count(*) as total from menu where menu_channel_id = %d $sel_options",
		intval($channel_id)
	);
	return $r[0]['total'];
}
220 221 222 223 224 225 226

function menu_edit($arr) {

	$menu_id   = intval($arr['menu_id']);

	$menu_name = trim(escape_tags($arr['menu_name']));
	$menu_desc = trim(escape_tags($arr['menu_desc']));
227
	$menu_flags = intval($arr['menu_flags']);
228

229 230 231
	//allow menu_desc (title) to be empty
	//if(! $menu_desc)
	//	$menu_desc = $menu_name;
232 233 234 235

	if(! $menu_name)
		return false;

236 237 238
	if(! $menu_flags)
		$menu_flags = 0;

239

240 241
	$menu_channel_id = intval($arr['menu_channel_id']);

242
	$r = q("select menu_id from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
243 244 245 246 247 248 249 250
		dbesc($menu_name),
		intval($menu_channel_id)
	);
	if(($r) && ($r[0]['menu_id'] != $menu_id)) {
		logger('menu_edit: duplicate menu name for channel ' . $menu_channel_id);
		return false;
	}

251 252 253 254 255 256 257 258 259
	$r = q("select * from menu where menu_id = %d and menu_channel_id = %d limit 1",
		intval($menu_id),
		intval($menu_channel_id)
	);
	if(! $r) {
		logger('menu_edit: not found: ' . print_r($arr,true));
		return false;
	}

260
	return q("update menu set menu_name = '%s', menu_desc = '%s', menu_flags = %d, menu_edited = '%s'
261
		where menu_id = %d and menu_channel_id = %d", 
262 263
 		dbesc($menu_name),
		dbesc($menu_desc),
264
		intval($menu_flags),
265
		dbesc(datetime_convert()),
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
		intval($menu_id),
		intval($menu_channel_id)
	);
}

function menu_delete($menu_name, $uid) {
	$r = q("select menu_id from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
		dbesc($menu_name),
		intval($uid)
	);

	if($r)
		return menu_delete_id($r[0]['menu_id'],$uid);
	return false;
}

function menu_delete_id($menu_id, $uid) {
	$r = q("select menu_id from menu where menu_id = %d and menu_channel_id = %d limit 1",
		intval($menu_id),
		intval($uid)
	);
	if($r) {
		$x = q("delete from menu_item where mitem_menu_id = %d and mitem_channel_id = %d",
			intval($menu_id),
			intval($uid)
		);
292
		return q("delete from menu where menu_id = %d and menu_channel_id = %d",
293 294 295 296 297 298 299 300 301 302 303 304 305 306
			intval($menu_id),
			intval($uid)
		);
	}			
	return false;
}


function menu_add_item($menu_id, $uid, $arr) {

	$mitem_link = escape_tags($arr['mitem_link']);
	$mitem_desc = escape_tags($arr['mitem_desc']);
	$mitem_order = intval($arr['mitem_order']);	
	$mitem_flags = intval($arr['mitem_flags']);
307

friendica's avatar
friendica committed
308
	if(local_channel() == $uid) {
redmatrix's avatar
redmatrix committed
309
		$channel = App::get_channel();	
310 311
	}

312
	$acl = new Zotlabs\Access\AccessList($channel);
313 314
	$acl->set_from_array($arr);
	$p = $acl->get();
315 316 317 318 319

	$r = q("insert into menu_item ( mitem_link, mitem_desc, mitem_flags, allow_cid, allow_gid, deny_cid, deny_gid, mitem_channel_id, mitem_menu_id, mitem_order ) values ( '%s', '%s', %d, '%s', '%s', '%s', '%s', %d, %d, %d ) ",
		dbesc($mitem_link),
		dbesc($mitem_desc),
		intval($mitem_flags),
320 321 322 323
		dbesc($p['allow_cid']),
		dbesc($p['allow_gid']),
		dbesc($p['deny_cid']),
		dbesc($p['deny_gid']),
324 325 326 327
		intval($uid),
		intval($menu_id),
		intval($mitem_order)
	);
328 329 330 331 332 333 334

	$x = q("update menu set menu_edited = '%s' where menu_id = %d and menu_channel_id = %d",
		dbesc(datetime_convert()),
		intval($menu_id),
		intval($uid)
	);

335 336 337 338 339 340 341 342 343 344 345 346
	return $r;

}

function menu_edit_item($menu_id, $uid, $arr) {


	$mitem_id = intval($arr['mitem_id']);
	$mitem_link = escape_tags($arr['mitem_link']);
	$mitem_desc = escape_tags($arr['mitem_desc']);
	$mitem_order = intval($arr['mitem_order']);	
	$mitem_flags = intval($arr['mitem_flags']);
347 348


friendica's avatar
friendica committed
349
	if(local_channel() == $uid) {
redmatrix's avatar
redmatrix committed
350
		$channel = App::get_channel();	
351 352
	}

353
	$acl = new Zotlabs\Access\AccessList($channel);
354 355 356
	$acl->set_from_array($arr);
	$p = $acl->get();

357

358
	$r = q("update menu_item set mitem_link = '%s', mitem_desc = '%s', mitem_flags = %d, allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s', mitem_order = %d  where mitem_channel_id = %d and mitem_menu_id = %d and mitem_id = %d",
359 360 361
		dbesc($mitem_link),
		dbesc($mitem_desc),
		intval($mitem_flags),
362 363 364 365
		dbesc($p['allow_cid']),
		dbesc($p['allow_gid']),
		dbesc($p['deny_cid']),
		dbesc($p['deny_gid']),
366 367 368 369 370
		intval($mitem_order),
		intval($uid),
		intval($menu_id),
		intval($mitem_id)
	);
371 372 373 374 375 376 377

	$x = q("update menu set menu_edited = '%s' where menu_id = %d and menu_channel_id = %d",
		dbesc(datetime_convert()),
		intval($menu_id),
		intval($uid)
	);

378 379 380 381 382 383 384
	return $r;
}




function menu_del_item($menu_id,$uid,$item_id) {
385
	$r = q("delete from menu_item where mitem_menu_id = %d and mitem_channel_id = %d and mitem_id = %d",
386 387 388 389
		intval($menu_id),
		intval($uid),
		intval($item_id)
	);
390 391 392 393 394 395 396

	$x = q("update menu set menu_edited = '%s' where menu_id = %d and menu_channel_id = %d",
		dbesc(datetime_convert()),
		intval($menu_id),
		intval($uid)
	);

397 398 399
	return $r;
}

redmatrix's avatar
redmatrix committed
400 401
function menu_sync_packet($uid,$observer_hash,$menu_id,$delete = false) {
	$r = menu_fetch_id($menu_id,$uid);
402
	$c = channelx_by_n($uid);
redmatrix's avatar
redmatrix committed
403
	if($r) {
redmatrix's avatar
redmatrix committed
404
		$m = menu_fetch($r['menu_name'],$uid,$observer_hash);	
redmatrix's avatar
redmatrix committed
405 406 407
		if($m) {
			if($delete)
				$m['menu_delete'] = 1;
408
			build_sync_packet($uid,array('menu' => array(menu_element($c,$m))));
redmatrix's avatar
redmatrix committed
409 410 411
		}
	}
}