2014-01-10 10:27:37 +02:00
|
|
|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
|
|
|
|
|
|
class Controller_Page extends Controller_Layout {
|
|
|
|
protected $secure_actions = array(
|
2014-01-16 14:29:16 +02:00
|
|
|
'drafts', 'create', 'edit', 'delete'
|
2014-01-10 10:27:37 +02:00
|
|
|
);
|
2014-02-02 10:49:39 +02:00
|
|
|
protected $controls = array(
|
|
|
|
'name' => 'input',
|
|
|
|
'content' => 'textarea',
|
|
|
|
);
|
2014-01-10 10:27:37 +02:00
|
|
|
/**
|
|
|
|
* View a page.
|
|
|
|
**/
|
|
|
|
public function action_view()
|
|
|
|
{
|
|
|
|
$this->auto_render = FALSE;
|
|
|
|
$id = $this->request->param('id');
|
|
|
|
$page = ORM::factory('Page', $id);
|
|
|
|
if (!$page->loaded())
|
|
|
|
{
|
|
|
|
$this->redirect('error/404');
|
|
|
|
}
|
|
|
|
$is_admin = Auth::instance()->logged_in('admin');
|
|
|
|
if ($page->is_draft AND !$is_admin)
|
|
|
|
{
|
|
|
|
$this->redirect('error/403');
|
|
|
|
}
|
|
|
|
$cache = Cache::instance('apcu');
|
|
|
|
$latest_change = $page->posted_at;
|
|
|
|
if (!$is_admin)
|
|
|
|
{
|
|
|
|
$body = $cache->get('page_'.$id);
|
|
|
|
if (!empty($body))
|
|
|
|
{
|
|
|
|
if ($cache->get('page_'.$id.'_changed') === $latest_change)
|
|
|
|
{
|
|
|
|
$this->response->body($body);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$cache->delete('page_'.$id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->template = new View_Page_View;
|
|
|
|
$this->template->title = $page->name;
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->content = $page->content;
|
2014-01-10 10:27:37 +02:00
|
|
|
if ($page->is_draft)
|
|
|
|
{
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->title .= ' ('.__('draft').')';
|
2014-01-10 10:27:37 +02:00
|
|
|
}
|
|
|
|
$renderer = Kostache_Layout::factory('layout');
|
|
|
|
$body = $renderer->render($this->template, $this->template->_view);
|
|
|
|
if (!$is_admin)
|
|
|
|
{
|
|
|
|
$cache->set('page_'.$id, $body, 60*60*24); //cache page for 1 day
|
|
|
|
}
|
|
|
|
$cache->set('page_'.$id.'_changed', $latest_change);
|
|
|
|
$this->response->body($body);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Page index
|
|
|
|
**/
|
|
|
|
public function action_index()
|
|
|
|
{
|
|
|
|
$this->template = new View_Index;
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->title = __('Page index');
|
2014-01-10 10:27:37 +02:00
|
|
|
$this->template->show_date = FALSE;
|
|
|
|
$this->template->items = ORM::factory('Page')
|
|
|
|
->where('is_draft', '=', '0')
|
|
|
|
->order_by('name', 'ASC')
|
2014-02-02 10:49:39 +02:00
|
|
|
->filter_by_page($this->request->param('page'))
|
2014-01-10 10:27:37 +02:00
|
|
|
->find_all();
|
|
|
|
}
|
|
|
|
public function action_delete()
|
|
|
|
{
|
|
|
|
$this->template = new View_Delete;
|
|
|
|
$id = $this->request->param('id');
|
|
|
|
$page = ORM::factory('Page', $id);
|
|
|
|
if (!$page->loaded())
|
|
|
|
{
|
|
|
|
$this->redirect('error/404');
|
|
|
|
}
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->title = __('Delete page');
|
2014-01-10 10:27:37 +02:00
|
|
|
$this->template->content_title = $page->name;
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->content = $page->content;
|
2014-01-10 10:27:37 +02:00
|
|
|
|
|
|
|
$confirmation = $this->request->post('confirmation');
|
|
|
|
if ($confirmation === 'yes') {
|
|
|
|
$page->delete();
|
|
|
|
$this->redirect('page/index');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create a page (for admin)
|
|
|
|
**/
|
|
|
|
public function action_create()
|
|
|
|
{
|
|
|
|
$this->template = new View_Edit;
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->title = __('New page');
|
2014-01-10 10:27:37 +02:00
|
|
|
$this->template->errors = array();
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->model = ORM::factory('Page');
|
|
|
|
$this->_edit($this->template->model);
|
2014-01-10 10:27:37 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Edit a page (for admin)
|
|
|
|
**/
|
|
|
|
public function action_edit()
|
|
|
|
{
|
|
|
|
$this->template = new View_Page_Edit;
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->title = __('Edit page');
|
2014-01-10 10:27:37 +02:00
|
|
|
$id = $this->request->param('id');
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->model = ORM::factory('Page', $id);
|
|
|
|
if (!$this->template->model->loaded())
|
2014-01-10 10:27:37 +02:00
|
|
|
{
|
|
|
|
$this->redirect('error/404');
|
|
|
|
}
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->_edit($this->template->model);
|
2014-01-10 10:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draft index
|
|
|
|
**/
|
|
|
|
public function action_drafts()
|
|
|
|
{
|
|
|
|
$this->template = new View_Index;
|
2014-02-02 10:49:39 +02:00
|
|
|
$this->template->title = __('Page drafts');
|
2014-01-10 10:27:37 +02:00
|
|
|
$this->template->show_date = FALSE;
|
|
|
|
$this->template->items = ORM::factory('Page')
|
|
|
|
->where('is_draft', '=', '1')
|
|
|
|
->order_by('name', 'DESC')
|
|
|
|
->find_all();
|
|
|
|
}
|
|
|
|
}
|