<?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");
include_once($UTIL_DIR . "/modules.php");

class Page {
  public $title;
  public $content;

  public function write($fp)
  {
    fwrite($fp, "  <page title=\"" .
	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\">");
    fwrite($fp, htmlspecialchars($this->content, ENT_QUOTES, "UTF-8") . "</page>\n");
  }

  public function show()
  {
    global $DATA_DIR, $modules;

    $str = Markdown($this->content);
		
    if(preg_match_all("/\[\[([\?,a-zA-Z0-9_=]+)\]\]/", $str, $res)) {

      $modulecodes = array_unique($res[1]);
      foreach($modulecodes as $modulecode) {
				$str = str_replace("[[" . $modulecode . "]]", runModule($modulecode), $str);
      }
    }

    echo $str;
  }

  public function Page($title, $content)
  {
    $this->title = $title;
    $this->content = $content;
  }
}

class Pages {

  private $file;
  public $pages = array();

  // Admin config
  public $admin_title = "Pages";
  public $admin_submodules = array("Add page" => "add",
																	 "Edit page" => "edit");

  public function admin_edit($action, $vars)
  {
    switch($action) {
    case "add":
      $this->pages[$vars["title"]]->content = $vars["content"];
      $this->write();
      echo "\"" . $this->pages[$vars["title"]]->title . "\" has now been edited.";
      break;
			
    case "preview":
      $p = new Page($vars["title"], $vars["content"]);
      echo "<div class=\"preview\">\n";
      echo "<div class=\"content\">\n";
      echo $p->show();
      echo "</div>\n";
      echo "</div>\n";
      echo "<p>Looking ok?</p>";
			$form = new Form("add");
			$form->addWidget(new Hidden($vars));
			$form->addWidget(new Button("yes"));
			$form->render();

			$form = new Form("retry");
			$form->addWidget(new Hidden($vars));
			$form->addWidget(new Button("no"));
			$form->render();
      break;
			
    case "edit":
    case "retry":
      if(isset($vars["content"])) $content = $vars["content"];
      else $content = $this->pages[$vars["title"]]->content;

      echo "<p>See <a rel=\"external\" href=\"http://daringfireball.net/projects/markdown/syntax\">markdown</a> syntax.</p>";
			
      $form = new Form("preview");
      $form->addWidget(new Hidden($vars));
      $form->addWidget(new TextEdit("Content", "content", $content));
      $form->addWidget(new Button("Preview"));
			$form->render();
      break;
			
    case "select":
    default:
      $pagelist = array();
      foreach($this->pages as $p) {
				$pagelist[$p->title] = $p->title; 
      }
			$form = new Form("edit");
      $form->addWidget(new ComboBox("Edit this entry:", "title", "", $pagelist));
      $form->addWidget(new Button("Edit..."));
			$form->render();
      break;
    }
  }

  public function admin_add($action, $vars)
  {
    switch($action) {
    case "add":
      $page = new Page($vars["title"], "");
      $this->add($page);
      $this->write();
      echo "The page titled \"" . $page->title . "\" has now been added.";
      break;
			
    default:
      $form = new Form("add");
      $form->addWidget(new LineEdit("Page title (must not contain spaces or special characters)", "title", ""));
      $form->addWidget(new Button("Add"));
			$form->render();
      break;
    }
  }

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "edit":
      $this->admin_edit($action, $vars);
      break;
    case "add":
      $this->admin_add($action, $vars);
      break;
    }
  }

  public function getPage($title)
  {
    return $this->pages[$title];
  }

  public function add($page) {
    $key = $page->title;
    $this->pages[$key] = $page;
  }
	
  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

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

    fclose($fp);
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = TRUE;
    $dom->load($this->file);
    $pages = $dom->getElementsByTagName('page');

    foreach ($pages as $p) {
      $page = new Page($p->getAttribute('title'),
		       $p->textContent);
      $this->add($page);
    }
  }

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

}

function pages_init()
{
  global $DATA_DIR;
  return new Pages($DATA_DIR . "/pages.xml");
}

?>