__construct( $id );
}
function __construct( $id = null ) {
global $bp, $wpdb;
if ( $id )
$this->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 ) )
return false;
$this->id = $group->id;
$this->name = stripslashes( $group->name );
$this->description = stripslashes( $group->description );
$this->can_delete = $group->can_delete;
$this->group_order = $group->group_order;
}
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_ref_array( 'xprofile_group_before_save', array( &$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 ( is_wp_error( $wpdb->query( $sql ) ) )
return false;
do_action_ref_array( 'xprofile_group_after_save', array( &$this ) );
if ( $this->id )
return $this->id;
else
return $wpdb->insert_id;
}
function delete() {
global $wpdb, $bp;
if ( !$this->can_delete )
return false;
/* Delete field group */
if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id ) ) ) {
return false;
} else {
/* Remove the group's fields. */
if ( BP_XProfile_Field::delete_for_group( $this->id ) ) {
/* Remove profile data for the groups fields */
for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) {
BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id );
}
}
return true;
}
}
/** Static Functions **/
/**
* get()
*
* Populates the BP_XProfile_Group object with profile field groups, fields, and field data
*
* @package BuddyPress XProfile
*
* @global $wpdb WordPress DB access object.
* @global object $bp Global BuddyPress settings object
*
* @param array $args Takes an array of parameters:
* 'profile_group_id' - Limit results to a single profile group
* 'user_id' - Required if you want to load a specific user's data
* 'hide_empty_groups' - Hide groups without any fields
* 'hide_empty_fields' - Hide fields where the user has not provided data
* 'fetch_fields' - Load each group's fields
* 'fetch_field_data' - Load each field's data. Requires a user_id
* 'exclude_groups' - Comma-separated list of groups to exclude
* 'exclude_fields' - Comma-separated list of fields to exclude
*
* @return array $groups
*/
function get( $args = '' ) {
global $wpdb, $bp;
$defaults = array(
'profile_group_id' => false,
'user_id' => $bp->displayed_user->id,
'hide_empty_groups' => false,
'hide_empty_fields' => false,
'fetch_fields' => false,
'fetch_field_data' => false,
'exclude_groups' => false,
'exclude_fields' => false
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$where_sql = '';
if ( $profile_group_id )
$where_sql = $wpdb->prepare( 'WHERE g.id = %d', $profile_group_id );
elseif ( $exclude_groups )
$where_sql = $wpdb->prepare( "WHERE g.id NOT IN ({$exclude_groups})");
if ( $hide_empty_groups )
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" ) );
else
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" ) );
if ( !$fetch_fields )
return $groups;
// Get the group ids
$group_ids = array();
foreach( (array)$groups as $group ) {
$group_ids[] = $group->id;
}
$group_ids = implode( ',', (array) $group_ids );
if ( empty( $group_ids ) )
return $groups;
$exclude_fields_sql = '';
if ( $exclude_fields )
$exclude_fields_sql = $wpdb->prepare( "AND id NOT IN ({$exclude_fields})" );
// Fetch the fields
$fields = $wpdb->get_results( $wpdb->prepare( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" ) );
if ( empty( $fields ) )
return $groups;
if ( $fetch_field_data ) {
// Fetch the field data for the user.
foreach( (array)$fields as $field )
$field_ids[] = $field->id;
$field_ids_sql = implode( ',', (array) $field_ids );
if ( !empty( $field_ids ) )
$field_data = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d", $user_id ) );
// Remove data-less fields, if necessary
if ( $hide_empty_fields ) {
// Loop through the results and find the fields that have data.
foreach( (array)$field_data as $data ) {
// Empty fields may contain a serialized empty array
$maybe_value = maybe_unserialize( $data->value );
if ( !empty( $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {
// Fields that have data get removed from the list
unset( $field_ids[$key] );
}
}
// The remaining members of $field_ids are empty. Remove them.
foreach( $fields as $field_key => $field ) {
if ( in_array( $field->id, $field_ids ) ) {
unset( $fields[$field_key] );
}
}
// Reset indexes
$fields = array_values( $fields );
}
// Field data was found
if ( !empty( $field_data ) && !is_wp_error( $field_data ) ) {
// Loop through fields
foreach( (array)$fields as $field_key => $field ) {
// Loop throught the data in each field
foreach( (array)$field_data as $data ) {
// Assign correct data value to the field
if ( $field->id == $data->field_id )
$fields[$field_key]->data->value = $data->value;
}
}
}
}
// Merge the field array back in with the group array
foreach( (array) $groups as $group ) {
// Indexes may have been shifted after previous deletions, so we get a
// fresh one each time through the loop
$index = array_search( $group, $groups );
foreach( (array) $fields as $field ) {
if ( $group->id == $field->group_id )
$groups[$index]->fields[] = $field;
}
// When we unset fields above, we may have created empty groups.
// Remove them, if necessary.
if ( empty( $group->fields ) && $hide_empty_groups ) {
unset( $groups[$index] );
}
// Reset indexes
$groups = array_values( $groups );
}
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;
}
}
function update_position( $field_group_id, $position ) {
global $wpdb, $bp;
if ( !is_numeric( $position ) )
return false;
return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET group_order = %d WHERE id = %d", $position, $field_group_id ) );
}
/* ADMIN AREA HTML.
* TODO: Get this out of here and replace with standard loops
*/
function render_admin_form() {
global $message;
if ( !$this->id ) {
$title = __( 'Add New Field Group', 'buddypress' );
$action = "admin.php?page=bp-profile-setup&mode=add_group";
$button = __( 'Create Field Group', 'buddypress' );
} else {
$title = __( 'Edit Field Group', 'buddypress' );
$action = "admin.php?page=bp-profile-setup&mode=edit_group&group_id=" . $this->id;
$button = __( 'Save Changes', 'buddypress' );
}
?>
__construct( $id, $user_id, $get_data );
}
function __construct( $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;
$user_id = 0;
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->description = stripslashes( $field->description );
$this->is_required = $field->is_required;
$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 && $user_id )
$this->data = $this->get_field_data( $user_id );
}
}
function delete() {
global $wpdb, $bp;
if ( !$this->id ||
/* Prevent deletion by url when can_delete is false. */
!$this->can_delete ||
/* Prevent deletion of option 1 since this invalidates fields with options. */
( $this->parent_id && $this->option_order == 1 ) )
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->description = apply_filters( 'xprofile_field_description_before_save', $this->description, $this->id );
$this->is_required = apply_filters( 'xprofile_field_is_required_before_save', $this->is_required, $this->id );
$this->order_by = apply_filters( 'xprofile_field_order_by_before_save', $this->order_by, $this->id );
$this->field_order = apply_filters( 'xprofile_field_field_order_before_save', $this->field_order, $this->id );
do_action_ref_array( 'xprofile_field_before_save', array( &$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, order_by = %s, field_order = %d WHERE id = %d", $this->group_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order, $this->id );
else
$sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, order_by, field_order ) VALUES (%d, %d, %s, %s, %s, %d, %s, %d )", $this->group_id, $this->parent_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order );
/*
* Check for null so field options can be changed without changing any other part of the field.
* The described situation will return 0 here.
*/
if ( $wpdb->query( $sql ) !== null ) {
if ( $this->id )
$field_id = $this->id;
else
$field_id = $wpdb->insert_id;
/* 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 ( 'radio' == $this->type ) {
$post_option = !empty( $_POST['radio_option'] ) ? $_POST['radio_option'] : '';
$post_default = !empty( $_POST['isDefault_radio_option'] ) ? $_POST['isDefault_radio_option'] : '';
$options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'radio' );
$defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'radio' );
} else if ( 'selectbox' == $this->type ) {
$post_option = !empty( $_POST['selectbox_option'] ) ? $_POST['selectbox_option'] : '';
$post_default = !empty( $_POST['isDefault_selectbox_option'] ) ? $_POST['isDefault_selectbox_option'] : '';
$options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'selectbox' );
$defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'selectbox' );
} else if ( 'multiselectbox' == $this->type ) {
$post_option = !empty( $_POST['multiselectbox_option'] ) ? $_POST['multiselectbox_option'] : '';
$post_default = !empty( $_POST['isDefault_multiselectbox_option'] ) ? $_POST['isDefault_multiselectbox_option'] : '';
$options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'multiselectbox' );
$defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'multiselectbox' );
} else if ( 'checkbox' == $this->type ) {
$post_option = !empty( $_POST['checkbox_option'] ) ? $_POST['checkbox_option'] : '';
$post_default = !empty( $_POST['isDefault_checkbox_option'] ) ? $_POST['isDefault_checkbox_option'] : '';
$options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'checkbox' );
$defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'checkbox' );
}
$counter = 1;
if ( $options ) {
foreach ( (array)$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++;
}
}
}
do_action_ref_array( 'xprofile_field_after_save', array( &$this ) );
return $field_id;
} else {
return false;
}
}
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 apply_filters( 'bp_xprofile_field_get_children', $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 );
}
/* Static Functions */
function get_type( $field_id ) {
global $wpdb, $bp;
if ( $field_id ) {
$sql = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id );
if ( !$field_type = $wpdb->get_var( $sql ) )
return false;
return $field_type;
}
return false;
}
function delete_for_group( $group_id ) {
global $wpdb, $bp;
if ( $group_id ) {
$sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id );
if ( $wpdb->get_var( $sql ) === false )
return false;
return true;
}
return false;
}
function get_id_from_name( $field_name ) {
global $wpdb, $bp;
if ( empty( $bp->profile->table_name_fields ) || !isset( $field_name ) )
return false;
return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", $field_name ) );
}
function update_position( $field_id, $position, $field_group_id ) {
global $wpdb, $bp;
if ( !is_numeric( $position ) || !is_numeric( $field_group_id ) )
return false;
/* Update $field_id with new $position and $field_group_id */
if ( $parent = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id ) ) ) {;
/* Update any children of this $field_id */
$children = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id ) );
return $parent;
}
return false;
}
/* ADMIN AREA HTML.
* TODO: Get this out of here and replace with standard template loops
*/
/* This function populates the items for radio buttons checkboxes and drop down boxes */
function render_admin_form_children() {
$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';
?>