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; /* Find the max value for field_order, if it is zero, order by field_id instead -- provides backwards compat ordering */ if ( !(int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $this->id ) ) ) $order_sql = "ORDER BY id"; else $order_sql = "ORDER BY field_order"; // 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_sql}", $this->id ) ) ) return false; return $fields; } /** Static Functions **/ function get_all( $hide_empty = false ) { global $wpdb, $bp; if ( $hide_empty ) { $sql = $wpdb->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; } } // ADMIN AREA HTML. TODO: Get this out of here. 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"; } else { $title = __('Edit Field Group', 'buddypress'); $action = "admin.php?page=bp-profile-setup&mode=edit_group&group_id=" . $this->id; } ?>


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->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 || // 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->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->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( '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, order_by = %s, field_order = %d WHERE id = %d", $this->group_id, $this->type, $this->name, $this->desc, $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, 0, %s, %s, %s, %d, %s, %d )", $this->group_id, $this->type, $this->name, $this->desc, $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 ) { // 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 ) { $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_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); } // 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 ( !$bp->profile->table_name_fields || !$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 ) { global $wpdb, $bp; if ( !is_numeric( $position ) ) return false; return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET field_order = %d WHERE id = %d", $position, $field_id ) ); } // ADMIN AREA HTML. TODO: Get this out of here. 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'; } ?>

get_children(true) ) { $i = 1; while ( isset( $_POST[$type . '_option'][$i] ) ) { (array) $options[] = (object) array( 'id' => -1, 'name' => $_POST[$type . '_option'][$i], 'is_default_option' => ( 'multiselectbox' != $type && 'checkbox' != $type && $_POST["isDefault_{$type}_option"] == $i ) ? 1 : $_POST["isDefault_{$type}_option"][$i] ); $i++; } } if ( !empty($options) ) { for ( $i = 0; $i < count($options); $i++ ) { $j = $i + 1; if ( 'multiselectbox' == $type || 'checkbox' == $type ) $default_name = '[' . $j . ']'; ?>

: is_default_option ) {?> checked="checked" " value="" /> id != -1 ) : ?>[x]

1:

id ) { $title = __('Add Field', 'buddypress'); $action = "admin.php?page=bp-profile-setup&group_id=" . $this->group_id . "&mode=add_field"; $this->name = $_POST['title']; $this->desc = $_POST['description']; $this->is_required = $_POST['required']; $this->type = $_POST['fieldtype']; $this->order_by = $_POST["sort_order_{$this->type}"]; } else { $title = __('Edit Field', 'buddypress'); $action = "admin.php?page=bp-profile-setup&mode=edit_field&group_id=" . $this->group_id . "&field_id=" . $this->id; } ?>


render_admin_form_children() ?>

  »" name="saveField" id="saveField" style="font-weight: bold" />

prepare( "SELECT f.id FROM {$bp->profile->table_name_fields} AS f, {$bp->profile->table_name_groups} AS g WHERE g.name = %s AND f.parent_id = 0 AND g.id = f.group_id ORDER BY f.id", get_site_option('bp-xprofile-base-group-name') ); if ( !$temp_fields = $wpdb->get_results($sql) ) return false; for ( $i = 0; $i < count($temp_fields); $i++ ) { $fields[] = new BP_XProfile_Field( $temp_fields[$i]->id, null, false ); } return $fields; } } Class BP_XProfile_ProfileData { var $id; var $user_id; var $field_id; var $value; var $last_updated; function bp_xprofile_profiledata( $field_id = null, $user_id = null ) { if ( $field_id ) { $this->populate( $field_id, $user_id ); } } function populate( $field_id, $user_id ) { global $wpdb, $bp; $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ); if ( $profiledata = $wpdb->get_row($sql) ) { $this->id = $profiledata->id; $this->user_id = $profiledata->user_id; $this->field_id = $profiledata->field_id; $this->value = stripslashes($profiledata->value); $this->last_updated = $profiledata->last_updated; } } function exists() { global $wpdb, $bp; // check to see if there is data already for the user. $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = %d", $this->user_id, $this->field_id ); if ( !$wpdb->get_row($sql) ) return false; return true; } function is_valid_field() { global $wpdb, $bp; // check to see if this data is actually for a valid field. $sql = $wpdb->prepare("SELECT id FROM {$bp->profile->table_name_fields} WHERE id = %d", $this->field_id ); if ( !$wpdb->get_row($sql) ) return false; return true; } function save() { global $wpdb, $bp; $this->user_id = apply_filters( 'xprofile_data_user_id_before_save', $this->user_id, $this->id ); $this->field_id = apply_filters( 'xprofile_data_field_id_before_save', $this->field_id, $this->id ); $this->value = apply_filters( 'xprofile_data_value_before_save', $this->value, $this->id ); $this->last_updated = apply_filters( 'xprofile_data_last_updated_before_save', date( 'Y-m-d H:i:s' ), $this->id ); do_action( 'xprofile_data_before_save', $this ); if ( $this->is_valid_field() ) { if ( $this->exists() && $this->value != '' ) { $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_data} SET value = %s, last_updated = %s WHERE user_id = %d AND field_id = %d", $this->value, $this->last_updated, $this->user_id, $this->field_id ) ); } else if ( $this->exists() && empty( $this->value ) ) { // Data removed, delete the entry. $result = $this->delete(); } else { $result = $wpdb->query( $wpdb->prepare("INSERT INTO {$bp->profile->table_name_data} (user_id, field_id, value, last_updated) VALUES (%d, %d, %s, %s)", $this->user_id, $this->field_id, $this->value, $this->last_updated ) ); } if ( !$result ) return false; do_action( 'xprofile_data_after_save', $this ); return true; } return false; } function delete() { global $wpdb, $bp; if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $this->field_id, $this->user_id ) ) ) return false; return true; } /** Static Functions **/ function get_value_byid( $field_id, $user_id = null ) { global $wpdb, $bp; if ( !$user_id ) $user_id = $bp->displayed_user->id; if ( !$bp->profile ) xprofile_setup_globals(); $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ); if ( $profile_data = $wpdb->get_row($sql) ) { return $profile_data->value; } else { return false; } } function get_value_byfieldname( $fields, $user_id = null ) { global $bp, $wpdb; if ( !$fields ) return false; if ( !$user_id ) $user_id = $bp->displayed_user->id; if ( !$bp->profile ) xprofile_setup_globals(); $field_sql = ''; if ( is_array($fields) ) { for ( $i = 0; $i < count($fields); $i++ ) { if ( $i == 0 ) $field_sql .= $wpdb->prepare( "AND ( f.name = %s ", $fields[$i] ); else $field_sql .= $wpdb->prepare( "OR f.name = %s ", $fields[$i] ); } $field_sql .= ')'; } else { $field_sql .= $wpdb->prepare( "AND f.name = %s", $fields ); } $sql = $wpdb->prepare( "SELECT d.value, f.name FROM {$bp->profile->table_name_data} d, {$bp->profile->table_name_fields} f WHERE d.field_id = f.id AND d.user_id = %d AND f.parent_id = 0 $field_sql", $user_id ); if ( !$values = $wpdb->get_results($sql) ) return false; $new_values = array(); if ( is_array($fields) ) { for ( $i = 0; $i < count($values); $i++ ) { for ( $j = 0; $j < count($fields); $j++ ) { if ( $values[$i]->name == $fields[$j] ) { $new_values[$fields[$j]] = $values[$i]->value; } else if ( !array_key_exists( $fields[$j], $new_values ) ) { $new_values[$fields[$j]] = NULL; } } } } else { $new_values = $values[0]->value; } return $new_values; } function delete_for_field( $field_id ) { global $wpdb, $userdata, $bp; if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d", $field_id ) ) ) return false; return true; } function get_last_updated( $user_id ) { global $wpdb, $bp; $last_updated = $wpdb->get_var( $wpdb->prepare( "SELECT last_updated FROM {$bp->profile->table_name_data} WHERE user_id = %d ORDER BY last_updated LIMIT 1", $user_id ) ); return $last_updated; } function delete_data_for_user( $user_id ) { global $wpdb, $bp; return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) ); } function get_random( $user_id, $exclude_fullname ) { global $wpdb, $bp; if ( $exclude_fullname ) $exclude_sql = $wpdb->prepare( " AND pf.id != 1" ); return $wpdb->get_results( $wpdb->prepare( "SELECT pf.type, pf.name, pd.value FROM {$bp->profile->table_name_data} pd INNER JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id AND pd.user_id = %d {$exclude_sql} ORDER BY RAND() LIMIT 1", $user_id ) ); } function get_fullname( $user_id = false ) { global $bp; if ( !$user_id ) $user_id = $bp->displayed_user->id; $data = xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $user_id ); return $data[BP_XPROFILE_FULLNAME_FIELD_NAME]; } } ?>