<?php



if(login_control()) {

	

	if($user["user_authority"] == 2) {



		if(!permission('access', 'appearence')){

			$page_title = "Yetkisiz Erişim";

	        require admin_view("unauthorized");

	        exit();

	    }



	    if(route(2) == "announcements"){



	    	if(!permission('access', 'announcements')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



		    if(!route(3)):



		    	$duyuru = $db->from('duyuru')->orderby('id', 'DESC')->all();

	    		$title = "Duyurular";

				$page_title = "Duyurular";



			elseif(route(3) == "edit"):



				if(route(4)):



					$id = route(4);

					$duyuru = $db->from('duyuru')->where('id', $id)->first();



					if($duyuru):



	    				$title = "Duyuru Düzenle"; 

						$page_title = "Duyuru Düzenle";



					else:



						header('Location:'. admin_url('appearence/announcements'));



					endif;



				else:



					header('Location:'. admin_url('appearence/announcements'));



				endif;



			elseif(route(3) == "add"):



				$title = "Duyuru Ekle";

				$page_title = "Duyuru Ekle";



			endif;

			

		}elseif(route(2) == "blog"){



			if(!permission('access', 'blog')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



			if(!route(3)):



		    	$blog = $db->from('blog')->orderby('id', 'DESC')->all();

	    		$title = "Bloglar";

				$page_title = "Bloglar";



			elseif(route(3) == "edit"):



				if(route(4)):



					$id = route(4);

					$blog = $db->from('blog')->where('id', $id)->first();



					if($blog):



	    				$title = "Blog Düzenle";

						$page_title = "Blog Düzenle";



					else:



						header('Location:'. admin_url('appearence/blog'));



					endif;



				else:



					header('Location:'. admin_url('appearence/blog'));



				endif;



			elseif(route(3) == "add"):



				$title = "Blog Ekle";

				$page_title = "Blog Ekle";



			endif;

			

		}elseif(route(2) == "fields"){



			if(!permission('access', 'fields')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



		    if(!route(3)):



		    	$fields = $db->from('announcements')->orderby('announcement_id', 'DESC')->all();

	    		$title = "Özel Alanlar";

				$page_title = "Özel Alanlar";



			elseif(route(3) == "edit"):



				if(route(4)):



					$id = route(4);

					$fields = $db->from('announcements')->where('announcement_id', $id)->first();



					if($fields):



						$announcement_content = json_decode($fields["announcement_content"], true);

						$name = null;

						$languages = [];

						foreach($announcement_content as $details) {

							$languages[$details["lang"]] = $details["content"];

						}

	    				$title = "Özel Alan Düzenle";

						$page_title = "Özel Alan Düzenle";



					else:



						header('Location:'. admin_url('appearence/fields'));



					endif;



				else:



					header('Location:'. admin_url('appearence/fields'));



				endif;



			endif;

			

		}elseif(route(2) == "links"){



			if(!permission('access', 'links')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



			$title = "Linkler";

			$page_title = "Linkler";

			

		}elseif(route(2) == "menus"){



			if(!permission('access', 'menus')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }

		    $menus = $db->from('menus')->orderby('menu_line', 'ASC')->all();

			$title = "Menüler";

			$page_title = "Menüler";

			

		}elseif(route(2) == "files"){



			if(!permission('access', 'files')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

			}

			if(route(3)){

				$image = route(4);
				if($image){

					unlink(dir.'/mp-public/uploads/'.$image.'.png');
					header('Location:'.admin_url('appearence/files'));

				}else{

					header('Location:'.admin_url('appearence/files'));

				}

			}else{

				if(@$_FILES['logo'] && ($_FILES['logo']['type'] == "image/jpeg" || $_FILES["logo"]["type"] == "image/jpg" || $_FILES["logo"]["type"] == "image/png" || $_FILES["logo"]["type"] == "image/gif")){


					@$filename 		= $_FILES['logo']['name'];
					@$uzanti		= substr($filename, -4, 4);
					@$new_filename	= "mp-public/uploads/".md5(rand(10,999)).".png";
					@$upload_logo   = move_uploaded_file($_FILES["logo"]["tmp_name"],$new_filename);

				}

				$dosyalar = scandir(dir."/mp-public/uploads");
				$title = "Dosyalar";
				$page_title = "Dosyalar";	

			}						

		}elseif(route(2) == "language"){



			if(!permission('access', 'language')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



		    if(!route(3)):



			    $languages = $db->query("SELECT * FROM `langs` ORDER BY lang_id ASC")->fetchAll(PDO::FETCH_ASSOC);

				$title = "Diller";

				$page_title = "Diller";



			else:



				$id = route(3);



				$codes = $db->query("SELECT A.code_id, B.translation_text FROM `translation_codes` AS A LEFT JOIN `translations` AS B ON B.code_id = A.code_id and B.lang_id = '$id' ORDER BY A.code_id ASC")->fetchAll(PDO::FETCH_ASSOC);

				$languages = $db->query("SELECT * FROM `langs` WHERE lang_id != '$id' ORDER BY lang_id ASC")->fetchAll(PDO::FETCH_ASSOC);

				$language = $db->query("SELECT * FROM `langs` WHERE lang_id = '$id'")->fetch(PDO::FETCH_ASSOC);

				$title = "Dil Düzenleyici";

				$page_title = "Dil Düzenleyici";



			endif;

			

		}elseif(route(2) == "themes"){



			if(!permission('access', 'themes')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



		    if(route(3)){

		    	$folder = dir."/mp-content/view/".route(3);
		    	$dosyalar = scandir($folder);
		    	$themeConfigFile = $folder . "/theme/theme.php";

		    	if(file_exists($themeConfigFile)){

		    		require $themeConfigFile;

		    		if(is_dir($folder)){
		    		
			    		if(route(4)){

			    			$theme = route(4);
					    	$content = dir."/mp-content/view/".route(3)."/" . route(4) . ".php";

					    	if(file_exists($content)) {

								$title = "Tema Düzenleyici";
					    		$page_title = "Tema Düzenleyici";
				    			$themename = $themeDetail['themeName'];

							} else {

								$title = "Tema Düzenleyici";
					    		$page_title = "Tema Düzenleyici";
				    			$themename = $themeDetail['themeName'];

							}

			    		}else{

			    			$title = "Tema Düzenleyici";
				    		$page_title = "Tema Düzenleyici";
				    		$themename = $themeDetail['themeName'];

			    		}
				    	

			    	}else{

			    		header('Location:'. admin_url('appearence/themes'));

			    	}

		    	}else{

		    		header('Location:'. admin_url('appearence/themes'));

		    	}

		    	

		    }else{

				$title = "Temalar";
				$page_title = "Temalar";

		    }



			

		}else{



			if(!permission('access', 'appearence')){

				$page_title = "Yetkisiz Erişim";

		        require admin_view("unauthorized");

		        exit();

		    }



		    if(!route(3)):



		    	$pages = $db->from('pages')->orderby('id', 'DESC')->all();


	    		$title = "Sayfalar";

				$page_title = "Sayfalar";

				$path = "pages";

			elseif(route(3) == "add"):

				$title = "Sayfa Oluştur";

				$page_title = "Sayfa Oluştur";

				$path = "pages";

			elseif(route(3) == "edit"):



				if(route(4)):



					$id = route(4);

					$page = $db->from('pages')->where('id', $id)->first();
					
		    	$seo = json_decode($page['page_seo'], true);


					if($page):



	    				$title = "Sayfa Düzenle";

						$page_title = "Sayfa Düzenle";



					else:



						header('Location:'. admin_url('appearence/pages'));



					endif;



				else:



					header('Location:'. admin_url('appearence/pages'));



				endif;



			endif;





		}





		require admin_view('appearence');

		



	}else{



		require controller("404");



	}



}else{



	require controller("404");

	

}