<?php

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

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

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

    $user = $users->getUser($this->user);

    echo "      <div class=\"event\">\n";
    echo "        <div class=\"title\">". $this->title . "\n";
		//    echo "          <a class=\"button\" href=\"\">Edit</a>\n";
    echo "        </div>\n";
    echo "        <div class=\"time\">" . date("G:i", $this->starttime) . " - " . 
      date("G:i", $this->starttime + $this->duration) .  "</div>\n";
    if(!$client_is_mobile_device) {
      echo "        <div class=\"description\">". $this->description . "</div>\n";
    } else {
      echo "        <div class=\"mobiledescription\">". $this->description . "</div>\n";
    }
    echo "        <div class=\"user\">By: ".$user->name . "</div>\n";
		//    echo "  <a href=\"?mode=calendar&amp;date=" . $date . "&amp;eid=" . $this->eid . "&amp;action=edit\">Edit</a>";
    echo "      </div>\n";
  }
  
  public function Event($eid, $title, $starttime, $duration, $description, $user)
  {
    $this->eid = $eid;
    $this->title = $title;
    $this->starttime = $starttime;
    $this->duration = $duration;
    $this->description = $description;
    $this->user = $user;
  }
}

class Events {

  private $file;
  public $events = array();
	
  public function add($event) {
    $key = $event->eid;
    $this->events[$key] = $event;
  }
	
  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");
      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 {
			echo "<p>ERROR: User! <em>".$id."</em> does not exist!</p>\n";
			return false;
		}
		return true;
	}
  */

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

  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('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();
  }

}
?>