<?php
  /**************************************************************************\
  * phpGroupWare - E-Mail                                                    *
  * http://www.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: hook_home.inc.php 18278 2007-09-22 13:40:21Z skwashd $ */

	$tmp_app_inc = $GLOBALS['phpgw']->common->get_inc_dir('felamimail');

	if ($GLOBALS['phpgw_info']['user']['preferences']['felamimail']['mainscreen_showmail'] == True)
	{
		$title = '<font color="#FFFFFF">'.lang('felamimail').'</font>';
	
		$portalbox = CreateObject('phpgwapi.listbox',
			array
			(
				'title'						=> $title,
				'primary'					=> $GLOBALS['phpgw_info']['theme']['navbar_bg'],
				'secondary'					=> $GLOBALS['phpgw_info']['theme']['navbar_bg'],
				'tertiary'					=> $GLOBALS['phpgw_info']['theme']['navbar_bg'],
				'width'						=> '100%',
				'outerborderwidth'			=> '0',
				'header_background_image'	=> $GLOBALS['phpgw']->common->image('phpgwapi/templates/phpgw_website','bg_filler.gif')
			)
		);

		$GLOBALS['phpgw']->translation->add_app('felamimail');
		$app_id = $GLOBALS['phpgw']->applications->name2id('felamimail');
		$GLOBALS['portal_order'][] = $app_id;
		$button = array
		(
			'up'		=> array('url'	=> '/set_box.php', 'app'	=> $app_id),
			'down'		=> array('url'	=> '/set_box.php', 'app'	=> $app_id),
			'close'		=> array('url'	=> '/set_box.php', 'app'	=> $app_id),
			'question'	=> array('url'	=> '/set_box.php', 'app'	=> $app_id),
			'edit'		=> array('url'	=> '/set_box.php', 'app'	=> $app_id)
		);

		foreach ( $button as $key => $value)
		{
			$portalbox->set_controls($key, $value);
		}

		$bofelamimail = createObject('felamimail.bofelamimail');
		$connected = $bofelamimail->openConnection();
		if ( $connected )
		{
			$headers = $bofelamimail->getHeaders(1, $GLOBALS['phpgw_info']["user"]["preferences"]["common"]["maxmatchs"], 0);
			$bofelamimail->closeConnection();
		}
		else
		{
			$err = lang('ERROR');
			$str = lang('Can not open connection to mail server');
			$extra_data = "<p><strong>$err</strong>$str</p>";
			echo "\r\n<!-- start felamimail error -->\r\n"
				. $portalbox->draw($extra_data)
				. "<!-- ends felamimail error -->\r\n";
			return;
		}

		if ( !is_array($headers['header']) || !count($headers['header']) )
		{
			$str = lang('You have no messages in your INBOX');
			$extra_data = "<p>$str</p>";
			echo "\r\n<!-- start felamimail error -->\r\n"
				. $portalbox->draw($extra_data)
				. "<!-- ends felamimail error -->\r\n";
			return;
		}

		$portalbox->data = array();
		foreach($headers['header'] as $msg)
		{
			if (strlen($msg['subject']) > 65)
			{
				$msg['subject'] = substr($msg['subject'], 0, 65).' ...';
			}
			$link_data = array
			(
				'menuaction'	=> 'felamimail.uidisplay.display',
				'uid'			=> (int) $msg['uid']
			);
			$portalbox->data[] = array
			(
				'text'	=> htmlspecialchars($msg['subject']),
				'link'	=> $GLOBALS['phpgw']->link('/index.php', $link_data)
			);
		}

		echo "\r\n<!-- start felamimail -->\r\n"
			. $portalbox->draw('')
			. "<!-- end felamimail -->\r\n";
	}
