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

		$X->NAVI[] 				= $X->ADMIN->module_name();
		$this->TABLE 			= '_modules';
		$this->LOG_ACTIVITY 	= false;
		$this->MULTILANG 		= false;
		$this->PARENT_DEPTH	= 2;

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


		if(IS_ROOT) {
			$this->USER_COND='1';
		} else {
			if(IS_SU) {
				$this->USER_COND='id>=2';
			} else {
				$this->USER_COND='id>=5';
			}
		}


		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).'
		', 0);


		$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('Sep','sep',50,'center').'
			'.$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'], $this->PARENT_DEPTH).'</td>
					<td>'.$dane['filename'].'</td>
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'sep').'</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'];
		}

		$REQ[]=array('text', 2, 'title');


		$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'], $this->PARENT_DEPTH)).'</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('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('users','Użytkownicy').'</td>
					<td>'.$X->FORM->multiselect('users',$X->FORM->select_get_options('SELECT id, CONCAT(title," - ",login) AS title FROM _admin_users WHERE '.$this->USER_COND.' ORDER BY id', $X->DB->get_array(' SELECT user_id FROM _modules2users WHERE module_id="'.$_GET['id'].'" ')),400,0,true,true).'</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, 'parent_id', 'title', 'filename', 'active');

		$X->DB->q(' UPDATE '.$this->TABLE.' SET '.$X->DB->save($SAVE).' WHERE id="'.$_GET['id'].'" ');
		$X->DB->save_multiselect('users', array('table'=>'_modules2users', 'col'=>'user_id', 'key'=>'module_id', 'val'=>$_GET['id']));

		$sql=$X->DB->q(' SELECT id FROM _admin_users WHERE '.$this->USER_COND.' ORDER BY id ');
		while($dane=$X->DB->fa($sql)) {
			$perm_array=$X->DB->get_array(' SELECT module_id FROM _modules2users WHERE user_id="'.$dane['id'].'" ORDER BY module_id ');
			$is_su=$X->DB->fast(' SELECT is_su FROM _admin_users WHERE id="'.$dane['id'].'" ');
			$perm_checksum=md5('mod_perm::'.$dane['id'].'>>'.$is_su.'>>'.@serialize($perm_array).'@dh');

			$X->DB->q(' UPDATE _admin_users SET perm_checksum="'.$perm_checksum.'" WHERE id="'.$dane['id'].'" ');
		}

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


	function _delete() {
		require(START_DELETE);

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

		$sql=$X->DB->q(' SELECT id FROM _admin_users WHERE '.$this->USER_COND.' ORDER BY id ');
		while($dane=$X->DB->fa($sql)) {
			$perm_array=$X->DB->get_array(' SELECT module_id FROM _modules2users WHERE user_id="'.$dane['id'].'" ORDER BY module_id ');
			$is_su=$X->DB->fast(' SELECT is_su FROM _admin_users WHERE id="'.$dane['id'].'" ');
			$perm_checksum=md5('mod_perm::'.$dane['id'].'>>'.$is_su.'>>'.@serialize($perm_array).'@dh');

			$X->DB->q(' UPDATE _admin_users SET perm_checksum="'.$perm_checksum.'" WHERE id="'.$dane['id'].'" ');
		}

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

}
?>