<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004-2006 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
/*
 * Created on Oct 4, 2005
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once('XTemplate/xtpl.php');
require_once("data/Tracker.php");
require_once('themes/'.$theme.'/layout_utils.php');

require_once('modules/Documents/Document.php');
require_once('modules/DocumentRevisions/DocumentRevision.php');
require_once('modules/Contacts/Contact.php');

global $app_strings;
global $app_list_strings;
global $mod_strings;
global $current_user;

$xtpl = new XTemplate('modules/MailMerge/Step4.html');
$xtpl->assign("MOD", $mod_strings);
$xtpl->assign("APP", $app_strings);

if(!empty($_POST['document_id']))
{
	$_SESSION['MAILMERGE_DOCUMENT_ID'] = $_POST['document_id'];
}
$document_id = $_SESSION['MAILMERGE_DOCUMENT_ID'];
$revision = new DocumentRevision();
$revision->retrieve($document_id);
//$document = new Document();
//$document->retrieve($document_id);

if(!empty($_POST['selected_objects']))
{
	$selObjs = urldecode($_POST['selected_objects']);
	$_SESSION['SELECTED_OBJECTS_DEF'] = $selObjs;
}
$selObjs = $_SESSION['SELECTED_OBJECTS_DEF'];
$sel_obj = array();
parse_str($selObjs,$sel_obj);

$relArray = array();
//build relationship array
foreach($sel_obj as $key=>$value)
{
	$id = 'rel_id_'.str_replace('-', '', $key);	
	if(isset($_POST[$id]) && !empty($_POST[$id]))
	{
		$relArray[$key] = $_POST[$id];	
	}
}


$builtArray = array();
if(count($relArray) > 0)
{
$_SESSION['MAILMERGE_RELATED_CONTACTS'] = $relArray;

$relModule = $_SESSION['MAILMERGE_CONTAINS_CONTACT_INFO'];
global $beanList, $beanFiles;
$class_name = $beanList[$relModule ];
require_once($beanFiles[$class_name]);

	$seed = new $class_name();
	foreach($sel_obj as $key=>$value)
	{	
		$builtArray[$key] = $value;
		if(isset($relArray[$key]))
		{
			$seed->retrieve($relArray[$key]);
			$name = "";
			if($relModule  == "Contacts"){
				$name = $seed->first_name." ".$seed->last_name;
			}
			else{
				$name = $seed->name;
			}
				$builtArray[$key] = str_replace('##', '&', $value)." (".$name.")";
		}
	}

}
else
{
	$builtArray = $sel_obj;	
}

$xtpl->assign("MAILMERGE_MODULE", $_SESSION['MAILMERGE_MODULE']);
$xtpl->assign("MAILMERGE_DOCUMENT_ID", $document_id);
$xtpl->assign("MAILMERGE_TEMPLATE", $revision->filename." (rev. ".$revision->revision.")");
$xtpl->assign("MAILMERGE_SELECTED_OBJECTS", get_select_options_with_id($builtArray,'0'));
$xtpl->assign("MAILMERGE_SELECTED_OBJECTS_DEF", urlencode($selObjs));
$step_num = 4;

if(isset($_SESSION['MAILMERGE_SKIP_REL']) && $_SESSION['MAILMERGE_SKIP_REL'] || !isset($_SESSION['MAILMERGE_RELATED_CONTACTS']) || empty($_SESSION['MAILMERGE_RELATED_CONTACTS']))
{
	$xtpl->assign("PREV_STEP", "2");
	$step_num = 3;
}
else
{
	$xtpl->assign("PREV_STEP", "3");
}
	
$xtpl->assign("STEP_NUM", "Step ".$step_num.":");

$xtpl->parse("main");
$xtpl->out("main");

?>
