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

		$X->NAVI[] 				= 'Moduły';
		$this->TABLE 			= '_modules';
		$this->LOG_ACTIVITY 	= false;
		$this->MULTILANG 		= false;

		$X->ADMIN->tree_path($this);

		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;
		}
	}


	function _list() {
		global $X;

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


		$split=$X->DB->split('
			SELECT this.*,
				(SELECT COUNT(id) FROM '.$this->TABLE.' WHERE parent_id=this.id) AS subs
			FROM '.$this->TABLE.' this
			WHERE 1 
				AND parent_id="'.(int)$_GET['parent_id'].'"
				'.($this->MULTILANG==1 ? ' AND lang="'.$_SESSION['backend']['lang'].'" ':'').'
			'.$X->DB->set_order($table).'
		', 5);


		$table['top_left'] = $X->FORM->dodaj('Dodaj moduł');//.'&nbsp;&nbsp;'.$X->FORM->button('Szkice (2)',$X->URL->get('drafts=1'),false,'button button_big');
		$table['head']='
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('Tytuł','title',300,'left').'
			'.$X->ADMIN->head_col('Plik','filename',false,'left').'
			'.$X->ADMIN->head_col('Aktywny','active',70,'center').'
			'.$X->ADMIN->head_col(false,false,75,'center').'
			'.$X->ADMIN->head_col(false,false,70,'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>'.$X->ADMIN->go_parent('page&parent_id='.$dane['id'], '<b>'.$X->STR->outt($dane['title']).'</b>', $dane['subs']).'</td>
					<td>'.$dane['filename'].'</td>
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'active').'</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($new==true) {
			$dane['parent_id']=$_GET['parent_id'];
			$dane['active']=1;
		} else {
			$sql_p=$X->DB->query(' SELECT * FROM '.$this->TABLE.'_pliki WHERE parent_id="'.$dane['id'].'" ORDER BY pozycja, id ');
			while($dane_p=$X->DB->fetch_array($sql_p)) {
				$pliki.=$X->ADMIN->file_row(array(
					'name'		=> 'pliki',
					'id'			=> $dane_p['id'],
					'pozycja'	=> $dane_p['pozycja'],
					'dir'			=> USR.'/flashyk/'.$_GET['id'].'/',
					'file'		=> $dane_p['file'],
					'ori_file' 	=> $X->STR->outt($dane_p['ori_file']),
					'title'		=> $X->STR->outt($dane_p['title']),
				));
			}
			$pliki=$X->ADMIN->table_file_row('pliki', $pliki, true);

			$foto=$X->ADMIN->table_file_single('foto', USR.'/foto/'.$_GET['id'].'/', $dane['foto'], $X->STR->outt($dane['foto_title']));
		}


		$REQ[]=array('file', 1, 'foto', false, array('ext'=>'jpg|gif|png'));
		$REQ[]=array('data', 0, 'date');
		$REQ[]=array('multiselect', 3, 'multi');
		$REQ[]=array('text', 3, 'title');
		$REQ[]=array('file', 3, 'pliki', false, array('ext'=>'jpg|gif|png'));
		$REQ[]=array('text', 5, 'filename');


		$html='
			'.$X->ADMIN->form_actions($edit).'
			<table class="form padd3">
				<tr>
					<td width="140">'.$X->FORM->label('parent_id','Grupa').'</td>
					<td>'.$X->FORM->select('parent_id','<option value="">[Grupa główna]</option>'.$X->FORM->get_options_tree('SELECT id,title FROM '.$this->TABLE.' WHERE parent_id="$" ORDER BY pozycja, id ', '$', 0, $dane['parent_id'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('title','Nazwa').'</td>
					<td>'.$X->FORM->input('title',400,$X->STR->outt($dane['title'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('date','Data').'</td>
					<td>'.$X->FORM->calendar('date').'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('color','Kolor').'</td>
					<td>'.$X->FORM->color_picker('color').'</td>
				</tr>
				<tr>
					<td class="top">'.$X->FORM->label('multi','Multiselect').'</td>
					<td>'.$X->FORM->multiselect('multi',$X->FORM->get_options_tree('SELECT id,title FROM '.$this->TABLE.' WHERE parent_id="$" ORDER BY pozycja, id ', '$', 0, $dane['parent_id']),400,0,true,true).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('filename','Plik .php').'</td>
					<td>'.$X->FORM->input('filename',400,$X->STR->outt($dane['filename'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('active','Aktywny').'</td>
					<td>'.$X->FORM->checkbox('active',1,$dane['active']).'</td>
				</tr>
				<tr><td><br /></td></tr>
				<tr>
					<td class="top">'.$X->FORM->label('foto','Foto ąłęółćżść').'</td>
					<td>'.$foto.$X->FORM->flash_upload($FORM_NAME, 'foto', false, false, 'jpg|gif|png', 2).'</td>
				</tr>
				<tr><td><br /></td></tr>
				<tr>
					<td class="top">'.$X->FORM->label('pliki','Pliki').'</td>
					<td>'.$pliki.$X->FORM->flash_upload($FORM_NAME, 'pliki', false, true, 'zip|rar|jpg|gif|png', 64).'</td>
				</tr>
			</table>
			'.$X->ADMIN->form_actions($edit).'
		';
		return $X->ADMIN->formbox($X->FORM->get($FORM_NAME, $REQ, $html));
	}
//					<td>'.$X->FORM->multiselect('multi',$X->FORM->get_options_tree('SELECT id,title FROM '.$this->TABLE.' WHERE parent_id="$" AND parent_id="0" ORDER BY pozycja, id ', '$', 0, $dane['parent_id']),400,0,true,true).'</td>
//					<td>'.$X->FORM->multiselect('multi',$X->FORM->select_get_options('SELECT id,browser FROM _admin_users_login LIMIT 9 ', $X->DB->get_array(' SELECT user_id FROM _modules2users WHERE module_id="'.$_GET['id'].'" ')),400,0,true,true).'</td>

	function _save() {
		require(START_SAVE);
		array_push($SAVE, 'parent_id', 'title', 'filename', 'active', 'foto_title');

		$X->ADMIN->save_flash_upload(array(
			'table'	=> $this->TABLE,
			'col'		=> 'foto',
			'cond'	=> 'id="'.$_GET['id'].'" ',
			'dir'		=> USR.'/foto/'.$_GET['id'].'/',
			'form'	=> $FORM_NAME,
			'name'	=> 'foto',
			'suff_'	=> array()
		));
		$X->ADMIN->save_flash_upload(array(
			'table'	=> $this->TABLE.'_pliki',
			'cond'	=> 'parent_id="'.$_GET['id'].'" ',
			'dir'		=> USR.'/flashyk/'.$_GET['id'].'/',
			'form'	=> $FORM_NAME,
			'name'	=> 'pliki',
			'suff_'	=> array()
		));
		$X->DB->save_multiselect('multi', array('table'=>'_modules2users', 'col'=>'user_id', 'key'=>'module_id', 'val'=>$_GET['id']));

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


	function _delete() {
		require(START_DELETE);

		$X->ADMIN->delete_flash_upload(array(
			'table'	=> $this->TABLE.'_pliki',
			'cond'	=> 'parent_id="'.$_GET['id'].'" ',
			'dir'		=> USR.'/flashyk/'.$_GET['id'].'/',
			'suff_'	=> array()
		));
		$X->DB->q(' DELETE FROM _modules2users WHERE module_id="'.$_GET['id'].'" ');
		$X->DB->q(' DELETE FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');

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

}
?>