From 4bc99c18ecbbb7bb7d0fac4592fcc63e3c568798 Mon Sep 17 00:00:00 2001
From: deva <deva>
Date: Sun, 26 Oct 2008 12:29:38 +0000
Subject: We now have a functional but very ugly addressbook.

---
 forum/utils/addressbook.php | 134 ++++++++++++++++++++++++-----
 forum/utils/contacts.php    | 201 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 274 insertions(+), 61 deletions(-)

diff --git a/forum/utils/addressbook.php b/forum/utils/addressbook.php
index 43477dd..29b0fb0 100644
--- a/forum/utils/addressbook.php
+++ b/forum/utils/addressbook.php
@@ -1,41 +1,137 @@
 <?php
 include_once($UTIL_DIR . "/contacts.php");
 
+function form($cid,
+							$posturl,
+							$buttontext,
+							$name = "",
+							$address = "",
+							$city = "",
+							$country = "",
+							$phone = "",
+							$phone2 = "",
+							$email = "",
+							$email2 = "",
+							$url = "",
+							$url2 = "",
+							$essential = "",
+							$notes = "")
+{
+?>
+<form method="post" action="<?php echo $posturl; ?>">
+  <input type="hidden" name="cid" value="<?php echo $cid;?>">
+  Name: <input name="name" value="<?php echo $name;?>"><br/>
+  Address: <input name="address" value="<?php echo $address;?>"><br/>
+  City: <input name="city" value="<?php echo $city;?>"><br/>
+  Country: <input name="country" value="<?php echo $country;?>"><br/>
+  Phone: <input name="phone" value="<?php echo $phone;?>"><br/>
+  Phone2: <input name="phone2" value="<?php echo $phone2;?>"><br/>
+  Email: <input name="email" value="<?php echo $email;?>"><br/>
+  Email2: <input name="email2" value="<?php echo $email2;?>"><br/>
+  URL: <input name="url" value="<?php echo $url;?>"><br/>
+  URL2: <input name="url2" value="<?php echo $url2;?>"><br/>
+  Essential: <input type="checkbox" name="essential" <?php if($essential == "on") echo " checked";?>><br/>
+  Notes:<br/>
+  <textarea name="notes" cols="60" rows="10"><?php echo $notes;?></textarea><br/>
+  <button type="submit"><?php echo $buttontext; ?></button>
+</form>
+<?php
+}
+
+
 $contacts = new Contacts($DATA_DIR . "/contacts.xml");
 
-if($cid  && $name) {
-	// Edit
-	$contact = new Contact($cid,
-												 $name,
-												 $address,
-												 $phone,
-												 $phone2,
-												 $email,
-												 $url);
-	$contacts->add($contact);
+if($action == "addgroup" && $gid) {
+	$contactgroup = new ContactGroup($gid, $name);
+	$contacts->add($contactgroup);
 	$contacts->write();
-} elseif($name) {
-	// Add
-	$cid = $contacts->getNextCID();
+	$gid = 0;
+}
+
+elseif($action == "addcontact" && $gid && $cid) {
 	$contact = new Contact($cid,
 												 $name,
 												 $address,
+												 $city,
+												 $country,
 												 $phone,
 												 $phone2,
 												 $email,
-												 $url);
-	$contacts->add($contact);
+												 $email2,
+												 $url,
+												 $url2,
+												 $essential,
+												 $notes);
+	$contactgroup = $contacts->getContactGroup($gid);
+	$contactgroup->add($contact);
+	$contacts->write();
+	$cid = 0;
+}
+
+elseif($action =="editcontact" && $cid) {
+	$contact = $contacts->getContact($cid);
+	form($contact->cid,
+			 "?mode=addressbook&amp;action=updatecontact".$gid,
+			 "Update contact",
+			 $contact->name,
+			 $contact->address,
+			 $contact->city,
+			 $contact->country,
+			 $contact->phone,
+			 $contact->phone2,
+			 $contact->email,
+			 $contact->email2,
+			 $contact->url,
+			 $contact->url2,
+			 $contact->essential,
+			 $contact->notes);
+}
+
+elseif($action == "updatecontact" && $cid) {
+	$contact = $contacts->getContact($cid);
+
+	$contact->name = $name;
+	$contact->address = $address;
+	$contact->city = $city;
+	$contact->country = $country;
+	$contact->phone = $phone;
+	$contact->phone2 = $phone2;
+	$contact->email = $email;
+	$contact->email2 = $email2;
+	$contact->url = $url;
+	$contact->url2 = $url2;
+	$contact->essential = $essential;
+	$contact->notes = $notes;
+
 	$contacts->write();
+
+	$contact->show();
 }
 
-if($cid) {
+elseif($cid) {
 	$contact = $contacts->getContact($cid);
 	$contact->show();
-} else {
-	$contacts->show();
+}
+
+elseif($gid) {
+	$contactgroup = $contacts->getContactGroup($gid);
+	$contactgroup->show();
 
-	// TODO: Editorbox for adding new contact
+	form($contacts->getNextCID(),
+			 "?mode=addressbook&amp;action=addcontact&amp;gid=".$gid,
+			 "Add contact");
 
+} else {
+	$contacts->show();
+	if($current_user->uid == 0) {
+?>
+<form method="post" action="?mode=addressbook&amp;action=addgroup">
+  <input type="hidden" name="gid" value="<?php echo $contacts->getNextGID();?>">
+  Name: <input name="name" value="">
+  <button type="submit">Add group</button>
+</form>
+<?php
+  }
 }
 
 ?>
\ No newline at end of file
diff --git a/forum/utils/contacts.php b/forum/utils/contacts.php
index cec6bd7..bd59d16 100644
--- a/forum/utils/contacts.php
+++ b/forum/utils/contacts.php
@@ -1,73 +1,159 @@
 <?php
 
 include_once($UTIL_DIR . "/convert.php");
+include_once($UTIL_DIR . "/parser.php");
 
 class Contact {
 	public $cid;
 	public $name;
 	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=\"" . htmlspecialchars($this->cid, ENT_QUOTES, "UTF-8") . "\"\n");
-		fwrite($fp, "           name=\"" . htmlspecialchars($this->name, ENT_QUOTES, "UTF-8") . "\"\n");
-		fwrite($fp, "           address=\"" . htmlspecialchars($this->address, ENT_QUOTES, "UTF-8") . "\"\n");
-		fwrite($fp, "           phone=\"" . htmlspecialchars($this->phone, ENT_QUOTES, "UTF-8") . "\"\n");
-		fwrite($fp, "           phone2=\"" . htmlspecialchars($this->phone2, ENT_QUOTES, "UTF-8") . "\"\n");
-		fwrite($fp, "           email=\"" . htmlspecialchars($this->email, ENT_QUOTES, "UTF-8") . "\"\n");	
-		fwrite($fp, "           url=\"" . htmlspecialchars($this->url, ENT_QUOTES, "UTF-8") . "\">\n");
-		fwrite($fp, "  </contact>\n");
+		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, "             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/>";
+		//		echo "\tcid: " . $this->cid . "<br/>";
 		echo "\tname: " . $this->name . "<br/>";
 		echo "\taddress: " . $this->address . "<br/>";
+		echo "\tcity: " . $this->city . "<br/>";
+		echo "\tcountry: " . $this->country . "<br/>";
 		echo "\tphone: " . $this->phone . "<br/>";
-		if($this->phone2) echo "\tphone2: " . $this->phone2 . "<br/>";
+		echo "\tphone2: " . $this->phone2 . "<br/>";
 		echo "\temail: <a href=\"mailto:" . $this->email . "\">" . $this->email . "</a><br/>";
+		echo "\temail2: <a href=\"mailto:" . $this->email2 . "\">" . $this->email2 . "</a><br/>";
 		echo "\turl: <a href=\"" . $this->url . "\">" . $this->url . "</a><br/>";
+		echo "\turl2: <a href=\"" . $this->url2 . "\">" . $this->url2 . "</a><br/>";
+		echo "\tnotes: " . parse($this->notes) . "<br/>";
+		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>";
+		echo "<div class=\"contact\"><a href=\"?mode=addressbook&amp;cid=" . $this->cid . "\">" . $this->name . "</a></div>";
 	}
 
-	function Contact($cid,
-									 $name,
-									 $address,
-									 $phone,
-									 $phone2,
-									 $email,
-									 $url)
+	function Contact($cid, $name, $address, $city, $country, $phone, $phone2, $email, $email2, $url, $url2, $essential, $notes)
 	{
 		$this->cid = $cid;
 		$this->name = $name;
 		$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()
+	{
+		echo "  <div class=\"contactgroup\">\n";
+		echo "    <div class=\"title\"><a href=\"?mode=addressbook&amp;gid=" . $this->gid . "\">" . $this->name . "</a></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 $contacts = array();
+	private $contactgroups = array();
 
 	public function show()
 	{
 		echo "<div class=\"contactlist\">\n";
-		foreach($this->contacts as $contact) {
-			$contact->showshort();
+		foreach($this->contactgroups as $contactgroup) {
+			$contactgroup->showshort();
 		}
 		echo "</div>\n";
 	}
@@ -75,20 +161,38 @@ class Contacts {
 	public function getNextCID()
 	{
 		$nextcid = 1;
-		foreach($this->contacts as $contact) {
-			if($nextcid < $contact->cid) $nextcid = $contact->cid;
+		foreach($this->contactgroups as $contactgroup) {
+			$cid = $contactgroup->getMaxCID();
+			if($nextcid < $cid) $nextcid = $cid;
 		}
-		return $nextcid;
+		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)
 	{
-		return $this->contacts[$cid];
+		foreach($this->contactgroups as $contactgroup) {
+			if($contactgroup->getContact($cid)) return $contactgroup->getContact($cid);
+		}
+		return false;
 	}
 
-	public function add($contact) {
-		$key = $contact->cid;
-		$this->contacts[$key] = $contact;
+	public function getContactGroup($gid)
+	{
+		return $this->contactgroups[$gid];
+	}
+
+	public function add($contactgroup) {
+		$key = $contactgroup->gid;
+		$this->contactgroups[$key] = $contactgroup;
 	}
 	
 	public function write()
@@ -101,8 +205,8 @@ class Contacts {
 		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 
 		fwrite($fp, "<contacts>\n");
-		foreach($this->contacts as $contact) {
-			$contact->write($fp);
+		foreach($this->contactgroups as $contactgroup) {
+			$contactgroup->write($fp);
 		}
 		fwrite($fp, "</contacts>\n");
 
@@ -113,19 +217,32 @@ class Contacts {
 	{
 
 		$dom = new DomDocument;
-		$dom->preserveWhiteSpace = FALSE;
+		$dom->preserveWhiteSpace = TRUE;
 		$dom->load($this->file);
-		$params = $dom->getElementsByTagName('contact');
-
-		foreach ($params as $param) {
-			$contact = new Contact($param->getAttribute('cid'),
-														 $param->getAttribute('name'),
-														 $param->getAttribute('address'),
-														 $param->getAttribute('phone'),
-														 $param->getAttribute('phone2'),
-														 $param->getAttribute('email'),
-														 $param->getAttribute('url'));
-			$this->add($contact);
+		$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('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);
 		}
 	}
 
-- 
cgit v1.2.3