Skip to content

Instantly share code, notes, and snippets.

@Schlaefer
Created March 25, 2015 17:38
Show Gist options
  • Save Schlaefer/8f1a579ea1c1391b2c2c to your computer and use it in GitHub Desktop.
Save Schlaefer/8f1a579ea1c1391b2c2c to your computer and use it in GitHub Desktop.
<?php
namespace Phile\Plugin\Phile\DemoPlugin;
use Phile\Core\Registry;
use Phile\Repository\Page;
class Plugin extends \Phile\Plugin\AbstractPlugin implements \Phile\Gateway\EventObserverInterface
{
public function __construct()
{
\Phile\Event::registerEvent('before_render_template', $this);
\Phile\Event::registerEvent('request_uri', $this);
}
public function on($eventKey, $data = null)
{
if ($eventKey === 'request_uri') {
$this->url = $data['uri'];
}
if ($eventKey !== 'before_render_template') {
return;
}
$repository = new Page();
$orgPage = $repository->findByPath($this->url);
$orgMeta = $orgPage->getMeta();
if ($orgMeta->template != 'post') {
return;
}
$pages = [$orgPage];
$needed = 4;
$next = 2;
$lastPage = null;
$page = $orgPage;
$pages = $this->addNext($page, $pages, $next);
$needed = $needed - (count($pages) + 1);
$next = $next - (count($pages) + 1);
$page = $orgPage;
while ($needed) {
$page = $page->getPreviousPage();
if (!$page) {
break;
}
$meta = $page->getMeta();
if ($meta->template != 'post') {
continue;
}
array_unshift($pages, $page);
$needed--;
}
if ($next == 0 && $needed > 0) {
$pages = $this->addNext(end($pages), $pages, $needed);
}
$templateVars = Registry::get('templateVars');
$templateVars['myPages'] = $pages;
Registry::set('templateVars', $templateVars);
}
protected function addNext($page, $pages, $needed = 1) {
while ($needed) {
$page = $page->getNextPage();
if (!$page) {
break;
}
$meta = $page->getMeta();
if ($meta->template != 'post') {
continue;
}
array_push($pages, $page);
$needed--;
}
return $pages;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment