<?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.12.2.1 2002/09/15 01:18:42 pjw Exp $ */

	if(@isset($GLOBALS['phpgw_info']))
	{
		settype($GLOBALS['phpgw_info'],'array');
	}

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

	function display_line($label,$data,$link='',$valign='')
	{
		$GLOBALS['tr_color'] = $GLOBALS['phpgw']->nextmatchs->alternate_row_color($GLOBALS['tr_color']);
		echo '<tr>'."\n"
			. ' <td align="left" bgcolor="' . $GLOBALS['tr_color'] . '" width="50%">' . $label . '</td>'."\n"
			. ' <td align="right" width="50%" bgcolor="' . $GLOBALS['tr_color'] . '"'.$valign.'>';
			
		if($link)
		{
			echo '<a href="'.$link.'">';
		}
		
		echo $data;
		
		if($link)
		{
			echo '</a>';
		}
		
		echo '</td>'."\n".'</tr>'."\n";
	}

	if ($groupid && !$userid)
	{
		$account = CreateObject('phpgwapi.accounts',$groupid,'g');
		$groupdata = $account->read();
		$membership = $account->member($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   = $GLOBALS['phpgw']->link('/index.php','menuaction=addressbook.uiaddressbook.add');
			$contact_hidden = '<form submit=True&profile=True&entry='.$sfields;
			$contact_submit = 'submit';
			$contact_button = lang('Add');
		} else {
			$contact_link   = $GLOBALS['phpgw']->link('/index.php',
				Array(
					'menuaction'	=> 'addressbook.uiaddressbook.view',
					'ab_id'	=> $fields[0]['id']
				)
			);
			$contact_submit = 'view';
			$contact_button = lang('View');
			$contact_num    = '#'.$fields[0]['id'];
		}

		$firstname = $fields[0]['n_given'];
		if (!$firstname)
		{
			$firstname = $groupdata['firstname'];
		}

		$lastname  = $fields[0]['n_family'];
		if (!$lastname)
		{
			$lastname  = $groupdata['lastname'];
		}

		$profile_comments   = $GLOBALS['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>';

		if($GLOBALS['phpgw_info']['user']['apps']['admin'])
		{
			$link = $GLOBALS['phpgw']->link('/index.php',
				Array(
					'menuaction'	=> 'admin.uiaccounts.edit_group',
					'account_id'	=> $groupdata['account_id']
				)
			);
		}
		else
		{
			$link = '';
		}
		display_line('Group Account LID:',$groupdata['account_lid'],$link);

		$link = '<form method="POST" action="' . $contact_link . '">' . "\n"
			. '<input type="submit" name="' . $contact_submit . '" value="'.$contact_button.'">' . "\n"
			. '<input type="hidden" name="entry" value="'.$sfields.'">' . "\n"
			. '</form>' . "\n";
		display_line('Group Profile ID:&nbsp'.$contact_num,$link,'',' valign="center"');

		display_line('Firstname:',$firstname);
		display_line('Lastname:',$lastname);
		display_line('Comments:',$profile_comments);
//		display_line('&nbsp;','<img src="' . $GLOBALS['phpgw']->link('/hr/view_image.php','con='.$groupdata['account_id']).'" width="100" height="120">');

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

		for ($i=0;$i<count($membership); $i++) {
			$GLOBALS['tr_color'] = $GLOBALS['phpgw']->nextmatchs->alternate_row_color($GLOBALS['tr_color']);
			echo '<tr bgcolor="'.$GLOBALS['tr_color'].'"><td colspan="2"><a href="'
				. $GLOBALS['phpgw']->link('/hr/index.php','userid='.$membership[$i]['account_id']) . '">'
				. $membership[$i]['account_name'] . '</a></td></tr>';
		}
		echo '</table>';
	}
	elseif (!$groupid && $userid)
	{
		$account = CreateObject('phpgwapi.accounts',$userid,'u');
		$account_info = $account->read_repository();
		$membership = $account->membership($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]['firstname']  = $account_info['firstname'];
			$fields[0]['lastname'] = $account_info['lastname'];
			$sfields = rawurlencode(serialize($fields[0]));
			$contact_link   = $GLOBALS['phpgw']->link('/index.php','menuaction=addressbook.uiaddressbook.add');
			$contact_hidden = '<form submit=True&profile=True&entry='.$sfields;
			$contact_submit = 'submit';
//			$contact_submit = 'Add';
			$contact_button = lang('Add');
		} else {
			$contact_link   = $GLOBALS['phpgw']->link('/index.php',
				Array(
					'menuaction'	=> 'addressbook.uiaddressbook.view',
					'ab_id'		=> $fields[0]['id']
				)
			);
			$contact_submit = 'view';
			$contact_button = lang('View');
			$contact_num    = '#'.$fields[0]['id'];
		}

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

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

		$profile_comments   = $GLOBALS['phpgw']->strip_html(stripslashes($fields[0]['note']));
		if (!$profile_comments)
		{
			$profile_comments = '&nbsp;';
		}
		
		$profile_work_phone = $GLOBALS['phpgw']->strip_html($fields[0]['tel_work']);
		if (!$profile_work_phone)
		{
			$profile_work_phone = '&nbsp;';
		}

		$profile_home_phone = $GLOBALS['phpgw']->strip_html($fields[0]['tel_home']);
		if (!$profile_home_phone)
		{
			$profile_home_phone = '&nbsp;';
		}

		$profile_title      = $GLOBALS['phpgw']->strip_html($fields[0]['title']);
		if (!$profile_title)
		{
			$profile_title = '&nbsp;';
		}

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

		if($GLOBALS['phpgw_info']['user']['apps']['admin'])
		{
			$link = $GLOBALS['phpgw']->link('/index.php',
				Array(
					'menuaction'	=> 'admin.uiaccounts.edit_user',
					'account_id'	=> $account_info['account_id']
				)
			);
		}
		else
		{
			$link = '';
		}
		display_line('Account LID:',$account_info['account_lid'],$link);

		$link = '<form method="POST" action="' . $contact_link . '">' . "\n"
			. '<input type="submit" name="' . $contact_submit . '" value="'.$contact_button.'">' . "\n"
			. '<input type="hidden" name="entry" value="'.$sfields.'">' . "\n"
			. '</form>' . "\n";
		display_line('Profile ID:&nbsp'.$contact_num,$link,'',' valign="center"');

		display_line('Firstname:',$firstname);
		display_line('Lastname:',$lastname);
		display_line('Title:',$profile_title);
		display_line('Work Phone:',$profile_work_phone);
		display_line('Home Phone:',$profile_home_phone);
		display_line('Work Email:',$profile_work_email);
		display_line('Home Email:',$profile_home_email);
		display_line('Comments:',$profile_comments);
		display_line('&nbsp;','<img src="' . $GLOBALS['phpgw']->link('/hr/view_image.php','con='.$account_info['account_id']).'" width="100" height="120">');

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

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