4 * Copyright (c) 2003, The Burgiss Group, LLC
5 * This source code is part of eWiki LiveUser Plugin.
7 * eWiki LiveUser Plugin is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * eWiki LiveUser Plugin is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Wiki LiveUser Plugin; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * ewiki: preferences api for liveuser plugin
25 * @author andy fundinger <afundinger@burgiss.com>
26 * @author alex wan <alex@burgiss.com>
27 * @author jeremy mikola <jmikola@arsjerm.net>
29 * This interface provides functions to save and retrieve user preferences, as
30 * well as to configure available preference fields.
33 require_once(dirname(__FILE__).'/auth_liveuser.php');
34 require_once(dirname(__FILE__).'/liveuser_aux.php');
36 $ewiki_plugins['page']['ChangePrefs'] = 'ewiki_page_liveuser_chprefs';
39 * changes user preferences based on form input
45 function ewiki_page_liveuser_chprefs($id, $data)
47 global $liveuser, $liveuserDB;
49 // if form was submitted, write
50 if (isset($_REQUEST['submit_prefs'])) {
51 return ewiki_t('CHPWFORM');
56 echo ewiki_make_title($id, $id, 2);
58 $results = $liveuserDB->getAll('SELECT * FROM '.LW_PREFIX.'_prefs_fields');
60 foreach ($results as $result) {
61 if (isset($_REQUEST[$result['field_name']])) {
62 liveuser_pref_setPref($liveuser->getHandle(), $result['field_name'], $_REQUEST[$result['field_name']]);
66 echo '<form action="" method="post"><table border="1">';
68 foreach ($results as $result) {
70 echo '<label for="'.$result['field_name'].'">'.$result['field_name'].'</label></td><td>';
71 echo '<input id="'.$result['field_name'].'" name="'.$result['field_name'].'" type="text" ';
72 echo ' value='.liveuser_pref_getPref($liveuser->getHandle(), $result['field_name']).'></td></tr>';
75 echo '<tr><td colspan="2"><input type="reset" text="Reset"><input type="submit"></td></tr></table></form>';
77 $o = ob_get_contents();
84 * Checks if the specified preference already exists for the specified user.
86 * @param string username user handle
87 * @param string field_name preference field name
88 * @return boolean true if preference exists, false otherwise
90 function liveuser_pref_checkPref($username, $field_name)
94 $user_id = liveuser_getPermUserId($username);
95 $field_id = $liveuserDB->getOne('SELECT field_id FROM '.LW_PREFIX.'_prefs_fields WHERE field_name = ?',
98 return (is_numeric($field_id) ? $field_id : false);
102 * Fetches the specified preference's value for the specified user.
104 * @param string username user handle
105 * @param string field_name preference field name
106 * @param boolean useDefault if true, default field value is supplied for non-existent pref, else null is used
107 * @return string preference field value if set, default value or null otherwise
109 function liveuser_pref_getPref($username, $field_name, $useDefault = true)
113 $user_id = liveuser_getPermUserId($username);
114 $field_value = $liveuserDB->getOne('
115 SELECT '.LW_PREFIX.'_prefs_data.field_value
116 FROM '.LW_PREFIX.'_prefs_data, '.LW_PREFIX.'_prefs_fields
117 WHERE '.LW_PREFIX.'_prefs_data.user_id = ?
118 AND '.LW_PREFIX.'_prefs_data.field_id = '.LW_PREFIX.'_prefs_fields.field_id
119 AND '.LW_PREFIX.'_prefs_fields.field_name = ?',
120 array((int)$user_id, $field_name));
122 if (is_null($field_value) && $useDefault) {
123 $field_value = $liveuserDB->getOne('
125 FROM '.LW_PREFIX.'_prefs_fields
126 WHERE field_name = ?',
134 * Sets the specified preference's value for the specified user.
136 * @param string username user handle
137 * @param string field_name preference field name
138 * @return boolean true if the preference was successfully set, false otherwise
140 function liveuser_pref_setPref($username, $field_name, $field_value)
144 $user_id = liveuser_getPermUserId($username);
146 /* attempt to fetch existing field_id for the field_name, or create a new field if necessary */
147 if (($field_id = liveuser_pref_checkField($field_name)) === false &&
148 ($field_id = liveuser_pref_setField($field_name)) === false) {
152 $pref_id = $liveuserDB->getOne('
154 FROM '.LW_PREFIX.'_prefs_data
155 WHERE user_id = ? AND field_id = ?',
156 array((int)$user_id, (int)$field_id));
158 if (isset($pref_id) && is_numeric($pref_id)) {
159 return ($liveuserDB->query('
160 UPDATE '.LW_PREFIX.'_prefs_data
163 array($field_value, (int)$pref_id)) == DB_OK);
165 return ($liveuserDB->query('
166 INSERT INTO '.LW_PREFIX.'_prefs_data (field_value, user_id, field_id) VALUES (?, ?, ?)',
167 array($field_value, (int)$user_id, (int)$field_id)) == DB_OK);
172 * Checks if the specified preference field already exists.
174 * @param mixed field_name field_name or id of preference field to check for
175 * @param boolean publicOnly if true, only check against public fields
176 * @return mixed integer identifier if field exists, false otherwise
178 function liveuser_pref_checkField($field_name, $publicOnly = false)
182 if (is_numeric($field_name)) {
183 $field_id = $liveuserDB->getOne('
185 FROM '.LW_PREFIX.'_prefs_fields
186 WHERE field_id = ? AND public >= ?',
187 array((int)$field_name, (int)$publicOnly));
189 $field_id = $liveuserDB->getOne('
191 FROM '.LW_PREFIX.'_prefs_fields
192 WHERE field_name = ? AND public >= ?',
193 array($field_name, (int)$publicOnly));
196 return (is_numeric($field_id) ? $field_id : false);
200 * Removes a field definition and all references in the preferences data table.
202 * @param mixed field_name field name or id of preference field
203 * @return boolean true if the field was removed successfully, false otherwise
205 function liveuser_pref_removeField($field_name)
209 if (($field_id = liveuser_pref_checkField($field_name)) === false) {
213 if ($liveuserDB->query('DELETE FROM '.LW_PREFIX.'_prefs_data WHERE field_id = ?',
214 array((int)field_id)) != DB_OK) {
218 if ($liveuserDB->query('DELETE FROM '.LW_PREFIX.'_prefs_fields WHERE field_id = ?',
219 array((int)field_id)) != DB_OK) {
225 * Sets a field (adds or updates) with the specified properties.
227 * @param mixed field_name field name or id of preference field
228 * @param boolean public preference field is public if true, private if false
229 * @param string default_value default field value if user preference is not set
230 * @param string possible_values possible_values field value for admin to set and user to choose
231 * @return mixed integer identifier of field if operation was successful, false otherwise
233 function liveuser_pref_setField($field_name, $public = true, $default_value = null, $possible_values = null)
237 if (($field_id = liveuser_pref_checkField($field_name)) !== false) {
238 return ($liveuserDB->query('
239 UPDATE '.LW_PREFIX.'_prefs_fields
240 SET public = ?, default_value = ?, possible_values = ?
242 array((int)$public, $default_value, ($possible_values ? serialize($possible_values) : null), (int)$field_id)) == DB_OK);
244 if ($liveuserDB->query('
245 INSERT INTO '.LW_PREFIX.'_prefs_fields (field_name, public, default_value, possible_values)
246 VALUES (?, ?, ?, ?)',
247 array($field_name, (int)$public, $default_value, ($possible_values ? serialize($possible_values) : null))) != DB_OK) {
250 return liveuser_pref_checkField($field_name);