<?php
/**
 * Ajax chat plugin
 *
 * Enables/disables Ajax driven chat
 *
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author  Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
 */

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once('chat.php');

global $ajax_chat_included;

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_chat extends DokuWiki_Syntax_Plugin {

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Pavel Vitis',
            'email'  => 'pavel [dot] vitis [at] seznam [dot] cz',
            'date'   => '2006-02-30',
            'name'   => 'Chat Plugin',
            'desc'   => 'Enables/disables Ajax-driven chat.',
            'url'    => 'http://wiki.splitbrain.org/plugin:chat',
        );
    }

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    /**
     * What about paragraphs?
     */
    function getPType(){
        return 'block';
    }

    /**
     * Where to sort in?
     */
    function getSort(){
        return 230;
    }


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        if ($mode == 'base') {
            $this->Lexer->addSpecialPattern('~~CHAT~~', $mode, 'plugin_chat');
            $this->Lexer->addSpecialPattern('~~NOCHAT~~', $mode, 'plugin_chat');
        }
    }


    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler) {
        global $conf;
        global $lang;
        global $ID;

        $dID = chat_plugin::addChatNS($ID);
        $dFN = wikiFN($dID);

        if ($match == '~~CHAT~~' && !chat_plugin::isChatID($ID)) {
            if (!file_exists($dFN)) {
                // Current discussion page doesn't exist.
                $revs = getRevisions($dID);
                if (count($revs)) {
                    // Restore previously delete discussion page.
                    $rev = array_shift($revs);
                    $txt = rawWiki($dID, $rev);
                    $sum = $lang['chat_restored'] . $rev;
                }
                else {
                    // Create new discussion page.
                    $txt = ' ';
                    $sum = $lang['chat_created'];
                }
                saveWikiText($dID, $txt, $sum);
            }
	    else {
	    }
        }
        else if ($match == '~~NOCHAT~~') {
            if (file_exists($dFN)) {
                saveWikiText($dID, '', $lang['chat_deleted']);
            }
        }

	return array($dID);
    }

    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
	global $ajax_chat_included;
	if ($mode == 'xhtml') {
	    if (isset($data[0]) && !$ajax_chat_included) {
		$renderer->info['cache'] = FALSE;//TODO: solve this. It remembers wrong username
//		$renderer->doc .= "D:".$data[0];
		$renderer->doc .= chat_plugin::chatHtml($data[0]);
		$ajax_chat_included = true;
		return true;
	    }
	}
        return true;
    }

}
