/**
 * Sets up and displays the screen output for the sub nav item "portfolio/edit/%d"
 */
function bp_portfolio_screen_edit()
{
    if (bp_is_portfolio_component() and bp_is_current_action('edit') and bp_displayed_user_id() == bp_loggedin_user_id()) {
        if (isset($_POST['edit'])) {
            // Check to see if the project belong to the logged_in user
            global $project;
            $project_id = bp_action_variable();
            $project = new BP_Portfolio_Item();
            $project->get(array('id' => $project_id));
            if ($project->query->post->post_author != bp_loggedin_user_id()) {
                bp_core_add_message(__('There was an error recording the project, please try again', 'bp-portfolio'), 'error');
                bp_core_load_template(apply_filters('bp_portfolio_template_screen_add', BP_PORTFOLIO_TEMPLATE . '/personal'));
            }
            // Check the nonce
            if (!wp_verify_nonce($_POST['_wpnonce'], 'project_form_nonce')) {
                bp_core_add_message(__('There was an error recording the project, please try again', 'bp-portfolio'), 'error');
                bp_core_load_template(apply_filters('bp_portfolio_template_screen_add', BP_PORTFOLIO_TEMPLATE . '/personal'));
            }
            if (empty($_POST['title-input']) or empty($_POST['url-input']) or empty($_POST['description'])) {
                bp_core_add_message(__('All fields are required', 'bp-portfolio'), 'error');
                $project_id = bp_action_variable();
                global $project;
                $project = new BP_Portfolio_Item();
                $project->get(array('id' => $project_id));
            } else {
                // Edit the post
                $posts = array('id' => bp_action_variable(), 'author_id' => bp_loggedin_user_id(), 'title' => $_POST['title-input'], 'description' => $_POST['description'], 'url' => $_POST['url-input']);
                // Is that a capture has been sent ?
                if (isset($_FILES['screenshot-input']) and $_FILES['screenshot-input']['error'] == 0) {
                    $posts['screenshot'] = $_FILES['screenshot-input'];
                }
                if ($item = bp_portfolio_save_item($posts)) {
                    bp_core_add_message(__('Project has been edited', 'bp-portfolio'));
                    bp_core_redirect(bp_core_get_user_domain(bp_loggedin_user_id()) . bp_get_portfolio_slug());
                } else {
                    bp_core_add_message(__('There was an error recording the item, please try again', 'bp-portfolio'), 'error');
                }
            }
        } else {
            // Create a global $project, so template will know that this is the edit page
            if ($project_id = bp_action_variable()) {
                global $project;
                $project_id = bp_action_variable();
                $project = new BP_Portfolio_Item();
                $project->get(array('id' => $project_id));
                if ($project->query->post->post_author == bp_loggedin_user_id()) {
                    bp_core_load_template(apply_filters('bp_portfolio_template_screen_one', BP_PORTFOLIO_TEMPLATE . '/add'));
                }
            }
        }
    }
}
/**
 * Delte an item 
 */
function bp_portfolio_item_delete()
{
    if (bp_is_portfolio_component() and bp_is_current_action('delete') and bp_displayed_user_id() == bp_loggedin_user_id()) {
        if ($project_id = bp_action_variable() and wp_verify_nonce($_REQUEST['_wpnonce'], 'delete_project')) {
            if (bp_portfolio_delete_item($project_id)) {
                bp_core_add_message(__('Project deleted !', 'bp-portfolio'));
            } else {
                bp_core_add_message(__('An error occured, please try again.', 'bp-portfolio'), 'error');
            }
        } else {
            bp_core_add_message(__('An error occured, please try again.', 'bp-portfolio'), 'error');
        }
        bp_core_redirect(bp_core_get_user_domain(bp_loggedin_user_id()) . bp_get_portfolio_slug());
    }
}