<?php
class Admin extends Controller {
	//zmienna przechowująca języki stałych elementów strony(szczzególnie admina)
		//$this->config->load('lang_pl');
	var $site = null;
	
	//zmienne przechowujące wymiary miniaturek newsa i galerii
	var $news = array();
	var $product = array();
	var $gal_img = array();
	
//zapoznaj sie z zaladowanymi configami, bibliotekami, helperami itd
	function Admin(){
		parent::Controller();	
		$this->load->model('admin_model','',TRUE);
		$this->load->library('session');
		$this->load->helper('form');		
		$this->load->helper('url');
		
		//plik z jezykiem polskim
		$this->config->load('lang_pl');
		//rozmiary miniaturek
		$this->config->load('file');
		$this->site = $this->config->item('PL');
		$this->news = $this->config->item('news');
		$this->product = $this->config->item('product');
		$this->gal_img = $this->config->item('gal_img');
	}
	
	function index($idlang = 3){
		$data = $this->get_menus();
		$data['content'] = $this->check_login('index');
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
//inne funkcje		
	function login(){
		$user = $this->admin_model->login($this->input->post('login'),md5($this->input->post('password')));
		if(isset($user[0])){
			$user = $user[0];
			if($user['active']){
				$tabsessyjna = array(	'login'  => $this->input->post('login'),
			                   			'loggedin' => TRUE	,
										'idusers' => $user['idusers'],
										'idgroups' => $user['iduser_groups']
								);
				$this->session->set_userdata($tabsessyjna);		
			}
		}
		redirect('/admin', 'location');
	}
	
	function logout(){
		$this->session->unset_userdata('login');
		$this->session->unset_userdata('loggedin');
		$this->session->unset_userdata('idusers');
		$this->session->unset_userdata('idgroups');
		redirect('/admin', 'location');
	}
	
	//funkcja dba o aktualizowanie języka
	private function set_language($idlang){
		$lang = $this->admin_model->get_language_by_id($idlang);
		if(isset($lang)){
			$lang = $lang[0];
			$this->site = $this->config->item($lang['shortname']);
		}
		return $lang;
	}

	//funkcja sprawdza czy uzytkownik jest zalogowany, czy ma prawa dostępu do danego elementu
	private function check_login($elementname = '', $content = '', $bool=false){
		$idgroups = $this->session->userdata('idgroups');
		$login = $this->session->userdata('login');
		$loggedin = $this->session->userdata('loggedin');
		
		if($login != null && $loggedin == true){
			$allowed_elements = $this->admin_model->get_elements_for_group($idgroups);
			if(isset($allowed_elements[0])){
				foreach($allowed_elements as $allowed_element){
					if($allowed_element['name'] == $elementname && $allowed_element['active'] == true){
						$flag = 1;
						break;
					}
				}
			}
		}
		// w zaleznosci od zmiennej $bool zwraca content albo wartosc booleanowska
		if($bool){
			if(isset($flag)){
				return true;
			}else{
				return false;
			}
		}else{
			if(isset($flag)){
				return $content;
			}else{
				if($elementname == 'index'){
					$content = $this->load->view('admin/layout/login','',true);
				}else{
					$content = ucfirst($this->site['nie_masz_uprawnien_do_tego_zasobu']);
				}
				return $content;
			}	
		}
	}
	
	function get_standard_metatags($type, $idlang){
		$result = $this->admin_model->get_default_metatags($type, $idlang);
		if(isset($result[0])){
			$result = $result[0];
		}else{
			$result = '';
		}
		return $result;
	}
	
	//pobiera metatagi danego typu w formacie jsona(potrzebne przy automatycznym ladowaniuu standardowych metatagow)	
	function get_json_meta($idmetatags){
		$result = $this->admin_model->get_metatag_by_id($idmetatags);
		if(isset($result[0])){
			$result = $result[0];
		}else{
			$result = '';
		}
		$data['metatags'] = json_encode($result);
		$this->load->view('admin/elements/metatags_json', $data);
	}
	
	function get_json_meta_types($idlangs){
		$results = $this->admin_model->get_metatag_types_languages_by_lang($idlangs);
		$result ='';
		if(isset($results[0])){
			foreach($results as $group){
				$result .= '<option value="'.$group['idmetatags'].'">'.$group['template'].'</option>';
			}
		}
		$result = array('content'=>$result);
		$data['metatags'] = json_encode($result);
		$this->load->view('admin/elements/metatags_json', $data);
	}
	
	//generowanie menu
	private function get_menus($layout = 'empty', $data=''){
		$idgroup = $this->session->userdata('idgroups');
		$result = '';
		if($idgroup != null){
			$result['menu_poziom'] = $this->generate_menu_poziom($idgroup);
			$result['menu_pion'] = 	$this->load->view('admin/elements/menus/menu_pion/'.$layout, $data, true);
		}
		return $result; 
	}
	
	private function generate_menu_poziom($idgroup){
		$data['elements'] = $this->admin_model->get_elements_for_group($idgroup);
		$content = $this->load->view('admin/elements/menus/menu_poziom', $data, true);		
		$data['content'] = $this->check_login('rights', $content);
		return $content;
	}

	private function generate_friendlyurl($url_before){
		$url_before = strtolower($url_before);
		$url_after = url_title($url_before, 'dash', TRUE);
		return $url_after;
	}
	
	//usunięcie całego katalogu razem z potomkami, potrzebne w galeriach
	private function full_rmdir($dirname){
        if ($dirHandle = opendir($dirname)){
            $old_cwd = getcwd();
            chdir($dirname);

            while ($file = readdir($dirHandle)){
                if ($file == '.' || $file == '..') continue;

                if (is_dir($file)){
                    if (!$this->full_rmdir($file)) return false;
                }else{
                    if (!unlink($file)) return false;
                }
            }

            closedir($dirHandle);
            chdir($old_cwd);
            if (!rmdir($dirname)) return false;

            return true;
        }else{
            return false;
        }
    }

//users
	function users($idlang = 3){	
		$data = $this->get_menus('users');	
		$list['users'] = $this->admin_model->get_users();
		$content = $this->load->view('admin/elements/users', $list, true);
		$data['content'] = $this->check_login('users', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function delete_user($id){
		$group = $this->session->userdata('group');
		$allow = $this->check_login('users', '', true);
		if($allow){
			$this->admin_model->delete_user($id);
		}				
		
		redirect('/admin/users', 'location');		
	}
	
	function add_user($idlang = 3){
		$allow = $this->check_login('users', '', true);
		$data = $this->get_menus('users');
		
		if($allow){			
			if(isset($_POST['login']) && $_POST['login'] != '' && isset($_POST['password']) && $_POST['password'] != ''){
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$data=array('idusers' => '',
							'iduser_groups' => $_POST['iduser_groups'],
							'fname' => $_POST['fname'],
							'lname' => $_POST['lname'],
							'login' => $_POST['login'],
							'password' => md5($_POST['password']),
							'active' => $active
				);
				$this->admin_model->add_user($data);
				redirect('/admin/users', 'location');	
			}else{
				//przygotowanie listy grup do boxa
				$groups = $this->admin_model->get_groups();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($groups as $group){
					$bufor1[$i]=$group['iduser_groups'];
					$bufor2[$i]=$group['name'];
					$i++;
				}
				$data['groups']=array_combine($bufor1,$bufor2);				
				
				$data['content'] = $this->load->view('admin/elements/add_user', $data, true);
				
				$group = $this->session->userdata('group');
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function edit_user($id=null, $idlang = 3){
		$allow = $this->check_login('users', '', true);
		if($allow){			
			if(isset($_POST['login']) && $_POST['login'] != ''){
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$data=array('iduser_groups' => $_POST['iduser_groups'],
							'fname' => $_POST['fname'],
							'lname' => $_POST['lname'],
							'login' => $_POST['login'],
							'active' => $active
				);
				if(isset($_POST['password']) && $_POST['password']!=''){
					$data['password'] = md5($_POST['password']);
				}
				$this->admin_model->update_user($id,$data);
				unset($_POST);
				redirect('/admin/users', 'location');	
			}else{
				$data = $this->get_menus('users');
				$user = $this->admin_model->get_user_by_id($id);
				if(isset($user[0])){
					$user=$user[0];
					$data['idusers'] = $id;
					$data['iduser_groups'] = $user['iduser_groups'];
					$data['fname'] = $user['fname'];
					$data['lname'] = $user['lname'];
					$data['login'] = $user['login'];
					$data['active'] = $user['active'];
				//przygotowanie listy grup do boxa
					$bufor1=array($user['iduser_groups']);
					$bufor2=array('-----');
					$i = 1;
				}else{
					$bufor1=array();
					$bufor2=array();
					$i = 0;
				}
				$groups = $this->admin_model->get_groups();
				foreach($groups as $group){
					$bufor1[$i]=$group['iduser_groups'];
					$bufor2[$i]=$group['name'];
					$i++;
				}
				$data['groups']=array_combine($bufor1,$bufor2);
								
				$data['content'] = $this->load->view('admin/elements/edit_user', $data, true);
				
				$metatags = array();
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

//galleries
	function galleries($idlang = 3){	
		$list['galleries'] = $this->admin_model->get_galleries();
		$list['gallery_languages'] = $this->admin_model->get_galleries_languages();
		
		$data = $this->get_menus('galleries');
		$content = $this->load->view('admin/elements/galleries', $list, true);
		$data['content'] = $this->check_login('galleries', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function gal_lang_versions($id = null){	
		if($id==null){	
			$list['galleries'] = $this->admin_model->get_galleries();
		}else{
			$list['galleries'] = $this->admin_model->get_gal_lang_versions($id);
		}
		$metatags = '';
		
		$data = $this->get_menus('galleries');
		
		//ten widok należy uzależnic od $idlang
		$content = $this->load->view('admin/elements/galleries', $list, true);
		$data['content'] = $this->check_login('galleries', $content);
		
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function delete_gallery($id){
		$allow = $this->check_login('galleries', '', true);
		if($allow){
			$this->admin_model->delete_gallery($id);
		}				
		redirect('/admin/galleries', 'location');		
	}
	
	function add_gallery($idlang = 3){
		$allow = $this->check_login('galleries', '', true);
		$data = $this->get_menus('galleries');
		if($allow){			
			if(isset($_POST['title'])){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$gal = array('idgalleries' => '',
							'showorder' => '',
							'active' => $active	
				);
				$this->admin_model->add_gallery($gal);				
				//przygotowanie folderu na fotki i miniaturki
				if(mkdir ("./files/img/galleries/".$this->db->insert_id(), 0777)){	
					chmod("./files/img/galleries/".$this->db->insert_id(), 0777);	
					mkdir ("./files/img/galleries/".$this->db->insert_id().'/min', 0777);
					chmod("./files/img/galleries/".$this->db->insert_id().'/min', 0777);	
					$gal_lang=array('idgalleries_languages' => '',
								'idgalleries' => $this->db->insert_id(),
								'idlanguages' => $_POST['idlanguage'],
								'title' => $_POST['title'],
								'content' => $_POST['artcontent'],
								'titleseo' => $_POST['seotitle'],
								'keywords' => $_POST['keywords'],
								'shortdesc' => $_POST['shortdesc'],
								'abstract' => $_POST['abstract'],
								'subject' => $_POST['subject'],
								'topic' => $_POST['topic'],
								'copyrights' => $_POST['copyrights'],
								'friendlyurl' => $friendlyurl
					);
					$this->admin_model->add_gallery_language($gal_lang);
				}else{
					$this->admin_model->delete_gallery($this->db->insert_id());
				}
				redirect('/admin/galleries', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];		
					$i++;								
				}
				$data['languages']=array_combine($bufor1,$bufor2);			
				$data['content'] = $this->load->view('admin/elements/add_gallery', $data, true);
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function edit_gallery($id=null, $idlang = 3){
		$allow = $this->check_login('galleries', '', true);		
		if($allow){			
			if(isset($_POST['title'])){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
												
				$gal_lang=array('idgalleries_languages' => $_POST['idgalleries_languages'],
							'idgalleries' => $id,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->update_gallery_language($gal_lang['idgalleries_languages'], $gal_lang);
				redirect('/admin/galleries', 'location');	
			}else{
				$gallery = $this->admin_model->get_gallery_by_id($id,$idlang);
				if(isset($gallery[0])){
					$gallery = $gallery[0];
					$data = array(
							'idgalleries' => $gallery['idgalleries'],
							'active' => $gallery['active'],
							'idgalleries_languages' => $gallery['idgalleries_languages'],
							'idlanguages' => $gallery['idlanguages'],
							'title' => $gallery['title'],
							'content' => $gallery['content'],
							'titleseo' => $gallery['titleseo'],
							'keywords' => $gallery['keywords'],
							'shortdesc' => $gallery['shortdesc'],
							'abstract' => $gallery['abstract'],
							'subject' => $gallery['subject'],
							'topic' => $gallery['topic'],
							'copyrights' => $gallery['copyrights'],
							'friendlyurl' => $gallery['friendlyurl']
						);
					
					//przygotowanie listy jezykow do boxa
					$languages = $this->admin_model->get_languages();
					$bufor1	=	array($gallery['idlanguages']);
					$bufor2	=	array('-----');
					$i = 1;
					foreach($languages as $lang){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
					$data['languages']=array_combine($bufor1,$bufor2);
				}
				$cus = $this->get_menus('galleries');
				$data['menu_poziom'] = $cus['menu_poziom'];
				$data['menu_pion'] =$cus['menu_pion'];		
				
				$data['content'] = $this->load->view('admin/elements/edit_gallery', $data, true);
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function delete_gallery_language($idgal, $idgal_lang){
		$allow = $this->check_login('galleries', '', true);
		if($allow){
			$this->admin_model->delete_gallery_language($idgal_lang);
			$bufor = $this->admin_model->get_gal_lang_versions($idgal);
			if(!isset($bufor[0])){
				$data = $this->admin_model->get_data_to_delete_gallery($idgal);
				if(isset($data[0])){
					foreach($data as $img_lang){
						$this->admin_model->delete_image_language($img_lang['idimages_languages']);
					}
					foreach($data as $img){
						$this->admin_model->delete_image($img['idimages']);
					}
				}
				$this->admin_model->delete_gallery($idgal);
				$this->full_rmdir('./files/img/galleries/'.$idgal);
			}
		}
		redirect('/admin/galleries', 'location');		
	}
	
	function add_gallery_language($id=null, $idlang = 3){
		$allow = $this->check_login('galleries', '', true);
		$data = $this->get_menus('galleries');
		if($allow){			
			if(isset($_POST['title']) && $id != null){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}	
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
												
				$gal_lang=array('idgalleries_languages' => '',
							'idgalleries' => $id,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_gallery_language($gal_lang);
				redirect('/admin/galleries', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$used_langs = $this->admin_model->get_gal_lang_versions($id);
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$flag = 1;
					foreach($used_langs as $used_lang){
						if($lang['idlanguages'] == $used_lang['idlanguages'])
						{
							$flag = 0;
							break;
						}
					}
					if($flag){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
				}
				if($bufor1!=array() && $bufor2!=array()){			
					$data['languages']=array_combine($bufor1,$bufor2);
					$data['id'] = $id;
					$data['idlang'] = $idlang;		
					$data['meta'] = $this->get_standard_metatags('gallery',$idlang);
					$data['content'] = $this->load->view('admin/elements/add_gallery_language', $data, true);
				}else{
					$data['content'] = $this->site['wszystkie_wersje_dostepne'];
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}				
	}

	function edit_gallery_language($idgal_lang, $idgal, $idlang){
		$allow = $this->check_login('galleries', '', true);
		if($allow){			
			if(isset($_POST['title'])){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}	
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
												
				$gal_lang=array('title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->update_gallery_language($idgal_lang, $gal_lang);
				redirect($_SERVER['HTTP_REFERER'], 'location');	
			}else{
				$gallery = $this->admin_model->get_gallery_by_id($idgal,$idlang);
				if(isset($gallery[0])){
					$gallery = $gallery[0];
					$data = array(
							'idgalleries_languages' => $gallery['idgalleries_languages'],
							'active' => $gallery['active'],
							'idlanguages' => $gallery['idlanguages'],
							'title' => $gallery['title'],
							'content' => $gallery['content'],
							'titleseo' => $gallery['titleseo'],
							'keywords' => $gallery['keywords'],
							'shortdesc' => $gallery['shortdesc'],
							'abstract' => $gallery['abstract'],
							'subject' => $gallery['subject'],
							'topic' => $gallery['topic'],
							'copyrights' => $gallery['copyrights'],
							'friendlyurl' => $gallery['friendlyurl']
						);
						
					//przygotowanie listy jezykow do boxa
					$languages = $this->admin_model->get_languages();
					$bufor1	=	array($gallery['idlanguages']);
					$bufor2	=	array('-----');
					$i = 1;
					foreach($languages as $lang){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
					$data['languages']=array_combine($bufor1,$bufor2);
				}
				$data['idgal_lang'] = $idgal_lang;
				$data['idgal'] = $idgal;
				$data['idlang'] = $idlang;		
				
				
				$cus = $this->get_menus('galleries');
				$data['menu_pion'] = $cus['menu_pion'];
				$data['menu_poziom'] = $cus['menu_poziom'];
				$data['content'] = $this->load->view('admin/elements/edit_gallery_language', $data, true);
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}				
	}

	function change_gallery_priority($id){
		$allow = $this->check_login('galleries', '', true);
		if($allow){
			$showorder = $_POST['priority'];
			$data=array( 'showorder' => $showorder);
			$this->admin_model->update_gallery($id, $data);
		}		
		redirect('/admin/galleries', 'location');		
	}

//images
	function show_gallery($id, $idlang){	
		$list['images'] = $this->admin_model->get_gallery_images($id);
		$list['image_languages'] = $this->admin_model->get_img_lang_versions();
		$list['idgalleries']=$id;
		$list['idlang']=$idlang;
		
		$data = $this->get_menus('images');
		$content = $this->load->view('admin/elements/show_gallery', $list, true);
		$data['content'] = $this->check_login('images', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}	
	
	function add_image($idgal, $idlang = 3){
		$allow = $this->check_login('images', '', true);
		$data = $this->get_menus('images');
		if($allow){			
			if(isset($_POST['title']) && isset($_FILES['file']['name']) && $_FILES['file']['name']!=''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				//dodawanie obrazka
				//uploadowanie pliku
				$config['upload_path'] = './files/img/galleries/'.$idgal;
				$config['allowed_types'] = 'gif|jpg|png';
				$config['remove_spaces'] = TRUE;		
				
				$this->load->library('upload', $config);
				$this->upload->do_upload('file');
				$file_data = $this->upload->data();
				//zmiana praw pliku
				chmod('./files/img/galleries/'.$idgal.'/'.$file_data['file_name'], 0777);
				//tworzenie miniaturki
				$img_config['image_library'] = 'gd2';
				$img_config['source_image'] = './files/img/galleries/'.$idgal.'/'.$file_data['file_name'];
				$img_config['new_image'] = './files/img/galleries/'.$idgal.'/min/';
				$img_config['maintain_ratio'] = TRUE;
				$img_config['width'] = $this->gal_img['width'];
				$img_config['height'] = $this->gal_img['height'];
				$this->load->library('image_lib', $img_config);
				$this->image_lib->resize();
				if ( ! $this->image_lib->resize()){
				    echo $this->image_lib->display_errors();
				}	
					
				
				$gal = array('idimages' => '',
							'idgalleries' => $idgal,
							'path'	=> $file_data['file_name'],
							'active' => $active	
				);
				
				$this->admin_model->add_image($gal);				
				$gal_lang=array('idimages_languages' => '',
							'idimages' => $this->db->insert_id(),
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_image_language($gal_lang);
				redirect($_SERVER['HTTP_REFERER'], 'location');	
			}else{
				//przygotowanie metatagów
				$gal_data = $this->admin_model->get_gallery_by_id($idgal, $idlang);
				if(isset($gal_data[0])){
					$data['metatags'] = $gal_data[0];					
				}else{
					$data['metatags'] = array('title' => '',
					'titleseo' => '',
					'artcontent' => '',
					'keywords' => '',
					'shortdesc' => '',
					'abstract' => '',
					'subject' => '',
					'topic' => '',
					'copyrights' => '',
					'friendlyurl' => ''
					);
				}
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				$data['languages'] = array_combine($bufor1,$bufor2);
				$data['idgal'] = $idgal;
				$data['idlang'] = $idlang;				
				$data['meta'] = $this->get_standard_metatags('image',$idlang);
				$data['content'] = $this->load->view('admin/elements/add_img', $data, true);
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function add_multiimage($idgal, $idlang = 3){
		//$allow = $this->check_login('images', '', true);
		//if($allow){		
			//dodawanie obrazka
			move_uploaded_file($_FILES['Filedata']['tmp_name'], './files/img/galleries/'.$idgal.'/'.$_FILES['Filedata']['name']);
			//zmiana praw pliku
			chmod('./files/img/galleries/'.$idgal.'/'.$_FILES['Filedata']['name'], 0777);
			//tworzenie miniaturki
			$img_config['image_library'] = 'GD2';
			$img_config['source_image'] = './files/img/galleries/'.$idgal.'/'.$_FILES['Filedata']['name'];
			$img_config['new_image'] = './files/img/galleries/'.$idgal.'/min/';
			$img_config['maintain_ratio'] = FALSE;
			$img_config['width'] = $this->gal_img['width'];
			$img_config['height'] = $this->gal_img['height'];
			$this->load->library('image_lib', $img_config);
			$this->image_lib->resize();
			if ( ! $this->image_lib->resize()){
			    echo $this->image_lib->display_errors();
			}	
				
			$gal = array('idimages' => '',
						'idgalleries' => $idgal,
						'path'	=> $_FILES['Filedata']['name'],
						'active' => true	
			);
			
			$this->admin_model->add_image($gal);				
			$gal_lang=array('idimages_languages' => '',
						'idimages' => $this->db->insert_id(),
						'idlanguages' => $idlang,
						'title' => '',
						'content' => '',
						'titleseo' => '',
						'keywords' => '',
						'shortdesc' => '',
						'abstract' => '',
						'subject' => '',
						'topic' => '',
						'copyrights' => '',
						'friendlyurl' => ''
			);
			$this->admin_model->add_image_language($gal_lang);
			
		//}					
	}
	
	function add_image_language($id=null, $idgal=null, $idlang = 3){
		$allow = $this->check_login('images', '', true);
		$data = $this->get_menus('images');
		if($allow){			
			if(isset($_POST['title']) && $id != null){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
					
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
												
				$gal_lang=array('idimages_languages' => '',
							'idimages' => $id,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_image_language($gal_lang);
				redirect($_SERVER['HTTP_REFERER'], 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$used_langs = $this->admin_model->get_img_lang_versions_by_id($id);
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$flag = 1;
					foreach($used_langs as $used_lang){
						if($lang['idlanguages'] == $used_lang['idlanguages'])
						{
							$flag = 0;
							break;
						}
					}
					if($flag){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
				}
				if($bufor1!=array() && $bufor2!=array()){			
					$data['languages']=array_combine($bufor1,$bufor2);
					$data['id'] = $id;
					$data['idgal'] = $idgal;	
					$data['idlang'] = $idlang;		
					$data['meta'] = $this->get_standard_metatags('image',$idlang);
					$data['content'] = $this->load->view('admin/elements/add_image_language', $data, true);
				}else{
					$data['content'] = $this->site['wszystkie_wersje_dostepne'];
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}				
	}

	function delete_image_language($idimg_lang, $idimg, $idgal, $idlang=3){
		$allow = $this->check_login('images', '', true);
		if($allow){
			$this->admin_model->delete_image_language($idimg_lang);
			
			$bufor = $this->admin_model->get_img_lang_versions_by_id($idimg);
			//print_r($bufor);
			if(!isset($bufor[0])){
				$image = $this->admin_model->get_image_by_id($idimg);
				if(isset($image[0])){
					$this->admin_model->delete_image($idimg);
					unlink("./files/img/galleries/".$idgal.'/'.$image[0]['path']);
					unlink("./files/img/galleries/".$idgal.'/min/'.$image[0]['path']);
				}
			}
		}				
		redirect('/admin/show_gallery/'.$idgal.'/'.$idlang, 'location');		
	}

	function edit_image_language($idimg_lang, $idgal, $idlang){
		$allow = $this->check_login('images', '', true);		
		if($allow){			
			if(isset($_POST['title'])){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
															
				$gal_lang=array('idimages_languages' => $idimg_lang,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->update_image_language($gal_lang['idimages_languages'], $gal_lang);
				redirect($_SERVER['HTTP_REFERER'], 'location');	
			}else{
				$image = $this->admin_model->get_image_lang_by_id($idimg_lang);
				if(isset($image[0])){
					$image = $image[0];
					$data = array(
							'idimages_languages' => $image['idimages_languages'],
							'active' => $image['active'],
							'idimages_languages' => $image['idimages_languages'],
							'idlanguages' => $image['idlanguages'],
							'title' => $image['title'],
							'content' => $image['content'],
							'titleseo' => $image['titleseo'],
							'keywords' => $image['keywords'],
							'shortdesc' => $image['shortdesc'],
							'abstract' => $image['abstract'],
							'subject' => $image['subject'],
							'topic' => $image['topic'],
							'copyrights' => $image['copyrights'],
							'friendlyurl' => $image['friendlyurl']
						);
									
					//przygotowanie listy jezykow do boxa
					$languages = $this->admin_model->get_languages();
					$bufor1=array($image['idlanguages']);
					$bufor2=array('-----');
					$i = 1;
					foreach($languages as $lang){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
					$data['languages']=array_combine($bufor1,$bufor2);
				}
				$cus = $this->get_menus('images');
				$data['menu_poziom'] = $cus['menu_poziom'];
				$data['menu_pion'] =$cus['menu_pion'];
				$data['idgallery'] = $idgal;	
				$data['idlang'] = $idlang;		
				$data['content'] = $this->load->view('admin/elements/edit_image', $data, true);
				
				$group = $this->session->userdata('group');
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function change_image_priority($id, $idgal){
		$allow = $this->check_login('images', '', true);
		if($allow){
			$showorder = $_POST['priority'];
			$data=array( 'showorder' => $showorder);
			$this->admin_model->update_image($id, $data);
		}		
		redirect($_SERVER['HTTP_REFERER'], 'location');		
	}

//metatagi
	function metatags($idlang = 3){	
		$list['metatags'] = $this->admin_model->get_metatags();
		$list['languages'] = $this->admin_model->get_languages();
		$list['types'] = $this->admin_model->get_metatag_types();
		
		$data = $this->get_menus('metatags');
		$content = $this->load->view('admin/elements/metatags', $list, true);
		$data['content'] = $this->check_login('metatags', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function add_metatag( $idlang = 3){
		$allow = $this->check_login('metatags', '', true);
		$data = $this->get_menus('metatags');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				$data=array('idmetatags' => '',
							'idmetatag_types' => $_POST['idmetatag_types'],
							'idlanguages' => $_POST['idlanguage'],
							'template' => $_POST['name'],
							'title' => $_POST['title'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights']
				);
				$this->admin_model->add_metatags($data);
				unset($_POST);
				redirect('/admin/metatags', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				if($bufor1!=array() && $bufor2!=array()){			
					$data['languages']=array_combine($bufor1,$bufor2);			
				}
				
				//szablony do boxa
				$types = $this->admin_model->get_metatag_types();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($types as $type){
					$bufor1[$i]=$type['idmetatag_types'];
					$bufor2[$i]=$type['name'];
					$i++;
				}
				if($bufor1!=array() && $bufor2!=array()){			
					$data['types']=array_combine($bufor1,$bufor2);			
				}
				
				$data['content'] = $this->load->view('admin/elements/add_metatag', $data, true);
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function edit_metatag($id = null,$idlang = 3){
		$allow = $this->check_login('metatags', '', true);
		$data = $this->get_menus('metatags');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				$data=array('idmetatags' => $id,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights']
				);
				$this->admin_model->update_metatag($id,$data);
				unset($_POST);
				redirect('/admin/metatags', 'location');	
			}else{
				$meta = $this->admin_model->get_metatag_by_id($id);
				$meta = $meta[0];
				$data['metatag'] = array('idmetatags' => $id,
										'idlanguages' => $meta['idlanguages'],
										'title' => $meta['title'],
										'keywords' => $meta['keywords'],
										'shortdesc' => $meta['shortdesc'],
										'abstract' => $meta['abstract'],
										'subject' => $meta['subject'],
										'topic' => $meta['topic'],
										'copyrights' => $meta['copyrights']
								);
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}				
				$data['languages'] = array_combine($bufor1,$bufor2);
				
				$data['id']= $id;
				$data['content'] = $this->load->view('admin/elements/edit_metatag', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function delete_metatags($id = null){
		$allow = $this->check_login('metatags', '', true);
		if($allow){
			if($id != null){
				$this->admin_model->delete_metatags($id);
			}
		}		
		redirect('/admin/metatags', 'location');		
	}

//newses	
	function newses($idlang = 3){	
		$list['newses'] = $this->admin_model->get_newses();
		$list['news_languages'] = $this->admin_model->get_news_languages();
		
		$data = $this->get_menus('newses');
		$content = $this->load->view('admin/elements/newses', $list, true);
		$data['content'] = $this->check_login('newses', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function change_news_priority($id){
		$allow = $this->check_login('newses', '', true);
		if($allow){
			$showorder = $_POST['priority'];
			$data=array( 'showorder' => $showorder);
			$this->admin_model->update_news($id, $data);
		}				
		
		redirect('/admin/newses', 'location');		
	}

	function delete_news_language($idnews, $idnews_language){
		$allow = $this->check_login('newses', '', true);
		if($allow){
			$this->admin_model->delete_news_language($idnews_language);
			$data = $this->admin_model->get_news_languages_by_idnewses($idnews);
			if(!isset($data[0])){
				$photo = $this->admin_model->get_newses_by_id($idnews);
				if(isset($photo[0]['image']) && $photo[0]['image'] != ''){
					unlink('./files/img/newses/'.$photo[0]['image']);
					unlink('./files/img/newses/min/'.$photo[0]['image']);					
				}
				$this->admin_model->delete_news($idnews);	
			}
		}
		redirect('/admin/newses', 'location');		
	}
	
	function add_news($idlang = 3){
		$allow = $this->check_login('newses', '', true);
		$data = $this->get_menus('newses');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				$file_data = array('file_name' => ''); 	
				if(isset($_FILES['file']) && $_FILES['file']['name']!=''){
					//uploadowanie pliku
					$config['upload_path'] = './files/img/newses/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;		
					
					$this->load->library('upload', $config);
					$this->upload->do_upload('file');
					$file_data = $this->upload->data();
					//zmiana praw pliku
					chmod('./files/img/newses/'.$file_data['file_name'], 0777);
					//tworzenie miniaturki
					$img_config['image_library'] = 'gd2';
					$img_config['source_image'] = './files/img/newses/'.$file_data['file_name'];
					$img_config['new_image'] = './files/img/newses/min/';
					$img_config['maintain_ratio'] = TRUE;
					$img_config['width'] = $this->news['width'];
					$img_config['height'] = $this->news['height'];
					$this->load->library('image_lib', $img_config);
					$this->image_lib->resize();
					if ( ! $this->image_lib->resize()){
					    echo $this->image_lib->display_errors();
					}	
				}
				
				$data=array('idnewses' => '',
							'image' => $file_data['file_name'],
							'date' => date('c'),
							'showorder' => '',
							'active' => $active
				);
				$this->admin_model->add_news($data);
				$data=array('idnews_languages' => '',
							'idnewses' => $this->db->insert_id(),
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_news_language($data);
				redirect('/admin/newses', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				$data['languages']=array_combine($bufor1,$bufor2);		
				$data['meta'] = $this->get_standard_metatags('news',$idlang);
				$data['content'] = $this->load->view('admin/elements/add_news', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function add_news_language($id_newses, $idlang = 3){
		$allow = $this->check_login('newses', '', true);
		$data = $this->get_menus('newses');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				
				$data=array('idnews_languages' => '',
							'idnewses' => $id_newses,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_news_language($data);
				redirect('/admin/newses', 'location');	
			}else{
				$data['id_newses'] = $id_newses;
				//przygotowanie listy jezykow do boxa
				//przygotowanie listy jezykow do boxa
				$used_langs = $this->admin_model->get_news_languages_by_idnewses($id_newses);
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$flag = 1;
					foreach($used_langs as $used_lang){
						if($lang['idlanguages'] == $used_lang['idlanguages'])
						{
							$flag = 0;
							break;
						}
					}
					if($flag){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
				}	
				if($bufor1!=array() && $bufor2!=array()){			
					$data['languages']=array_combine($bufor1,$bufor2);		
					$data['meta'] = $this->get_standard_metatags('news',$idlang);
					$data['content'] = $this->load->view('admin/elements/add_news_language', $data, true);	
				}else{
					$data['content'] = $this->site['wszystkie_wersje_dostepne'];
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function edit_news_language($idnews_languages, $idlang = 3){
		if($idnews_languages != null){
			$allow = $this->check_login('newses', '', true);
			$data2 = $this->get_menus('newses');
			if($allow){			
				if(isset($_POST['title']) && $_POST['title'] != ''){
					if($_POST['friendlyurl'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
					}else{
						if($_POST['seotitle'] != ''){
							$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
						}else{
							$friendlyurl = $this->generate_friendlyurl($_POST['title']);
						}
					}
					
					$active = false;
					if(isset($_POST['active'])){
						$active = true;
					}
					if(isset($_FILES) && $_FILES['file']['name']!=''){
						$photo = $this->admin_model->get_newses_by_id($_POST['idnewses']);
						if(isset($photo[0]) && $photo[0]['image']!=''){
							unlink('./files/img/newses/'.$photo[0]['image']);
							unlink('./files/img/newses/min/'.$photo[0]['image']);
						}
						//uploadowanie pliku
						$config['upload_path'] = './files/img/newses/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['remove_spaces'] = TRUE;		
						
						$this->load->library('upload', $config);
						$this->upload->do_upload('file');
						$file_data = $this->upload->data();
						
						//tworzenie miniaturki
						$img_config['image_library'] = 'GD2';
						$img_config['source_image'] = './files/img/newses/'.$file_data['file_name'];
						$img_config['new_image'] = './files/img/newses/min/';
						$img_config['maintain_ratio'] = TRUE;
						$img_config['width'] = $this->news['width'];
						$img_config['height'] = $this->news['height'];
						$this->load->library('image_lib', $img_config);
						$this->image_lib->resize();
						if ( ! $this->image_lib->resize()){
						    echo $this->image_lib->display_errors();
						}	
						$data = array('image'=> $file_data['file_name']);
						$this->admin_model->update_news($_POST['idnewses'], $data);
					}
					
					$data=array('active' => $active);
					$this->admin_model->update_news($_POST['idnewses'], $data);
					
					$data=array('idlanguages' => $_POST['idlanguage'],
								'title' => $_POST['title'],
								'content' => $_POST['artcontent'],
								'titleseo' => $_POST['seotitle'],
								'keywords' => $_POST['keywords'],
								'shortdesc' => $_POST['shortdesc'],
								'abstract' => $_POST['abstract'],
								'subject' => $_POST['subject'],
								'topic' => $_POST['topic'],
								'copyrights' => $_POST['copyrights'],
								'friendlyurl' => $friendlyurl
					);
					$this->admin_model->update_news_language($idnews_languages, $data);
					
					redirect('/admin/newses', 'location');	
				}else{
					//przygotowanie danych
					$news = $this->admin_model->get_news_lang_by_id($idnews_languages);
					if(isset($news[0])){
						$news = $news[0];	
						
						$data = array(	'idnews_languages' => $idnews_languages,
										'idnewses' => $news['idnewses'],
										'title' => $news['title'],
										'content' => $news['content'],
										'titleseo' => $news['titleseo'],
										'keywords' => $news['keywords'],
										'shortdesc' => $news['shortdesc'],
										'abstract' => $news['abstract'],
										'subject' => $news['subject'],
										'topic' => $news['topic'],
										'copyrights' => $news['copyrights'],
										'friendlyurl' => $news['friendlyurl'],
										'active' => $news['active']
									);		
						
						//przygotowanie listy jezykow do boxa
						$languages = $this->admin_model->get_languages();
						$bufor1=array($news['idlanguages']);
						$bufor2=array('---');
						$i = 1;
						foreach($languages as $lang){
							$bufor1[$i]=$lang['idlanguages'];
							$bufor2[$i]=$lang['shortname'];
							$i++;
						}
						$data['languages']=array_combine($bufor1,$bufor2);
						
						
						$data2['content'] = $this->load->view('admin/elements/edit_news_language', $data, true);	
						
						$metatags = $this->get_standard_metatags('admin',$idlang);
						$this->load->view('admin/layout/header', $metatags);
						$this->load->view('admin/layout/admin',$data2);
					}else{
						redirect($_SERVER['HTTP_REFERER'], 'location');
					}
				}
			}		
		}else{
			redirect('/admin/newses', 'location');
		}
	}

//articles	
	function articles($idlang = 3){	
		$list['language'] = $this->set_language($idlang);
		$list['articles'] = $this->admin_model->get_articles();
		
		$list['article_languages'] = $this->admin_model->get_article_languages();
		$list['site'] = $this->site;
		
		//przygotowanie listy jezykow do boxa
		$languages = $this->admin_model->get_languages();
		$i = 0;
		foreach($languages as $lang){
			$bufor1[$i]=$lang['idlanguages'];
			$bufor2[$i]=$lang['fullname'];
			$i++;
		}
		$list['languages'] = array_combine($bufor1,$bufor2);
		
		$data = $this->get_menus('articles');
		$content = $this->load->view('admin/elements/articles', $list, true);
		$data['content'] = $this->check_login('articles', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function change_article_priority($id){
		$allow = $this->check_login('articles', '', true);
		if($allow){
			$showorder = $_POST['priority'];
			$data=array( 'showorder' => $showorder);
			$this->admin_model->update_article($id, $data);
		}				
		redirect('/admin/articles', 'location');		
	}
	
	function add_article($idparent = null, $idlang = 3){
		$allow = $this->check_login('articles', '', true);
		$data = $this->get_menus('articles');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				$file_data = array('file_name' => ''); 	
				if(isset($_FILES['file']) && $_FILES['file']['name']!=''){
					//uploadowanie pliku
					$config['upload_path'] = './files/img/articles/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;		
					
					$this->load->library('upload', $config);
					$this->upload->do_upload('file');
					$file_data = $this->upload->data();
					//zmiana praw pliku
					chmod('./files/img/articles/'.$file_data['file_name'], 0777);
					//tworzenie miniaturki
					$img_config['image_library'] = 'gd2';
					$img_config['source_image'] = './files/img/articles/'.$file_data['file_name'];
					$img_config['new_image'] = './files/img/articles/min/';
					$img_config['maintain_ratio'] = TRUE;
					$img_config['width'] = $this->news['width'];
					$img_config['height'] = $this->news['height'];
					$this->load->library('image_lib', $img_config);
					$this->image_lib->resize();
					if ( ! $this->image_lib->resize()){
					    echo $this->image_lib->display_errors();
					}	
				}				
				$data=array('idarticles' => '',
							'image' => $file_data['file_name'],
							'date' => date('c'),
							'idparent' => $idparent,
							'active' => $active
				);
				$this->admin_model->add_article($data);
				$data=array('idarticle_languages' => '',
							'idarticles' => $this->db->insert_id(),
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'additional_data' => $_POST['additional_data'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_article_language($data);
				redirect('/admin/articles', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				$data['languages']=array_combine($bufor1,$bufor2);
				$data['meta'] = $this->get_standard_metatags('article',$idlang);
				$data['idparent'] = $idparent;				
				$data['content'] = $this->load->view('admin/elements/add_article', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function delete_article_language($idarticle){
		$allow = $this->check_login('articles', '', true);
		if($allow){
			$idarticle_language = $_POST['idarticle_languages'];
			$this->admin_model->delete_article_language($idarticle_language);
			$data = $this->admin_model->get_article_languages_by_idarticles($idarticle);
			if(!isset($data[0])){
				$deleted_article = $this->admin_model->get_article_by_id($idarticle);
				$this->admin_model->delete_article($idarticle);	
				//kasacja fotek
				if(isset($deleted_article[0]) && $deleted_article[0]['image']!=''){
						unlink('./files/img/articles/'.$deleted_article[0]['image']);
						unlink('./files/img/articles/min/'.$deleted_article[0]['image']);
					}
				//zmiana id parenta u potomkow
				$update_data = array( 'idparent' => $deleted_article[0]['idparent'] );
				$this->admin_model->update_change_article_parent($idarticle,$update_data);
			}
		}
		redirect('/admin/articles', 'location');	
	}
	
	function add_article_language($id_articles, $idlang = 3){
		$allow = $this->check_login('articles', '', true);
		$data = $this->get_menus('articles');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				
				$data=array('idarticle_languages' => '',
							'idarticles' => $id_articles,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'additional_data' => $_POST['additional_data'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_article_language($data);
				redirect('/admin/articles', 'location');	
			}else{
				$data['id_articles'] = $id_articles;
				//przygotowanie listy jezykow do boxa
				$used_langs = $this->admin_model->get_article_languages_by_idarticles($id_articles);
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$flag = 1;
					foreach($used_langs as $used_lang){
						if($lang['idlanguages'] == $used_lang['idlanguages'])
						{
							$flag = 0;
							break;
						}
					}
					if($flag){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
				}	
				if($bufor1!=array() && $bufor2!=array()){			
					$data['languages']=array_combine($bufor1,$bufor2);		
					$data['meta'] = $this->get_standard_metatags('article',$idlang);
					$data['content'] = $this->load->view('admin/elements/add_article_language', $data, true);	
				}else{
					$data['content'] = $this->site['wszystkie_wersje_dostepne'];
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function edit_article_language($idlang = 3){
		$allow = $this->check_login('articles', '', true);
		$data2 = $this->get_menus('articles');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$idarticle_languages = $_POST['idarticle_languages'];
				
				if(isset($_FILES) && $_FILES['file']['name']!=''){
					$photo = $this->admin_model->get_article_by_id($_POST['idarticles']);
					if(isset($photo[0]) && $photo[0]['image']!=''){
						unlink('./files/img/articles/'.$photo[0]['image']);
						unlink('./files/img/articles/min/'.$photo[0]['image']);
					}
					//uploadowanie pliku
					$config['upload_path'] = './files/img/articles/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;		
					
					$this->load->library('upload', $config);
					$this->upload->do_upload('file');
					$file_data = $this->upload->data();
					
					//tworzenie miniaturki
					$img_config['image_library'] = 'GD2';
					$img_config['source_image'] = './files/img/articles/'.$file_data['file_name'];
					$img_config['new_image'] = './files/img/articles/min/';
					$img_config['maintain_ratio'] = TRUE;
					$img_config['width'] = $this->news['width'];
					$img_config['height'] = $this->news['height'];
					$this->load->library('image_lib', $img_config);
					$this->image_lib->resize();
					if ( ! $this->image_lib->resize()){
					    echo $this->image_lib->display_errors();
					}	
					$data = array('image'=> $file_data['file_name']);
					$this->admin_model->update_article($_POST['idarticles'], $data);
				}
									
				$data=array('active' => $active);
				$this->admin_model->update_article($_POST['idarticles'], $data);
				
				$data=array('idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'additional_data' => $_POST['additional_data'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->update_article_language($idarticle_languages, $data);
				
				redirect('/admin/articles', 'location');	
			}else{
				$idarticle_languages = $_POST['idarticle_languages'];
				//przygotowanie danych
				$article = $this->admin_model->get_article_lang_by_id($idarticle_languages);
				if(isset($article[0])){
					$article = $article[0];	
					
					$data = array(	'idarticle_languages' => $idarticle_languages,
									'idarticles' => $article['idarticles'],
									'title' => $article['title'],
									'content' => $article['content'],
									'titleseo' => $article['titleseo'],
									'keywords' => $article['keywords'],
									'shortdesc' => $article['shortdesc'],
									'abstract' => $article['abstract'],
									'subject' => $article['subject'],
									'topic' => $article['topic'],
									'copyrights' => $article['copyrights'],
									'friendlyurl' => $article['friendlyurl'],
									'additional_data' => $article['additional_data'],
									'active' => $article['active']
								);		
					
					//przygotowanie listy jezykow do boxa
					$languages = $this->admin_model->get_languages();
					$bufor1=array($article['idlanguages']);
					$bufor2=array('---');
					$i = 1;
					foreach($languages as $lang){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
					$data['languages']=array_combine($bufor1,$bufor2);
					
					$data2['content'] = $this->load->view('admin/elements/edit_article_language', $data, true);	
					
					$metatags = $this->get_standard_metatags('admin',$idlang);
					$this->load->view('admin/layout/header', $metatags);
					$this->load->view('admin/layout/admin',$data2);
				}else{
					redirect('/admin/articles', 'location');
				}
			}
		}	
	}

//menu
	function menus($idlang = 3){
		$list['menus'] = $this->admin_model->get_menus();
		$list['menu_items'] = $this->admin_model->get_menu_items();
		
		$data = $this->get_menus('menus');
		$list['site'] = $this->site;
		
		//przygotowanie listy jezykow do boxa
		$languages = $this->admin_model->get_languages();
		$i = 0;
		foreach($languages as $lang){
			$bufor1[$i]=$lang['idlanguages'];
			$bufor2[$i]=$lang['fullname'];
			$i++;
		}
		$list['languages'] = array_combine($bufor1,$bufor2);
		
		$content = $this->load->view('admin/elements/menus', $list, true);
		$data['content'] = $this->check_login('menus', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function add_menu($idlang = 3){
		$allow = $this->check_login('menus', '', true);
		$data = $this->get_menus('menus');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
						
				$data=array('idmenus' => '',
							'name' => $_POST['title']
				);
				$this->admin_model->add_menu($data);
				redirect('/admin/menus', 'location');
			}else{			
				$data['content'] = $this->load->view('admin/elements/add_menu', '', true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function delete_menu($id){
		$allow = $this->check_login('menus', '', true);
		if($allow){
			$this->admin_model->delete_menu($id);
		}				
		redirect('/admin/menus', 'location');		
	}

	function change_menu_item_priority($id){
		$allow = $this->check_login('menus', '', true);
		if($allow){
			$data=array( 'showorder' => $_POST['priority']);
			$this->admin_model->update_menu_items($id, $data);
		}		
		redirect('/admin/menus', 'location');		
	}
	
	function add_menu_item($idmenus, $idparent=null, $idlang = 3){
		$allow = $this->check_login('menus', '', true);
		$data = $this->get_menus('menus');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != '' && isset($_POST['idlanguage']) && $_POST['idlanguage'] != '' && isset($_POST['idmenu_item_types']) && $_POST['idmenu_item_types'] != ''){
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				if($_POST['friendlyurl'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
					
				$data=array('idmenu_items' => '',
							'idmenus' => $idmenus,
							'idmenu_item_types' => $_POST['idmenu_item_types'],
							'idelement' => $_POST['idelement'],
							'idparent' => $idparent,
							'active' => $active
				);
				$this->admin_model->add_menu_item($data);
				
				$mi_lang=array('idmenu_item_languages' => '',
								'idmenu_items' => $this->db->insert_id(),
								'idlanguages' => $_POST['idlanguage'],
								'friendlyurl' => $friendlyurl,
								'elementname' => $_POST['title']
				);
				$this->admin_model->add_menu_item_language($mi_lang);
				
				redirect('/admin/menus', 'location');	
			}else{		
				$data['idmenus'] = $idmenus;
				$data['idparent'] = $idparent;
				
			//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1[0]='';
				$bufor2[0]='';
				$i = 1;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				$data['languages']=array_combine($bufor1,$bufor2);
			//przygotowanie listy typow do boxa
				$mits = $this->admin_model->get_menu_item_types();
				$bufor1[0]='';
				$bufor2[0]='';
				$i = 1;
				foreach($mits as $mit){
					$bufor1[$i]=$mit['idmenu_item_types'];
					$bufor2[$i]=$mit['type'];
					$i++;
				}
				$data['menu_item_types']=array_combine($bufor1,$bufor2);
				
				$data['content'] = $this->load->view('admin/elements/add_menu_item', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function delete_menu_item_language(){
		$allow = $this->check_login('menus', '', true);
		if($allow){
			$menu_item_language = $this->admin_model->get_menu_item_by_id($_POST['idmenu_item_languages']);
			$this->admin_model->delete_menu_item_language($_POST['idmenu_item_languages']);
			$data = $this->admin_model->get_menu_item_languages($menu_item_language[0]['idmenu_items']);
			if(!isset($data[0])){
				$deleted_menu_items = $this->admin_model->get_menu_item_to_delete($menu_item_language[0]['idmenu_items']);
				$update_data = array( 'idparent' => $deleted_menu_items[0]['idparent'] );
				$children = $this->admin_model->get_menu_item_children($deleted_menu_items[0]['idmenu_items']);
				//zmiana id parenta u potomkow
				$this->admin_model->delete_menu_item($menu_item_language[0]['idmenu_items']);
				foreach($children as $child){
					$this->admin_model->update_menu_items($child['idmenu_items'],$update_data);
				}
				$this->admin_model->delete_menu_item($menu_item_language[0]['idmenu_items']);	
			}
		}
		redirect('/admin/menus', 'location');		
	}
	
	function delete_menu_item($idmenu_item){
		$allow = $this->check_login('menus', '', true);
		if($allow){
			$this->admin_model->delete_menu_item_language_by_idmenu_items($idmenu_item);
			$data = $this->admin_model->get_menu_item_languages($idmenu_item);
			if(!isset($data[0])){
				$deleted_menu_items = $this->admin_model->get_menu_item_to_delete($idmenu_item);
				$update_data = array( 'idparent' => $deleted_menu_items[0]['idparent'] );
				$children = $this->admin_model->get_menu_item_children($deleted_menu_items[0]['idmenu_items']);
				//zmiana id parenta u potomkow
				$this->admin_model->delete_menu_item($idmenu_item);
				foreach($children as $child){
					$this->admin_model->update_menu_items($child['idmenu_items'],$update_data);
				}
				$this->admin_model->delete_menu_item($idmenu_item);	
			}
		}
		redirect('/admin/menus', 'location');		
	}

	function edit_menu_item_language($idlang = 3){
		$allow = $this->check_login('menus', '', true);
		$data = $this->get_menus('menus');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					$friendlyurl = $this->generate_friendlyurl($_POST['title']);
				}
				
				
				$idmenu_items = $_POST['idmenu_items'];
				$data = array( 'active' => $active );
				$this->admin_model->update_menu_items($idmenu_items, $data);
				
				$mi_lang=array(	'friendlyurl' => $friendlyurl,
								'elementname' => $_POST['title']
				);
				$this->admin_model->update_menu_item_languages($_POST['idmenu_item_languages'], $mi_lang);
				
				redirect('/admin/menus', 'location');	
			}else{		
				$menu_item_language = $this->admin_model->get_menu_item_by_id($_POST['idmenu_item_languages']);
				$data['idmenu_item_languages'] = $_POST['idmenu_item_languages'];
				$data['idmenu_items'] = $menu_item_language[0]['idmenu_items'];
				$data['active'] = $menu_item_language[0]['active'];
				$data['idlanguages'] = $menu_item_language[0]['idlanguages'];
				$data['friendlyurl'] = $menu_item_language[0]['friendlyurl'];
				$data['title'] = $menu_item_language[0]['elementname'];
				
				$data['content'] = $this->load->view('admin/elements/edit_menu_item', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function add_menu_item_language($idmenu_item, $idlang = 3){
		$allow = $this->check_login('menus', '', true);
		$data = $this->get_menus('menus');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$mi_lang=array('idmenu_item_languages' => '',
								'idmenu_items' => $idmenu_item,
								'idlanguages' => $_POST['idlanguage'],
								'friendlyurl' => $_POST['friendlyurl'],
								'elementname' => $_POST['title']
				);
				$this->admin_model->add_menu_item_language($mi_lang);
				
				redirect('/admin/menus', 'location');	
			}else{		
				$data['idmenu_item'] = $idmenu_item;
				
				if(isset($_POST['idlanguage']) && $_POST['idlanguage']!=null){	
					$data['languages'] = $_POST['idlanguage'];		
					$data['content'] = $this->load->view('admin/elements/add_menu_item_language', $data, true);	
				}else{
					$data['content'] = $this->site['wszystkie_wersje_dostepne'];
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

//notes
	function notes($idlang = 3){	
		$data = $this->get_menus('notes');	
		$list['notes'] = $this->admin_model->get_notes();
		
		$content = $this->load->view('admin/elements/notes', $list, true);
		$data['content'] = $this->check_login('notes', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}

	function add_note($idlang = 3){
		$allow = $this->check_login('notes', '', true);
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				$data=array('idnotes' => '',				
							'idusers' => $this->session->userdata('idusers'),
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'date' => date('c')
				);
				$this->admin_model->add_note($data);
				redirect('/admin/notes', 'location');	
			}else{				
				$data = $this->get_menus('notes');
				$data['content'] = $this->load->view('admin/elements/add_note', '', true);	

				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}

	function delete_note($idnote){
		$allow = $this->check_login('notes', '', true);
		if($allow){
			$this->admin_model->delete_note($idnote);
		}
		redirect('/admin/notes', 'location');		
	}

//json
	function get_json_data_for_menu($idmenu_item_types, $idlang){
		$bufor1=array();
		$bufor2=array();
		$i = 0;
		
		switch ($idmenu_item_types) {
		    case 4:
		        $languages = $this->admin_model->get_article_languages();
				foreach($languages as $lang){
					if($lang['idlanguages']==$idlang){
						$bufor1[$i]=$lang['idarticles'];
						$bufor2[$i]=$lang['title'];
						$i++;
					}
				}
		        break;
		    case 5:
		        $languages = $this->admin_model->get_news_languages();
				foreach($languages as $lang){
					if($lang['idlanguages']==$idlang){
						$bufor1[$i]=$lang['idnewses'];
						$bufor2[$i]=$lang['title'];
						$i++;
					}
				}
		        break;
		    case 6:
		       $languages = $this->admin_model->get_galleries_languages();
			   	foreach($languages as $lang){
					if($lang['idlanguages']==$idlang){
						$bufor1[$i]=$lang['idgalleries'];
						
						$i++;
					}
				}
		        break;
			case 8:
		        $languages = $this->admin_model->get_product_categories();
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idproduct_categories'];
					$bufor2[$i]=$lang['type'];
					$i++;
				}
		        break;
			case 9:
		        $languages = $this->admin_model->get_product_languages();
				foreach($languages as $lang){
					if($lang['idlanguages']==$idlang){
						$bufor1[$i]=$lang['idproducts'];
						$bufor2[$i]=$lang['title'];
						$i++;
					}
				}
		        break;
			default:
			  $bufor1[$i] = 1;
			  $bufor2[$i] = '---';
			  break; 
		};
		//wczesniej napisany widok-nie zmieniam, bo nie ma sensu dodawac nowy
		if($bufor1 != array() && $bufor2 != array()){
			$data['metatags'] = json_encode(array_combine($bufor1,$bufor2));
		}else{
			$data['metatags'] = json_encode(array());
		}
		$data['metatags'] = json_encode(array_combine($bufor1,$bufor2));
		$this->load->view('admin/elements/metatags_json', $data);
	}

//panel zarzadzania CMSem - prawa
	function show_rights($idlang = 3){
		$data = $this->get_menus();	
		$list['rights'] = $this->admin_model->get_rights();
		$list['groups'] = $this->admin_model->get_user_groups();		
		$list['user_group_elements'] = $this->admin_model->get_user_group_elements();
		
		$content = $this->load->view('admin/elements/rights', $list, true);
		$data['content'] = $this->check_login('rights', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function change_user_group_elements($idelement, $idgroup){
		$allow = $this->check_login('rights', '', true);
		if($allow){
			$exist = $this->admin_model->get_user_group_element($idelement, $idgroup);
			if(isset($exist[0])){
				if($exist[0]['active']){
					//zmiana statusu na false
					$data = array( 'active'=> false);
					$this->admin_model->update_user_group_elements($exist[0]['iduser_group_elements'], $data);
				}else{
					//zmiana statusu na true
					$data = array( 'active'=> true);
					$this->admin_model->update_user_group_elements($exist[0]['iduser_group_elements'], $data);
				}
			}else{
				//utworzenie powiazania grupy z elementem
				$data = array( 'idelements' => $idelement,
								'iduser_groups' => $idgroup,
								'active' => true
							);
				$this->admin_model->add_user_group_elements($data);
			}
		}
		redirect('/admin/rights', 'location');
	}
		
	function change_element_priority($id){
		$allow = $this->check_login('rights', '', true);
		if($allow){
			$data=array( 'showorder' => $_POST['priority']);
			$this->admin_model->update_elements($id, $data);
		}		
		redirect('/admin/rights', 'location');		
	}

//jezyki
	function languages($idlang = 3){
		$data = $this->get_menus('languages');	
		$list['languages'] = $this->admin_model->get_all_languages();
		
		$content = $this->load->view('admin/elements/languages', $list, true);
		$data['content'] = $this->check_login('languages', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function change_language_priority($id){
		$allow = $this->check_login('languages', '', true);
		if($allow){
			$data=array( 'showorder' => $_POST['priority']);
			$this->admin_model->update_languages($id, $data);
		}		
		redirect('/admin/languages', 'location');		
	}
	
	function add_language($idlang = 3){
		$allow = $this->check_login('languages', '', true);
		if($allow){			
			if(isset($_POST['fullname']) && $_POST['fullname'] != ''){
				$data=array('idlanguages' => '',				
							'fullname' => $_POST['fullname'],
							'shortname' => $_POST['shortname']
				);
				$this->admin_model->add_language($data);
				redirect('/admin/languages', 'location');	
			}else{			
				$data = $this->get_menus('languages');
				$data['content'] = $this->load->view('admin/elements/add_language', '', true);	

				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function edit_language($idlang_to_edit, $idlang = 3){
		$allow = $this->check_login('languages', '', true);
		if($allow){			
			if(isset($_POST['fullname']) && $_POST['fullname'] != ''){
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$data=array('fullname' => $_POST['fullname'],
							'active' => $active,
							'shortname' => $_POST['shortname']
				);
				$this->admin_model->update_languages($idlang_to_edit, $data);
				redirect('/admin/languages', 'location');	
			}else{			
				$data = $this->get_menus('languages');				
				$languages = $this->admin_model->get_language_by_id($idlang_to_edit);
				if(isset($languages[0])){
					$data['content'] = $this->load->view('admin/elements/edit_language', $languages[0], true);	
				}else{
					$data['content'] = 'Nie ma takiego jezyka.';
				}
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
//generowanie sitemap.xml	
	function generate_sitemap(){
		$allow = $this->check_login('sitemap', '', true);
		if($allow){
			$this->load->helper('file');

			$newline = chr(10);
			$tab = chr(9);
			$twotabs = $tab.$tab;


			$data = '<?xml version="1.0" encoding="UTF-8"?>'.$newline;
			$data .= '<urlset>'.$newline;
				//$data .= $tab.'<url>'.$newline;
				//	$data .= $twotabs.'<loc>'.base_url().'</loc>'.$newline;
				//	$data .= $twotabs.'<changefreq>daily</changefreq>'.$newline;
				//	$data .= $twotabs.'<priority>1.0</priority>'.$newline;
				//$data .= $tab.'</url>'.$newline;
			//tu beda wszystkie foreachy
			//przejscie po jezykach
                $printed_articles = array();
                $indeks_printed_articles = 0;
                $articles = $this->admin_model->get_menu_items();
				$languages = $this->admin_model->get_languages();

				foreach($languages as $language){
					$site = $this->config->item($language['shortname']);
					$short = strtolower($language['shortname']);
                    foreach($articles as $article){
                        if($article['shortname'] == $language['shortname']){
                            $link = '';
                            switch($article['type']){
                                case 'article':
                                    $link = $twotabs.'<loc>'.base_url().$short.'/'.$site['linki_do_sitemapy']['artykul'].'/'.$article['friendlyurl'].'-'.$article['idelement'].'.html</loc>'.$newline;
                                    break;
                                case 'gallery':
                                    $link = $twotabs.'<loc>'.base_url().$short.'/'.$site['linki_do_sitemapy']['galeria'].'/'.$article['friendlyurl'].'-'.$article['idelement'].'.html</loc>'.$newline;
                                    break;
                                case 'news':
                                    $link = $twotabs.'<loc>'.base_url().$short.'/'.$site['linki_do_sitemapy']['news'].'/'.$article['friendlyurl'].'-'.$article['idelement'].'.html</loc>'.$newline;
                                    break;
                                case 'none':
                                    if($article['friendlyurl']!=''){
                                        $link = $twotabs.'<loc>'.base_url().$short.'/'.$article['friendlyurl'].'</loc>'.$newline;
                                    }else{
                                        $link = $twotabs.'<loc>'.base_url().'</loc>'.$newline;
                                    }
                                    break;
                                case 'product_category':
                                    $link = $twotabs.'<loc>'.base_url().$short.'/'.$site['linki_do_sitemapy']['artykul'].'/'.$article['friendlyurl'].'.html</loc>'.$newline;
                                    break;
                                case 'product':
                                    $link = $twotabs.'<loc>'.base_url().$short.'/'.$site['linki_do_sitemapy']['produkt'].'/'.$article['friendlyurl'].'-'.$article['idelement'].'.html</loc>'.$newline;
                                    break;
                            }

                            $flag = true;
                            foreach($printed_articles as $printed_article){
                                if($link == $printed_article){
                                    $flag = false;
                                    break;
                                }
                            }

                            if($flag){
                               $data .= $tab.'<url>'.$newline;
                                    $data .= $link;
                                    $data .= $twotabs.'<changefreq>daily</changefreq>'.$newline;
                                    if($article['friendlyurl'] == ''){
                                        $data .= $twotabs.'<priority>1.0</priority>'.$newline;
                                    }else{
                                        $data .= $twotabs.'<priority>0.9</priority>'.$newline;
                                    }
                              $data .= $tab.'</url>'.$newline;
                              $printed_articles[$indeks_printed_articles] = $link;
                              $indeks_printed_articles++;
                            }
                        }
                    }
                }
			$data .= '</urlset>';

		//zapisanie do pliku i potwierdzenie wykonania mapy strony
			$wyswietl = $this->get_menus();
			if ( ! write_file('./files/sitemap.xml', $data, 'w'))
			{
				$wyswietl['content'] = 'Nie udało się zapisanie nowej mapy strony.';
			}
			else
			{
				if (!copy('./files/sitemap.xml','./sitemap.xml')) {
				    echo 'Kopiowanie mapy strony zakońzyło się niepowodzeniem.';
				}
				$wyswietl['content'] = 'Pomyślnie wygenerowano mapę strony.';
			}
		//wyswietlenie potwierdzenia
			$metatags = $this->get_standard_metatags('admin','3');
			$this->load->view('admin/layout/header', $metatags);
			$this->load->view('admin/layout/admin',$wyswietl);
		}
	}
//generowanie .htaccess
	function generate_htaccess(){
		$allow = $this->check_login('htaccess', '', true);
		if($allow){			
			$this->load->helper('file');
			$newline = chr(10);
			$tab = chr(9);
			$data = '<IfModule mod_rewrite.c>'.$newline;
			$data .= $tab.'RewriteEngine On'.$newline;
			$data .= $tab.'RewriteCond $1 !^(index\.php|images|files|robots\.txt)'.$newline;
			//przejscie po jezykach
			$languages = $this->admin_model->get_languages();
			foreach($languages as $language){	
				$site = $this->config->item($language['shortname']);
				$short = strtolower($language['shortname']);
				$data .= $tab.'RewriteRule ^'.$short.'$ /index.php/frontside/index/'.$language['idlanguages'].' [L]'.$newline;
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['artykuly'].'.html$ /index.php/frontside/articles/'.$language['idlanguages'].' [L]'.$newline;
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['artykuly'].'.html$ /index.php/frontside/articles/'.$language['idlanguages'].' [L]'.$newline;				
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['artykul'].'/(.*)\-(.+)\.html$ /index.php/frontside/show_article/$2/'.$language['idlanguages'].' [L]'.$newline;	
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['newsy'].'.html$ /index.php/frontside/newses/'.$language['idlanguages'].' [L]'.$newline;			
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['news'].'/(.*)\-(.+)\.html$ /index.php/frontside/show_news/$2/'.$language['idlanguages'].' [L]'.$newline;			
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['galerie'].'.html$ /index.php/frontside/galleries/'.$language['idlanguages'].' [L]'.$newline;	
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['galeria'].'-(.+)\.html$ /index.php/frontside/show_gallery/$1/'.$language['idlanguages'].' [L]'.$newline;			
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['zdjecie'].'/(.*)\-(.+)\.html$ frontside/show_image/$2/'.$language['idlanguages'].' [L]'.$newline;
				$data .= $tab.'RewriteRule ^'.$short.'/aktualnosci.html$ /index.php/frontside/newses/3 [L]'.$newline;		
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['strona_glowna'].'.html$ /index.php/ [L]'.$newline;
				$data .= $tab.'RewriteRule ^'.$short.'/'.$site['linki_do_sitemapy']['kontakt'].'.html$ /index.php/frontside/send_contact/'.$language['idlanguages'].' [L]'.$newline;
			}
			$data .= $tab.'RewriteRule ^admin(.*)$ /index.php/admin$1 [L]'.$newline;
			$data .= $tab.'RewriteRule ^kontakt.html$ /index.php/frontside/send_contact [L]'.$newline;
			$data .= $tab.'RewriteRule ^frontside(.*)$ /index.php/frontside$1 [L]'.$newline;
			$data .= $tab.'RewriteRule ^files/sitemap.xml$ /index.php/files/sitemap.xml [L]'.$newline;
			$data .= $tab.'ErrorDocument 404 /index.php/'.$newline;
    		$data .= '</IfModule>';
		//zapisanie do pliku i potwierdzenie wykonania mapy strony
			$wyswietl = $this->get_menus();
			if ( ! write_file('./files/htaccess/.htaccess', $data, 'w'))
			{				
				$wyswietl['content'] = 'Nie udało się zapisanie nowego htaccessa.';
			}
			else
			{	
				//chmod('./.htaccess',0777);
				//unlink('./.htaccess');
				if (!copy('./files/htaccess/.htaccess','./.htaccess')) {
				    echo "Kopiowanie htaccessa zakońzyło się niepowodzeniem.";
				}
				$wyswietl['content'] = 'Pomyślnie wygenerowano htaccessa.';
			}
		//wyswietlenie potwierdzenia
			$metatags = $this->get_standard_metatags('admin','3');
			$this->load->view('admin/layout/header', $metatags);
			$this->load->view('admin/layout/admin',$wyswietl);
		}					
	}
//backup bazy
	function backup_database(){
		$allow = $this->check_login('backup_database', '', true);
		if($allow){		
			$this->load->dbutil();
			$this->load->helper('file');	
			$filname = date('YndHis').'.zip';
			$prefs = array('tables' => array(	'languages',
												'user_groups',
												'elements',
												'user_group_elements',
												'users',
												'notes',
												'metatag_types',
												'metatags',
												'newses',
												'news_languages',
												'articles',
												'article_languages',
												'galleries',
												'galleries_languages',
												'images',
												'images_languages',
												'menu_item_types',
												'menus',
												'menu_items',
												'mit_languages',
												'menu_item_languages',												
												'products',
												'subproducts',
												'products_languages',
												'product_categories',
												'product_product_categories',
												'product_categories_languages'
											),
			                'format'      => 'zip',
			                'filename'    => $filname
			              );
			$backup =& $this->dbutil->backup($prefs); 
			
			//zapisanie do pliku i potwierdzenie wykonania mapy strony
			$wyswietl = $this->get_menus();
			if ( ! write_file('./files/dbbackups/'.$filname, $backup, 'w'))
			{
				$wyswietl['content'] = 'Utworzenie kopii bazy danych nie powiodło się.';
			}
			else
			{
				$wyswietl['content'] = 'Pomyślnie utworzono kopię bazy danych.';
			}
		//wyswietlenie potwierdzenia
			$metatags = $this->get_standard_metatags('admin','3');
			$this->load->view('admin/layout/header', $metatags);
			$this->load->view('admin/layout/admin',$wyswietl);
		}					
	}

//produkty
	function products($idlang = 3){	
		$list['language'] = $this->set_language($idlang);
		$list['products'] = $this->admin_model->get_products();
		$list['product_categories'] = $this->admin_model->get_product_categories();
		$list['product_languages'] = $this->admin_model->get_product_languages();
		$list['categories_languages'] = $this->admin_model->get_product_categories_languages();
		$list['children'] = $this->admin_model->get_product_children();
		$list['site'] = $this->site;
		//przygotowanie listy gallerii
		$galleries = $this->admin_model->get_galleries();
		$galleries_languages = $this->admin_model->get_galleries_languages();
		$list['galleries'] = array();
		foreach($galleries as $gallery){
			foreach($galleries_languages as $galleries_language){
				if($gallery['idgalleries'] == $galleries_language['idgalleries']){
					$list['galleries'][$gallery['idgalleries']] = $galleries_language['title'];
					break;
				}
			}
		}
		//przygotowanie listy podproduktów
		$list['subproducts'] = $this->admin_model->get_products_by_cat_name('Elementy');
		//przygotowanie listy jezykow do boxa
		$languages = $this->admin_model->get_languages();
		$i = 0;
		foreach($languages as $lang){
			$bufor1[$i]=$lang['idlanguages'];
			$bufor2[$i]=$lang['fullname'];
			$i++;
		}
		$list['languages'] = array_combine($bufor1,$bufor2);
		$data = $this->get_menus('products');
		$content = $this->load->view('admin/elements/products', $list, true);
		$data['content'] = $this->check_login('products', $content);
		
		$metatags = $this->get_standard_metatags('admin',$idlang);
		$this->load->view('admin/layout/header', $metatags);
		$this->load->view('admin/layout/admin',$data);
	}
	
	function change_product_price($id){
		$allow = $this->check_login('products', '', true);
		if($allow){
			$data=array( 'price' => $_POST['priority']);
			$this->admin_model->update_product($id, $data);
		}				
		redirect('/admin/products', 'location');		
	}
	
	function change_complet_price($idparent, $idchild){
		$allow = $this->check_login('products', '', true);
		if($allow){
			$data=array( 'complet_price' => $_POST['priority']);
			$this->admin_model->update_subproduct($idparent, $idchild, $data);
		//	echo 'boom';
		}				
		redirect('/admin/products', 'location');		
	}
	
	function add_product($idlang = 3){
		$allow = $this->check_login('products', '', true);
		$data = $this->get_menus('products');
				
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				$file_data = array('file_name' => ''); 	
				if(isset($_FILES['file']) && $_FILES['file']['name']!=''){
					//uploadowanie pliku
					$config['upload_path'] = './files/img/products/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;		
					
					$this->load->library('upload', $config);
					$this->upload->do_upload('file');
					$file_data = $this->upload->data();
					//zmiana praw pliku
					chmod('./files/img/products/'.$file_data['file_name'], 0777);
					//tworzenie miniaturki
					$img_config['image_library'] = 'gd2';
					$img_config['source_image'] = './files/img/products/'.$file_data['file_name'];
					$img_config['new_image'] = './files/img/products/min/';
					$img_config['maintain_ratio'] = TRUE;
					$img_config['width'] = $this->product['width'];
					$img_config['height'] = $this->product['height'];
					$this->load->library('image_lib', $img_config);
					$this->image_lib->resize();
					if ( ! $this->image_lib->resize()){
					    echo $this->image_lib->display_errors();
					}	
				}				
				$data=array('idproducts' => '',
							'name' => $_POST['title'],
							'photo' => $file_data['file_name'],
							'price' => $_POST['price'],
							'active' => $active
				);
				$this->admin_model->add_product($data);
				$idadded_product = $this->db->insert_id();
				$data=array('idproducts_languages' => '',
							'idproducts' => $idadded_product,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],							
							'additional_data' => $_POST['additional_data'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_product_language($data);
				
				$subproducts = $this->admin_model->get_products_by_cat_name('Elementy');
				foreach($subproducts as $subproduct){
					if(isset($_POST[$subproduct['name']])){
						$data = array(	'idsubproducts' => '',
										'idparent' => $idadded_product,
										'idchild' => $subproduct['idproducts']
									);
						$this->admin_model->add_subproduct($data);
					}
				}
				
				$data = array(	'idproduct_product_categories' => '',
								'idproduct_categories' => $_POST['idcategory'],
								'idproducts' => $idadded_product
							);
				$this->admin_model->add_product_product_category($data);
						
				redirect('/admin/products', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				
				$data['languages']=array_combine($bufor1,$bufor2);
				
				//przygotowanie listy kategorii
				$categories = $this->admin_model->get_product_categories();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($categories as $category){
					$bufor1[$i]=$category['idproduct_categories'];
					$bufor2[$i]=$category['type'];
					$i++;
				}
				$data['categories']=array_combine($bufor1,$bufor2);
				
				//przygotowanie listy podproduktów
				$data['subproducts'] = $this->admin_model->get_products_by_cat_name('Elementy');
				
				//$data['meta'] = $this->get_standard_metatags('article',$idlang);
				$data['content'] = $this->load->view('admin/elements/add_product', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function delete_product_language($idproduct){
		$allow = $this->check_login('products', '', true);
		if($allow){
			$idproduct_languages = $_POST['idproduct_languages'];
			$this->admin_model->delete_product_language($idproduct_languages);
			$data = $this->admin_model->get_product_languages_by_idproducts($idproduct);
			if(!isset($data[0])){
				$deleted_product = $this->admin_model->get_product_by_id($idproduct);
				$this->admin_model->delete_subproduct_by_idparent($idproduct);	
				$this->admin_model->delete_subproduct_by_idchild($idproduct);	
				$this->admin_model->delete_product_product_category_by_idproduct($idproduct);	
				$this->admin_model->delete_product($idproduct);	
				//kasacja fotek
				if(isset($deleted_product[0])){
					if($deleted_product[0]['photo']!=''){
						unlink('./files/img/products/'.$deleted_product[0]['photo']);
						unlink('./files/img/products/min/'.$deleted_product[0]['photo']);
					}
				}
			}
		}
		redirect('/admin/products', 'location');	
	}
	
	function add_product_language($idproduct, $idlang = 3){
		$allow = $this->check_login('products', '', true);
		$data = $this->get_menus('products');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				
				$data=array('idproducts_languages' => '',
							'idproducts' => $idproduct,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],
							'additional_data' => $_POST['additional_data'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_product_language($data);
				redirect('/admin/products', 'location');	
			}else{
				$data['id_articles'] = $idproduct;
				//przygotowanie listy jezykow do boxa
				$used_langs = $this->admin_model->get_product_languages_by_idproducts($idproduct);
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$flag = 1;
					foreach($used_langs as $used_lang){
						if($lang['idlanguages'] == $used_lang['idlanguages'])
						{
							$flag = 0;
							break;
						}
					}
					if($flag){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
				}	
				if($bufor1!=array() && $bufor2!=array()){			
					$data['languages']=array_combine($bufor1,$bufor2);		
					$data['meta'] = $this->get_standard_metatags('product',$idlang);
					$data['content'] = $this->load->view('admin/elements/add_product_language', $data, true);	
				}else{
					$data['content'] = $this->site['wszystkie_wersje_dostepne'];
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function edit_product_language($idlang = 3){
		$allow = $this->check_login('products', '', true);
		$data2 = $this->get_menus('products');
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
				
				$idarticle_languages = $_POST['idarticle_languages'];
				
				if(isset($_FILES) && $_FILES['file']['name']!=''){
					$photo = $this->admin_model->get_product_by_id($_POST['idarticles']);
					if(isset($photo[0]) && $photo[0]['photo']!=''){
						unlink('./files/img/products/'.$photo[0]['photo']);
						unlink('./files/img/products/min/'.$photo[0]['photo']);
					}
					//uploadowanie pliku
					$config['upload_path'] = './files/img/products/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;		
					
					$this->load->library('upload', $config);
					$this->upload->do_upload('file');
					$file_data = $this->upload->data();
					
					//tworzenie miniaturki
					$img_config['image_library'] = 'GD2';
					$img_config['source_image'] = './files/img/products/'.$file_data['file_name'];
					$img_config['new_image'] = './files/img/products/min/';
					$img_config['maintain_ratio'] = TRUE;
					$img_config['width'] = $this->product['width'];
					$img_config['height'] = $this->product['height'];
					$this->load->library('image_lib', $img_config);
					$this->image_lib->resize();
					if ( ! $this->image_lib->resize()){
					    echo $this->image_lib->display_errors();
					}	
					$data = array('photo'=> $file_data['file_name']);
					$this->admin_model->update_product($_POST['idarticles'], $data);
				}
									
				$data=array('active' => $active);
				$this->admin_model->update_product($_POST['idarticles'], $data);
				
				$data=array('idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],							
							'additional_data' => $_POST['additional_data'],
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->update_product_language($idarticle_languages, $data);
				
				redirect('/admin/products', 'location');	
			}else{
				$idarticle_languages = $_POST['idproduct_languages'];
				//przygotowanie danych
				$article = $this->admin_model->get_product_lang_by_id($idarticle_languages);
				if(isset($article[0])){
					$article = $article[0];	
					
					$data = array(	'idarticle_languages' => $idarticle_languages,
									'idarticles' => $article['idproducts'],
									'title' => $article['title'],
									'content' => $article['content'],
									'titleseo' => $article['titleseo'],
									'keywords' => $article['keywords'],
									'shortdesc' => $article['shortdesc'],
									'abstract' => $article['abstract'],
									'subject' => $article['subject'],
									'topic' => $article['topic'],
									'copyrights' => $article['copyrights'],
									'friendlyurl' => $article['friendlyurl'],
									'additional_data' => $article['additional_data'],
									'active' => $article['active']
								);		
					
					//przygotowanie listy jezykow do boxa
					$languages = $this->admin_model->get_languages();
					$bufor1=array($article['idlanguages']);
					$bufor2=array('---');
					$i = 1;
					foreach($languages as $lang){
						$bufor1[$i]=$lang['idlanguages'];
						$bufor2[$i]=$lang['shortname'];
						$i++;
					}
					$data['languages']=array_combine($bufor1,$bufor2);
					
					$data2['content'] = $this->load->view('admin/elements/edit_product_language', $data, true);	
					
					$metatags = $this->get_standard_metatags('admin',$idlang);
					$this->load->view('admin/layout/header', $metatags);
					$this->load->view('admin/layout/admin',$data2);
				}else{
					redirect('/admin/products', 'location');
				}
			}
		}	
	}

	function set_subproducts($idparent){
		$allow = $this->check_login('products', '', true);
		if($allow){
			//print_r($_POST);
			$subproducts = $this->admin_model->get_products_by_cat_name('Elementy');
			//print_r($subproducts);
				foreach($subproducts as $subproduct){
					if(isset($_POST[$subproduct['name']])){
						$isInDB = $this->admin_model->get_subproduct_by_parent_by_child($idparent,$subproduct['idproducts']);
						if(!isset($isInDB[0])){
							$data = array(	'idsubproducts' => '',
											'idparent' => $idparent,
											'idchild' => $subproduct['idproducts']
										);
							$this->admin_model->add_subproduct($data);
						}
					}else{
						$isInDB = $this->admin_model->get_subproduct_by_parent_by_child($idparent,$subproduct['idproducts']);
						if(isset($isInDB[0])){
							$this->admin_model->delete_subproduct_by_parent_by_child($idparent,$subproduct['idproducts']);
						}
					}
				}
		}				
		redirect('/admin/products', 'location');
	}

	function add_product_category($idlang = 3){
		$allow = $this->check_login('products', '', true);
		$data = $this->get_menus('products');
				
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}		
				$data=array('idproduct_categories' => '',
							'type' => $_POST['title']
				);
				$this->admin_model->add_product_category($data);
				$idadded_product = $this->db->insert_id();
				$data=array('idproduct_categories_languages' => '',
							'idproduct_categories' => $idadded_product,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],			
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_product_category_language($data);	
				
				redirect('/admin/products', 'location');	
			}else{
				//przygotowanie listy jezykow do boxa
				$languages = $this->admin_model->get_languages();
				$bufor1=array();
				$bufor2=array();
				$i = 0;
				foreach($languages as $lang){
					$bufor1[$i]=$lang['idlanguages'];
					$bufor2[$i]=$lang['shortname'];
					$i++;
				}
				
				$data['languages']=array_combine($bufor1,$bufor2);
				
				//$data['meta'] = $this->get_standard_metatags('article',$idlang);
				$data['content'] = $this->load->view('admin/elements/add_product_category', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function add_product_category_language($id, $idlang = 3){
		$allow = $this->check_login('products', '', true);
		$data = $this->get_menus('products');
				
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}	
				$data=array('idproduct_categories_languages' => '',
							'idproduct_categories' => $id,
							'idlanguages' => $_POST['idlanguage'],
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],			
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->add_product_category_language($data);	
				
				redirect('/admin/products', 'location');	
			}else{
				$data['idproduct_categories'] = $id;
				$data['idlanguage'] = $_POST['idlanguage'];
				//$data['meta'] = $this->get_standard_metatags('article',$idlang);
				$data['content'] = $this->load->view('admin/elements/add_prod_cat_language', $data, true);	
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function edit_product_category_language($idlang = 3){
		$allow = $this->check_login('products', '', true);
		$data = $this->get_menus('products');
				
		if($allow){			
			if(isset($_POST['title']) && $_POST['title'] != ''){
				if($_POST['friendlyurl'] != ''){
					$friendlyurl = $this->generate_friendlyurl($_POST['friendlyurl']);
				}else{
					if($_POST['seotitle'] != ''){
						$friendlyurl = $this->generate_friendlyurl($_POST['seotitle']);
					}else{
						$friendlyurl = $this->generate_friendlyurl($_POST['title']);
					}
				}
				$active = false;
				if(isset($_POST['active'])){
					$active = true;
				}
					
				$id = $_POST['idproduct_categories_languages'];
				$data=array(
							'title' => $_POST['title'],
							'content' => $_POST['artcontent'],
							'titleseo' => $_POST['seotitle'],
							'keywords' => $_POST['keywords'],
							'shortdesc' => $_POST['shortdesc'],
							'abstract' => $_POST['abstract'],
							'subject' => $_POST['subject'],
							'topic' => $_POST['topic'],
							'copyrights' => $_POST['copyrights'],			
							'friendlyurl' => $friendlyurl
				);
				$this->admin_model->update_product_category_language($id, $data);	
				
				redirect('/admin/products', 'location');	
			}else{
				$id = $_POST['idproduct_categories_languages'];
				$data['idproduct_categories'] = $id;
				$categories = $this->admin_model->get_product_category_language($id);
				if(isset($categories[0])){
					$data['content'] = $this->load->view('admin/elements/edit_prod_cat_language', $categories[0], true);		
				}else{
					$data['content'] = 'Wystapił błąd. Przepraszamy';
				}
				
				$metatags = $this->get_standard_metatags('admin',$idlang);
				$this->load->view('admin/layout/header', $metatags);
				$this->load->view('admin/layout/admin',$data);
			}
		}					
	}
	
	function delete_product_category_language($idcategory){
		$allow = $this->check_login('products', '', true);
		if($allow){
			$idcategory_languages = $_POST['idproduct_languages'];
			$this->admin_model->delete_prod_cat_language($idcategory_languages);
			$data = $this->admin_model->get_prod_cat_languages_by_idcategory($idcategory);
			if(!isset($data[0])){
				$deleted_categories = $this->admin_model->get_prod_prod_cat_idcategory($idcategory);
				
				if(isset($deleted_categories[0])){
					foreach($deleted_categories as $deleted_category){
						$deleted_product = $this->admin_model->get_product_by_id($idproduct);
						$this->admin_model->delete_subproduct_by_idparent($deleted_category['idproduct']);	
						$this->admin_model->delete_subproduct_by_idchild($deleted_category['idproduct']);	
						$this->admin_model->delete_product_product_category_by_idproduct($deleted_category['idproduct']);
						$this->admin_model->delete_product_languages_by_idproduct($deleted_category['idproduct']);	
						$this->admin_model->delete_product($deleted_category['idproduct']);	
						if(isset($deleted_product[0])){
							if($deleted_product[0]['photo']!=''){
								unlink('./files/img/products/'.$deleted_product[0]['photo']);
								unlink('./files/img/products/min/'.$deleted_product[0]['photo']);
							}
						}
					}
				}
				$this->admin_model->delete_product_category($idcategory);	
			}
		}
		redirect('/admin/products', 'location');	
	}

	function change_product_gallery($id){
		$allow = $this->check_login('products', '', true);
		if($allow){
			$data=array( 'idgalleries' => $_POST['idgallery']);
			$this->admin_model->update_product($id, $data);
		}		
		redirect('/admin/products', 'location');		
	}
}
?>