<?php
/**
 * Plugin Ticket Linker: Automatic links to Cerberus tickets"
 * 
 * @license    GNU General Public License (GPL) 
 * @author     Dominik Smatana <dominiks@users.sourceforge.net>
 */
 
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');
 
class syntax_plugin_ticketlinker extends DokuWiki_Syntax_Plugin {
 
    function getInfo(){
        return array(
            'author' => 'Dominik Smatana',
            'email'  => 'dominiks@users.sourceforge.net',
            'date'   => '2007-08-04',
            'name'   => 'Ticket linker',
            'desc'   => 'Automatic links to Cerberus tickets',
            'url'    => 'https://sourceforge.net/projects/ticket-linker',
        );
    }
 
    function getType(){
        return 'substition';
    }
 
    function getSort(){
        return 999;
    }
 
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern('ticket#.{'.$this->getConf('ticketMaskLength').'}', $mode, 'plugin_ticketlinker');
    }
 
    function handle($match, $state, $pos, &$handler){
        switch ($state) {
          case DOKU_LEXER_ENTER : 
            break;
          case DOKU_LEXER_MATCHED :
            break;
          case DOKU_LEXER_UNMATCHED :
            break;
          case DOKU_LEXER_EXIT :
            break;
          case DOKU_LEXER_SPECIAL :
			return array(0 => $match);
            break;
        }
        return array();
    }
 
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
			$ticketId = substr($data[0], -1 * $this->getConf('ticketMaskLength'));
            $renderer->doc .= '<a href="'.$this->getConf('cerberusUrl').$ticketId.'" target="_blank" title="Ticket #'.$ticketId.'">#'.$ticketId.'</a>';
            return true;
        }
        return false;
    }
	
}
?>
