<?php
/**
 * Custom Group extension
 */


class BP_Group_Extension_IRON extends BP_Group_Extension {

    function __construct() {
        
        // required
        $this->name = 'Messages Groups';
        $this->slug = 'send-messages';
        // optional
        $this->visibility  = 'private';
        $this->enable_create_step  = false;
        $this->enable_edit_item  = true;
        $this->enable_nav_item = true;
    }
    
    function display() {
        global $bp;
        
        wp_redirect( home_url() . '/grupos/' . $bp->current_item . '/' );
    }
    
    function edit_screen() {
        if ( !bp_is_group_admin_screen( $this->slug ) )
                return false;
        else
            require_once( dirname(__FILE__) . '/include/send-messages.php');

        wp_nonce_field( 'groups_edit_save_' . $this->slug );
    }
    
    function edit_screen_save() {
        global $bp;
    
        if ( !isset( $_POST['save'] ) and !isset($_POST['content']) )
            return false;
        
        $SendMessages = new BP_Messages_Message;
        
        $SendMessages->sender_id  = wp_get_current_user()->ID;
        $SendMessages->subject    = $_POST['subject'];
        $SendMessages->message    = $_POST['content'];
        $SendMessages->date_sent  = date('Y-m-d H-i-s');
        $SendMessages->recipients = $_POST['idsSendMail'];
        $SendMessages->group      = true;
        
        $threadID = $SendMessages->send();
        
        bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . '/admin/' . $this->slug . '?send=true' );
    }

}

bp_register_group_extension( 'BP_Group_Extension_IRON' );