soo_plugin_pref Page 2 of 4 « »
soo_plugin_pref source code
1 : // event handler called by other plugins on plugin_lifecycle and plugin_prefs events
2 : function soo_plugin_pref( $event, $step, $defaults ) {
3 : preg_match('/^(.+)\.(.+)/', $event, $match);
4 : list( , $type, $plugin) = $match;
5 : $message = $step ? soo_plugin_pref_query($plugin, $step, $defaults): '';
6 : if ( $type == 'plugin_prefs' )
7 : soo_plugin_pref_ui($plugin, $defaults, $message);
8 : }
9 :
10 : // user interface for preference setting (plugin_prefs events)
11 : function soo_plugin_pref_ui( $plugin, $defaults, $message = '' ) {
12 : $cols = 2;
13 : $align_rm = ' style="text-align:right;vertical-align:middle"';
14 : $prefs = soo_plugin_pref_query($plugin, 'select');
15 :
16 : // install prefs if necessary
17 : if ( $defaults and ! $prefs ) {
18 : soo_plugin_pref_query($plugin, 'enabled', $defaults);
19 : $prefs = soo_plugin_pref_query($plugin, 'select');
20 : }
21 :
22 : pagetop(gTxt('edit_preferences') . " › $plugin", $message);
23 : echo
24 : n. '<form method="post" name="soo_plugin_pref_form">' .
25 : n. startTable('list') .
26 : tr(n. tdcs(hed(
27 : gTxt('plugin') .' '. gTxt('edit_preferences') . ": $plugin"
28 : , 1),
29 : $cols))
30 : ;
31 :
32 : foreach ( $prefs as $pref ) {
33 : extract($pref);
34 : $name = str_replace("$plugin.", '', $name);
35 : $input = $html == 'yesnoradio' ?
36 : yesnoRadio($name, $val) :
37 : fInput('text', $name, $val, 'edit', '', '', 20);
38 : echo
39 : n. tr(
40 : n.t. tda($defaults[$name]['text'], $align_rm) .
41 : n. td($input)
42 : );
43 : }
44 :
45 : echo
46 : n. sInput('update') .
47 : n. eInput("plugin_prefs.$plugin") .
48 : n. tr(n. tdcs(fInput('submit', 'soo_plugin_pref_update',
49 : gTxt('save'), 'publish'), $cols)) .
50 : tr(n. tdcs(href(
51 : gTxt('go_to') .' '. gTxt('plugins') .' '. gTxt('list')
52 : , '?event=plugin')
53 : ,$cols)) .
54 : endTable() . '</form>' .
55 : n;
56 : }
57 :
58 : // preference CRUD
59 : function soo_plugin_pref_query( $plugin, $action, $defaults = array() ) {
60 :
61 : if ( $action == 'select' )
62 : return safe_rows(
63 : 'name, val, html, position',
64 : 'txp_prefs',
65 : "name like '$plugin.%' order by position asc"
66 : );
67 :
68 : elseif ( $action == 'update' ) {
69 : $post = doSlash(stripPost());
70 : $allowed = array_keys(soo_plugin_pref_vals($plugin));
71 : foreach ( $post as $name => $val )
72 : if ( in_array($name, $allowed) )
73 : if ( ! set_pref("$plugin.$name", $val) )
74 : $error = true;
75 : return empty($error) ? gTxt('preferences_saved') : '';
76 : }
77 :
78 : elseif ( $action == 'enabled' ) {
79 : $prefs = soo_plugin_pref_vals($plugin);
80 : $add = array_diff_key($defaults, $prefs);
81 : $remove = array_diff_key($prefs, $defaults);
82 : foreach ( $add as $name => $pref )
83 : set_pref(
84 : $plugin . '.' . $name,
85 : $pref['val'],
86 : 'plugin_prefs',
87 : 2,
88 : $pref['html']
89 : );
90 : foreach ( $remove as $name => $val )
91 : safe_delete('txp_prefs', "name = '$plugin.$name'");
92 :
93 : // update position values
94 : foreach ( array_keys($defaults) as $i => $name )
95 : safe_update('txp_prefs',
96 : "position = $i",
97 : "name = '$plugin.$name'");
98 : }
99 :
100 : elseif ( $action == 'deleted' )
101 : safe_delete('txp_prefs', "name like '$plugin.%'");
102 : }
103 :
104 : // get a plugin's prefs; return as associative name:value array
105 : function soo_plugin_pref_vals( $plugin ) {
106 : $rs = soo_plugin_pref_query($plugin, 'select');
107 : foreach ( $rs as $r ) {
108 : extract ($r);
109 : $name = str_replace("$plugin.", '', $name);
110 : $out[$name] = $val;
111 : }
112 : return isset($out) ? $out : array();
113 : }








