/** * admin gui for modifying LiveUser preferences * * @param string id * @param mixed data * @param string action * @return string page output response */ function ewiki_page_liveuser_admin_prefs($id, $data, $action) { global $liveuserDB; ob_start(); echo ewiki_make_title($id, $id, 2); // delete or update preference fields if (isset($_POST['changeprefs_submit'])) { // extract input fields foreach ($_POST as $key => $value) { list($prefix, $id) = explode('_', $key, 2); // deleted checked preferences if ($prefix == 'chk' && is_numeric($id) && $value == 'on') { if (liveuser_pref_removeField($id)) { echo '<p>Field ' . $id . ' was successfully deleted</p>.'; } else { echo '<p>An error occurred deleting field ' . $id . '</p>'; } } // update preference fields for differing new/old values if ($prefix == 'public' && is_numeric($id) && $_POST['origpublic_' . $id] != $value) { echo 'old value: ' . $_POST['origpublic_' . $id] . 'new value: ' . $value . '<br />'; } if ($prefix == 'default' && is_numeric($id) && $_POST['origdefault_' . $id] != $value) { echo 'old value: ' . $_POST['origdefault_' . $id] . 'new value: ' . $value . '<br />'; } } } // Handle POSTed new rows if (!empty($_POST['prefname_text'])) { $livewebpref = liveuser_pref_checkField($_POST['prefname_text']); if ($livewebpref === false) { if (!empty($_POST['default_text'])) { $livewebpref = $_POST['public_chk'] ? liveuser_pref_setField($_POST['prefname_text'], true, $_POST['default_text']) : liveuser_pref_setField($_POST['prefname_text'], false, $_POST['default_text']); } else { $livewebpref = $_POST['public_chk'] ? liveuser_pref_setField($_POST['prefname_text'], true) : liveuser_pref_setField($_POST['prefname_text'], false); } if ($livewebpref !== false) { echo $_POST['prefname_text'] . ' was inserted into liveweb_prefs_fields...'; } } else { echo $_POST['prefname_text'] . ' could NOT be inserted into liveweb_prefs_fields...'; } } echo '<br /><br />'; // Get list of prefs fields in liveuser system $results = $liveuserDB->getAll('SELECT * FROM liveweb_prefs_fields'); // show form for modifying preferences if (is_array($results) && !empty($results)) { ?> <form method="post" action=""> <h3>Modify Preference Fields</h3> <table border="1"> <tr><th>Delete</th><th>ID</th><th>Preference</th><th>Public</th><th>Default Value</th></tr> <?php foreach ($results as $result) { ?> <tr> <td><input name="chk_<?php echo $result['field_id']; ?> " type="checkbox"></td><td><?php echo $result['field_id']; ?> </td> <td><?php echo $result['field_name']; ?> </td> <td> <input name="origpublic_<?php echo $result['field_id']; ?> " value="<?php echo $result['public']; ?> " type="hidden"> <select id="public_<?php echo $result['field_id']; ?> " name="public_<?php echo $result['field_id']; ?> "> <option value="1" <?php echo $result['public'] == 1 ? 'selected="selected"' : ''; ?> >Y</option> <option value="0" <?php echo $result['public'] == 0 ? 'selected="selected"' : ''; ?> >N</option> </select> </td> <td> <input name="origdefault_<?php echo $result['field_id']; ?> " value="<?php echo $result['default_value']; ?> " type="hidden"> <input name="default_<?php echo $result['field_id']; ?> " value="<?php echo $result['default_value']; ?> "> </td> </tr> <?php } ?> </table> <input type="reset" text="Reset"> <input type="submit" name="changeprefs_submit"> </form> <?php } else { echo '<p>No preference fields were found.</p>'; } // show form for adding preferences ?> <form method="post" action=""> <h3>Add a Preference Field</h3> <label for="prefname_text">Preference Name</label> <input id="prefname_text" name="prefname_text" type="text"><br /> <label for="public_chk">Public</label> <input id="public_chk" name="public_chk" type="checkbox"><br /> <label for="default_text">Default Value</label> <input id="default_text" name="default_text" type="text"><br /> <input type="submit" name="addprefs_submit"> </form> <?php $o = ob_get_contents(); ob_end_clean(); return $o; }
/** * Sets a field (adds or updates) with the specified properties. * * @param mixed field_name field name or id of preference field * @param boolean public preference field is public if true, private if false * @param string default_value default field value if user preference is not set * @param string possible_values possible_values field value for admin to set and user to choose * @return mixed integer identifier of field if operation was successful, false otherwise */ function liveuser_pref_setField($field_name, $public = true, $default_value = null, $possible_values = null) { global $liveuserDB; if (($field_id = liveuser_pref_checkField($field_name)) !== false) { return $liveuserDB->query(' UPDATE ' . LW_PREFIX . '_prefs_fields SET public = ?, default_value = ?, possible_values = ? WHERE field_id = ?', array((int) $public, $default_value, $possible_values ? serialize($possible_values) : null, (int) $field_id)) == DB_OK; } else { if ($liveuserDB->query(' INSERT INTO ' . LW_PREFIX . '_prefs_fields (field_name, public, default_value, possible_values) VALUES (?, ?, ?, ?)', array($field_name, (int) $public, $default_value, $possible_values ? serialize($possible_values) : null)) != DB_OK) { return false; } return liveuser_pref_checkField($field_name); } }