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
|
<?php /** @file */
require_once('include/security.php');
function menu_fetch($name,$uid,$observer_xchan) {
$sql_options = permissions_sql($uid);
$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",
intval($r[0]['menu_id']),
intval($uid)
);
return array('menu' => $r[0], 'items' => $x );
}
return null;
}
function menu_render($menu) {
if(! $menu)
return '';
for($x = 0; $x < count($menu['items']); $x ++)
if($menu['items']['mitem_flags'] & MENU_ITEM_ZID)
$menu['items']['link'] = zid($menu['items']['link']);
return replace_macros(get_markup_template('usermenu.tpl'),array(
'$menu' => $menu['menu'],
'$items' => $menu['items']
));
}
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);
}
function menu_create($arr) {
$menu_name = trim(escape_tags($arr['menu_name']));
$menu_desc = trim(escape_tags($arr['menu_desc']));
if(! $menu_desc)
$menu_desc = $menu_name;
if(! $menu_name)
return false;
$menu_channel_id = intval($arr['menu_channel_id']);
$r = q("select * from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
dbesc($menu_name),
intval($menu_channel_id)
);
if($r)
return false;
$r = q("insert into menu ( menu_name, menu_desc, menu_channel_id )
values( '%s', '%s', %d )",
dbesc($menu_name),
dbesc($menu_desc),
intval($menu_channel_id)
);
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;
}
function menu_list($channel_id) {
$r = q("select * from menu where menu_channel_id = %d order by menu_name",
intval($channel_id)
);
return $r;
}
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']));
if(! $menu_desc)
$menu_desc = $menu_name;
if(! $menu_name)
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) && ($r[0]['menu_id'] != $menu_id)) {
logger('menu_edit: duplicate menu name for channel ' . $menu_channel_id);
return false;
}
$menu_channel_id = intval($arr['menu_channel_id']);
$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;
}
$r = q("select * from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
dbesc($menu_name),
intval($menu_channel_id)
);
if($r)
return false;
return q("update menu set menu_name = '%s', menu_desc = '%s'
where menu_id = %d and menu_channel_id = %d limit 1",
dbesc($menu_name),
dbesc($menu_desc),
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)
);
return q("delete from menu where menu_id = %d and menu_channel_id = %d limit 1",
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']);
$allow_cid = perms2str($arr['allow_cid']);
$allow_gid = perms2str($arr['allow_gid']);
$deny_cid = perms2str($arr['deny_cid']);
$deny_gid = perms2str($arr['deny_gid']);
$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),
dbesc($allow_cid),
dbesc($allow_gid),
dbesc($deny_cid),
dbesc($deny_gid),
intval($uid),
intval($menu_id),
intval($mitem_order)
);
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']);
$allow_cid = perms2str($arr['allow_cid']);
$allow_gid = perms2str($arr['allow_gid']);
$deny_cid = perms2str($arr['deny_cid']);
$deny_gid = perms2str($arr['deny_gid']);
$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 limit 1",
dbesc($mitem_link),
dbesc($mitem_desc),
intval($mitem_flags),
dbesc($allow_cid),
dbesc($allow_gid),
dbesc($deny_cid),
dbesc($deny_gid),
intval($mitem_order),
intval($uid),
intval($menu_id),
intval($mitem_id)
);
return $r;
}
function menu_del_item($menu_id,$uid,$item_id) {
$r = q("delete from menu_item where mitem_menu_id = %d and mitem_channel_id = %d and mitem_id = %d limit 1",
intval($menu_id),
intval($uid),
intval($item_id)
);
return $r;
}
|