<?
$CLS='ustawienia';
class ustawienia
{
	function init() {
		global $X;
		$X->NAVI[] 		= $X->ADMIN->module_name();
		$this->TABLE 	= '_front_conf';


		if($_POST['add']==1) $this->_add();
		if($_POST['save']==1) $this->_save();


		$sql=$X->DB->q(' SELECT * FROM '.$this->TABLE.' ORDER BY nazwa, id ');
		while($dane=$X->DB->fa($sql)) {
			switch($dane['typ']) {
				default:
				case 'text':			$field = $X->FORM->input('wartosc['.$dane['klucz'].']', 400, $X->STR->outt($dane['wartosc'])); 				break;
				case 'text_numeric':	$field = $X->FORM->input_center('wartosc['.$dane['klucz'].']', 40, $X->STR->outt($dane['wartosc'])); 		break;
				case 'checkbox':		$field = $X->FORM->checkbox('wartosc['.$dane['klucz'].']', 1, $X->STR->outt($dane['wartosc'])); 			break;
				case 'textarea':		$field = $X->FORM->textarea('wartosc['.$dane['klucz'].']', 400, 70, $X->STR->outt($dane['wartosc'])); 	break;
			}

			$body.='
				<tr>
					'.(IS_ROOT ? '<td>'.$X->FORM->input('edit_klucz['.$dane['klucz'].']', 200, $X->STR->outt($dane['klucz'])).'</td>':'').'
					<td>'.(IS_ROOT ? $X->FORM->input('edit_nazwa['.$dane['klucz'].']', 300, $X->STR->outt($dane['nazwa'])) : $X->STR->outt($dane['nazwa']).'&nbsp;&nbsp;&nbsp;&nbsp;').'</td>
					<td>'.$field.$X->FORM->hidden('typ[]', $dane['typ']).$X->FORM->hidden('klucz[]', $dane['klucz']).'</td>
					'.(IS_ROOT ? '<td class="center">'.$X->FORM->checkbox('delete[]',$dane['id']).'</td>':'').'
				</tr>
			';
		}

		$html='
			'.(IS_ROOT ? '
				<form action="'.$X->URL->get().'" method="post">
				<table cellpadding="2" cellspacing="0">
					<tr>
						<td align="center"><b>Typ</b></td>
						<td align="center"><b>Klucz</b></td>
						<td align="center"><b>Nazwa</b></td>
						<td align="center"><b>Wartość</b></td>
						<td></td>
					</tr>
					<tr>
						<td>'.$X->FORM->select('new_typ','<option value="text">Text</option><option value="text_numeric">Text (numeric)</option><option value="textarea">Textarea</option><option value="checkbox">Checkbox</option>').'</td>
						<td>'.$X->FORM->input('new_klucz',200).'&nbsp;&nbsp;</td>
						<td>'.$X->FORM->input('new_nazwa',200).'&nbsp;&nbsp;</td>
						<td>'.$X->FORM->input('new_wartosc',200).'&nbsp;&nbsp;</td>
						<td>&nbsp;&nbsp;&nbsp;&nbsp;'.$X->FORM->submit('Dodaj').$X->FORM->hidden('add',1).'</td>
					</tr>
				</table>
				</form>
				<script type="text/javascript">$(\'#new_klucz\').focus()</script>
				<br /><br /><br />
			':'').'

			<form action="'.$X->URL->get().'" method="post">
				'.$X->FORM->submit('Zapisz ustawienia').'<br /><br />
				'.$X->ADMIN->inline_table(array(
					'head' => '
						'.(IS_ROOT ? '<th>Klucz</th>':'').'
						<th class="left">Nazwa</th>
						<th>Wartość</th>
						'.(IS_ROOT ? '<th width="20" class="center">X</th>':'').'
					',
					'body' => $body,
					'class' => 'padd5'
				)).'
				<br />'.$X->FORM->submit('Zapisz ustawienia').'<br /><br />
				'.$X->FORM->hidden('save',1).'
			</form>
		';


		return $X->ADMIN->formbox($html);
	}


	function _add() {
		global $X;
		$X->STR->post_filter();

        $X->DB->q('SET sql_mode ="NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"');
		$_POST['new_klucz']=$X->URL->mod($_POST['new_klucz']);

		$sql_test=$X->DB->q(' SELECT * FROM '.$this->TABLE.' WHERE klucz="'.$_POST['new_klucz'].'" ');
		if($X->DB->num_rows($sql_test)==0 && !empty($_POST['new_klucz'])) {
			$X->DB->q(' INSERT INTO '.$this->TABLE.' (klucz,typ,nazwa,wartosc) VALUES ("'.$_POST['new_klucz'].'","'.$_POST['new_typ'].'","'.$_POST['new_nazwa'].'","'.$_POST['new_wartosc'].'") ');
		}
		$X->URL->go();
	}


	function _save() {
		global $X;
		$X->STR->post_filter();

        $X->DB->q('SET sql_mode ="NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"');
		if(IS_ROOT && count($_POST['delete'])>0) foreach($_POST['delete'] as $delete_id) {
			$X->DB->q(' DELETE FROM '.$this->TABLE.' WHERE id="'.$delete_id.'" ');
		}

		if(count($_POST['typ'])>0) foreach($_POST['typ'] as $key => $value) {
			switch($_POST['typ'][$key]) {
				case 'text_numeric':
					$X->DB->q(' UPDATE '.$this->TABLE.' SET wartosc="'.str_replace(',','.',$_POST['wartosc'][$_POST['klucz'][$key]]).'" WHERE klucz="'.$_POST['klucz'][$key].'" ');
				break;

				case 'text':
					$X->DB->q(' UPDATE '.$this->TABLE.' SET wartosc="'.$_POST['wartosc'][$_POST['klucz'][$key]].'" WHERE klucz="'.$_POST['klucz'][$key].'" ');
				break;

				case 'checkbox':
					if(empty($_POST['wartosc'][$_POST['klucz'][$key]])) $_POST['wartosc'][$_POST['klucz'][$key]]=0;
					$X->DB->q(' UPDATE '.$this->TABLE.' SET wartosc="'.$_POST['wartosc'][$_POST['klucz'][$key]].'" WHERE klucz="'.$_POST['klucz'][$key].'" ');
				break;

				case 'textarea':
					$X->DB->q(' UPDATE '.$this->TABLE.' SET wartosc="'.($_POST['wartosc'][$_POST['klucz'][$key]]).'" WHERE klucz="'.$_POST['klucz'][$key].'" ');
				break;
			}
		}

		if(IS_ROOT && count($_POST['typ'])>0) foreach($_POST['typ'] as $key => $value) {
			$klucz=$_POST['klucz'][$key];
			$X->DB->q(' UPDATE '.$this->TABLE.' SET klucz="'.$_POST['edit_klucz'][$klucz].'", nazwa="'.$_POST['edit_nazwa'][$klucz].'" WHERE klucz="'.$klucz.'" ');
		}

		$X->ADMIN->set_notif('ok','Ustawienia zostały zapisane!',2);
		if($this->SKIP_OPS!=true) $X->ADMIN->log_op(array('action'=>'save'));

		$X->URL->go();
	}


}
?>