<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings, $current_user, $app_list_strings;

require_once('modules/Tasks/Task.php');
require_once('modules/Tasks/Forms.php');
require_once('include/json_config.php');

$json_config = new json_config();  
$json = getJSONobj();

require_once('include/MVC/View/SugarView.php');  
require_once('modules/Tasks/views/EditView/view.edit.tasks.php');

$focus = new Task(); 

echo $focus->id;
if(isset($_REQUEST['record'])) {	
	$focus->retrieve($_REQUEST['record']);
}
$edit = new ViewEditTasks();
$edit->ss = new Sugar_Smarty();
$edit->module = 'Tasks';
$edit->bean = $focus;
//$edit->tplFile = 'include/ECM/EcmViews/EditView/Tabs/EditView.tpl';
$edit->tplFile = 'include/EditView/EditView.tpl';

$focus = new Task(); 

//custom subpanel create button
if ((isset($_REQUEST['first_parent_id'])) && (isset($_REQUEST['first_parent_type']))) {

	switch ($_REQUEST['first_parent_type']) {
		case 'Accounts': {};
		case 'EcmProducts': $fields=' name ';
							break;
		case 'Contacts': $fields = ' first_name, last_name ';
							break;
		case 'EcmQuotes': $fields = ' document_no, name ';
							break;
	}
	
	$q="SELECT ".$fields." FROM ".strtolower($_REQUEST['first_parent_type'])." WHERE id='".$_REQUEST['first_parent_id']."'";
	
	$res = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query($q));	
	
	switch ($_REQUEST['first_parent_type']) {
		case 'Accounts': {};
		case 'EcmProducts': $name=$res['name'];
							break;
		case 'Contacts': $name=$res['first_name'].' '.$res['last_name'];
							break;
		case 'EcmQuotes': if ($res['name']!='') 
							$name = $res['name']; else $name=$res['document_no'];
							break;
	}

	$focus->first_parent_type=$_REQUEST['first_parent_type'];
	$focus->first_parent_name=$name;
	$focus->first_parent_id=$_REQUEST['first_parent_id'];
		
	$edit->ss->assign("FIRST_PARENT_ID", $focus->first_parent_id);
	$edit->ss->assign("FIRST_PARENT_NAME", $focus->first_parent_name);	
}
//end custom subpanel create
if(isset($_REQUEST['record'])) {
	
	$focus->retrieve($_REQUEST['record']);
	
	$edit->ss->assign("FIRST_PARENT_ID", $focus->first_parent_id);
	$edit->ss->assign("FIRST_PARENT_NAME", $focus->first_parent_name);	
	$edit->ss->assign("SECOND_PARENT_ID", $focus->second_parent_id);
	$edit->ss->assign("SECOND_PARENT_NAME", $focus->second_parent_name);
	$edit->ss->assign("THIRD_PARENT_ID", $focus->third_parent_id);
	$edit->ss->assign("THIRD_PARENT_NAME", $focus->third_parent_name);
	
	echo '<script language="javascript">
			var NEW_TASK = 0;
		  </script>';

} else {
   echo '<script language="javascript">
			var NEW_TASK = 1;
		</script>';
}
//fields for first, second and thirs parent
//add mz 2012-05-14
$pto = ''; //second parent type options
foreach ($app_list_strings['parent_type_display'] as $k=>$v){
	$pto.='<option value="'.$k.'"';
	if ($k==$focus->first_parent_type) $pto.=' selected';
	$pto.='>'.$v.'</option>';
	}
$edit->ss->assign("FIRST_PARENT_TYPE_OPTIONS",$pto);

$pto = ''; //second parent type options
foreach ($app_list_strings['parent_type_display'] as $k=>$v){
	$pto.='<option value="'.$k.'"';
	if ($k==$focus->second_parent_type) $pto.=' selected';
	$pto.='>'.$v.'</option>';
	}
$edit->ss->assign("SECOND_PARENT_TYPE_OPTIONS",$pto);

$pto = ''; //third parent type options
foreach ($app_list_strings['parent_type_display'] as $k=>$v){
	$pto.='<option value="'.$k.'"';
	if ($k==$focus->third_parent_type) $pto.=' selected';
	$pto.='>'.$v.'</option>';
	}
$edit->ss->assign("THIRD_PARENT_TYPE_OPTIONS",$pto);

$edit->preDisplay();        


echo $edit->display();
//echo '<div id="subpanels">';
//require_once('subpanels.php');
//echo '</div>';
?>