<?php
/**
 * User annotations plugin
 * 
 * @author     Gabriel Birke <birke@d-scribe.de>
 */
 
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
 
class action_plugin_userannotations extends DokuWiki_Action_Plugin {
 
  /**
   * return some info
   */
  function getInfo(){
    return array(
		 'author' => 'Gabriel Birke',
		 'email'  => 'birke@d-scribe.de',
		 'date'   => '2009-02-23',
		 'name'   => 'User annotations (action plugin component)',
		 'desc'   => 'Displays user annotations below regular wiki text.'
		 );
  }
 
  /*
   * Register its handlers with the dokuwiki's event controller
   */
  function register(&$controller) {
	  $controller->register_hook('TPL_ACT_RENDER', 'BEFORE',  $this, '_displayMessage');
      $controller->register_hook('TPL_ACT_RENDER', 'AFTER',  $this, '_displayAnnotations');
  }
  
  /**
   * Displays a hint if a page in the annotation namespace is displayed.
   */
  function _displayMessage(&$event, $param) {
	  global $ID;
	  $privatepath = $this->getConf('annotation_namespace');
	  $pattern = '/^'.preg_quote($privatepath.':').'[^:]+\:/';
	  if(preg_match($pattern, $ID, $matches))
		  {
			  $annotated_page = str_replace($matches[0], '', $ID);
			  $title = p_get_first_heading($annotated_page);
			  $title = empty($title)?noNS($annotated_page):$title;
			echo sprintf('<p class="annotation-info">'.$this->getLang('annotation_info').'</p>', '<a href="'.wl($annotated_page).'">'.$title.'</a>');
			
		  }
  }
	
 
  /**
   * Collect all annotations from the annotation namespace that match the current 
   * page ID and display the annotations.
   */
  function _displayAnnotations(&$event, $param) {
	  
	if($event->data != "show")
		return;
	global $ID;
	
	$privatepath = $this->getConf('annotation_namespace');
	$anonymous = $this->getConf('anonymous_user');
	$current = empty($_SERVER['REMOTE_USER']) ? $anonymous : $_SERVER['REMOTE_USER'];
	$annotation_ns = array($current);
	// Namespaces for each user
	if($this->getConf('all_namespaces'))
	{
		// Collect names of users that have already posted something in the annotation namespace
		// For wikis with big user lists and less annotations this may be more efficient than iterating all users
		foreach ( new DirectoryIterator(preg_replace('/\.txt$/', '', wikiFN($privatepath))) as $item )
        {
			if($item->isDir() && !$item->isDot()) {
			    $user = $item->getBasename();
			    if($user != $current)
    				$annotation_ns[] = $user;
                
            }
        }
	}
	foreach($annotation_ns as $user)
	{
		$annotation_id = $privatepath.':'.$user.':'.$ID;
		$perm = auth_quickaclcheck($annotation_id);
		// Only show annotation pages that exist. Maybe this user has not posted anything here
		if(file_exists(wikiFN($annotation_id, '', false)) &&  $perm >= AUTH_READ)
		{
			echo '<div class="annotation annotation-'.preg_replace('/[^a-z0-9]/', '', $ns).'">';
			echo '<h2>'.sprintf($this->getLang('annotationfrom'), $user).'</h2>';
			if($perm >= AUTH_EDIT)
    			echo '<p class="editannotation"><a href="'.DOKU_SCRIPT.'?id='.$annotation_id.'&do=edit">'.$this->getLang('edit_annotation').'</a></p>';
			echo p_wiki_xhtml($annotation_id, '', false);
			echo '</div>';
		}
		// The currently logged in user can create his own annotation
		elseif($user == $current && auth_quickaclcheck($privatepath.':'.$user) >= AUTH_CREATE)
		{
			echo '<div class="annotation"><p class="editannotation createannotation"><a href="'.DOKU_SCRIPT;
			echo '?id='.$annotation_id.'&do=edit" class="editlink">'.sprintf($this->getLang('create_annotation'), $current).'</a></p></div>';
		}
	}
  }
}
