}
// last check
if (!$usermessage->_id) {
    $usermessage->creator_id = $user->_id;
    if ($subject) {
        $usermessage->subject = $subject;
    }
    // in reply to
    if ($in_reply_to) {
        $temp_message = new CUserMessage();
        $temp_message->load($in_reply_to);
        $usermessage->subject = "Re: " . $temp_message->subject;
        $usermessage->in_reply_to = $in_reply_to;
        $usermessage->creator_id = $user->_id;
        if ($answer_to_all) {
            $temp_message->loadRefDests();
            $usermessage->_ref_destinataires = array();
            foreach ($temp_message->_ref_destinataires as $_destinataire) {
                if ($_destinataire->to_user_id != $user->_id) {
                    $dest = new CUserMessageDest();
                    $dest->to_user_id = $_destinataire->to_user_id;
                    $dest->from_user_id = $usermessage->creator_id;
                    $dest->loadRefTo()->loadRefFunction();
                    $usermessage->_ref_destinataires[] = $dest;
                }
            }
        }
    }
    if ($to_id) {
        $dest = new CUserMessageDest();
        $dest->to_user_id = $to_id;
$usermessage = new CUserMessage();
// edit mode (draft)
$usermessage->load($_POST["usermessage_id"]);
if ($del && $usermessage->_id) {
    if ($msg = $usermessage->delete()) {
        CAppUI::stepAjax($msg, UI_MSG_ERROR);
    }
    $msg = 'CUserMessage-msg-delete';
    $message_id = null;
} else {
    $usermessage->bind($_POST);
    if ($msg = $usermessage->store()) {
        CAppUI::stepAjax($msg, UI_MSG_ERROR);
    }
    $message_id = $usermessage->usermessage_id;
    $destinataires = $usermessage->loadRefDests();
    foreach ($destinataires as $_dest) {
        // mine reception
        if ($_dest->to_user_id == $user->_id) {
            $_dest->archived = $archive_mine;
            if (!$_dest->datetime_read) {
                $_dest->datetime_read = $date;
            }
            if ($msg = $_dest->store()) {
                CAppUI::stepAjax($msg, UI_MSG_ERROR);
            }
        }
        // in edit mode, we don't find a dest, (delete it !)
        if (!$read_only && !in_array($_dest->to_user_id, $dests)) {
            if ($msg = $_dest->delete()) {
                CAppUI::stepAjax($msg, UI_MSG_ERROR);
 * $Id$
 *  
 * @package    Mediboard
 * @subpackage messagerie
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 * @link       http://www.mediboard.org
 */
CCanDo::checkRead();
$usermessage_id = CValue::get('usermessage_id');
$user = CMediusers::get();
$usermessage = new CUserMessage();
$usermessage->load($usermessage_id);
if ($usermessage->_id) {
    $usermessage->loadRefDests();
    $usermessage->loadRefDestUser();
    $usermessage->loadRefCreator()->loadRefFunction();
    foreach ($usermessage->_ref_destinataires as $_destinataire) {
        $_destinataire->loadRefTo()->loadRefFunction();
    }
    $mode = 'sentbox';
    if ($usermessage->_ref_dest_user->_id) {
        if ($usermessage->_ref_dest_user->archived) {
            $mode = 'archive';
        } else {
            $mode = 'inbox';
        }
        if (!$usermessage->_ref_dest_user->datetime_read) {
            $usermessage->_ref_dest_user->datetime_read = CMbDT::dateTime();
            $usermessage->_ref_dest_user->store();