<?php
include_once($UTIL_DIR . "/contacts.php");

function form($cid,
							$posturl,
							$buttontext,
							$name = "",
							$co = "",
							$address = "",
							$city = "",
							$country = "",
							$phone = "",
							$phone2 = "",
							$email = "",
							$email2 = "",
							$url = "",
							$url2 = "",
							$essential = "on",
							$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/>
  c/o: <input name="co" value="<?php echo $co;?>"><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";?>> (show on frontpage)<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($action == "addgroup" && $gid) {
	$contactgroup = new ContactGroup($gid, $name);
	$contacts->add($contactgroup);
	$contacts->write();
	$gid = 0;
}

elseif($action == "addcontact" && $gid && $cid) {
	$contact = new Contact($cid,
												 $name,
												 $co,
												 $address,
												 $city,
												 $country,
												 $phone,
												 $phone2,
												 $email,
												 $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->co,
			 $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->co = $co;
	$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();
}

elseif($cid) {
	$contact = $contacts->getContact($cid);
	$contact->show();
}

elseif($gid) {
	$contactgroup = $contacts->getContactGroup($gid);
	$contactgroup->show();

	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
  }
}

?>