<?php
/**
 * BlogMeta plugin
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Martin Plicka <mail@mplicka.cz>
 */

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

if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

require_once(DOKU_PLUGIN.'action.php');

class action_plugin_blogmeta extends DokuWiki_Action_Plugin{

    function getInfo() {
        return array(
                'author' => 'Martin Plicka',
                'email'  => 'mail@mplicka.cz',
                'date'   =>  @file_get_contents(DOKU_PLUGIN . 'blogmeta/VERSION'),
                'name'   => 'BlogMeta Plugin (action plugin)',
                'desc'   => 'Inserts some metadata after the dokuwiki page content on selected pages, always above Discussion plugin output. Suitable for people who hates metadata displayed far far below comments.',
                'url'    => 'http://mplicka.cz/en/projects/dokuwiki_goodies',
                );
    }

    function register(&$contr) {
        $contr->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'printout', array() );
                
        
        // HACK to make this plugin the first listener
        
        //get array with listeners
        $collection = &$contr->_hooks['TPL_ACT_RENDER_AFTER'];
        
        if ( ($len = count($collection)) > 1) {//more than 1 listener
          //save first listener
          $save = $collection[0];
          //exchange last registered listener with first one in array
          $collection[0]=$collection[$len-1];
          $collection[$len-1] = $save; 
        }
    }
    
  
    function printout(&$event, $param) {
        global $ID;
        global $INFO;
        global $conf;
        global $ACT;
        
        //only when show action, page exists and its ID matches with settings
        if ($ACT == 'show' && $INFO['exists'] && ereg( ($this->getConf('pages_regexp')) , $ID) ){
          print "<div class=\"plugin_blogmeta\">";
          print $this->getLang('posted')." ".strftime($conf['dformat'],$INFO['meta']['date']['created']);
          if ($this->getConf('show_user')) print " &middot; ".$INFO['meta']['creator'];
          print "</div>".DOKU_LF;
        }
    }
  
}

