2013-10-05 17:12:51 +02:00
|
|
|
<?php
|
|
|
|
class Bootstrap
|
|
|
|
{
|
2013-10-05 19:24:08 +02:00
|
|
|
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))
|
|
|
|
{
|
2013-10-05 20:00:13 +02:00
|
|
|
$dummy = R::dispense('user');
|
|
|
|
$dummy->name = 'Anonymous';
|
|
|
|
$dummy->access_rank = AccessRank::Anonymous;
|
|
|
|
$this->context->user = $dummy;
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 17:12:51 +02:00
|
|
|
public function workWrapper($workCallback)
|
|
|
|
{
|
2013-10-05 19:24:08 +02:00
|
|
|
$this->config->chibi->baseUrl = 'http://' . rtrim($_SERVER['HTTP_HOST'], '/') . '/';
|
|
|
|
R::setup('sqlite:' . $this->config->main->dbPath);
|
2013-10-05 17:12:51 +02:00
|
|
|
session_start();
|
2013-10-05 19:24:08 +02:00
|
|
|
|
2013-10-05 21:22:28 +02:00
|
|
|
$this->context->title = $this->config->main->title;
|
2013-10-09 19:39:30 +02:00
|
|
|
$this->context->stylesheets = ['core.css', 'jquery-ui.css'];
|
|
|
|
$this->context->scripts = ['core.js','jquery.min.js','jquery-ui.min.js'];
|
2013-10-05 22:52:55 +02:00
|
|
|
|
2013-10-05 17:12:51 +02:00
|
|
|
$this->context->layoutName = isset($_GET['json'])
|
|
|
|
? 'layout-json'
|
|
|
|
: 'layout-normal';
|
|
|
|
$this->context->transport = new StdClass;
|
|
|
|
$this->context->transport->success = null;
|
|
|
|
|
2013-10-05 19:24:08 +02:00
|
|
|
$this->attachUser();
|
|
|
|
|
2013-10-05 17:12:51 +02:00
|
|
|
if (empty($this->context->route))
|
|
|
|
{
|
|
|
|
$this->context->viewName = 'error-404';
|
|
|
|
(new \Chibi\View())->renderFile($this->context->layoutName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$workCallback();
|
|
|
|
}
|
2013-10-05 21:24:20 +02:00
|
|
|
catch (SimpleException $e)
|
|
|
|
{
|
|
|
|
$this->context->transport->errorMessage = rtrim($e->getMessage(), '.') . '.';
|
|
|
|
$this->context->transport->exception = $e;
|
|
|
|
$this->context->transport->success = false;
|
2013-10-06 13:22:58 +02:00
|
|
|
(new \Chibi\View())->renderFile($this->context->layoutName);
|
2013-10-05 21:24:20 +02:00
|
|
|
}
|
2013-10-05 17:12:51 +02:00
|
|
|
catch (Exception $e)
|
|
|
|
{
|
2013-10-06 19:53:33 +02:00
|
|
|
$this->context->transport->errorMessage = rtrim($e->getMessage(), '.') . '.';
|
|
|
|
$this->context->transport->exception = $e;
|
|
|
|
$this->context->transport->success = false;
|
2013-10-05 17:12:51 +02:00
|
|
|
$this->context->viewName = 'error-exception';
|
|
|
|
(new \Chibi\View())->renderFile($this->context->layoutName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|