<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pages extends Controller_Backend {
	public function action_index()
	{
		if(isset($_POST['title']))
		{
			$orm = ORM::factory("page");
			$orm->title = gen::arr_get($_POST,"title");
			$orm->content = gen::arr_get($_POST,"content");
			$orm->slug = gen::to_slug($orm->title);
			$orm->date = gen::now();
			$orm->lang = gen::arr_get($_POST,"comb");
			$orm->save();
		}
		$pages = ORM::factory("page");
		$view = gen::paged($pages, "backend/pages");
		$this->template->content = $view;
	}
	
	public function action_edit($id ="")
	{
		$page = ORM::factory("page",$id);
		if(!$page->loaded())
		{
			$this->template->content = "Böyle bir sayfa yok";
			return;
		}
		if(isset($_POST['title']))
		{
			$page->title = gen::arr_get($_POST,"title");
			$page->content = gen::arr_get($_POST,"content");
			$page->slug = gen::to_slug($page->title);
			$page->date = gen::now();
			$page->lang = gen::arr_get($_POST,"comb");
			$page->save();
		}
		else
		{
			$_POST["title"] = $page->title;
			$_POST["content"] = $page->content;
			$_POST["comb"] = $page->lang;
			
		}
		$pages = ORM::factory("page");
		$view = gen::paged($pages, "backend/pages");
		$this->template->content = $view;
	}
	public function action_del($id)
	{
		ORM::factory("page",$id)->delete()->save();
		gen::re_back();
	}
	public function action_create($id)
	{
		
		$orm = ORM::factory("page",$id);
		if(isset($_POST['title']))
		{
		
		$orm2 = ORM::factory("page");
		$orm2->title = gen::arr_get($_POST,"title");
		$orm2->content = gen::arr_get($_POST,"content");
		$orm2->slug = $orm->slug;
		$orm2->date = gen::now();
		$orm2->lang = gen::arr_get($_POST,"comb");	
		$orm->save();
		$orm2->save();
		}
		
		$pages = ORM::factory("page");
		$view = gen::paged($pages, "backend/create",array("orm"=>$orm));
		
		//$view = View::factory("backend/create", array("pages"=>$pages,"orm"=>$orm));
		$this->template->content = $view;
	}
	
}