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

		$X->NAVI[] 			= $X->ADMIN->module_name();
		$this->TABLE 		= '_front_lang';
		$this->IMG_DIR		= ROOT.'/admin/images/flags/';


		switch($_GET['action']) {
			default:	 			return $this->_list(); 		break;
			case 'form':		return $this->_form();		break;
			case 'save':		return $this->_save();		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).'
		', 20);


		$table['top_left'] = $X->FORM->dodaj();
		$table['bottom_left'] = $X->ADMIN->batch_actions($split);
		$table['head']='
			'.$X->ADMIN->batch_col('th').'
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('Flaga','flag',50,'center').'
			'.$X->ADMIN->head_col('Kod','kod',40,'center').'
			'.$X->ADMIN->head_col('Tytuł','title',150,'left').'
			'.$X->ADMIN->head_col('Znaki testowe','test',false,'left').'
			'.$X->ADMIN->head_col('Aktywny','active',64,'center').'
			'.$X->ADMIN->head_col('Domyślny','active',72,'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->batch_col('td',$dane['id']).'
					'.$X->ADMIN->sortable_col($table,$split,'td').'
					<td class="center">'.$dane['id'].'</td>
					<td class="center">'.(!empty($dane['flag']) ? '<img src="'.$this->IMG_DIR.$dane['flag'].'" alt="" />':'').'</td>
					<td class="center"><b>'.$X->STR->outt($dane['kod']).'</b></td>
					<td><a href="'.$X->URL->get('action=form&id='.$dane['id']).'"><b>'.$X->STR->outt($dane['title']).'</b></a></td>
					<td>'.$X->STR->outt($dane['test']).'</td>
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'active').'</td>
					<td class="center">'.$X->FORM->ajax_radio($this->TABLE, $dane, 'domyslny').'</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'])).'</td>
				</tr>
			';
		}
		return $X->ADMIN->listbox($this, $table, $rows, $split).$html;
	}


	function _form() {
		require(START_FORM);

		if($edit==true) $image=$X->ADMIN->table_file_single('flag', $this->IMG_DIR, $dane['flag'], false, false, false, '');

		$REQ[]=array('text', 2, 'title');
		$REQ[]=array('text', 2, 'kod');
		$REQ[]=array('file', 1, 'flag', false, 'jpg|gif|png');


		$html='
			'.$X->ADMIN->form_actions($edit).'
			<table class="form padd3">
				<tr>
					<td width="150">'.$X->FORM->label('title','Tytuł').'</td>
					<td>'.$X->FORM->input('title',400,$X->STR->outt($dane['title'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('kod','Kod').'</td>
					<td>'.$X->FORM->input_center('kod',30,$X->STR->outt($dane['kod'])).'</td>
				</tr>
				<tr>
					<td class="top">'.$X->FORM->label('flag','Flaga (max 16x16)').'</td>
					<td>'.$image.$X->FORM->flash_upload($FORM_NAME, 'flag', false, false, 'jpg|gif|png', 0.001).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('test','Znaki testowe').'</td>
					<td>'.$X->FORM->input('test',400,$X->STR->outt($dane['test'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('active','Aktywny').'</td>
					<td>'.$X->FORM->checkbox('active',1,$dane['active']).'</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','kod','test','active');

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

		$IMG=$X->ADMIN->save_flash_upload(array(
			'table'	=> $this->TABLE,
			'col'		=> 'flag',
			'cond'	=> 'id="'.$_GET['id'].'"',
			'dir'		=> $this->IMG_DIR,
			'form'	=> $FORM_NAME,
			'name'	=> 'flag',
			'suff_'	=> array(),
			'force_filename' => strtolower($_POST['kod'])
		));

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


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

		$X->ADMIN->delete_flash_upload(array(
			'table'	=> $this->TABLE,
			'col'		=> 'flag',
			'cond'	=> 'id="'.$_GET['id'].'"',
			'dir'		=> $this->IMG_DIR,
			'suff_'	=> array()
		));
		$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);
		}
		$X->URL->go2('action&id');
	}

}
?>