@@ -14,8 +14,7 @@ use Blog\DB\User; | |||
use Blog\DB\Blog as BlogDB; | |||
use Blog\View\Twig; | |||
use Blog\DB\DB; | |||
use Blog\Model\Menu; | |||
use Blog\Model\Blogs; | |||
use Blog\Model\Blog; | |||
@@ -47,11 +46,24 @@ $container->set( | |||
function addNavbar($twig) | |||
{ | |||
$menu = new Menu('primary'); | |||
$twig->addBlockVariable('navbar', ['urls' => $menu]); | |||
$urls = [ | |||
["link" => "https://www.google.be", "name" => "Google"], | |||
["link" => "https://www.facebook.com", "name" => "Facebook"], | |||
["link" => "https://www.twiter.com", "name" => "Twitter"] | |||
]; | |||
$twig->addBlockVariable('navbar', ['urls' => $urls]); | |||
} | |||
function addNavbar2($twig) | |||
{ | |||
$urls = [ | |||
["link" => "https://www.google.be", "name" => "Bing"], | |||
["link" => "https://www.facebook.com", "name" => "Apple"], | |||
["link" => "https://www.twiter.com", "name" => "TikTok"] | |||
]; | |||
$twig->addBlockVariable('navbar', ['urls' => $urls]); | |||
} | |||
function addFooter($response) | |||
{ | |||
@@ -59,14 +71,19 @@ function addFooter($response) | |||
$response->getBody()->write("</body></html>"); | |||
return; | |||
} | |||
function init() | |||
{ | |||
$art1 = ["slug" => "artikel-1", "title" => "Dit is titel 1", "content" => "Lorem ipsum 1"]; | |||
$art2 = ["slug" => "artikel-2", "title" => "Dit is titel 2", "content" => "Lorem ipsum 2"]; | |||
$art3 = ["slug" => "artikel-3", "title" => "Dit is titel 3", "content" => "Lorem ipsum 3"]; | |||
$art4 = ["slug" => "artikel-4", "title" => "Dit is titel 4", "content" => "Lorem ipsum 4"]; | |||
$_SESSION['blogs'] = [$art1, $art2, $art3, $art4]; | |||
} | |||
$app->get('/', function (Request $request, Response $response, array $args) { | |||
$blogs = new Blogs(); | |||
$blogs = $blogs->getBlogs(); | |||
//TODO: load the blog data | |||
if (!isset($_SESSION['blogs'])) { | |||
init(); | |||
} | |||
$vars = [ | |||
"content" => "Dit is de main body met info" | |||
@@ -74,6 +91,10 @@ $app->get('/', function (Request $request, Response $response, array $args) { | |||
$twig = $this->get('twig'); | |||
addNavbar($twig); | |||
addNavbar2($twig); | |||
$err = ['type' => 'error', 'text' => "something's wrong"]; | |||
$twig->addMessage($err); | |||
$a = $twig->render('index.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
return $response; |
@@ -16,12 +16,4 @@ class Blog extends DB | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
public function getBlogs() | |||
{ | |||
$sql = "SELECT * FROM blogs"; | |||
$stmt = $this->prepare($sql); | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
} |
@@ -1,16 +0,0 @@ | |||
<?php | |||
namespace Blog\DB; | |||
class Menu extends DB | |||
{ | |||
// Fetch all links for a given menu type | |||
public function getMenu($type) | |||
{ | |||
$sql = "SELECT * FROM menu WHERE type = :type ;"; | |||
$stmt = $this->prepare($sql); | |||
$stmt->bindValue(":type", $type); | |||
$res = $stmt->execute(); | |||
return $res; | |||
} | |||
} |
@@ -33,9 +33,13 @@ class Twig | |||
// add block variables to the global variable bag | |||
public function addBlockVariable($block, $data) | |||
{ | |||
$current = $this->variables[$block]; | |||
$new = array_merge($current, $data); | |||
$this->variables[$block] = $new; | |||
$current = $this->variables[$block]['urls']; | |||
if ($current) { | |||
$new = array_merge($current, $data['urls']); | |||
} else { | |||
$new = $data['urls']; | |||
} | |||
$this->variables[$block]['urls'] = $new; | |||
} | |||
public function render($tmpl, $vars) |
@@ -37,45 +37,6 @@ class Blog | |||
return $this->author; | |||
} | |||
public function setContent($value) | |||
{ | |||
$this->content = $value; | |||
} | |||
public function setID($value) | |||
{ | |||
$this->id = $value; | |||
} | |||
public function setSlug($value) | |||
{ | |||
$this->slug = $value; | |||
} | |||
public function setTitle($value) | |||
{ | |||
$this->title = $value; | |||
} | |||
public function getContent() | |||
{ | |||
return $this->content; | |||
} | |||
public function getID() | |||
{ | |||
return $this->id; | |||
} | |||
public function getSlug() | |||
{ | |||
return $this->slug; | |||
} | |||
public function getTitle() | |||
{ | |||
return $this->title; | |||
} | |||
public function getComments() | |||
{ | |||
$comments = new Comment(); | |||
@@ -90,4 +51,8 @@ class Blog | |||
//$db->createBlog($data); | |||
$db->createBlog($this); | |||
} | |||
public function getAssociative() | |||
{ | |||
} | |||
} |
@@ -1,34 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
use Blog\DB\Blog as BlogDB; | |||
use Blog\Model\Blog; | |||
class Blogs | |||
{ | |||
protected $blogs = []; | |||
public function getBlog($id) | |||
{ | |||
} | |||
// TODO/ filter system | |||
// $filter is assoc array vb ['id' => 'dbase id', 'category'=>['PHP', 'Slim']] | |||
// of : ['id' => [1,2,3]] | |||
public function getBlogs() | |||
{ | |||
$db = new BlogDB(); | |||
$res = $db->getBlogs(); | |||
while ($result = $res->fetchArray(SQLITE3_ASSOC)) { | |||
$blog = new Blog(); | |||
$blog->setSlug($result['slug']); | |||
$blog->setID($result['id']); | |||
$blog->setTitle($result['title']); | |||
$blog->setContent($result['content']); | |||
$this->blogs[] = $blog; | |||
} | |||
return $this; | |||
} | |||
} |
@@ -1,67 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
class Link | |||
{ | |||
protected $id; | |||
protected $name; | |||
protected $url; | |||
protected $type; | |||
protected $parent; | |||
public function setID($value) | |||
{ | |||
$this->id = $value; | |||
return; | |||
} | |||
public function getOD() | |||
{ | |||
return $this->id; | |||
} | |||
public function setName($value) | |||
{ | |||
$this->name = $value; | |||
return; | |||
} | |||
public function getName() | |||
{ | |||
return $this->name; | |||
} | |||
public function setUrl($value) | |||
{ | |||
$this->url = $value; | |||
return; | |||
} | |||
public function getUrl() | |||
{ | |||
return $this->url; | |||
} | |||
public function setType($value) | |||
{ | |||
$this->type = $value; | |||
return; | |||
} | |||
public function getType() | |||
{ | |||
return $this->type; | |||
} | |||
public function setParent($value) | |||
{ | |||
$this->parent = $value; | |||
return; | |||
} | |||
public function getParent() | |||
{ | |||
return $this->parent; | |||
} | |||
} |
@@ -1,37 +0,0 @@ | |||
<?php | |||
namespace Blog\Model; | |||
use \Blog\Model\Link; | |||
use \Blog\DB\Menu as DB_Menu; | |||
class Menu | |||
{ | |||
protected $links = []; | |||
public function __construct($type) | |||
{ | |||
$this->_loadMenu($type); | |||
} | |||
protected function _loadMenu($type) | |||
{ | |||
$db = new DB_Menu(); | |||
$res = $db->getMenu($type); | |||
while ($result = $res->fetchArray(SQLITE3_ASSOC)) { | |||
$link = new Link(); | |||
$link->setID($result['id']); | |||
$link->setName($result['name']); | |||
$link->setUrl($result['url']); | |||
$link->setType($result['type']); | |||
$link->setParent($result['parent']); | |||
$this->links[] = $link; | |||
} | |||
} | |||
public function getLinks() | |||
{ | |||
return $this->links; | |||
} | |||
} |