populate($id);
}
}
function populate( $id ) {
global $wpdb, $bp;
$sql = $wpdb->prepare("SELECT * FROM {$bp->profile->table_name_groups} WHERE id = %d", $id);
if ( $group = $wpdb->get_row($sql) ) {
$this->id = $group->id;
$this->name = $group->name;
$this->description = $group->description;
$this->can_delete = $group->can_delete;
// get the fields for this group.
$this->fields = $this->get_fields();
}
}
function save() {
global $wpdb, $bp;
$this->name = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id );
$this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id );
do_action( 'xprofile_group_before_save', $this );
if ( $this->id ) {
$sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id );
} else {
$sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_groups} (name, description, can_delete) VALUES (%s, %s, 1)", $this->name, $this->description );
}
if ( !$wpdb->query($sql) )
return false;
do_action( 'xprofile_group_after_save', $this );
return true;
}
function delete() {
global $wpdb, $bp;
if ( !$this->can_delete )
return false;
$sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id );
if ( !$wpdb->query($sql) ) {
return false;
} else {
// Now the group is deleted, remove the group's fields.
if ( BP_XProfile_Field::delete_for_group($this->id) ) {
// Now delete all the profile data for the groups fields
for ( $i = 0; $i < count($this->fields); $i++ ) {
BP_XProfile_ProfileData::delete_for_field($this->fields[$i]->id);
}
}
return true;
}
}
function get_fields() {
global $wpdb, $bp;
// Get field ids for the current group.
if ( !$fields = $wpdb->get_results( $wpdb->prepare("SELECT id, type FROM {$bp->profile->table_name_fields} WHERE group_id = %d AND parent_id = 0 ORDER BY id", $this->id ) ) )
return false;
return $fields;
}
function render_admin_form() {
global $message;
if ( !$this->id ) {
$title = __('Add Group', 'buddypress');
$action = "admin.php?page=" . BP_PLUGIN_DIR . "/bp-xprofile.php&mode=add_group";
} else {
$title = __('Edit Group', 'buddypress');
$action = "admin.php?page=" . BP_PLUGIN_DIR . "/bp-xprofile.php&mode=edit_group&group_id=" . $this->id;
}
?>
prepare( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id ORDER BY g.id ASC" );
} else {
$sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_groups} ORDER BY id ASC" );
}
if ( !$groups_temp = $wpdb->get_results($sql) )
return false;
for ( $i = 0; $i < count($groups_temp); $i++ ) {
$group = new BP_XProfile_Group($groups_temp[$i]->id);
$groups[] = $group;
}
return $groups;
}
function admin_validate() {
global $message;
// Validate Form
if ( empty( $_POST['group_name'] ) ) {
$message = __('Please make sure you give the group a name.', 'buddypress');
return false;
} else {
return true;
}
}
}
Class BP_XProfile_Field {
var $id;
var $group_id;
var $parent_id;
var $type;
var $name;
var $desc;
var $is_required;
var $can_delete;
var $field_order;
var $option_order;
var $order_by;
var $is_default_option;
var $data;
var $message = null;
var $message_type = 'err';
function bp_xprofile_field( $id = null, $user_id = null, $get_data = true ) {
if ( $id ) {
$this->populate( $id, $user_id, $get_data );
}
}
function populate( $id, $user_id, $get_data ) {
global $wpdb, $userdata, $bp;
if ( is_null($user_id) ) {
$user_id = $userdata->ID;
}
$sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $id );
if ( $field = $wpdb->get_row($sql) ) {
$this->id = $field->id;
$this->group_id = $field->group_id;
$this->parent_id = $field->parent_id;
$this->type = $field->type;
$this->name = stripslashes($field->name);
$this->desc = stripslashes($field->description);
$this->is_required = $field->is_required;
$this->is_public= $field->is_public;
$this->can_delete = $field->can_delete;
$this->field_order = $field->field_order;
$this->option_order = $field->option_order;
$this->order_by = $field->order_by;
$this->is_default_option = $field->is_default_option;
if ( $get_data ) {
$this->data = $this->get_field_data($user_id);
}
}
}
function delete() {
global $wpdb, $bp;
if ( !$this->id )
return false;
if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id ) ) )
return false;
// delete the data in the DB for this field
BP_XProfile_ProfileData::delete_for_field($this->id);
return true;
}
function save() {
global $wpdb, $bp;
$error = false;
$this->group_id = apply_filters( 'xprofile_field_group_id_before_save', $this->group_id, $this->id );
$this->parent_id = apply_filters( 'xprofile_field_parent_id_before_save', $this->parent_id, $this->id );
$this->type = apply_filters( 'xprofile_field_type_before_save', $this->type, $this->id );
$this->name = apply_filters( 'xprofile_field_name_before_save', $this->name, $this->id );
$this->desc = apply_filters( 'xprofile_field_description_before_save', $this->desc, $this->id );
$this->is_required = apply_filters( 'xprofile_field_is_required_before_save', $this->is_required, $this->id );
$this->is_public = apply_filters( 'xprofile_field_is_public_before_save', $this->is_public, $this->id );
$this->order_by = apply_filters( 'xprofile_field_order_by_before_save', $this->order_by, $this->id );
do_action( 'xprofile_field_before_save', $this );
if ( $this->id != null ) {
$sql = $wpdb->prepare("UPDATE {$bp->profile->table_name_fields} SET group_id = %d, parent_id = 0, type = %s, name = %s, description = %s, is_required = %d, is_public = %d, order_by = %s WHERE id = %d", $this->group_id, $this->type, $this->name, $this->desc, $this->is_required, $this->is_public, $this->order_by, $this->id);
} else {
$sql = $wpdb->prepare("INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, is_public, order_by) VALUES (%d, 0, %s, %s, %s, %d, %d, %s)", $this->group_id, $this->type, $this->name, $this->desc, $this->is_required, $this->is_public, $this->order_by);
}
if ( $wpdb->query($sql) ) {
// Only do this if we are editing an existing field
if ( $this->id != null ) {
// Remove any radio or dropdown options for this
// field. They will be re-added if needed.
// This stops orphan options if the user changes a
// field from a radio button field to a text box.
$this->delete_children();
}
// Check to see if this is a field with child options.
// We need to add the options to the db, if it is.
if ( 'radio' == $this->type || 'selectbox' == $this->type || 'checkbox' == $this->type || 'multiselectbox' == $this->type ) {
if ( $this->id ) {
$parent_id = $this->id;
} else {
$parent_id = $wpdb->insert_id;
}
if ( !empty( $_POST['field_file'] ) ) {
// Add a prebuilt field from a csv file
$field_file = $_POST['field_file'];
if ( $fp = fopen($field_file, 'r') ) {
$start_reading = false;
while ( ! feof($fp) && !$start_reading) {
if ( $s = fgets ($fp, 1024) ) {
if ( preg_match ( '/\*\//', $s ) ) {
$start_reading = true;
}
}
}
while ( ( $data = fgetcsv( $fp ) ) ) {
$num = count($data);
$name = '';
$description = '';
if ( $num >= 1 )
$name = $data[0];
if ( $num >= 2 )
$description = $data[1];
if ( $num > 0 ) {
$sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, option_order) VALUES (%d, %d, 'option', %s, %s, 0, %d)", $this->group_id, $parent_id, $name, $description, $option_order);
$wpdb->query($sql);
}
}
fclose($fp);
}
} else {
if ( 'radio' == $this->type ) {
$options = $_POST['radio_option'];
$defaults = $_POST['isDefault_radio_option'];
} else if ( 'selectbox' == $this->type ) {
$options = $_POST['selectbox_option'];
$defaults = $_POST['isDefault_selectbox_option'];
} else if ( 'multiselectbox' == $this->type ) {
$options = $_POST['multiselectbox_option'];
$defaults = $_POST['isDefault_multiselectbox_option'];
} else if ( 'checkbox' == $this->type ) {
$options = $_POST['checkbox_option'];
$defaults = $_POST['isDefault_checkbox_option'];
}
$counter = 1;
if ( $options ) {
foreach ( $options as $option_key => $option_value ) {
$is_default = 0;
if ( is_array($defaults) ) {
if ( isset($defaults[$option_key]) )
$is_default = 1;
} else {
if ( (int) $defaults == $option_key )
$is_default = 1;
}
if ( '' != $option_value ) {
if ( !$wpdb->query( $wpdb->prepare("INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, option_order, is_default_option) VALUES (%d, %d, 'option', %s, '', 0, %d, %d)", $this->group_id, $parent_id, $option_value, $counter, $is_default ) ) )
return false;
}
$counter++;
}
}
}
}
} else {
$error = true;
}
if ( !$error ) {
do_action( 'xprofile_field_after_save', $this );
return true;
} else {
return false;
}
}
function get_edit_html( $value = null ) {
global $bp;
$asterisk = '';
if ( $this->is_required ) {
$asterisk = '* ';
}
$error_class = '';
if ( $this->message ) {
$this->message = '' . $this->message . '
';
$message_class = ' class="' . $this->message_type . '"';
}
if ( !is_null($value) ) {
$this->data->value = $value;
}
$this->data->value = stripslashes( wp_filter_kses( $this->data->value ) );
switch ( $this->type ) {
case 'textbox':
$html .= '';
$html .= '' . $asterisk . $this->name . ': ';
$html .= $this->message . ' ';
$html .= '' . $this->desc . ' ';
$html .= '
';
break;
case 'textarea':
$html .= '';
$html .= '' . $asterisk . $this->name . ': ';
$html .= $this->message . '';
$html .= '' . $this->desc . ' ';
$html .= '
';
break;
case 'selectbox':
$options = $this->get_children();
$html .= '';
$html .= '' . $asterisk . $this->name . ': ';
$html .= $this->message . '';
$html .= '-------- ';
for ( $k = 0; $k < count($options); $k++ ) {
$option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id);
if ( $option_value == $options[$k]->name || $value == $options[$k]->name || $options[$k]->is_default_option ) {
$selected = ' selected="selected"';
} else {
$selected = '';
}
$html .= '' . $options[$k]->name . ' ';
}
$html .= ' ';
$html .= '' . $this->desc . ' ';
$html .= '
';
break;
case 'multiselectbox':
$options = $this->get_children();
$html .= '';
$html .= '' . $asterisk . $this->name . ': ';
$html .= $this->message . '';
if ( $value ) {
$option_values = maybe_unserialize($value);
} else {
$option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id);
$option_values = maybe_unserialize($option_values);
}
for ( $k = 0; $k < count($options); $k++ ) {
if ( @in_array( $options[$k]->name, $option_values ) ) {
$selected = ' selected="selected"';
} else {
$selected = '';
}
$html .= '' . $options[$k]->name . ' ';
}
$html .= ' ';
$html .= '' . $this->desc . ' ';
$html .= '
';
break;
case 'radio':
$options = $this->get_children();
$html .= '' . $asterisk . $this->name . ': ' . $this->message;
for ( $k = 0; $k < count($options); $k++ ) {
$option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id);
if ( $option_value == $options[$k]->name || $value == $options[$k]->name || $options[$k]->is_default_option ) {
$selected = ' checked="checked"';
} else {
$selected = '';
}
$html .= '
' . $options[$k]->name . '';
}
if ( !$this->is_required ) {
$html .= '
' . __( 'Clear', 'buddypress' ) . '';
}
$html .= '
' . $this->desc . ' ';
$html .= '
';
break;
case 'checkbox':
$options = $this->get_children();
$html .= '' . $asterisk . $this->name . ': ' . $this->message;
if ( $value ) {
$option_values = maybe_unserialize($value);
} else {
$option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id);
$option_values = maybe_unserialize($option_values);
}
for ( $k = 0; $k < count($options); $k++ ) {
for ( $j = 0; $j < count($option_values); $j++ ) {
if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) || $options[$k]->is_default_option ) {
$selected = ' checked="checked"';
break;
}
}
$html .= '
' . $options[$k]->name . '';
$selected = '';
}
$html .= '
' . $this->desc . ' ';
$html .= '
';
break;
case 'datebox':
if ( $this->data->value != '' ) {
$day = date("j", $this->data->value);
$month = date("F", $this->data->value);
$year = date("Y", $this->data->value);
$default_select = ' selected="selected"';
}
$html .= '';
$html .= '' . $asterisk . $this->name . ': ';
$html .= $this->message . '
';
$html .= '-- ';
for ( $i = 1; $i < 32; $i++ ) {
if ( $day == $i ) {
$selected = ' selected = "selected"';
} else {
$selected = '';
}
$html .= '' . $i . ' ';
}
$html .= ' ';
$months = array( __( 'January', 'buddypress' ), __( 'February', 'buddypress' ), __( 'March', 'buddypress' ),
__( 'April', 'buddypress' ), __( 'May', 'buddypress' ), __( 'June', 'buddypress' ),
__( 'July', 'buddypress' ), __( 'August', 'buddypress' ), __( 'September', 'buddypress' ),
__( 'October', 'buddypress' ), __( 'November', 'buddypress' ), __( 'December', 'buddypress' )
);
$html .= '
';
$html .= '------ ';
for ( $i = 0; $i < 12; $i++ ) {
if ( $month == $months[$i] ) {
$selected = ' selected = "selected"';
} else {
$selected = '';
}
$html .= '' . $months[$i] . ' ';
}
$html .= ' ';
$html .= '
';
$html .= '---- ';
for ( $i = date( 'Y', time() ); $i > 1899; $i-- ) {
if ( $year == $i ) {
$selected = ' selected = "selected"';
} else {
$selected = '';
}
$html .= '' . $i . ' ';
}
$html .= ' ';
$html .= '' . $this->desc . ' ';
$html .= '
';
break;
}
return $html;
}
function get_field_data($user_id) {
return new BP_XProfile_ProfileData($this->id, $user_id);
}
function get_children($for_editing = false) {
global $wpdb, $bp;
// This is done here so we don't have problems with sql injection
if ( 'asc' == $this->order_by && !$for_editing ) {
$sort_sql = 'ORDER BY name ASC';
} else if ( 'desc' == $this->order_by && !$for_editing ) {
$sort_sql = 'ORDER BY name DESC';
} else {
$sort_sql = 'ORDER BY option_order ASC';
}
//This eliminates a problem with getting all fields when there is no id for the object
if ( !$this->id ) {
$parent_id = -1;
} else {
$parent_id = $this->id;
}
$sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE parent_id = %d AND group_id = %d $sort_sql", $parent_id, $this->group_id );
if ( !$children = $wpdb->get_results($sql) )
return false;
return $children;
}
function delete_children() {
global $wpdb, $bp;
$sql = $wpdb->prepare("DELETE FROM {$bp->profile->table_name_fields} WHERE parent_id = %d", $this->id);
$wpdb->query($sql);
}
function render_admin_form_children() {
//This function populates the items for radio buttons checkboxes and drop down boxes
$input_types = array( 'checkbox', 'selectbox', 'multiselectbox', 'radio' );
foreach ($input_types as $type) {
$default_name = '';
if ( 'multiselectbox' == $type || 'checkbox' == $type ) {
$default_input = 'checkbox';
} else {
$default_input = 'radio';
}
?>
id ) {
$title = __('Add Field', 'buddypress');
$action = "admin.php?page=" . BP_PLUGIN_DIR . "/bp-xprofile.php&group_id=" . $this->group_id . "&mode=add_field";
} else {
$title = __('Edit Field', 'buddypress');
$action = "admin.php?page=" . BP_PLUGIN_DIR . "/bp-xprofile.php&mode=edit_field&group_id=" . $this->group_id . "&field_id=" . $this->id;
$options = $this->get_children();
}
?>