<?php
  /**************************************************************************\
  * phpGroupWare - Human Resources                                           *
  * http://www.phpgroupware.org                                              *
  * Written by Joseph Engo <jengo@phpgroupware.org>                          *
  * --------------------------------------------                             *
  *  This program is free software; you can redistribute it and/or modify it *
  *  under the terms of the GNU General Public License as published by the   *
  *  Free Software Foundation; either version 2 of the License, or (at your  *
  *  option) any later version.                                              *
  \**************************************************************************/

  /* $Id: index.php,v 1.10 2001/03/27 09:54:00 milosch Exp $ */

	$phpgw_info["flags"]["currentapp"] = "hr";
	$phpgw_info["flags"]["enable_nextmatchs_class"] = True;
	include("../header.inc.php");
?>
<p>
<table border="0" width="100%">
 <tr>
  <td align="left" width="50%" valign="top">
   <?php
	echo '<table border="0" width="80%">';
	echo '<tr><td bgcolor="' . $phpgw_info["theme"]["th_bg"] . '" align="center">Users</td>'
		. '</tr>';
	
	$account_info = $phpgw->accounts->get_list('accounts');
	while (list($null,$account) = each($account_info)) {
		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td bgcolor="' . $tr_color . '"><a href="'
			. $phpgw->link("/hr/index.php","userid=" . $account["account_id"]) . '">&nbsp;'
			. $account["account_firstname"] . ' ' . $account["account_lastname"]
			. '</a></td></tr>';
	}
	echo "</table>";
	echo '<p><table border="0" width="80%">';
	echo '<tr><td bgcolor="' . $phpgw_info["theme"]["th_bg"] . '" align="center">Groups</td>'
		. '</tr>';

	$group_info = $phpgw->accounts->get_list('groups');
	while (list($null,$group) = each($group_info)) {
		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td bgcolor="' . $tr_color . '">&nbsp;<a href="'
			. $phpgw->link("/hr/index.php","groupid=" . $group["account_id"]) . '">'
			. $group["account_firstname"] . '</a></td></tr>';
	}
	echo "</table>";
   ?>
  </td>
  <td align="right" width="50%" valign="top">
   <?php
	if ($groupid && !$userid)
	{
		$account = CreateObject('phpgwapi.accounts',$groupid);
		$groupdata = $account->read();
		$membership = $account->memberships($groupid);
		$contacts = CreateObject('phpgwapi.contacts');

		$qcols = array(
			'n_given'  => 'n_given',
			'n_family' => 'n_family',
			'title'    => 'title',
			'note'     => 'note'
		);

		$fields = $contacts->read(0,0,$qcols,'',"tid=p,lid=".$groupdata["account_lid"]);
		echo $groupdata["account_id"];

		if (!$fields[0]["id"]) {
			$fields[0]["tid"]      = "p";
			$fields[0]["lid"]      = $groupdata["account_lid"];
			$fields[0]["n_given"]  = $groupdata["firstname"];
			$fields[0]["n_family"] = $groupdata["lastname"];
			$sfields = rawurlencode(serialize($fields[0]));
			$contact_link   = $phpgw->link("/addressbook/add.php");
			$contact_hidden = "<form submit=True&profile=True&fields=".$sfields;
			$contact_submit = "submit";
			$contact_button = lang('Add');
		} else {
			$contact_link   = $phpgw->link("/addressbook/view.php","ab_id=".$fields[0]["id"]);
			$contact_submit = "view";
			$contact_button = lang('View');
			$contact_num    = "#".$fields[0]["id"];
		}

		$firstname = $fields[0]["n_given"];
		$lastname  = $fields[0]["n_family"];
		if (!$firstname) $firstname = $groupdata["firstname"];
		if (!$lastname)  $lastname  = $groupdata["lastname"];

		$profile_comments   = $phpgw->strip_html(stripslashes($fields[0]["note"]));
		if (!$profile_comments) $profile_comments = "&nbsp;";

		echo '<table border="0" width="80%">';
		echo '<tr><td colspan="2" bgcolor="' . $phpgw_info["theme"]["th_bg"]
			. '">&nbsp;</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Group Account LID:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. '<a href="' . $phpgw->link("/admin/editgroup.php","account_id=".$groupdata["account_id"]) .'"</a>'
			. $groupdata["account_lid"] . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Group Profile ID:&nbsp'.$contact_num . '</td>'
			. '<td valign="center" align="right" width="50%" bgcolor="' . $tr_color . '">'
			. '<form method="POST" action="' . $contact_link . '">' . "\n"
			. '<input type="submit" name="' . $contact_submit . '" value="'.$contact_button.'">' . "\n"
			. '<input type="hidden" name="fields" value="'.$sfields.'">' . "\n"
			. '</form>' . "\n"
			. '</td></tr>' . "\n";

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Firstname:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $firstname. '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Lastname:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $lastname . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Comments:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_comments . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">&nbsp;</td>'
			. '<td width="50%" align="right" bgcolor="' . $tr_color . '">'
			. '<img src="' . $phpgw->link("/hr/view_image.php","con=" . $groupdata["account_id"])
			. '" width="100" height="120"></td></tr>';

		echo '<tr><td colspan="2">&nbsp;</td></tr>';
		echo '<tr><td colspan="2" align="left" bgcolor="' . $phpgw_info["theme"]["th_bg"]
			. '">Member of the following groups</td></tr>';

		for ($i=0;$i<count($membership); $i++) {
			$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
			echo "<tr bgcolor=\"$tr_color\"><td colspan=\"2\"><a href=\""
				. $phpgw->link("/hr/index.php","groupid=" . $membership[$i]['account_id']) . "\">"
				. $membership[$i]['account_name'] . "</a></td></tr>";
		}
		echo "</table>";
	}
	elseif (!$groupid && $userid)
	{
		$account = CreateObject('phpgwapi.accounts',$userid);
		$account_info = $account->read_repository();
		$membership = $account->memberships($userid);
		$contacts = CreateObject('phpgwapi.contacts');

		$qcols = array(
			'n_given'    => 'n_given',
			'n_family'   => 'n_family',
			'tel_work'   => 'tel_work',
			'tel_home'   => 'tel_home',
			'tel_cell'   => 'tel_cell',
			'title'      => 'title',
			'email'      => 'email',
			'email_home' => 'email_home',
			'note'       => 'note'
		);

		$fields = $contacts->read(0,0,$qcols,'',"tid=p,lid=".$account_info["account_lid"]);

		if(!$fields[0]["id"]) {
			$fields[0]["tid"]      = "p";
			$fields[0]["lid"]      = $account_info["account_lid"];
			$fields[0]["n_given"]  = $account_info["firstname"];
			$fields[0]["n_family"] = $account_info["lastname"];
			$sfields = rawurlencode(serialize($fields[0]));
			$contact_link   = $phpgw->link("/addressbook/add.php");
			$contact_hidden = "<form submit=True&profile=True&fields=".$sfields;
			$contact_submit = "submit";
			$contact_button = lang('Add');
		} else {
			$contact_link   = $phpgw->link("/addressbook/view.php","ab_id=".$fields[0]["id"]);
			$contact_submit = "view";
			$contact_button = lang('View');
			$contact_num    = "#".$fields[0]["id"];
		}

		$firstname = $fields[0]["n_given"];
		$lastname  = $fields[0]["n_family"];
		if (!$firstname) $firstname = $account_info["firstname"];
		if (!$lastname)  $lastname  = $account_info["lastname"];

		$profile_comments   = $phpgw->strip_html(stripslashes($fields[0]["note"]));
		$profile_work_phone = $phpgw->strip_html($fields[0]["tel_work"]);
		$profile_home_phone = $phpgw->strip_html($fields[0]["tel_home"]);
		$profile_title      = $phpgw->strip_html($fields[0]["title"]);

		if ($phpgw_info["user"]["apps"]["email"]) {
			if ($fields[0]["email"]) {
				$profile_work_email = '<a href="'
					. $phpgw->link("/email/compose.php","to="
					.urlencode($fields[0]["email"])).'" target="_new">';
			}
			if ($fields[0]["email_home"]) {
				$profile_work_email = '<a href="'
					. $phpgw->link("/email/compose.php","to="
					. urlencode($fields[0]["email_home"])).'" target="_new">';
			}
		} else {
			if ($fields[0]["email"]) {
				$profile_work_email='<a href="mailto:'. $fields[0]["email"] .'">';
			}
			if ($fields[0]["email_home"]) {
				$profile_home_email='<a href="mailto:'. $fields[0]["email_home"] .'">';
			}
		}

		if (!$profile_comments) $profile_comments = "&nbsp;";
		if (!$profile_phone_number) $profile_phone_number = "&nbsp;";
		if (!$profile_title) $profile_title = "&nbsp;";

		echo '<table border="0" width="80%">';
		echo '<tr><td colspan="2" bgcolor="' . $phpgw_info["theme"]["th_bg"]
			. '">&nbsp;</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Account LID:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. '<a href="' . $phpgw->link("/admin/editaccount.php","account_id=".$account_info["account_id"]) .'"</a>'
			. $account_info["account_lid"] . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Profile ID:&nbsp'.$contact_num . '</td>'
			. '<td valign="center" align="right" width="50%" bgcolor="' . $tr_color . '">'
			. '<form method="POST" action="' . $contact_link . '">' . "\n"
			. '<input type="submit" name="' . $contact_submit . '" value="'.$contact_button.'">' . "\n"
			. '<input type="hidden" name="fields" value="'.$sfields.'">' . "\n"
			. '</form>' . "\n"
			. '</td></tr>' . "\n";

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Firstname:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $firstname. '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Lastname:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $lastname . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Title:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_title . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Work number:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_work_phone . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Home number:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_home_phone . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Work email:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_work_email. '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Home email:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_home_email . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">Comments:</td>'
			. '<td align="right" width="50%" bgcolor="' . $tr_color . '">'
			. $profile_comments . '</td></tr>';

		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo '<tr><td align="left" bgcolor="' . $tr_color . '" width="50%">&nbsp;</td>'
			. '<td width="50%" align="right" bgcolor="' . $tr_color . '">'
			. '<img src="' . $phpgw->link("/hr/view_image.php","con=" . $account_info["account_id"])
			. '" width="100" height="120"></td></tr>';

		echo '<tr><td colspan="2">&nbsp;</td></tr>';
		echo '<tr><td colspan="2" align="left" bgcolor="' . $phpgw_info["theme"]["th_bg"]
			. '">Member of the following groups</td></tr>';

		for ($i=0;$i<count($membership); $i++) {
			$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
			echo "<tr bgcolor=\"$tr_color\"><td colspan=\"2\"><a href=\""
				. $phpgw->link("/hr/index.php","groupid=" . $membership[$i]['account_id']) . "\">"
				. $membership[$i]['account_name'] . "</a></td></tr>";
		}
		echo "</table>";
	}
?>
  </td>
 </tr>
</table>
<?php
	$phpgw->common->phpgw_footer();
?>
