/** * xprofile_edit() * * Renders the edit form for the profile fields within a group as well as * handling the save action. * * [NOTE] This is old code that was written when editing was not done in the theme. * It is big and clunky and will be broken up in future versions. * * @package BuddyPress XProfile * @param $group_id The ID of the group of fields to edit * @param $action The HTML form action * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals() * @global $wpdb WordPress DB access object. * @global $userdata WordPress global object containing current logged in user userdata */ function xprofile_edit($group_id, $action) { global $wpdb, $userdata, $bp; // Create a new group object based on the group ID. $group = new BP_XProfile_Group($group_id); ?> <div class="wrap"> <h2><?php echo attribute_escape($group->name); ?> <?php _e("Information", 'buddypress'); ?> </h2> <?php // If this group has fields then continue if ($group->fields) { $errors = null; $list_html = '<ul class="forTab" id="' . strtolower($group_name) . '">'; // Loop through each field in the group for ($j = 0; $j < count($group->fields); $j++) { // Create a new field object for this field based on the field ID. $field = new BP_XProfile_Field($group->fields[$j]->id); // Add the ID for this field to the field_ids array $field_ids[] = $group->fields[$j]->id; // If the user has submitted the form - validate and save the new value for this field if (isset($_GET['mode']) && 'save' == $_GET['mode']) { /* Check the nonce */ if (!check_admin_referer('bp_xprofile_edit')) { return false; } // If the current field is a datebox, we need to append '_day' to the end of the field name // otherwise the field name will not exist $post_field_string = 'datebox' == $group->fields[$j]->type ? '_day' : null; // Explode the posted field IDs into an array so we know which fields have been submitted $posted_fields = explode(',', $_POST['field_ids']); // Fetch the current field from the _POST array based on field ID. $current_field = $_POST['field_' . $posted_fields[$j] . $post_field_string]; // If the field is required and has been left blank then we need to add a callback error. if ($field->is_required && !isset($current_field) || $field->is_required && empty($current_field)) { // Add the error message to the errors array $field->message = sprintf(__('%s cannot be left blank.', 'buddypress'), $field->name); $errors[] = $field->message . "<br />"; // If the field is not required and the field has been left blank, delete any values for the // field from the database. } else { if (!$field->is_required && (empty($current_field) || is_null($current_field))) { // Create a new profile data object for the logged in user based on field ID. $profile_data = new BP_Xprofile_ProfileData($group->fields[$j]->id, $bp->loggedin_user->id); if ($profile_data) { // Delete any data $profile_data->delete(); // Also remove any selected profile field data from the $field object. $field->data->value = null; } // If we get to this point then the field validates ok and we have new data. } else { // Create an empty profile data object and populate it with new data $profile_data = new BP_Xprofile_ProfileData(); $profile_data->field_id = $group->fields[$j]->id; $profile_data->user_id = $userdata->ID; $profile_data->last_updated = time(); // If the $post_field_string we set up earlier is not null, then this is a datebox // we need to concatenate each of the three select boxes for day, month and year into // one value. if ($post_field_string != null) { // Concatenate the values. $date_value = $_POST['field_' . $group->fields[$j]->id . '_day'] . $_POST['field_' . $group->fields[$j]->id . '_month'] . $_POST['field_' . $group->fields[$j]->id . '_year']; // Turn the concatenated value into a timestamp $profile_data->value = strtotime($date_value); } else { // Checkbox and multi select box fields will submit an array as their value // so we need to serialize them before saving to the DB. if (is_array($current_field)) { $current_field = serialize($current_field); } $profile_data->value = $current_field; } // Finally save the value to the database. if (!$profile_data->save()) { $field->message = __('There was a problem saving changes to this field, please try again.', 'buddypress'); } else { $field->data->value = $profile_data->value; } } } } // Each field object comes with HTML that can be rendered to edit that field. // We just need to render that to the page by adding it to the $list_html variable // that will be rendered when the field loop has finished. $list_html .= '<li>' . $field->get_edit_html() . '</li>'; } // Now that the loop has finished put the final touches on the HTML including the submit button. $list_html .= '</ul>'; $list_html .= '<p class="submit"> <input type="submit" name="save" id="save" value="' . __('Save Changes »', 'buddypress') . '" /> </p>'; $list_html .= wp_nonce_field('bp_xprofile_edit'); // If the user submitted the form to save new values, and there were errors, make sure we display them. if ($errors && isset($_POST['save'])) { $type = 'error'; $message = __('There were problems saving your information. Please fix the following:<br />', 'buddypress'); for ($i = 0; $i < count($errors); $i++) { $message .= $errors[$i]; } // If there were no errors then we can display a nice "Changes saved." message. } else { if (!$errors && isset($_POST['save'])) { $type = 'success'; $message = __('Changes saved.', 'buddypress'); // Record in activity stream xprofile_record_activity(array('item_id' => $group->id, 'component_name' => 'profile', 'component_action' => 'updated_profile', 'is_private' => 0)); do_action('xprofile_updated_profile', $group->id); } } } else { ?> <div id="message" class="error fade"> <p><?php _e('That group does not exist.', 'buddypress'); ?> </p> </div> <?php } ?> <?php // Finally, we can now render everything to the screen. ?> <?php if ($message != '') { $type = 'error' == $type ? 'error' : 'updated'; ?> <div id="message" class="<?php echo $type; ?> fade"> <p><?php echo $message; ?> </p> </div> <?php } ?> <p><form action="<?php echo $action; ?> " method="post" id="profile-edit-form" class="generic-form"> <?php if ($field_ids) { $field_ids = implode(",", $field_ids); } ?> <input type="hidden" name="field_ids" id="field_ids" value="<?php echo attribute_escape($field_ids); ?> " /> <?php echo $list_html; ?> </form> </p> </div> <?php }