<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/**
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 */




require_once('include/Dashlets/DashletGeneric.php');
require_once('modules/EmailCaches/EmailCache.php');

class MyEcmEmailsDashlet extends DashletGeneric { 

	var $ie_id = '';
	
    function MyEcmEmailsDashlet($id, $def = null) {
        global $current_user, $app_strings;
		require('modules/EcmEmails/Dashlets/MyEcmEmailsDashlet/MyEcmEmailsDashlet.data.php');
		
        parent::DashletGeneric($id, $def);
        
        if(empty($def['title'])) $this->title = translate('LBL_DASHLET_MY_EMAILS', 'Emails');
		if(isset($def['ie_id'])) $this->ie_id = $def['ie_id']; else $this->ie_id = array_keys($this->getInbounds());

        $this->searchFields = $dashletData['MyEcmEmailsDashlet']['searchFields'];
        $this->columns = $dashletData['MyEcmEmailsDashlet']['columns'];
   
        $this->configureTpl = 'modules/EcmEmails/Dashlets/MyEcmEmailsDashlet/MyEcmEmailsDashletGenericConfigure.tpl';
		$this->seedBean = new EmailCache();   
		$this->myItemsOnly = true;
		$this->checkEmails();     
    }    
	
    function getTitle($text) {
		
        global $image_path, $app_strings, $sugar_config;
    	
		$this->checkEmails();
				
        if($this->isConfigurable) 
            $additionalTitle = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text 
                               . '</td><td nowrap width="1%"><div style="width: 100%;text-align:right"><a href="#" onclick="SUGAR.mySugar.configureDashlet(\'' 
                               . $this->id . '\'); return false;" class="chartToolsLink">'    
                               . get_image($image_path.'edit','title="' . translate('LBL_DASHLET_EDIT', 'Home') . '" alt="' . translate('LBL_DASHLET_EDIT', 'Home') . '"  border="0"  align="absmiddle"').'</a> ' 
                               . '';
        else 
            $additionalTitle = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="99%">' . $text 
                   . '</td><td nowrap width="1%"><div style="width: 100%;text-align:right">';
        
        if($this->isRefreshable)
            $additionalTitle .= '<a href="#" onclick="SUGAR.mySugar.retrieveDashlet(\'' 
                                . $this->id . '\'); return false;"><img width="13" height="13" border="0" align="absmiddle" title="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" alt="' . translate('LBL_DASHLET_REFRESH', 'Home') . '" src="' 
                                . $image_path . 'refresh.gif"/></a> ';
        $additionalTitle .= '<a href="#" onclick="SUGAR.mySugar.deleteDashlet(\'' 
                            . $this->id . '\'); return false;"><img width="13" height="13" border="0" align="absmiddle" title="' . translate('LBL_DASHLET_DELETE', 'Home') . '" alt="' . translate('LBL_DASHLET_DELETE', 'Home') . '" src="' 
                            . $image_path . 'close_dashboard.gif"/></a></div></td></tr></table>';
            
        if(!function_exists('get_form_header')) {
            global $theme;
            require_once('themes/'.$theme.'/layout_utils.php');
        }
        
        $str = '<div ';
        if(empty($sugar_config['lock_homepage']) || $sugar_config['lock_homepage'] == false) $str .= ' onmouseover="this.style.cursor = \'move\';"';
        $str .= 'id="dashlet_header_' . $this->id . '">' . get_form_header($this->title, $additionalTitle, false) . '</div>';
        
		
		global $current_language;
		$str .= "<script type=\"text/javascript\" src=\"cache/jsLanguage/Emails/".$current_language.".js\"></script><script type=\"text/javascript\" src=\"modules/Emails/javascript/Email.js\"></script>";
				
        return $str;
    }
	
	function checkEmails() {
		if(isset($this->ie_id) && is_array($this->ie_id)) {
			foreach($this->ie_id as $key => $ie_id) $this->checkEmail($ie_id);
		}
	}
	function checkEmail($ie_id) {
	/*
		$post_tmp = $_POST;
		$get_tmp = $_GET;
		$request_tmp = $_REQUEST;
		
		ob_start();
		$arr_tmp = array();
		$arr_tmp['module'] = "Emails";
		$arr_tmp['action'] = "EmailUIAjax";
		$arr_tmp['emailUIAction'] = "checkEmailProgress";
		$arr_tmp['currentCount'] = "0";
		$arr_tmp['ieId'] = $ie_id;
		$arr_tmp['sugar_body_only'] = "true";
		$arr_tmp['to_pdf'] = "true";
		$_POST = $arr_tmp;
		$_REQUEST = $arr_tmp;
		 global $current_user;
		
		include('index.php');
		
		$out2 = ob_get_contents();
		ob_end_clean();
		$_POST = $post_tmp;
		$_GET = $get_tmp;
		$_REQUEST = $request_tmp;
	*/
	}

	function buildWhere() {
		$where = array();
		$where[] = "`ie_id` IN ('".implode("', '", $this->ie_id)."')";
		$where[] = "`mbox` != 'INBOX.Trash'";
		return $where;
	}

	function getInbounds() {
		global $current_user;
		$GLOBALS['db'] = new MysqlManager();
		$GLOBALS['db']->connect();
		$query = "SELECT `id`, `name` FROM `inbound_email` WHERE `deleted`=0  AND `group_id` = '$current_user->id' ORDER BY `name`";
		$results = $GLOBALS['db']->query($query);
		$arr = array();
		if(is_resource($results))
			while($row = $GLOBALS['db']->fetchByAssoc($results)) $arr[$row['id']] = $row['name'];
		return $arr;
	}

    function displayOptions() {
        $this->processDisplayOptions();

		global $app_list_strings;
	
		
		$arr = $this->getInbounds();
		
		$this->currentSearchFields['ie_id']['input'] = '<select id="ie_id" name="ie_id[]"  multiple="multiple" size="4" style="width:150px;">'.get_select_options_with_id($arr,(empty($this->ie_id) ? '' : $this->ie_id)).'</select>';
		
		$this->configureSS->assign('searchFields', $this->currentSearchFields);
	 	return $this->configureSS->fetch($this->configureTpl);
		
	}

    function saveOptions($req) {
		$options = parent::saveOptions($req);
		$options['ie_id'] = !is_array($req['ie_id']) ? array('') : $req['ie_id'];
		return $options;
	}

}

?>
