<?php
/**
 * Mailto plugin, mail the current page to several people
 *
 * @author     Cédric Villemain <cedric.villemain@dalibo.com>
 */
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

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

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

    /**
     * return some info
     */
    function getInfo(){
        return confToHash(dirname(__FILE__).'/info.txt');
    }

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

    /**
     * Where to sort in?
     */
    function getSort(){
        return 155; /// Just before mantis actually TODO do we care of that, really ?
    }

    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('~~MAILTO:.*~~',$mode,'plugin_mailto');
    }

    function handle($match, $state, $pos, &$handler){
        $match = substr( $match, 9, -2 ); // strip "~~MAILTO:" from start and "~~" from end
        return explode( ',', $match );
	}
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        // store info in metadata
        if($mode == 'metadata'){
            $renderer->meta['plugin']['mailto']['to'] = $data[0];
            $renderer->meta['plugin']['mailto']['cc'] = implode(',', array_slice($data,1));
			return true;
        }
		elseif ($mode != 'metadata') {
			$renderer->doc .= $this->getLang('mailto_attention_to')
							  . implode (', ',$data);
			global $ID;
			$metas=p_get_metadata($ID);
			if ($metas['plugin']['mailto']['sent'])
			  $renderer->doc .= $this->getLang('mailto_attention_sent')
							    . strftime($this->getConf('mailto_dformat'), $metas['plugin']['mailto']['sent']) . ' ) ';
			return true;
		}
        return false;		
    }
}

