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

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

class Event {
  public $title;
  public $time;
  public $description;
  public $flyer;

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

	public function show()
	{
		global $ICONS_DIR;
		$icons = new Icons($ICONS_DIR."/");
		$icon = $icons->icons[$this->flyer];

    $content = Markdown(htmlspecialchars_decode($this->description));

		$str = "<div class=\"event\">\n";
		$str .= "  <div class=\"event_title\">" . 
			htmlspecialchars_decode($this->title, ENT_QUOTES) . "</div>\n";
		$str .= "  <div class=\"event_time\">" . date("D M jS Y", $this->time) . "</div>\n";
		$str .= "  <div class=\"event_description\">" . $content . "</div>\n";
		if($this->flyer) {
      $str .= "  <img class=\"event_flyer\" alt=\"flyer\" src=\"".$icon->prefix.$icon->file."\"/>\n";
    }
    $str .= "</div>\n";
    return $str;
  }
	
  public function Event($title, $time, $description, $flyer = "")
  {
    $this->title = $title;
    $this->time = $time;
    $this->description = $description;
    $this->flyer = $flyer;
  }
}

class Events {

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

  // Admin config
  public $admin_title = "Events";
  public $admin_submodules = array("Add Event" => "add",
																	 "Edit Event" => "edit",
																	 "Delete Event" => "delete");

  public function admin_add($action, $vars)
	{
		global $ICONS_DIR;

		switch($action) {
		case "add":	
			$event = new Event($vars["title"], DateTimeEdit::toTimestamp($vars, "time"),
												 $vars["description"], $vars["icon"]);
			echo "\"" .$event->title . "\" has now been added.";
			$this->add($event);
			$this->write();
			break;
			
		case "preview":
			$event = new Event($vars["title"], DateTimeEdit::toTimestamp($vars, "time"),
												 $vars["description"], $vars["icon"]);
			echo "<div class=\"preview\">\n";
			echo "<div class=\"content\">\n";
			echo $event->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 "retry":
			$title = $vars["title"];
			$time = DateTimeEdit::toTimestamp($vars, "time");
			$description = $vars["description"];
			$flyer = $vars["icon"];
		default:
			$form = new Form("preview");
			$form->addWidget(new LineEdit("Title", "title", $title));
			$form->addWidget(new DateTimeEdit("Time", "time", $time));
			$form->addWidget(new TextEdit("Description", "description", $description));
			$form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/")));
			$form->addWidget(new Button("Add event"));
			$form->render();
			break;
		}
	}

	public function admin_edit($action, $vars)
	{
		global $ICONS_DIR;

		switch($action) {
		case "add":
			$this->events[$vars["eventid"]]->title = $vars["title"];
			$this->events[$vars["eventid"]]->time = DateTimeEdit::toTimestamp($vars, "time");
			$this->events[$vars["eventid"]]->description = $vars["description"];
			$this->events[$vars["eventid"]]->flyer = $vars["icon"];
			$this->write();
			echo "\"" . $this->events[$vars["eventid"]]->title . "\" has now been edited.";
			break;
			
		case "preview":
			$event = new Event($vars["title"], DatetimeEdit::toTimestamp($vars, "time"), 
												 $vars["description"], $vars["icon"]);
			echo "<div class=\"preview\">\n";
			echo "<div class=\"content\">\n";
			echo $event->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["title"])) $title = $vars["title"];
			else $title = $this->events[$vars["eventid"]]->title;
			if(isset($vars["time_year"])) $time = DateTimeEdit::toTimestamp($vars, "time");
			else $time = $this->events[$vars["eventid"]]->time;
			if(isset($vars["description"])) $description = $vars["description"];
			else $description = $this->events[$vars["eventid"]]->description;
			if(isset($vars["icon"])) $icon = $vars["icon"];
			else $icon = $this->events[$vars["eventid"]]->flyer;
			
			$form = new Form("preview");
			$form->addWidget(new Hidden($vars));
			$form->addWidget(new LineEdit("Title", "title", $title));
			$form->addWidget(new DateTimeEdit("Time", "time", $time));
			$form->addWidget(new TextEdit("Description", "description", $description));
			$form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/")));
			$form->addWidget(new Button("Update event"));
			$form->render();
			break;
			
		case "select":
		default:
			$eventlist = array();
			foreach($this->events as $event) {
				$eventlist[$event->title] = $event->time; 
			}

			$form = new Form("edit");
			$form->addWidget(new ComboBox("Edit this event:", "eventid", "", $eventlist));
			$form->addWidget(new Button("Edit..."));
			$form->render();
			break;
		}
	}

  public function admin_delete($action, $vars)
	{
		switch($action) {
		case "delete":
			echo "\"". $this->events[$vars["eventid"]]->title . "\" has now been deleted.";
			unset($this->events[$vars["eventid"]]);
			$this->write();
			break;
			
		case "confirm":
			echo "Really delete: " . $this->events[$vars["eventid"]]->title . "?";
			$form = new Form("delete");
			$form->addWidget(new Hidden($vars));
			$form->addWidget(new Button("yes"));
			$form->render();

			$form = new Form("select");
			$form->addWidget(new Hidden($vars));
			$form->addWidget(new Button("no"));
			$form->render();
			break;
			
		case "select":
		default:
			$eventlist = array();
			foreach($this->events as $event) {
				$eventlist[$event->title] = $event->time; 
			}

			$form = new Form("confirm");
			$form->addWidget(new ComboBox("Delete this event:", "eventid", "", $eventlist));
			$form->addWidget(new Button("Delete..."));
			$form->render();
			break;
		}
	}

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

  public function run($params)
  {
    foreach($params as $param) {
      switch($param) {
      case "coming":
				return $this->showcoming(-1);
				break;

      case "all":
        return $this->showall(-1);
        break;

      case "old":
      default:
				return $this->showold(-1);
				break;
      }
    }
  }

  public function showall($number)
  {
    $str = "";

    $foundany = false;

    // Key sort
    ksort($this->events);

    // If number is -1 show all shows.
    if($number == -1) $number = 100000;

    foreach($this->events as $event) {
      $foundany = true;
      $str .= $event->show();
      $number--;
      if(!$number) return $str;
    }

    if($foundany == false) return "No shows available at the moment.";
    return $str;
  }

  public function showcoming($number)
  {
    $str = "";

    $foundany = false;

    // Key sort
    ksort($this->events);

    // If number is -1 show all shows.
    if($number == -1) $number = 100000;
		
    foreach($this->events as $event) {
      if($event->time >= time()) {
				$foundany = true;
				$str .= $event->show();
				$number--;
      }
      if(!$number) return $str;
    }

    if($foundany == false) return "No shows available at the moment.";
    return $str;
  }

  public function showold($number)
  {
    $str = "";

    // Key sort
    krsort($this->events);

    // If number is -1 show all shows.
    if($number == -1) $number = 100000;

    foreach($this->events as $event) {
      if($event->time <= time()) {
				$str .= $event->show();
				$number--;
      }
      if(!$number) return $str;
    }
    return $str;
  }
	
  public function add($event) {
    $key = $event->time;
    //		array_push($this->events, $event);
    $this->events[$key] = $event;
  }
	
  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

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

    fclose($fp);
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $params = $dom->getElementsByTagName('event');

    foreach ($params as $param) {
      $event = new Event($param->getAttribute('title'),
			 $param->getAttribute('time'),
			 $param->getAttribute('description'),
			 $param->getAttribute('flyer'));
      $this->add($event);
    }
  }

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

}

function events_init()
{
  global $DATA_DIR;
  return new Events($DATA_DIR . "/events.xml");
}

?>