add additional pages and handle introduction page as ?system? page

This commit is contained in:
oliver 2015-10-30 22:45:05 +01:00
commit 8c5c7e0b13
15 changed files with 812 additions and 44 deletions

View file

@ -19,6 +19,12 @@
*/
class IntroductionController extends \hhu\z\controllers\IntermediateController
{
/**
* Required models
*
* @var array
*/
public $models = array('pages');
@ -28,8 +34,60 @@
*/
public function index()
{
// Get introduction text
$page = $this->Pages->getPageByUrl(\hhu\z\models\PagesModel::PAGEURL_INTRODUCTION);
$text = (!is_null($page)) ? $page['text'] : null;
// Pass data to view
$this->set('userId', $this->Auth->getUserId());
$this->set('text', $text);
}
/**
* Action: edit.
*/
public function edit()
{
// Get page
$page = $this->Pages->getPageByUrl(\hhu\z\models\PagesModel::PAGEURL_INTRODUCTION);
// Values
$text = (!is_null($page)) ? $page['text'] : null;
// Check request method
if($this->request->getRequestMethod() == 'POST')
{
if(!is_null($this->request->getPostParam('edit')))
{
// Get values
$text = $this->request->getPostParam('text');
// Create page if necessary
if(is_null($page)) {
$pageId = $this->Pages->createPage(
$this->Auth->getUserId(),
\hhu\z\models\PagesModel::PAGEURL_INTRODUCTION
);
$page = $this->Pages->getPageById($pageId);
}
// Save text
$this->Pages->editPage(
$page['id'],
$page['title'],
$text
);
}
// Redirect
$this->redirect($this->linker->link(array('index'), 1));
}
// Pass data to view
$this->set('text', $text);
}
}

View file

@ -19,6 +19,12 @@
*/
class MenuController extends \hhu\z\Controller
{
/**
* Required models
*
* @var array
*/
public $models = array('pages');
@ -45,6 +51,12 @@
*/
public function index()
{
// Get additional pages
$pages = $this->Pages->getPages();
// Pass data to view
$this->set('pages', $pages);
}
}

View file

@ -0,0 +1,237 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\controllers;
/**
* Controller of the PagesAgent to manage additional pages.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class PagesController extends \hhu\z\controllers\IntermediateController
{
/**
* Required components
*
* @var array
*/
public $components = array('validation');
/**
* Required models
*
* @var array
*/
public $models = array('pages');
/**
* User permissions
*
* @var array
*/
public $permissions = array(
'index' => array('admin')
/*
'create' => array('admin', 'moderator'),
'edit' => array('admin', 'moderator'),
'delete' => array('admin')
*/
);
/**
* Action: index.
*
* List all registered pages.
*/
public function index()
{
// Get registered pages
$pages = $this->Pages->getPages();
// Pass data to view
$this->set('pages', $pages);
}
/**
* Action: page.
*
* Show a page.
*
* @throws \nre\exceptions\IdNotFoundException
* @param string $pageUrl URL of page to show
*/
public function page($pageUrl)
{
// Get page
$page = $this->Pages->getPageByUrl($pageUrl);
// Pass data to view
$this->set('page', $page);
}
/**
* Action: create.
*
* Create a new page.
*/
public function create()
{
// Values
$title = '';
$fields = array('title');
$validation = array();
// Create new page
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('create')))
{
// Get params and validate them
$validation = $this->Validation->validateParams($this->request->getPostParams(), $fields);
$title = $this->request->getPostParam('title');
if($this->Pages->titleExists($title)) {
$validation = $this->Validation->addValidationResult($validation, 'title', 'exist', true);
}
// Create
if($validation === true)
{
$pageId = $this->Pages->createPage(
$this->Auth->getUserId(),
$title
);
// Redirect to page
$page = $this->Pages->getPageById($pageId);
$this->redirect($this->linker->link(array('page', $page['url']), 1));
}
}
// Get validation settings
$validationSettings = array();
foreach($fields as &$field) {
$validationSettings[$field] = \nre\configs\AppConfig::$validation[$field];
}
// Pass data to view
$this->set('title', $title);
$this->set('validation', $validation);
$this->set('validationSettings', $validationSettings);
}
/**
* Action: edit.
*
* Edit page.
*
* @throws \nre\exceptions\IdNotFoundException
* @param string $pageUrl URL of page to edit
*/
public function edit($pageUrl)
{
// Get page
$page = $this->Pages->getPageByUrl($pageUrl);
// Values
$title = $page['title'];
$text = $page['text'];
$fields = array('title');
$validation = array();
// Edit content
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('save')))
{
// Get params and validate them
$validation = $this->Validation->validateParams($this->request->getPostParams(), $fields);
$title = $this->request->getPostParam('title');
if($this->Pages->titleExists($title, $page['id'])) {
$validation = $this->Validation->addValidationResult($validation, 'title', 'exist', true);
}
$text = $this->request->getPostParam('text');
var_dump($text);
// Edit
if($validation === true)
{
$this->Pages->editPage(
$page['id'],
$title,
$text
);
// Redirect to entry
$this->redirect($this->linker->link(array('page', $page['url']), 1));
}
}
// Get validation settings
$validationSettings = array();
foreach($fields as &$field) {
$validationSettings[$field] = \nre\configs\AppConfig::$validation[$field];
}
// Pass data to view
$this->set('page', $page);
$this->set('title', $title);
$this->set('text', $text);
$this->set('validation', $validation);
$this->set('validationSettings', $validationSettings);
}
/**
* Action: delete.
*
* Delete a page.
*
* @throws \nre\exceptions\IdNotFoundException
* @param string $pageUrl URL of page to delete
*/
public function delete($pageUrl)
{
// Get page
$page = $this->Pages->getPageByUrl($pageUrl);
// Check request method
if($this->request->getRequestMethod() == 'POST')
{
// Check confirmation
if(!is_null($this->request->getPostParam('delete')))
{
// Delete page
$this->Pages->deletePage($page);
// Redirect to overview
$this->redirect($this->linker->link(null, 1));
}
// Redirect to entry
$this->redirect($this->linker->link(array('page', $page['url']), 1));
}
// Set titile
$this->addTitleLocalized('Delete page');
// Show confirmation
$this->set('page', $page);
}
}
?>