Let there be placeholders

This commit is contained in:
Marcin Kurczewski 2013-10-05 19:24:08 +02:00
parent 3ba0d60d78
commit c905f1d7dd
11 changed files with 137 additions and 55 deletions

View file

@ -1,17 +1,38 @@
<?php <?php
class Bootstrap class Bootstrap
{ {
public function attachUser()
{
$this->context->loggedIn = false;
if (isset($_SESSION['user-id']))
{
$this->context->user = R::findOne('user', 'id = ?', [$_SESSION['user-id']]);
if (!empty($this->context->user))
{
$this->context->loggedIn = true;
}
}
if (empty($this->context->user))
{
#todo: construct anonymous user
$this->context->user = null;
}
}
public function workWrapper($workCallback) public function workWrapper($workCallback)
{ {
$this->config->chibi->baseUrl = 'http://' . rtrim($_SERVER['HTTP_HOST'], '/') . '/';
R::setup('sqlite:' . $this->config->main->dbPath);
session_start(); session_start();
$this->context->layoutName = isset($_GET['json']) $this->context->layoutName = isset($_GET['json'])
? 'layout-json' ? 'layout-json'
: 'layout-normal'; : 'layout-normal';
$this->context->transport = new StdClass; $this->context->transport = new StdClass;
$this->context->transport->success = null; $this->context->transport->success = null;
$this->config->chibi->baseUrl = 'http://' . rtrim($_SERVER['HTTP_HOST'], '/') . '/'; $this->attachUser();
R::setup('sqlite:' . $this->config->main->dbPath);
if (empty($this->context->route)) if (empty($this->context->route))
{ {
$this->context->viewName = 'error-404'; $this->context->viewName = 'error-404';

View file

@ -1,28 +1,8 @@
<?php <?php
abstract class AbstractController abstract class AbstractController
{ {
protected function attachUser()
{
$this->context->loggedIn = false;
if (isset($_SESSION['user-id']))
{
$this->context->user = R::findOne('user', 'id = ?', [$_SESSION['user-id']]);
if (!empty($this->context->user))
{
$this->context->loggedIn = true;
}
}
if (empty($this->context->user))
{
#todo: construct anonymous user
$this->context->user = null;
}
}
public function workWrapper($workCallback) public function workWrapper($workCallback)
{ {
$this->attachUser();
try try
{ {
$workCallback(); $workCallback();

View file

@ -15,7 +15,7 @@ class AuthController extends AbstractController
//check if already logged in //check if already logged in
if ($this->context->loggedIn) if ($this->context->loggedIn)
{ {
\Chibi\HeadersHelper::set('Location', \Chibi\UrlHelper::route('post', 'search')); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
return; return;
} }
@ -38,7 +38,7 @@ class AuthController extends AbstractController
throw new SimpleException('You haven\'t confirmed your e-mail address yet'); throw new SimpleException('You haven\'t confirmed your e-mail address yet');
$_SESSION['user-id'] = $dbUser->id; $_SESSION['user-id'] = $dbUser->id;
\Chibi\HeadersHelper::set('Location', \Chibi\UrlHelper::route('post', 'search')); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
$this->context->transport->success = true; $this->context->transport->success = true;
} }
} }
@ -51,7 +51,7 @@ class AuthController extends AbstractController
$this->context->viewName = null; $this->context->viewName = null;
$this->context->viewName = null; $this->context->viewName = null;
unset($_SESSION['user-id']); unset($_SESSION['user-id']);
\Chibi\HeadersHelper::set('Location', \Chibi\UrlHelper::route('post', 'search')); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
} }
/** /**
@ -62,7 +62,7 @@ class AuthController extends AbstractController
//check if already logged in //check if already logged in
if ($this->context->loggedIn) if ($this->context->loggedIn)
{ {
\Chibi\HeadersHelper::set('Location', \Chibi\UrlHelper::route('post', 'search')); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
return; return;
} }
@ -167,7 +167,7 @@ class AuthController extends AbstractController
if (!$emailActivation and !$adminActivation) if (!$emailActivation and !$adminActivation)
{ {
$_SESSION['user-id'] = $dbUser->id; $_SESSION['user-id'] = $dbUser->id;
$this->attachUser(); \Chibi\Registry::getBootstrap()->attachUser();
} }
} }
} }
@ -180,7 +180,7 @@ class AuthController extends AbstractController
//check if already logged in //check if already logged in
if ($this->context->loggedIn) if ($this->context->loggedIn)
{ {
\Chibi\HeadersHelper::set('Location', \Chibi\UrlHelper::route('post', 'search')); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
return; return;
} }
@ -203,7 +203,7 @@ class AuthController extends AbstractController
if (!$adminActivation) if (!$adminActivation)
{ {
$_SESSION['user-id'] = $dbUser->id; $_SESSION['user-id'] = $dbUser->id;
$this->attachUser(); \Chibi\Registry::getBootstrap()->attachUser();
} }
} }
} }

View file

@ -0,0 +1,11 @@
<?php
class CommentController extends AbstractController
{
/**
* @route /comments
*/
public function listAction()
{
throw new Exception('Not implemented');
}
}

View file

@ -0,0 +1,11 @@
<?php
class IndexController extends AbstractController
{
/**
* @route /
* @route /index
*/
public static function indexAction()
{
}
}

View file

@ -2,15 +2,28 @@
class PostController extends AbstractController class PostController extends AbstractController
{ {
/** /**
* @route / * @route /posts
* @route /index * @route /posts/{query}
* @validate query .*
*/ */
public function searchAction() public function listAction($query = null)
{ {
$tp = new StdClass; throw new Exception('Not implemented');
$tp->posts = []; }
$tp->posts []= 1;
$tp->posts []= 2; /**
$this->context->transport = $tp; * @route /post/upload
*/
public function uploadAction()
{
throw new Exception('Not implemented');
}
/**
* @route /post/{id}
*/
public function showAction($id)
{
throw new Exception('Not implemented');
} }
} }

View file

@ -0,0 +1,11 @@
<?php
class TagController extends AbstractController
{
/**
* @route /tags
*/
public static function listAction()
{
throw new Exception('Not implemented');
}
}

View file

@ -0,0 +1,20 @@
<?php
class UserController extends AbstractController
{
/**
* @route /users
*/
public function listAction()
{
throw new Exception('Not implemented');
}
/**
* @route /user/{name}
* @validate name [^\/]+
*/
public function showAction($name)
{
throw new Exception('Not implemented');
}
}

View file

@ -0,0 +1 @@
Todo: index

View file

@ -6,24 +6,38 @@
</head> </head>
<body> <body>
<div> <nav>
<?php if (empty($this->context->user)): ?> <ul>
<a href="<?php echo \Chibi\UrlHelper::route('auth', 'login') ?>"> <li><a href="<?php echo \Chibi\UrlHelper::route('index', 'index') ?>">Home</a></li>
login <li><a href="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>">Browse</a></li>
</a> <li><a href="<?php echo \Chibi\UrlHelper::route('comment', 'list') ?>">Comments</a></li>
&nbsp;or&nbsp; <li><a href="<?php echo \Chibi\UrlHelper::route('post', 'list', ['query' => 'favmin:1']) ?>">Favorites</a></li>
<a href="<?php echo \Chibi\UrlHelper::route('auth', 'register') ?>"> <li><a href="<?php echo \Chibi\UrlHelper::route('user', 'show', ['name' => $this->context->user->name]) ?>">Account</a></li>
register <li><a href="<?php echo \Chibi\UrlHelper::route('post', 'upload') ?>">Upload</a></li>
</a> <li>
<?php else: ?> <form action="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>" method="get">
logged in as <?php echo $this->context->user->name ?> <input type="search" name="query" placeholder="search&hellip;">
&nbsp; <input type="submit" name="submit" value="Go">
<a href="<?php echo \Chibi\UrlHelper::route('auth', 'logout') ?>"> </form>
logout </li>
</a> <li>
<?php endif ?> <?php if (empty($this->context->user)): ?>
</div> <a href="<?php echo \Chibi\UrlHelper::route('auth', 'login') ?>">Login</a>
&nbsp;or&nbsp;
<a href="<?php echo \Chibi\UrlHelper::route('auth', 'register') ?>">register</a>
<?php else: ?>
<?php echo $this->context->user->name ?>
&nbsp;
<a href="<?php echo \Chibi\UrlHelper::route('auth', 'logout') ?>">
Logout
</a>
<?php endif ?>
</li>
</ul>
</nav>
<?php echo $this->renderView() ?> <section>
<?php echo $this->renderView() ?>
</section>
</body> </body>
</html> </html>