<?php

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

class Contact {
	public $cid;
	public $name;
	public $co;
	public $address;
	public $city;
	public $country;
	public $phone;
	public $phone2;
	public $email;
	public $email2;
	public $url;
	public $url2;
	public $essential;
	public $notes;

	public function write($fp) {
		fwrite($fp, "    <contact cid=\"" . convert_xml($this->cid, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             name=\"" . convert_xml($this->name, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "             co=\"" . convert_xml($this->co, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             address=\"" . convert_xml($this->address, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             city=\"" . convert_xml($this->city, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             country=\"" . convert_xml($this->country, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             phone=\"" . convert_xml($this->phone, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             phone2=\"" . convert_xml($this->phone2, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             email=\"" . convert_xml($this->email, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "             email2=\"" . convert_xml($this->email2, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "             url=\"" . convert_xml($this->url, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             url2=\"" . convert_xml($this->url2, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             essential=\"" . convert_xml($this->essential, ENT_QUOTES, "UTF-8") . "\">");
		fwrite($fp, convert_xml($this->notes, ENT_QUOTES, "UTF-8"));
		fwrite($fp, "</contact>\n");
	}

	public function show()
	{
		echo "<p>\n";
		//		echo "\tcid: " . $this->cid . "<br/>\n";
		echo "\tName: " . $this->name . "<br/>\n";
		if($this->co) echo "\tc/o: " . $this->co . "<br/>\n";
		if($this->address) echo "\tAddress: " . $this->address . "<br/>\n";
		if($this->city) echo "\tCity: " . $this->city . "<br/>\n";
		if($this->country) echo "\tCountry: " . $this->country . "<br/>\n";
		if($this->phone) echo "\tPhone: " . $this->phone . "<br/>\n";
		if($this->phone2) echo "\tPhone2: " . $this->phone2 . "<br/>\n";
		if($this->email) echo "\tEmail: <a href=\"mailto:" . $this->email . "\">" . $this->email . "</a><br/>\n";
		if($this->email2) echo "\tEmail2: <a href=\"mailto:" . $this->email2 . "\">" . $this->email2 . "</a><br/>\n";
		if($this->url) echo "\tURL: <a href=\"" . $this->url . "\">" . $this->url . "</a><br/>\n";
		if($this->url2) echo "\tURL2: <a href=\"" . $this->url2 . "\">" . $this->url2 . "</a><br/>\n";
		if($this->notes) echo "\tNotes: <br/>\n" . parse($this->notes) . "<br/>\n";
		echo "\t<a href=\"?mode=addressbook&amp;action=editcontact&amp;cid=" . $this->cid . "\">Edit</a>\n";
		echo "</p>\n";
	}

	public function showshort()
	{
		echo "<div class=\"contact\"><a href=\"?mode=addressbook&amp;cid=" . $this->cid . "\">" . $this->name . "</a></div>";
	}

	function Contact($cid, $name, $co, $address, $city, $country, $phone, $phone2, $email, $email2, $url, $url2, $essential, $notes)
	{
		$this->cid = $cid;
		$this->name = $name;
		$this->co = $co;
		$this->address = $address;
		$this->city = $city;
		$this->country = $country;
		$this->phone = $phone;
		$this->phone2 = $phone2;
		$this->email = $email;
		$this->email2 = $email2;
		$this->url = $url;
		$this->url2 = $url2;
		$this->essential = $essential;
		$this->notes = $notes;
	}
}

class ContactGroup {
	public $gid;
	public $name;
	private $contacts = array();

	public function getContact($cid)
	{
		return $this->contacts[$cid];
	}

	public function write($fp) {
		fwrite($fp, "  <contactgroup gid=\"" . convert_xml($this->gid, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "                name=\"" . convert_xml($this->name, ENT_QUOTES, "UTF-8") . "\">\n");

		foreach($this->contacts as $contact) {
			$contact->write($fp);
		}

		fwrite($fp, "  </contactgroup>\n");
	}

	public function getMaxCID()
	{
		$maxcid = 1;
		foreach($this->contacts as $contact) {
			if($maxcid < $contact->cid) $maxcid = $contact->cid;
		}
		return $maxcid;
	}

	public function add($contact) {
		$key = $contact->cid;
		$this->contacts[$key] = $contact;
	}

	public function show()
	{
		echo "  <div class=\"contactgroup\">\n";
		echo "    <div class=\"title\">" . $this->name . "</div>\n";
		echo "    <div class=\"contacts\">\n";
		foreach($this->contacts as $contact) {
			$contact->showshort();
		}
		echo "    </div>\n";
		echo "  </div>\n";
	}

	public function showshort()
	{
		global $client_is_mobile_device;
		$ness = 0;
		foreach($this->contacts as $contact) {
			if($contact->essential) $ness += 1;
		}

		echo "  <div class=\"contactgroup\">\n";
		echo "    <div class=\"title\"><a href=\"?mode=addressbook&amp;gid=" . $this->gid . "\">" . $this->name . "</a>";
		if(!$client_is_mobile_device)
			echo " - ".sizeof($this->contacts)." contacts (". sprintf("%d", sizeof($this->contacts) - $ness)." hidden)";
		echo "</div>\n";
		echo "    <div class=\"contacts\">\n";
		foreach($this->contacts as $contact) {
			if($contact->essential) $contact->showshort();
		}
		echo "    </div>\n";
		echo "  </div>\n";
	}

	function ContactGroup($gid,
												$name)
	{
		$this->gid = $gid;
		$this->name = $name;
	}
}

class Contacts {

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

	public function show()
	{
		echo "<div class=\"contactlist\">\n";
		foreach($this->contactgroups as $contactgroup) {
			$contactgroup->showshort();
		}
		echo "</div>\n";
	}

	public function getNextCID()
	{
		$nextcid = 1;
		foreach($this->contactgroups as $contactgroup) {
			$cid = $contactgroup->getMaxCID();
			if($nextcid < $cid) $nextcid = $cid;
		}
		return $nextcid + 1;
	}

	public function getNextGID()
	{
		$nextgid = 1;
		foreach($this->contactgroups as $contactgroup) {
			if($nextgid < $contactgroup->gid) $nextgid = $contactgroup->gid;
		}
		return $nextgid + 1;
	}

	public function getContact($cid)
	{
		foreach($this->contactgroups as $contactgroup) {
			if($contactgroup->getContact($cid)) return $contactgroup->getContact($cid);
		}
		return false;
	}

	public function getContactGroup($gid)
	{
		return $this->contactgroups[$gid];
	}

	public function add($contactgroup) {
		$key = $contactgroup->gid;
		$this->contactgroups[$key] = $contactgroup;
	}
	
	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, "<contacts>\n");
		foreach($this->contactgroups as $contactgroup) {
			$contactgroup->write($fp);
		}
		fwrite($fp, "</contacts>\n");

		fclose($fp);
	}
	
	private function read()
	{

		$dom = new DomDocument;
		$dom->preserveWhiteSpace = TRUE;
		$dom->load($this->file);
		$contactgroups = $dom->getElementsByTagName('contactgroup');

		foreach ($contactgroups as $cg) {
			$contactgroup = new ContactGroup($cg->getAttribute('gid'),
																			 $cg->getAttribute('name'));

			$contacts = $cg->getElementsByTagName('contact');
			foreach ($contacts as $c) {
				$contact = new Contact($c->getAttribute('cid'),
															 $c->getAttribute('name'),
															 $c->getAttribute('co'),
															 $c->getAttribute('address'),
															 $c->getAttribute('city'),
															 $c->getAttribute('country'),
															 $c->getAttribute('phone'),
															 $c->getAttribute('phone2'),
															 $c->getAttribute('email'),
															 $c->getAttribute('email2'),
															 $c->getAttribute('url'),
															 $c->getAttribute('url2'),
															 $c->getAttribute('essential'),
															 $c->textContent);
				$contactgroup->add($contact);
			}
			$this->add($contactgroup);
		}
	}

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

}
?>