<?php

/**
 * $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');
$usermessage = new CUserMessage();
$usermessage->load($usermessage_id);
if ($usermessage->_id) {
    echo nl2br($usermessage->content);
}
/** $Id: write_usermessage.php 20898 2013-11-07 10:19:55Z charlyecho $ **/
/**
* @package Mediboard
* @subpackage dPportail
* @version $Revision: 20898 $
* @author Thomas Despoix
*/
CCanDo::checkRead();
$user = CUser::get();
$usermessage = new CUserMessage();
$usermessage->from = $user->_id;
$usermessage->to = CValue::get("to");
$usermessage->subject = CValue::get("subject");
$usermessage->in_reply_to = CValue::get("in_reply_to");
$usermessage->load(CValue::getOrSession("usermessage_id"));
$usermessage->loadRefsFwd();
// Vérifiction de la première lecture par le destinataire
if ($usermessage->to == $user->_id && $usermessage->date_sent && !$usermessage->date_read) {
    $usermessage->date_read = CMbDT::dateTime();
    $usermessage->store();
}
if ($usermessage->in_reply_to) {
    $origin = $usermessage->loadOriginMessage();
    if ($origin->_id) {
        if (!$usermessage->subject) {
            $usermessage->subject = "Re: " . $origin->subject;
        }
        $usermessage->to = $origin->from;
    }
}
 * @package Mediboard
 * @subpackage messagerie
 * @version $Revision: 28703 $
 * @author Thomas despoix
 */
$user = CMediusers::get();
$date = CMbDT::dateTime();
$dests = CValue::post("dest", array());
$del = CValue::post("del", 0);
$send_it = CValue::post("_send");
$archive_mine = CValue::post("_archive");
$read_only = CValue::post("_readonly");
$callback = CValue::post('callback');
$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
        $_dest->datetime_read = CMbDT::dateTime();
        $_dest->store();
    }
    $_dest->loadRefTo()->loadRefFunction();
    $_dest->loadRefFrom()->loadRefFunction();
}
// 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;
                }
            }