<?php
// $Id: ajax_post.php 87 2006-09-01 02:51:52Z yosha_01 $
require './ajax_header.php';

// initialize handlers
$discuss_handler  =& xoops_getmodulehandler("discuss");
$message_handler  =& xoops_getmodulehandler("message");
$response_handler =& xoops_getmodulehandler("response");

$discuss_id = !empty($_POST["discuss_id"]) ? intval($_POST["discuss_id"]) : 0;
$discuss_object =& $discuss_handler->get($discuss_id);

/////////////
// Message //
/////////////

if (is_object($discuss_object)) {

    $message_object =& $message_handler->create();

    $message_object->setVar('discuss_id', $discuss_object->getVar("discuss_id"));
    $message_object->setVar('uid', $uid);
    $message_object->setVar('uname', $uname);
    $message_object->setVar('message', DiscussEncoder::fromUtf8($_POST["msg"]));
    $message_object->setVar('timestamp', time());
    if (!empty($_POST["color"])) {
        $message_object->setVar('color', $_POST["color"]);
    }

    $message_handler->insert($message_object, true);

    $message_objects =& $message_handler->getMessages($discuss_object->getVar('discuss_id'), 5);
    $response_handler->addMessageXML($message_objects);

    ////////////////
    // Render XML //
    ////////////////

    $response_handler->render();
}
?>