Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions SimplePagesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,26 @@ public function hookDefineRoutes($args)
return;
}

$router = $args['router'];

// Add custom routes based on the page slug.
$pages = get_db()->getTable('SimplePagesPage')->findAll();
foreach ($pages as $page) {
$router->addRoute(
'simple_pages_show_page_' . $page->id,
new Zend_Controller_Router_Route(
$page->slug,
array(
'module' => 'simple-pages',
'controller' => 'page',
'action' => 'show',
'id' => $page->id
)
)
);
// Add a custom route based on the page slugs.
$slugs = get_db()->getTable('SimplePagesPage')->findSlugs();
if (empty($slugs)) {
return;
}

$router = $args['router'];
$quotedSlugs = array_map('preg_quote', $slugs);
$router->addRoute('simple_pages_show_pages', new Zend_Controller_Router_Route_Regex(
'(' . implode('|', $quotedSlugs) . ')',
array(
'module' => 'simple-pages',
'controller' => 'page',
'action' => 'show',
),
array(
1 => 'slug',
),
'%s'
));
}

/**
Expand Down
11 changes: 7 additions & 4 deletions controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
*/
class SimplePages_PageController extends Omeka_Controller_AbstractActionController
{
/**
* Show a simple page.
*/
public function showAction()
{
// Get the page object from the passed ID.
$pageId = $this->_getParam('id');
$page = $this->_helper->db->getTable('SimplePagesPage')->find($pageId);
// Get the page object from the passed slug.
$slug = $this->_getParam('slug');
$page = $this->_helper->db->getTable('SimplePagesPage')->findBySlug($slug);

// Restrict access to the page when it is not published.
if (!$page->is_published
&& !$this->_helper->acl->isAllowed('show-unpublished')) {
Expand Down
35 changes: 32 additions & 3 deletions models/SimplePagesPageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ public function findAllPagesOrderBySlug()
$select = $this->getSelect()->order('slug');
return $this->fetchObjects($select);
}


/**
* Find all slugs.
*
* @return array All slugs of pages.
*/
public function findSlugs()
{
$alias = $this->getTableAlias();
$select = $this->getSelect();
$select->reset(Zend_Db_Select::COLUMNS);
$select->from(array(), array($alias . '.slug'));
return $this->fetchCol($select);
}

public function applySearchFilters($select, $params)
{
$alias = $this->getTableAlias();
Expand Down Expand Up @@ -52,9 +66,24 @@ public function applySearchFilters($select, $params)
$select->order("{$alias}.title ASC");
break;
}
}
}
}


/**
* Retrieve a simple page by slug.
*
* @param sllug $slug Slug of the page to retrieve.
* @return SimplePagePage|null The simple page that is returned.
*/
public function findBySlug($slug)
{
$select = $this->getSelect();
$select->where($this->getTableAlias() . '.slug = ?', $slug);
$select->limit(1);
$select->reset(Zend_Db_Select::ORDER);
return $this->fetchObject($select, array());
}

/**
* Retrieve child pages from list of pages matching page ID.
*
Expand Down