<?php

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

class Event {
  public $eid;
  public $title;
  public $starttime;
  public $duration;
  public $description;
	public $concert;
  public $user;

  public function show()
  {
    global $users, $GLOBALS, $client_is_mobile_device;

		$str = "";

    $user = $users->getUser($this->user);
    $str .=  "      <div class=\"event";
		if($this->concert == "true") $str .= " concert";
		$str .= "\">\n";
    $str .=  "        <div class=\"title\">". $this->title . "\n";
		//    $str .=  "          <a class=\"button\" href=\"\">Edit</a>\n";
    $str .=  "        </div>\n";
    $str .=  "        <div class=\"time\">" . date("G:i", $this->starttime) .
			" - " .  date("G:i", $this->starttime + $this->duration) .  "</div>\n";
    if(!$client_is_mobile_device) {
      $str .=  "        <div class=\"description\">". $this->description .
				"</div>\n";
    } else {
      $str .=  "        <div class=\"mobiledescription\">" .
				$this->description . "</div>\n";
    }
    $str .=  "        <div class=\"user\">By: ".$user->name . "</div>\n";

		$datestr = "";
		if(isset($GLOBALS['date'])) $datestr = "&amp;date=". $GLOBALS['date'];

		$str .=  "        <a href=\"?mode=calendar" . $datestr .
			"&amp;eid=" . $this->eid . "&amp;action=delete\">Delete</a>";
    $str .=  "      </div>\n";

		return $str;
  }
  
  public function show_simple()
  {
    global $users, $GLOBALS, $client_is_mobile_device;

		$str = "";

    $str .=  "      <div class=\"event_simple\">\n";
    $str .=  "        <span class=\"date\">" . date("D d M Y", $this->starttime) . "</span>\n";
    $str .=  "        <span class=\"title\">". $this->title . "\n";
    $str .=  "        </span>\n";
    $str .=  "        <span class=\"time\">" . date("G:i", $this->starttime) .
			" - " .  date("G:i", $this->starttime + $this->duration) .  "</span>\n";
    $str .=  "      </div>\n";

		return $str;
  }
  
  public function Event($eid, $title, $concert, $starttime, $duration,
												$description, $user)
  {
    $this->eid = $eid;
    $this->title = $title;
    $this->starttime = $starttime;
    $this->duration = $duration;
    $this->description = $description;
    $this->user = $user;
		$this->concert = $concert;
  }
}

class Events {

  private $file;
  public $events = array();
	
  public function add($event) {
    $key = $event->eid;
    $this->events[$key] = $event;
  }
	
	public function delete($eid) {
		if(isset($this->events[$eid])) {
			unset($this->events[$eid]);
			echo "DELETE";
		}
	}

  public function write()
  {
    $fp = fopen($this->file, "w");
    
    $block = TRUE;
    flock($fp, LOCK_EX, $block); // do an exclusive lock
    
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    
    fwrite($fp, "<events>\n");
    foreach($this->events as $event) {
      fwrite($fp, "  <event eid=\"" .
	     htmlspecialchars($event->eid, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         title=\"" . 
	     htmlspecialchars($event->title, ENT_QUOTES, "UTF-8") . "\"\n");

			if($event->concert == "true") $concert = "true";
			else $concert = "false";
			fwrite($fp, "         concert=\"" . $concert . "\"\n");

      fwrite($fp, "         starttime=\"" . 
	     htmlspecialchars($event->starttime, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         duration=\"" . 
	     htmlspecialchars($event->duration, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         description=\"" . 
	     htmlspecialchars($event->description, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         user=\"" . 
	     htmlspecialchars($event->user, ENT_QUOTES, "UTF-8") . "\">\n");
      fwrite($fp, "  </event>\n");
    }
    fwrite($fp, "</events>\n");
    
    fclose($fp);
  }

  /*
	public function deleteForumUser($id)
	{
		if($this->members[$id]) {
			unset($this->members[$id]); 
			//			$this->write();
		} else {
			$str .=  "<p>ERROR: User! <em>".$id."</em> does not exist!</p>\n";
			return false;
		}
		return true;
	}
  */

  public function show($starttime, $endtime)
  {
		$str = "";
    foreach($this->events as $event) {
      if($event->starttime > $starttime && $event->starttime < $endtime)
				$str .= $event->show();
    }
		return $str;
  }

  public function getEvent($eid)
  {
    $event = $this->events[$eid]; 
    return $event;
  }

  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $events = $dom->getElementsByTagName('event');
    
    foreach ($events as $e) {
      $event = new Event($e->getAttribute('eid'),
			 $e->getAttribute('title'),
			 $e->getAttribute('concert') == "true",
			 $e->getAttribute('starttime'),
			 $e->getAttribute('duration'),
			 $e->getAttribute('description'),
			 $e->getAttribute('user'));
      
      $this->add($event);
    }
    
  }

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

}
?>