<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/markdown.php");

// See: http://www.loc.gov/standards/iso639-2/php/code_list.php
function lang2text($lang)
{

	if(function_exists('locale_get_display_language'))
		return locale_get_display_language($lang, 'en');

	switch($lang) {
	case "en": return "english";
	case "da": return "danish";
	case "nl": return "dutch";
	case "bul": return "bulgarian";
  case "fr": return "french";
  case "it": return "italian";
	default: return "unknown";
	}
}

class Text {
	public $text;
	public $lang;
	public $original;

	public function show()
	{
		$str = "";

		$prefix = "Original";
		if(!$this->original) $prefix = "Translated";

		$str .= "<div class=\"header\"><h3>".$prefix." ".lang2text($this->lang)." text</h3></div>\n";
		$str .= markdown($this->text) . "\n";
		return $str;
	}

	public function Text($text, $lang, $original)
	{
		$this->text = $text;
		$this->lang = $lang;
		$this->original = $original;
	}
}

class Review {
	public $title;
	public $id;
	public $src;
	public $url;
	public $texts = array();

	public function write($fp)
	{
/*
		fwrite($fp, "      <track title=\"" .
					 htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             number=\"" . $this->number . "\"\n");
		fwrite($fp, "             playtime=\"" . $this->playtime . "\"\n");
		fwrite($fp, "             previewurl=\"" . $this->previewurl . "\">\n");
		fwrite($fp, "        <lyrics>".htmlspecialchars($this->lyrics, ENT_QUOTES, "UTF-8")."</lyrics>\n");
		fwrite($fp, "      </track>\n");
*/
	}

	public function show()
	{
		$str = "";

		//		$str .= $this->id . "\n";
		$str .= "<div class=\"header\"><h2>".$this->title."</h2></div>\n";
		$str .= "<p>Origin: <em>".$this->src."</em> <a href=\"".$this->url."\">".$this->url."</a></p>\n";

		foreach($this->texts as $text) {
			if($text->original) {
				$str .= $text->show();
				break;
			}
		}

		foreach($this->texts as $text) {
			if(!$text->original) {
				$str .= $text->show();
			}
		}

		return $str;
	}

	public function showRef()
	{
		$str = "";
		$str .= "<a href=\"?page=reviews&amp;rid=".
			$this->id."\">".$this->title." from ".$this->src."</a>\n";
		return $str;
	}

	public function add($text)
	{
		array_push($this->texts, $text);
	}

	public function Review($id, $title, $src, $url)
	{
		$this->id = $id;
		$this->title = $title;
		$this->src = $src;
		$this->url = $url;
	}
}

class Reviews {

  private $file;
  private $reviews = array();

  // Admin config
  public $admin_title = "Reviews";
  public $admin_submodules = array();

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "delete":
      break;
    }
  }

  public function run($params)
  {
		global $GLOBALS;
		$str = "";
		foreach($this->reviews as $review) {
			if($GLOBALS['rid'] == $review->id) {
				$str .= $review->show();
			}
		}

		return $str;
  }

	public function get($id)
	{
		return $this->reviews[$id];
	}

	public function showRef($id)
	{
		if(!$this->reviews[$id]) return "";
		return $this->reviews[$id]->showRef();
	}

  public function add($review) {
    $key = $review->id;
    $this->reviews[$key] = $review;
  }

  public function write()
  {
		/*
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<discography>\n");
    foreach($this->discs as $disc) {
			$disc->write($fp);
    }
    fwrite($fp, "</discography>\n");

    fclose($fp);
		*/
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);

		$discography = $dom->documentElement;
		
    $revs = $dom->getElementsByTagName('review');

    foreach($revs as $rev) {
			$review = new Review($rev->getAttribute('id'),
													 $rev->getAttribute('title'),
													 $rev->getAttribute('src'),
													 $rev->getAttribute('url'));

			$ts = $rev->getElementsByTagName('text');
			foreach($ts as $t) {
				$text = new Text($t->textContent,
												 $t->getAttribute('lang'),
												 $t->getAttribute('original'));
				$review->add($text);
			}

      $this->add($review);
    }
  }

  public function Reviews($file)
  {
    $this->file = $file;
    if(file_exists($file)) $this->read();
  }

}

function reviews_init()
{
  global $DATA_DIR;
  return new Reviews($DATA_DIR . "/reviews.xml");
}

?>