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

		$X->NAVI[] 					= $X->ADMIN->module_name();
		$this->TABLE 				= 'strony';
		$this->USE_SHORT			= false;
		$this->USE_COUNTER		= true;
		$this->ALLOW_ADD			= true;

		$this->GALERIA_TABLE		= 'strony_galerie';
		$this->GALERIA_DIR		= USR.'/strony_galerie';
		$this->GALERIA_USE_DESC	= false;

		$this->PARENT_DEPTH		= 99;
		$X->ADMIN->tree_path($this,false,false,'AND dis_enter="0"');

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

		$X->ADMIN->text_filter($this, 'id', 'ID', '=');
		$X->ADMIN->text_filter($this, 'title', 'Tytuł');
		$X->ADMIN->text_filter($this, 'content', 'Treść');
//		$X->ADMIN->filter_form($this, $table, 225);


		$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
				'.(empty($_GET['filter']) || (!empty($_GET['filter']) && !empty($_GET['parent_id'])) ? 'AND parent_id="'.(int)$_GET['parent_id'].'"':'').'
				'.($this->MULTILANG==1 ? ' AND lang="'.$_SESSION['backend']['lang'].'" ':'').'
				'.$this->FILTER_SQL.'
			'.$X->DB->set_order($table).'
		', 0);


		$table['top_left'] = ($this->ALLOW_ADD==true || IS_ROOT ? $X->FORM->dodaj():'');
		$table['top_right'] = $this->FILTER;
		$table['head']='
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('Tytuł','title',false,'left').'
			'.($this->USE_COUNTER ? $X->ADMIN->head_col('Odsłon','counter',64,'center'):'').'
			'.$X->ADMIN->head_col('Aktywna','active',64,'center').'
			'.$X->ADMIN->head_col('Górne menu','top_menu',80,'center').'
			'.$X->ADMIN->head_col('Dolne menu','bottom_menu',80,'center').'
			'.$X->ADMIN->head_col(false,false,70,'center').'
			'.$X->ADMIN->head_col(false,false,66,'center').'
			'.(IS_ROOT ? $X->ADMIN->head_col('X.Actions',false,96,'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, $dane['dis_enter'], $dane['x_special']).'</td>
					'.($this->USE_COUNTER ? '<td class="center">'.$dane['counter'].'</td>':'').'
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'active', false, false, $dane['dis_active']).'</td>
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'top_menu', false, false, $dane['dis_active']).'</td>
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'bottom_menu', false, false, $dane['dis_active']).'</td>
					<td class="center">'.$X->FORM->button('Edytuj', $X->URL->get('action=form&id='.$dane['id']), false, false, $dane['dis_edit']).'</td>
					<td class="center">'.$X->FORM->button_potwierdzenie('Usuń', $X->URL->get('action=delete&id='.$dane['id']), false, false, false, $dane['dis_delete']+$dane['subs']).'</td>
					'.(IS_ROOT ? '<td class="center x_func">'.
						$X->FORM->ajax_checkbox($this->TABLE, $dane, 'dis_delete', false, '<b>^</b> DEL').
						$X->FORM->ajax_checkbox($this->TABLE, $dane, 'dis_edit', false, '<b>^</b> EDIT').
						$X->FORM->ajax_checkbox($this->TABLE, $dane, 'dis_enter', false, '<b>^</b> ENTER').
						$X->FORM->ajax_checkbox($this->TABLE, $dane, 'dis_move', false, '<b>^</b> MOVE').
						$X->FORM->ajax_checkbox($this->TABLE, $dane, 'dis_active', false, '<b>^</b> ACT').
						$X->FORM->ajax_checkbox($this->TABLE, $dane, 'x_special', false, 'SPECIAL').
					'</td>':'').'
				</tr>
			';
		}
		return $X->ADMIN->listbox($this, $table, $rows, $split).$html;
	}


	function _form() {
		require(START_FORM);

        $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"');
		$W=140;

		if($new==true) {
			if($this->ALLOW_ADD!=true && !IS_ROOT) $X->URL->go2('action&id');
			$dane['active']=1;
			$dane['parent_id']=$_GET['parent_id'];
		} else {
			if($dane['dis_edit']==1 && !IS_ROOT) $X->URL->go2('action&id');
			if($dane['dis_move']==1) $dis_label=$X->DB->fast(' SELECT title FROM '.$this->TABLE.' WHERE id="'.$dane['parent_id'].'" ');

			$sql_p=$X->DB->query(' SELECT * FROM '.$this->GALERIA_TABLE.' WHERE parent_id="'.$dane['id'].'" ORDER BY pozycja, id ');
			while($dane_p=$X->DB->fetch_array($sql_p)) {
				$pliki.=$X->ADMIN->file_div(array(
					'name'		=> 'pliki',
					'id'			=> $dane_p['id'],
					'pozycja'	=> $dane_p['pozycja'],
					'dir'			=> $this->GALERIA_DIR.'/'.$_GET['id'].'/',
					'file'		=> $dane_p['file'],
					'ori_file' 	=> $X->STR->outt($dane_p['ori_file']),
					'title'		=> ($this->GALERIA_USE_DESC ? $X->STR->outt($dane_p['title']) : null),
					'title_w'	=> 270,
					'img_w'		=> 270,
					'img_h'		=> 200,
					'preview_suff' => '_',
				));
			}
			$pliki=$X->ADMIN->sort_file_div('pliki', $pliki, true);
		}

		if($dane['x_hide_title']!=1) {
			$REQ[]=array('text', 2, 'title');
			$REQ[]=array('text', 1, 'x_key');
		} else {
			$REQ[]='';
		}



		$html='
			'.$X->ADMIN->form_actions($edit).'
			<table class="form padd3">
				'.(IS_SU ? '
					<tr>
						<td>'.$X->FORM->label('x_static_file','<i><b>Plik HTML/PHP</b></i>').'</td>
						<td>'.$X->FORM->input('x_static_file',400,$X->STR->outt($dane['x_static_file'])).'</td>
					</tr>
					<tr>
						<td>'.$X->FORM->label('x_hide_title','<i><b>Ukryj tytuł</b></i>').'</td>
						<td>'.$X->FORM->checkbox('x_hide_title',1,$X->STR->outt($dane['x_hide_title'])).'</td>
					</tr>
					<tr>
						<td>'.$X->FORM->label('x_hide_content','<i><b>Ukryj treść</b></i>').'</td>
						<td>'.$X->FORM->checkbox('x_hide_content',1,$X->STR->outt($dane['x_hide_content'])).'</td>
					</tr>
					<tr><td><br /></td></tr>
				':'').'
				'.($dane['x_hide_title']!=1 ? '
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('title','Tytuł').'</td>
						<td>'.($dane['x_hide_title']!=1 ? $X->FORM->input('title',770,$X->STR->outt($dane['title'])) : '<b>'.$X->STR->outt($dane['title']).'</b>').'</td>
					</tr>
				':'').'
				<tr>
					<td width="'.$W.'">'.$X->FORM->label('x_key','Adres strony').'</td>
					<td><div style="position:absolute; margin-left:-28px; width:24px; text-align:right; margin-top:6px; font-size:15px;">/</div>'.$X->FORM->input('x_key',770,$X->STR->outt($dane['x_key'])).'</td>
				</tr>
				<tr><td><br /></td></tr>
				'.($dane['x_special']!=1 && 1==2 ? '
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('parent_id','Grupa').'</td>
						'.($dane['dis_move']==1 ? '
							<td><b>['.(!empty($dis_label) ? $dis_label : 'Grupa główna').']</b></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="$" AND dis_enter="0" ORDER BY pozycja, id ', '$', 0, $dane['parent_id'], $this->PARENT_DEPTH)).'</td>
						').'
					</tr>
				':'').'

				'.($this->USE_SHORT ? '
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('short','Skrót treści').'</td>
						<td>'.$X->FORM->textarea('short',400,70,$X->STR->outt($dane['short'])).'</td>
					</tr>
				':'').'
				'.($dane['x_hide_content']!=1 ? '
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('content','Treść').'</td>
						<td>'.$X->FORM->ck('content',$X->STR->out($dane['content']),770,400).'</td>
					</tr>
				':'').'
				'.($dane['dis_active']!=1 ? '
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('active','Aktywna').'</td>
						<td>'.$X->FORM->checkbox('active',1,$dane['active']).'</td>
					</tr>


					<tr><td><br /></td></tr>
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('link_href','Własny link z menu').'</td>
						<td>'.$X->FORM->input('link_href',770,$X->STR->outt($dane['link_href'])).'</td>
					</tr>
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('target_blank','Otwórz w nowej zakładce').'</td>
						<td>'.$X->FORM->checkbox('target_blank',1,$dane['target_blank']).'</td>
					</tr>
					<tr><td><br /></td></tr>
				':'').'
				'.($dane['x_special']!=1 ? '
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('seo_t','SEO - Title').'</td>
						<td>'.$X->FORM->input('seo_t',770,$X->STR->outt($dane['seo_t'])).'</td>
					</tr>
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('seo_k','SEO - Keywords').'</td>
						<td>'.$X->FORM->input('seo_k',770,$X->STR->outt($dane['seo_k'])).'</td>
					</tr>
					<tr>
						<td width="'.$W.'">'.$X->FORM->label('seo_d','SEO - Description').'</td>
						<td>'.$X->FORM->input('seo_d',770,$X->STR->outt($dane['seo_d'])).'</td>
					</tr>
					<tr><td><br /></td></tr>

					<tr><td><br /></td></tr>
					<tr>
						<td class="top">'.$X->FORM->label('pliki','Galeria').'</td>
						<td>'.$X->FORM->flash_upload($FORM_NAME, 'pliki', $this->GALERIA_DIR.'/'.$_GET['id'].'/', true, 'jpg|gif|png', 8).'</td>
					</tr>
					<tr><td colspan="10">'.$pliki.'</td></tr>
				':'').'
			</table>
			'.$X->ADMIN->form_actions($edit).'
		';
		return $X->ADMIN->formbox($X->FORM->get($FORM_NAME, $REQ, $html));
	}


	function _save() {
		$GET_CURRENT=true;
		require(START_SAVE);
        $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($this->USE_SHORT) array_push($SAVE,'short');
		if($current['dis_move']!=1 && $current['x_special']!=1) array_push($SAVE,'parent_id');
		if($current['dis_active']!=1) array_push($SAVE,'active','target_blank');
		if(IS_SU) array_push($SAVE,'x_static_file','x_hide_title','x_hide_content');
		if($current['x_hide_title']!=1) array_push($SAVE,'title');
		if($current['x_hide_content']!=1) array_push($SAVE,'content');
		if($current['dis_active']!=1) array_push($SAVE,'link_href');
		if($current['x_special']!=1) array_push($SAVE, 'seo_t', 'seo_k', 'seo_d');
		array_push($SAVE,'x_key');


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


		$IMGS=$X->ADMIN->save_flash_upload(array(
			'table'	=> $this->GALERIA_TABLE,
			'cond'	=> 'parent_id="'.$_GET['id'].'" ',
			'dir'		=> $this->GALERIA_DIR.'/'.$_GET['id'].'/',
			'form'	=> $FORM_NAME,
			'name'	=> 'pliki',
			'suff_'	=> array('_')
		));
		if(count($IMGS)>0) foreach($IMGS as $IMG) {
			$X->IMG->resize($this->GALERIA_DIR.'/'.$_GET['id'].'/'.$IMG, array(
				array('w'=>900, 'h'=>900, 'name'=>'', 'base_res'=>1),
				array('w'=>270, 'h'=>180, 'name'=>'_'),
			));
		}

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


	function _delete() {
		require(START_DELETE);
        $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($X->DB->fast(' SELECT dis_delete FROM '.$this->TABLE.' WHERE id="'.(int)$_GET['id'].'" ')==1 && !IS_ROOT) $X->URL->go2('action&id');

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

		$X->DB->q(' DELETE FROM '.$this->GALERIA_TABLE.' WHERE parent_id="'.(int)$_GET['id'].'" ');
		$X->UTIL->rmdir_rf($this->GALERIA_DIR.'/'.(int)$_GET['id']);

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

}
?>