<?php
include_once(dirname(__FILE__) . '/../printer.inc.php');

$BIBTEX_EXCLUDES = array('bibtexEntryType', 'bibtexEntryType', 'bibtexCitation', 'download');

class DefaultBibtexPrinter implements BibtexPrinter {
	private static $ENTRY_DIVIDER = ', ';
	private static $SPECIAL_CHARACTERS = array(
		'\dots' => '...',
		'\\\'' => '’',
		'\_' => '_',
		'\underline' => '_',
		'\backslash' => '\\',
		'\textbackslash' => '\\',
		'\lbrack' => '[',
		'\rbrack' => ']',
		'\langle' => '<',
		'\rangle' => '>',
		'\S' => '$',
		'\$' => '$',
		'\&' => '&',
		'\#' => '#',
		'\{' => '{',
		'\}' => '}',
		'\%' => '%',
		'\\"a' => '&auml;',
		'\\"o' => '&ouml;',
		'\\"u' => '&uuml;',
		'\\"A' => '&Auml;',
		'\\"O' => '&Ouml;',
		'\\"U' => '&Uuml;',
		'\\,' => ',',
	);

	public function getStyleFile() {
		return 'default.css';
	}

	public function getScriptFile() {
		return 'default.js';
	}

	public function printEntry($entry, $class = false) {
		if(!$entry) return '';

		$result = '<p class="bibentry';
		if(is_string($class)) {
			$result .= ' ' . $class;
		}
		$result .= '">';

		$result .= '<span class="bibauthors">' . $this->sanitizeFieldValue($entry['author']) . '</span>' . self::$ENTRY_DIVIDER;

		$result .= '<span class="bibtitle">' . $this->sanitizeFieldValue($entry['title']) . '</span>' . self::$ENTRY_DIVIDER;

		if($entry['booktitle'] != '') {
			$result .= '<span class="bibbooktitle">' . $this->sanitizeFieldValue($entry['booktitle']) . '</span>' . self::$ENTRY_DIVIDER;	
		}	

		if($entry['publisher'] != '') {
			$result .= '<span class="bibpublisher">' . $this->sanitizeFieldValue($entry['publisher']) . '</span>' . self::$ENTRY_DIVIDER;
		}

		if($entry['year'] != '') {
		$result .= '<span class="bibyear">' . $this->sanitizeFieldValue($entry['year']) . '</span>' . self::$ENTRY_DIVIDER;
		}

		if($entry['pages'] != '') {
			$result .= '<span class="bibpages">pp.' . $this->sanitizeFieldValue($entry['pages']) . '</span>' . self::$ENTRY_DIVIDER;
		}

		if($entry['note'] != '') {
			$result .= '<span class="bibnote">' . $this->sanitizeFieldValue($entry['note']) . '</span>' . self::$ENTRY_DIVIDER;
		}

		if($entry['download'] != '') {
			$result .= '<span class="bibdownload"><a href="' . $entry['download'] . '" title="download publication">[download]</a></span>' . self::$ENTRY_DIVIDER;
		}

		$result .= $this->getBibtex($entry);
		
//		$result = substr($result, 0, strlen($result) - 2);

		$result .= '</p>';

		return $result;
	}

	private function getBibtex($entry) {
		global $BIBTEX_EXCLUDES;

		$result = '<a href="javascript:toggleBibtexView(\'bibtex_' . $entry['bibtexCitation'];
		$result .= '\')" title="toggle bibtex">[bibtex]</a>';
		$result .= '<pre id="bibtex_' . $entry['bibtexCitation'] . '" class="bibtexCode">';

		$result .= '@' . $entry['bibtexEntryType'] . '{';
		$result .= $entry['bibtexCitation'] . ",\n";

		foreach(array_keys($entry) as $curKey) {
			if(in_array($curKey, $BIBTEX_EXCLUDES)) continue;
			if(!isset($entry[$curKey]) || $entry[$curKey] == '') continue;

			$result .= '  ';
			$result .= $curKey. ' = {' . $entry[$curKey] . "},\n";
		}

		$result .= '}';

		$result .= '</pre>';

		return $result;
	}

	private function sanitizeFieldValue($value) {
		foreach(self::$SPECIAL_CHARACTERS as $toSearch => $replacement) {
			$value = str_replace($toSearch, $replacement, $value);
		}

		return $value;
	}
}

?>
