1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This is the class which should make it easier to add new options to the admin menu |
---|
5 | * It may only be invoked statically like: options::add(...); |
---|
6 | * more functions will be added as needed, if you want one, add it or contact exi |
---|
7 | */ |
---|
8 | |
---|
9 | class options |
---|
10 | { |
---|
11 | // i just want to make sure.... |
---|
12 | function options() |
---|
13 | { |
---|
14 | trigger_error('The class "options" may only be invoked statically.', E_USER_ERROR); |
---|
15 | } |
---|
16 | |
---|
17 | // field = generic, subfield = look and feel, name = banner, type = select |
---|
18 | function add($field, $subfield, $set, $description, $name, $type, $buildcallback = '', $onchange = '', $hint = '') |
---|
19 | { |
---|
20 | $data = &options::_getData(); |
---|
21 | $data[$field][$subfield][$set][] = array('descr' => $description, 'name' => $name, 'type' => $type, |
---|
22 | 'callback' => $buildcallback, |
---|
23 | 'onchange' => $onchange, 'hint' => $hint); |
---|
24 | } |
---|
25 | |
---|
26 | // fast add uses the last used category by options::cat so you don't have to retype everything |
---|
27 | function fadd($description, $name, $type, $buildcallback = '', $onchange = '', $hint = '') |
---|
28 | { |
---|
29 | global $options_faddcat; |
---|
30 | |
---|
31 | $data = &options::_getData(); |
---|
32 | $data[$options_faddcat[0]][$options_faddcat[1]][$options_faddcat[2]][] = array('descr' => $description, 'name' => $name, 'type' => $type, |
---|
33 | 'callback' => $buildcallback, |
---|
34 | 'onchange' => $onchange, 'hint' => $hint); |
---|
35 | } |
---|
36 | |
---|
37 | function cat($field, $subfield, $set) |
---|
38 | { |
---|
39 | global $options_faddcat; |
---|
40 | |
---|
41 | $options_faddcat = array($field, $subfield, $set); |
---|
42 | } |
---|
43 | |
---|
44 | // this will emulate the old options menu |
---|
45 | function oldMenu($field, $subfield, $link) |
---|
46 | { |
---|
47 | $data = &options::_getData(); |
---|
48 | |
---|
49 | $data[$field][$subfield] = $link; |
---|
50 | } |
---|
51 | |
---|
52 | // this handles the submit from the optionspage |
---|
53 | function handlePost() |
---|
54 | { |
---|
55 | $data = &options::_getData(); |
---|
56 | |
---|
57 | $current = &$data[$_POST['field']][$_POST['sub']]; |
---|
58 | foreach ($current as $elements) |
---|
59 | { |
---|
60 | foreach ($elements as $element) |
---|
61 | { |
---|
62 | // for callbacks we check their callback function on postdata to deal with it |
---|
63 | if ($element['callback']) |
---|
64 | { |
---|
65 | if (!is_callable($element['callback'])) |
---|
66 | { |
---|
67 | trigger_error('Unable to callback to '.$element['callback'][0].'::'.$element['callback'][1], E_USER_ERROR); |
---|
68 | return false; |
---|
69 | } |
---|
70 | call_user_func($element['callback']); |
---|
71 | continue; |
---|
72 | } |
---|
73 | |
---|
74 | // for checkboxes we need to set the value to zero if the option is not there |
---|
75 | if ($element['type'] == 'checkbox') |
---|
76 | { |
---|
77 | if ($_POST['option'][$element['name']] == 'on') |
---|
78 | { |
---|
79 | config::set($element['name'], '1'); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | config::set($element['name'], '0'); |
---|
84 | } |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | // edits and options will be set directly |
---|
89 | config::set($element['name'], $_POST['option'][$element['name']]); |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | function genOptionsPage() |
---|
96 | { |
---|
97 | $data = &options::_getData(); |
---|
98 | |
---|
99 | $field = $_REQUEST['field']; |
---|
100 | $sub = $_REQUEST['sub']; |
---|
101 | |
---|
102 | global $smarty, $page; |
---|
103 | |
---|
104 | if (is_object($page)) |
---|
105 | { |
---|
106 | $page->setTitle('Administration - '.$sub); |
---|
107 | } |
---|
108 | |
---|
109 | // create the option field |
---|
110 | $smarty->assign('field', $field); |
---|
111 | $smarty->assign('sub', $sub); |
---|
112 | $html = $smarty->fetch(get_tpl('admin_options_field_head')); |
---|
113 | |
---|
114 | // create all option sets |
---|
115 | foreach ($data[$field][$sub] as $set => $options) |
---|
116 | { |
---|
117 | $smarty->assign('set', $set); |
---|
118 | $html .= $smarty->fetch(get_tpl('admin_options_set_head')); |
---|
119 | |
---|
120 | // create all options in the set |
---|
121 | foreach ($options as $option) |
---|
122 | { |
---|
123 | $html .= options::assembleElement($option); |
---|
124 | } |
---|
125 | $html .= $smarty->fetch(get_tpl('admin_options_set_foot')); |
---|
126 | } |
---|
127 | $html .= $smarty->fetch(get_tpl('admin_options_field_foot')); |
---|
128 | return $html; |
---|
129 | } |
---|
130 | |
---|
131 | function assembleElement(&$element) |
---|
132 | { |
---|
133 | global $smarty; |
---|
134 | |
---|
135 | // this will extract all options into an array |
---|
136 | $options = array(); |
---|
137 | if (strpos($element['type'], ':')) |
---|
138 | { |
---|
139 | $array = explode(':', $element['type']); |
---|
140 | $element['type'] = array_shift($array); |
---|
141 | |
---|
142 | $max = count($array); |
---|
143 | for ($i=0; $i<=$max; $i+=2) |
---|
144 | { |
---|
145 | // make sure we assign a value |
---|
146 | if (isset($array[$i+1])) |
---|
147 | { |
---|
148 | $options[$array[$i]] = $array[$i+1]; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | if ($element['type'] == 'select') |
---|
154 | { |
---|
155 | if (!is_callable($element['callback'])) |
---|
156 | { |
---|
157 | trigger_error('Unable to callback to '.$element['callback'][0].'::'.$element['callback'][1], E_USER_ERROR); |
---|
158 | return false; |
---|
159 | } |
---|
160 | |
---|
161 | $option = call_user_func($element['callback']); |
---|
162 | $smarty->assign('options', $option); |
---|
163 | $smarty->assign_by_ref('opt', $element); |
---|
164 | return $smarty->fetch(get_tpl('admin_options_select')); |
---|
165 | } |
---|
166 | |
---|
167 | if ($element['type'] == 'checkbox') |
---|
168 | { |
---|
169 | $smarty->assign_by_ref('opt', $element); |
---|
170 | return $smarty->fetch(get_tpl('admin_options_checkbox')); |
---|
171 | } |
---|
172 | |
---|
173 | if ($element['type'] == 'edit') |
---|
174 | { |
---|
175 | $smarty->assign_by_ref('opt', $element); |
---|
176 | |
---|
177 | if (!$options['size']) |
---|
178 | { |
---|
179 | $options['size'] = 20; |
---|
180 | } |
---|
181 | if (!$options['maxlength']) |
---|
182 | { |
---|
183 | $options['maxlength'] = 80; |
---|
184 | } |
---|
185 | $smarty->assign_by_ref('options', $options); |
---|
186 | return $smarty->fetch(get_tpl('admin_options_edit')); |
---|
187 | } |
---|
188 | |
---|
189 | // for a custom element we call the callback to get the html we want |
---|
190 | if ($element['type'] == 'custom') |
---|
191 | { |
---|
192 | if (!is_callable($element['callback'])) |
---|
193 | { |
---|
194 | trigger_error('Unable to callback to '.$element['callback'][0].'::'.$element['callback'][1], E_USER_ERROR); |
---|
195 | return false; |
---|
196 | } |
---|
197 | |
---|
198 | $element['html'] = call_user_func($element['callback']); |
---|
199 | $smarty->assign_by_ref('opt', $element); |
---|
200 | return $smarty->fetch(get_tpl('admin_options_custom')); |
---|
201 | } |
---|
202 | |
---|
203 | // unknown/not implemented element type |
---|
204 | return $element['name']; |
---|
205 | } |
---|
206 | |
---|
207 | function genAdminMenu() |
---|
208 | { |
---|
209 | $data = &options::_getData(); |
---|
210 | |
---|
211 | // sort the menu alphabetically |
---|
212 | ksort($data); |
---|
213 | |
---|
214 | // create a standardbox to print all links into |
---|
215 | $menubox = new Box('Options'); |
---|
216 | $menubox->setIcon('menu-item.gif'); |
---|
217 | foreach ($data as $field => $subfields) |
---|
218 | { |
---|
219 | $menubox->addOption('caption', $field); |
---|
220 | |
---|
221 | foreach ($subfields as $subfield => $array) |
---|
222 | { |
---|
223 | // if this subfield has no options then it is a category |
---|
224 | if (!is_array($array)) |
---|
225 | { |
---|
226 | $menubox->addOption('link', $subfield, $array); |
---|
227 | continue; |
---|
228 | } |
---|
229 | |
---|
230 | // we're not a category, make it klickable |
---|
231 | $menubox->addOption('link', $subfield, '?a=admin&field='.$field.'&sub='.$subfield); |
---|
232 | } |
---|
233 | $lastfield = $field; |
---|
234 | } |
---|
235 | return $menubox->generate(); |
---|
236 | } |
---|
237 | |
---|
238 | // private data storage to store all options |
---|
239 | function &_getData() |
---|
240 | { |
---|
241 | static $data; |
---|
242 | |
---|
243 | if (!isset($data)) |
---|
244 | { |
---|
245 | $data = array(); |
---|
246 | } |
---|
247 | return $data; |
---|
248 | } |
---|
249 | } |
---|
250 | ?> |
---|