<?php
/**
 * DokuWiki Plugin outdated (Syntax Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Andreas Böhler <dev@aboehler.at>
 */


// must be run within DokuWiki
if(!defined('DOKU_INC')) die();


class syntax_plugin_outdated extends DokuWiki_Syntax_Plugin {

    /**
     * @var helper_plugin_publish
     */
    private $hlp;

    function __construct(){
        $this->hlp = plugin_load('helper','outdated');
    }

    function pattern() {
        return '\{\{outdated>[^}]*\}\}';
    }

    function getType() {
        return 'substition';
    }

    function getSort() {
        return 150;
    }

    function PType() {
        return 'block';
    }

    function connectTo($mode) {
        $this->Lexer->addSpecialPattern($this->pattern(),$mode,'plugin_outdated');
    }

    function handle($match, $state, $pos, Doku_Handler $handler){
        $options = trim(substr($match,11,-2));
        $options = explode(',', $options);
        
        $data = array(
            'timedelta' => '',
            'message' => '',
        );
        
        foreach($options as $option)
        {
            list($key, $val) = explode('=', $option);
            switch($key) {
            case 'timedelta':
                $lastchr = substr($val, -1);
                $multiplier = 1;
                switch($lastchr) {
                case 'd':
                    $multiplier = 24 * 3600;
                    break;
                case 'h':
                    $multiplier = 3600;
                    break;
                case 'y':
                    $multiplier = 365 * 24 * 3600;
                    break;
                }
                $val = intval(substr($val, 0, -1)) * $multiplier;
                // fall-through            
            default:
                $data[$key] = $val;
            }
        }
        return $data;
    }

    function render($mode, Doku_Renderer $renderer, $data) {
        global $conf;

        if($mode != 'metadata') {
            return false;
        }
        $renderer->meta['plugin_outdated'] = $data;
        return true;
    }

}


