<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'emails.php');
include(DIR_WS_CLASSES.'email_accounts.php');
require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC . 'xtc_php_mail.inc.php');

$email_accounts = new email_accounts();
$emails = new emails;

$id = xtc_db_input($_GET['id']);
$customers_id = xtc_db_input($_GET['customers_id']);

$smarty = new yesSmarty;
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';

if($_GET['close']){
	$smarty->assign('CLOSE',1);
	$smarty->display(DIR_FS_ADMIN.'templates/mailmaster.html');
}
if($_GET['tab']){
	include(DIR_WS_CLASSES.'class.adodb-pager.php');
	// WE NEED LANGUAGE-FILE FOR EMAILS
	include(DIR_FS_LANGUAGES.$_SESSION['language'].'/admin/emails.php');
	$pager = new ADODB_Pager($conn,sprintf("SELECT emails_id,concat(from_email,' ',from_name) as from_string,subject,date_saved FROM %s",$emails->get_tablename() ));
	$pager->whereConditions = array(
	 	sprintf("customers_id='%s'",$customers_id)
	);
	$pager->orderbyDefault = array(
		'date_saved'
	);
	$pager->index_field = $emails->get_index();

	// KEINE AJAX BOX
	$pager->pager_tab_file = false;
	
	$pager->get_row_selector = 'eID';
	$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_FROM,TABLE_HEADING_SUBJECT,TABLE_HEADING_SAVED);
	$selected_item = $pager->selected_row;
	$pager->Render($rows_per_page=MAX_DISPLAY_SEARCH_RESULTS_ADMIN);
	
	$grid_smarty = $smarty;
	$smarty->assign('HEADER',$pager->getHeader());
	$smarty->assign('SORTING',$pager->getSortingInput());
	
	$grid_smarty->assign('grid',$pager->getGrid());
	$grid = $grid_smarty->fetch(DIR_FS_ADMIN.'templates/pager_grid.tpl');
	
	$smarty->assign('GRID',$grid);
	$smarty->assign('FOOTER',$pager->getFooter());
	echo $smarty->fetch(DIR_FS_ADMIN.'templates/pager.tpl');
	exit;
}



$body_newline = "<br /><br />-------------------------------------------------------<br /><br />";
if($id){
	$email = $emails->get_email($id);
	$subject = 'RE: '.$email->data->subject;
	$body = $body_newline.$email->data->body;
}else{
	if($customers_id){
		$email = $emails->get_empty_email_object_from_customer($customers_id);
		$subject = $email->data->subject;
		$body = $email->data->body;
	}
}

$action = ($_GET['action']) ? $_GET['action'] : 'write';
$heading_title = constant('HEADING_'.strtoupper($action.'_EMAIL'));

include(DIR_WS_CLASSES.'class.dbform.php');
$dbf = new dbform('emails','emails_id',$conn);

$dbf->fields['from_email']->set_title(TITLE_SENDER,5);

$dbf->add_field( 'recipient','varchar(128)',$extern_table = false );
$dbf->fields['recipient']->set_title(TITLE_RECIPIENT,10);
$dbf->fields['recipient']->set_required($min_length=3,$validate_func = 'valid_email_address');
$dbf->fields['recipient']->set_default_value($email->data->from_email);
$dbf->fields['recipient']->set_style('width:95%');

$dbf->fields['subject']->set_title(TITLE_SUBJECT,15);
$dbf->fields['subject']->set_required($min_length=3,$validate_func = '');
$dbf->fields['subject']->set_style('width:95%');

$dbf->fields['body']->set_title(TITLE_BODY,20);
$dbf->fields['body']->set_style('width:95%');

// ADD HIDDEN FIELDS
if($customers_id){
	$dbf->add_hidden_field('customers_id',$customers_id,$extern_table=true);
}

// SET SELECT FIELD OPTIONS
$sender_array = $email_accounts->get_selectbox_list(array('email_address','email_address'),'email_address');
$dbf->fields['from_email']->set_options($sender_array,'');

if($action != 'write'){
	$dbf->fields['emails_id']->set_title(TITLE_ID,0);
	$dbf->fields['from_name']->set_title(TITLE_FROM_NAME,4);
	$dbf->fields['date_saved']->set_title(TITLE_SAVED,1);
}


// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$email = $emails->get_email($dbf->select_id);
}else{
	$mode = 'add';
}

$dbf->no_view_fields = array('messageid','own_mail','customers_id','reply_id','editor');

if($customers_id){
	$smarty->assign('PAGER_TAB_FILE',xtc_href_link(FILENAME_MAILMASTER,xtc_get_all_get_params(array('id')).'id='.$customers_id.'&tab=1'));
	if($emails->get_number_of_allocated_emails_for_customer($customers_id)>0)
	$smarty->assign('BUTTON_HISTORY',xtc_draw_input_field('history',IMAGE_MAILHISTORY,'onclick="ajax_show_tab(\'\');" class="iButton"',false,'button'));
}
switch($action){
	case 'write':
		$result = $dbf->get_form($form_name = 'email',$page = yes_get_php_self(),$mode,$template = DIR_FS_ADMIN.'templates/formular.tpl',$smarty);
		break;
	case 'view':
		$result = $dbf->get_view( $page = yes_get_php_self(), $print=false,$template = DIR_FS_ADMIN.'templates/formular.tpl',$smarty);
		break;
	case 'print':
		$result = $dbf->get_view($page = yes_get_php_self(),$print=true,$template = DIR_FS_ADMIN.'templates/formular.tpl',$smarty);
		$smarty->assign('PRINT',1);
		break;
}
		
if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'edit':
			$reply_id = $dbf->select_id;
			break;
		case 'add':
			$reply_id = 0;
			break;
	}
	$customers_id = ($_POST['customers_id']) ? xtc_db_input($_POST['customers_id']) : 0;
	$subject = xtc_db_input($_POST['subject']);
	$body = xtc_db_input($_POST['body']);
	$from_email = xtc_db_input($_POST['from_email']);
	$from_name = xtc_get_editor_name($_SESSION['customer_id']);
	$recipient = xtc_db_input($_POST['recipient']);
	$sql_array = array(
					'body'=>$body,
					'from_name'=>$from_name,
					'from_email'=>$from_email,
					'customers_id'=>$customers_id,
					'date_saved'=>'now()',
					'editor'=>$_SESSION['customer_id'],
					'own_mail'=>1,
					'subject'=>$subject,
					'reply_id'=>$reply_id
	);
	if(xtc_php_mail($from_email, $from_name, $recipient, $recipient, $forward='', $from_email, $from_name, $path_to_attachement='', $path_to_more_attachements='', $subject,$body,'')){
		xtc_db_perform(TABLE_EMAILS,$sql_array);
		$id = $conn->Insert_ID();
		if($customers_id){
			$YES_HISTORY->add('customers',sprintf(HISTORY_CUSTOMER_MAIL_SEND,$subject),$customers_id);
		}else{
			$YES_HISTORY->add('system',sprintf(HISTORY_MAIL_SEND,$recipient,$subject));
		}
		xtc_redirect(xtc_href_link(FILENAME_MAILMASTER,'close=1'));
	}
}
$smarty->assign('FORMULAR', $result);

if($customers_id){
	$smarty->assign('PAGER_TAB_FILE',xtc_href_link(FILENAME_MAILMASTER,xtc_get_all_get_params(array('id')).'id='.$customers_id.'&tab=1'));
	if($emails->get_number_of_allocated_emails_for_customer($customers_id)>0)
	$smarty->assign('BUTTON_HISTORY',xtc_draw_input_field('history',IMAGE_MAILHISTORY,'onclick="ajax_show_tab(\'\');" class="iButton"',false,'button'));
}

$smarty->assign('SELF_LINK',xtc_href_link(FILENAME_MAILMASTER,xtc_get_all_get_params(array('id'))));
$smarty->assign('HEADING_TITLE',$heading_title);
$smarty->assign('BUTTON_CLOSE',WINDOW_CLOSE);
$smarty->display(DIR_FS_ADMIN.'templates/mailmaster.html');
