Untitled

                Never    
PHP
       
<?php

class Controller_Admin extends Controller_Index
{

	private $user_id;
	
	private function log($action)
	{
		$user_id = Cookie::get('user_id');
		$log = ORM::factory('log');
		$log->user_id = $this->user_id;
		$log->time = time();
		$log->action = $action;
		$log->save();
		if($log->saved())
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	public function action_index()
	{
		$this->user_id = Cookie::get('user_id');
		$this->template->title = 'Admin';
		if($this->user_id)
		{
			$msg = '';
			$user = ORM::factory('user', $this->user_id);
			if(Arr::get($_GET, 'msg') == 'login_sucess')
			{
				$msg = '<div class="sucess">Pomyślnie zalogowano!</div>';
			}
			$logs = ORM::factory('log')->find_all();
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/index')
				->set('logs', $logs)
			)
			->set('msg', $msg);
		}
		else
		{
			$msg = '';
			if(Arr::get($_GET, 'msg') == 'logout')
			{
				$msg = '<div class="sucess">Pomyślnie wylogowano!</div>';
			}
			if(Arr::get($_POST, 'admin_login'))
			{
				$nick = Arr::get($_POST, 'nick');
				$pass = sha1(Arr::get($_POST, 'pass'));
				$checkLogin = ORM::factory('user')
							  ->where('nick', '=', $nick)
							  ->and_where('password', '=', sha1($pass))
							  ->find();
				if($checkLogin->loaded())
				{
					Cookie::set('user_id', $checkLogin->id);
					$this->log('Zalogowano z IP '.$_SERVER['REMOTE_ADDR']);
					$this->request->redirect(URL::base(TRUE, FALSE).'admin/?msg=login_sucess');
				}
				else
				{
					$msg = '<div class="error">Błędny nick i/lub hasło.</div>';
				}
			}
			$this->template->content = View::factory('admin/login')
			->set('msg', $msg);
		}
	}
	
	public function action_logout()
	{
		$this->log('Wylogowano z IP '.$_SERVER['REMOTE_ADDR']);
		Cookie::delete('user_id');
		$this->request->redirect(URL::base(TRUE, FALSE).'admin/?msg=logout');
	}
	
	public function action_manage()
	{
		$this->user_id = Cookie::get('user_id');
		$msg = '';
		$this->template->title = 'Zarządzaj';
		if(Arr::get($_GET, 'msg') == 'delete')
		{
			$msg = '<div class="sucess">Usunięto!</div>';
		}
		else if(Arr::get($_GET, 'msg') == 'accept')
		{
			$msg = '<div class="sucess">Opublikowano artykuł!</div>';
		}
		else if(Arr::get($_GET, 'msg') == 'deaccept')
		{
			$msg = '<div class="sucess">Wycofano artykuł artykuł!</div>';
		}
		else if(Arr::get($_GET, 'msg') == 'create')
		{
			$msg = '<div class="sucess">Stworzono artykuł!</div>';
		}
		if($this->user_id)
		{
			$type = $this->request->param('type');
			$limit = 5;
			$page_number = Arr::get($_GET, 'page');
			if(empty($page_number)) {
			  $offset = 0;
			  $page_number = 1;
			}
			else {
			  $offset = $limit * ($page_number - 1);
			}			
			$offset2 = $offset + $limit;
			$posts = ORM::factory('article')->order_by('id', 'DESC')->limit($limit)->offset($offset)->find_all();			
			$check = ORM::factory('article')->limit($limit)->offset($offset2)->find_all();
			$next = FALSE;
			foreach($check as $c)
			{
				if(!empty($c->title))
				{
					$next = TRUE;
				}
			}
			$bonus = '';
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/manage')
				->set('posts', $posts)
				->set('this_page', $page_number)
				->set('next_page', $next)
			)
			->set('msg', $msg);
		}
		else
		{
			$this->request->redirect(URL::base(TRUE, FALSE).'admin');
		}
	}
	
	public function action_create()
	{
		$this->user_id = Cookie::get('user_id');
		$this->template->title = 'Stwórz';
		if($this->user_id)
		{
			$msg = '';
			if(Arr::get($_POST, 'edit_form'))
			{
				$post = ORM::factory('article');
				$post->title = Arr::get($_POST, 'title');
				$post->url = URL::title(Arr::get($_POST, 'title'));
				$post->author_id = $this->user_id;
				$post->category_id = Arr::get($_POST, 'category');
				$post->content = Arr::get($_POST, 'content');
				$post->create_date = time();
				if(Arr::get($_POST, 'accept'))
					$post->published = 1;
				else
					$post->published = 0;
				$post->save();
				if($post->saved())
				{
					$post = ORM::factory('article')->order_by('id', 'DESC')->find();
					$this->log('Stworzono artykuł "'.$post->title.'" [#'.$post->id.']');
					$this->request->redirect(URL::base(TRUE, FALSE).'admin/manage/?msg=create');
				}
				else
				{
					$msg = '<div class="error">Wystąpił błąd podczas dodawania artykułu.</div>';
				}
			}
			$this->template->admin = TRUE;
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/edit_form')
				->set('post', '')
				->set('type', 'create')
				->set('categories', ORM::factory('category')->order_by('title', 'ASC')->find_all())
			)
			->set('msg', $msg);
		}
		else
		{
			$this->request->redirect(URL::base(TRUE, FALSE).'admin');
		}
	}
	
	public function action_edit()
	{
		$this->user_id = Cookie::get('user_id');
		$this->template->title = 'Edytuj';
		if($this->user_id)
		{
			$msg = '';
			$postid = $this->request->param('id');
			if(Arr::get($_POST, 'edit_form'))
			{
				$post = ORM::factory('article', $postid);
				$post->title = Arr::get($_POST, 'title');
				$post->url = URL::title(Arr::get($_POST, 'title'));
				$post->category_id = Arr::get($_POST, 'category');
				$post->content = Arr::get($_POST, 'content');
				if(Arr::get($_POST, 'accept'))
					$post->published = 1;
				else
					$post->published = 0;
				if(Arr::get($_POST, 'dateupdate'))
					$post->create_date = time();
				$post->save();
				if($post->saved())
				{
					$post = ORM::factory('article', $postid);
					$this->log('Edytowano artykuł "'.$post->title.'" [#'.$post->id.']');
					$msg = '<div class="sucess">Edytowano artykuł!</div>';
				}
				else
				{
					$msg = '<div class="error">Wystąpił błąd podczas edycji artykułu!</div>';
				}
			}
			$post = ORM::factory('article', $postid);
			$this->template->admin = TRUE;
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/edit_form')
				->set('post', $post)
				->set('type', 'edit')
				->set('categories', ORM::factory('category')->order_by('title', 'ASC')->find_all())
			)
			->set('msg', $msg);
		}
		else
		{
			$this->request->redirect(URL::base(TRUE, FALSE).'admin');
		}
	}
	
	public function action_accept()
	{
		$this->user_id = Cookie::get('user_id');
		if($this->user_id)
		{
			$msg = '';
			$postid = $this->request->param('id');
			$post = ORM::factory('article', $postid);
			$post->published = 1;
			$post->save();
			if($post->saved())
			{
				$post = ORM::factory('article', $postid);
				$this->log('Opublikowano artykuł "'.$post->title.'" [#'.$post->id.']');
				$this->request->redirect(URL::base(TRUE, FALSE).'admin/manage/?msg=accept');
			}
			else
			{
				$msg = '<div class="error">Wystąpił błąd podczas publikowania artykułu!</div>';
			}
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/msg'))
			->set('msg', $msg);
		}
		else
		{
			$this->request->redirect(URL::base(TRUE, FALSE).'admin');
		}
	}
	
	public function action_deaccept()
	{
		$this->user_id = Cookie::get('user_id');
		if($this->user_id)
		{
			$msg = '';
			$postid = $this->request->param('id');
			$post = ORM::factory('article', $postid);
			$post->published = 0;
			$post->save();
			if($post->saved())
			{
				$post = ORM::factory('article', $postid);
				$this->log('Wycofano artykuł "'.$post->title.'" [#'.$post->id.']');
				$this->request->redirect(URL::base(TRUE, FALSE).'admin/manage/?msg=deaccept');
			}
			else
			{
				$msg = '<div class="error">Wystąpił błąd podczas wycofywania artykułu!</div>';
			}
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/msg'))
			->set('msg', $msg);
		}
		else
		{
			$this->request->redirect(URL::base(TRUE, FALSE).'admin');
		}
	}
	
	public function action_delete()
	{
		$this->user_id = Cookie::get('user_id');
		if($this->user_id)
		{
			$msg = '';
			$postid = $this->request->param('id');
			$post = ORM::factory('article', $postid);
			$post->delete();
			$post = ORM::factory('article', $postid);
			$this->log('Usunięto artykuł "'.$post->title.'" [#'.$post->id.']');
			$this->request->redirect(URL::base(TRUE, FALSE).'admin/manage/?msg=delete');
			$this->template->content = View::factory('admin/template')
			->set('content', View::factory('admin/pages/msg'))
			->set('msg', $msg);
		}
		else
		{
			$this->request->redirect(URL::base(TRUE, FALSE).'admin');
		}
	}

}

?>

Raw Text