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

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

		switch($_GET['action']) {
			default:	 			return $this->_list(); 		break;
			case 'form':		return $this->_form();		break;
			case 'save':		return $this->_save();		break;
			case 'test':		return $this->_test();		break;
			case 'delete':		return $this->_delete();	break;
			case 'batch':		return $this->_batch();		break;
		}
	}


	function _list() {
		global $X;

		$table['sortable_table_id']	= $this->TABLE;
		$table['order_by']				= 'pozycja';
		$table['order_dir']				= 'asc';

		$split=$X->DB->split('
			SELECT this.*
			FROM '.$this->TABLE.' this
			WHERE 1
			'.$X->DB->set_order($table).'
		', 0);


		$table['top_left'] = ($this->ALLOW_ADD==true || IS_ROOT ? $X->FORM->dodaj():'');
		$table['head']='
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('Klucz','klucz',250,'left').'
			'.$X->ADMIN->head_col('Tytuł','title',false,'left').'
			'.$X->ADMIN->head_col('Testowy E-mail',false,96,'center').'
			'.$X->ADMIN->head_col(false,false,70,'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').'
					<td class="center">'.$dane['id'].'</td>
					<td><b>'.$X->STR->outt($dane['klucz']).'</b></td>
					<td><a href="'.$X->URL->get('action=form&id='.$dane['id']).'"><b>'.$X->STR->outt($dane['title']).'</b></a></td>
					<td class="center">'.$X->FORM->button('Wyślij test', $X->URL->get('action=test&id='.$dane['id'])).'</td>
					<td class="center">'.$X->FORM->button('Edytuj', $X->URL->get('action=form&id='.$dane['id'])).'</td>
					<td class="center">'.$X->FORM->button_potwierdzenie('Usuń', $X->URL->get('action=delete&id='.$dane['id']), false, false, false, !IS_ROOT).'</td>
				</tr>
			';
		}
		return $X->ADMIN->listbox($this, $table, $rows, $split).$html;
	}


	function _form() {
		require(START_FORM);
		if($new==true && $this->ALLOW_ADD!=true && !IS_ROOT) $X->URL->go2('action&id');


		if(IS_ROOT) $REQ[]=array('klucz', 2, 'klucz');
		$REQ[]=array('text', 3, 'title');
		$REQ[]=array('text', 3, 'content');


		$vars_temp=@explode('
',$X->STR->outt($dane['vars']));
		if(count($vars_temp)>0) foreach($vars_temp as $var) {
			$var=trim($var);
			if(empty($var)) continue;
			$vars.=$X->FORM->button_OC($var,'CKEDITOR.instances.content.insertText(\'{{'.$var.'}}\')').'&nbsp;';
		}


		$html='
			'.$X->ADMIN->form_actions($edit).'
			<table class="form padd3">
				'.(IS_ROOT ? '
					<tr>
						<td>'.$X->FORM->label('klucz','<i><b>Klucz</b></i>').'</td>
						<td>'.$X->FORM->input('klucz',300,$X->STR->outt($dane['klucz'])).'</td>
					</tr>
					<tr>
						<td>'.$X->FORM->label('vars','<i><b>Zmienne</b></i>').'</td>
						<td>'.$X->FORM->textarea('vars',300,100,$X->STR->out($dane['vars'])).'</td>
					</tr>
					<tr><td><br /></td></tr>
				':'').'
				'.(!empty($vars) ? '
					<tr>
						<td><b>Wstaw zmienne:</b></td>
						<td>{{&nbsp;'.$vars.'}}<br /><br /></td>
					</tr>
				':'').'
				<tr>
					<td width="140">'.$X->FORM->label('title','Tytuł E-maila').'</td>
					<td>'.$X->FORM->input('title',770,$X->STR->outt($dane['title'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('content','Treść E-maila').'</td>
					<td>'.$X->FORM->ck('content',$X->STR->out($dane['content'])).'</td>
				</tr>
			</table>
			'.$X->ADMIN->form_actions($edit).'
		';
		return $X->ADMIN->formbox($X->FORM->get($FORM_NAME, $REQ, $html));
	}


	function _save() {
		require(START_SAVE);
		array_push($SAVE,'title','content');
		if(IS_ROOT) array_push($SAVE,'klucz','vars');

		$X->DB->q(' UPDATE '.$this->TABLE.' SET '.$X->DB->save($SAVE).' WHERE id="'.$_GET['id'].'" ');

		$X->URL->go2('action&id');
	}


	function _delete($id=false) {
		require(START_DELETE);
		if(!IS_ROOT) $X->URL->go2('action&id');

		$X->DB->q(' DELETE FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');
		if(!is_numeric($id)) $X->URL->go2('action&id');
	}


	function _test() {
		$FLAT=true;
		require(START_FORM);
		$X->NAVI[]='Wyślij testowy E-mail';

		if(!empty($_POST['email'])) {
			$template=$X->DB->fast(' SELECT title, content FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');
			$X->MAIL->send($_POST['email'], $X->STR->outt($template['title']), $X->STR->out($template['content']));

			$X->ADMIN->set_notif('ok','Testowy E-mail został wysłany na adres <b>'.$_POST['email'].'</b>',3);
			$X->URL->go2('action&id');
		}

		$REQ[]=array('email', 1, 'email');

		$html='
			'.$X->ADMIN->form_actions(0,0,0,'Wyślij').'
			<br /><br />&nbsp;<b>Podaj adres E-mail, na który ma zostać wysłany testowy szablon:</b><br />

			<table class="form padd3">
				<tr>
					<td width="140">'.$X->FORM->label('email','E-mail').'</td>
					<td>'.$X->FORM->input('email',300).'</td>
				</tr>
			</table>
			'.$X->ADMIN->form_actions(0,0,0,'Wyślij').'
		';
		return $X->ADMIN->formbox($X->FORM->get($FORM_NAME, $REQ, $html, false, 'test'));
	}

}
?>