<? 
$CLS='unsend_emails';
class unsend_emails
{
	function init() {
		global $X;

		$X->NAVI[] 				= $X->ADMIN->module_name();
		$this->TABLE 			= '_mail_unsend';

		switch($_GET['action']) {
			default:	 				return $this->_list(); 			break;
			case 'batch':			return $this->_batch();			break;
			case 'view':			return $this->_view();			break;
			case 'delete':			return $this->_delete();		break;
			case 'delete_all':	return $this->_delete_all();	break;
			case 'resend':			return $this->_resend();		break;
		}
	}


	function _list() {
		global $X;

		$table['order_by']	= 'id';
		$table['order_dir']	= 'desc';

		$X->ADMIN->text_filter($this, 'id', 'ID', '=');
		$X->ADMIN->text_filter($this, 'email', 'E-mail');
		$X->ADMIN->text_filter($this, 'subject', 'Temat');
		$X->ADMIN->filter_form($this, $table, 225);


		$split=$X->DB->split('
			SELECT this.*
			FROM '.$this->TABLE.' this
			WHERE 1 '.($_SESSION['backend']['is_root']!=1 ? 'AND id>=5':'').'
			'.$this->FILTER_SQL.'
			'.$X->DB->set_order($table).'
		', 20);


		$table['top_left']=$this->FILTER;
		$table['top_right']=$X->FORM->button_potwierdzenie('<span class="ico w_no"></span><span>All</span>',$X->URL->get('action=delete_all'),false,false,'button button_gfx');
		$table['bottom_left']=$X->ADMIN->batch_actions($split, array('delete'=>'Usuń', 'resend'=>'Wyślij'));
		$table['head']='
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->batch_col('th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('Data','date',120,'center').'
			'.$X->ADMIN->head_col('E-mail','email',250,'left').'
			'.$X->ADMIN->head_col('Temat','subject',false,'left').'
			'.$X->ADMIN->head_col('Error','error',300,'left').'
			'.$X->ADMIN->head_col(false,false,82,'center').'
			'.$X->ADMIN->head_col(false,false,72,'center').'
			'.$X->ADMIN->head_col(false,false,66,'center').'
		';
		while($dane=$X->DB->fa($split['sql'])) {
			$rows.='
				<tr id="R:'.$dane['id'].'">
					'.$X->ADMIN->sortable_col($table,$split,'td').'
					'.$X->ADMIN->batch_col('td',$dane['id']).'
					<td class="center">'.$dane['id'].'</td>
					<td>'.$X->STR->outt($dane['date']).'</td>
					<td><b>'.$X->STR->outt($dane['email']).'</b></td>
					<td><b>'.$X->STR->outt($dane['subject']).'</b></td>
					<td>'.$X->STR->outt($dane['full_error']).'</td>
					<td class="center">'.$X->FORM->button('Podgląd', $X->URL->get('action=view&id='.$dane['id'])).'</td>
					<td class="center">'.$X->FORM->button_potwierdzenie('Wyślij', $X->URL->get('action=resend&id='.$dane['id']),false,'Wysłać ponownie maila?').'</td>
					<td class="center">'.$X->FORM->button_potwierdzenie('Usuń', $X->URL->get('action=delete&id='.$dane['id'])).'</td>
				</tr>
			';
		}
		return $X->ADMIN->listbox($this, $table, $rows, $split).$html;
	}


	function _view() {
		$FLAT=true;
		require(START_FORM);
		$dane=$X->DB->fast(' SELECT * FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');

		$body=$X->STR->out($dane['body']);


		$html='
			<div class="form_actions">'.$X->FORM->powrot().'</div>
			<table class="form padd5">
				<tr>
					<td>Data</td>
					<td><b>'.$X->STR->outt($dane['date']).'</b></td>
				</tr>
				<tr>
					<td>Error</td>
					<td><b>'.$X->STR->outt($dane['error']).'</b></td>
				</tr>
				<tr>
					<td>Full error</td>
					<td><b>'.$X->STR->outt($dane['full_error']).'</b></td>
				</tr>
				<tr><td><br /></td></tr>
				<tr>
					<td width="150">E-mail</td>
					<td><b>'.$X->STR->outt($dane['email']).'</b></td>
				</tr>
				<tr>
					<td>Temat</td>
					<td><b>'.$X->STR->outt($dane['subject']).'</b></td>
				</tr>
				<tr><td><br /></td></tr>
				<tr>
					<td>Treść</td>
					<td>'.$body.'</td>
				</tr>
			</table>
			<div class="form_actions">'.$X->FORM->powrot().'</div>
		';
		return $X->ADMIN->formbox($X->FORM->get($FORM_NAME, $REQ, $html));
	}


	function _delete($id=false) {
		require(START_DELETE);

		$X->DB->q(' DELETE FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');

		if(!is_numeric($id)) $X->URL->go2('action&id');///
	}


	function _delete_all() {
		require(START_DELETE);

		$X->DB->q(' TRUNCATE TABLE '.$this->TABLE.' ');
		$X->URL->go2('action&id');
	}


	function _resend($id=false) {
		require(START_OTHER);//

		$state=$X->MAIL->resend($X->DB->fast(' SELECT input_data FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" '));
		if($state==true) $X->DB->q(' DELETE FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');

		if(!is_numeric($id)) $X->URL->go2('action&id');///
	}


	function _batch() {
		global $X;

		if(count($_POST['batch'])>0) foreach($_POST['batch'] as $b_id) {
			if($_POST['batch_action']=='delete') $this->_delete($b_id);
			if($_POST['batch_action']=='resend') $this->_resend($b_id);
		}
		$X->URL->go2('action&batch_type&id');
	}

}
?>