<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'emails.php');

function _draw_checkbox_id($id){
	return xtc_draw_checkbox_field('id[]',$id,false).'&nbsp;'.$id;
}

$emails = new emails();
// LIST ACTIONS
if($_POST['action'] and xtc_not_null($_POST['action'])){
	switch($_POST['action']){
		case 'delete':
			if(is_array($_POST['id']) and sizeOf($_POST['id'])>0){
				foreach($_POST['id'] as $k=>$id){
					$id = xtc_db_input($id);
					$emails->delete( $id );
				}
				$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
			}else{
				$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
			}
			xtc_redirect(xtc_href_link(FILENAME_EMAILS));
			break;
	}
}
switch($_GET['action']){
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$email = $emails->get_email($id);
			$emails->delete( $id );
			// KEINE HISTORY
			//$YES_HISTORY->add('system',sprintf(HISTORY_COMPANY_DELETED,$email->data->name));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_EMAILS));
		break;
}

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


include(DIR_WS_CLASSES.'class.adodb-pager.php');

$pager = new ADODB_Pager($conn,sprintf("SELECT emails_id,subject,recipient,concat(from_email,' ',from_name) as from_string,date_saved FROM %s",$emails->get_tablename() ));
$pager->index_field = $emails->get_index();

$pager->get_row_selector = 'cID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_SUBJECT,TABLE_HEADING_RECIPIENT,TABLE_HEADING_FROM,TABLE_HEADING_SAVED);
$pager->sortOrders = array(
	'emails_id'=>TABLE_HEADING_ID,
	'subject'=>TABLE_HEADING_SUBJECT,
	'recipient'=>TABLE_HEADING_RECIPIENT,
	'date_saved'=>TABLE_HEADING_SAVED
);
$pager->gridHelper = array(
	'emails_id'=>'_draw_checkbox_id'
);

include(DIR_WS_INCLUDES.'header.php');

$pager->Render($rows_per_page=MAX_DISPLAY_SEARCH_RESULTS_ADMIN);

$selected_item = $pager->selected_row;

$grid_smarty = $smarty;
$smarty->assign('pager_tab_file',$pager->pager_tab_file);
$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');
echo $pager->draw_list_selector();
echo yes_draw_href_text(IMAGE_DELETE,'javascript:pager_list_action(document.forms[\'pagergrid_form\'],\'delete\',\'\');',$access='func_emails_delete');

include('includes/footer.php');
