From 4bc99c18ecbbb7bb7d0fac4592fcc63e3c568798 Mon Sep 17 00:00:00 2001 From: deva Date: Sun, 26 Oct 2008 12:29:38 +0000 Subject: We now have a functional but very ugly addressbook. --- forum/utils/contacts.php | 201 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 159 insertions(+), 42 deletions(-) (limited to 'forum/utils/contacts.php') 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 @@ 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, " \n"); + fwrite($fp, " 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, "\n"); } public function show() { echo "

\n"; - echo "\tcid: " . $this->cid . "
"; + // echo "\tcid: " . $this->cid . "
"; echo "\tname: " . $this->name . "
"; echo "\taddress: " . $this->address . "
"; + echo "\tcity: " . $this->city . "
"; + echo "\tcountry: " . $this->country . "
"; echo "\tphone: " . $this->phone . "
"; - if($this->phone2) echo "\tphone2: " . $this->phone2 . "
"; + echo "\tphone2: " . $this->phone2 . "
"; echo "\temail: email . "\">" . $this->email . "
"; + echo "\temail2: email2 . "\">" . $this->email2 . "
"; echo "\turl: url . "\">" . $this->url . "
"; + echo "\turl2: url2 . "\">" . $this->url2 . "
"; + echo "\tnotes: " . parse($this->notes) . "
"; + echo "\tcid . "\">Edit\n"; echo "

\n"; } public function showshort() { - echo "
cid . "\">" . $this->name . "
"; + echo "
cid . "\">" . $this->name . "
"; } - 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, " 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, " \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 "
\n"; + echo "
" . $this->name . "
\n"; + echo "
\n"; + foreach($this->contacts as $contact) { + $contact->showshort(); + } + echo "
\n"; + echo "
\n"; + } + + public function showshort() + { + echo "
\n"; + echo " \n"; + echo "
\n"; + foreach($this->contacts as $contact) { + if($contact->essential) $contact->showshort(); + } + echo "
\n"; + echo "
\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 "
\n"; - foreach($this->contacts as $contact) { - $contact->showshort(); + foreach($this->contactgroups as $contactgroup) { + $contactgroup->showshort(); } echo "
\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, "\n"); fwrite($fp, "\n"); - foreach($this->contacts as $contact) { - $contact->write($fp); + foreach($this->contactgroups as $contactgroup) { + $contactgroup->write($fp); } fwrite($fp, "\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