|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Albert221\Blog\Widget; |
|
4
|
|
|
|
|
5
|
|
|
use Albert221\Blog\Repository\CategoryRepositoryInterface; |
|
6
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
|
7
|
|
|
use Albert221\Blog\Repository\TagRepositoryInterface; |
|
8
|
|
|
use Twig_Environment; |
|
9
|
|
|
use Twig_Extension; |
|
10
|
|
|
use Twig_Extension_GlobalsInterface; |
|
11
|
|
|
|
|
12
|
|
|
// TODO: Load widgets from database |
|
13
|
|
|
class TwigWidgetExtension extends Twig_Extension implements Twig_Extension_GlobalsInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $twig; |
|
16
|
|
|
private $posts; |
|
17
|
|
|
private $categories; |
|
18
|
|
|
private $tags; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
Twig_Environment $twig, |
|
22
|
|
|
PostRepositoryInterface $posts, |
|
23
|
|
|
CategoryRepositoryInterface $categories, |
|
24
|
|
|
TagRepositoryInterface $tags |
|
25
|
|
|
) { |
|
26
|
|
|
$this->twig = $twig; |
|
27
|
|
|
$this->posts = $posts; |
|
28
|
|
|
$this->categories = $categories; |
|
29
|
|
|
$this->tags = $tags; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function getSidebarWidgets() |
|
33
|
|
|
{ |
|
34
|
|
|
$sidebarWidgetManager = new WidgetManager(); |
|
35
|
|
|
$sidebarWidgetManager->add(new RecentPosts($this->posts, $this->twig, 10)); |
|
36
|
|
|
$sidebarWidgetManager->add(new TagCloud($this->tags, $this->twig)); |
|
37
|
|
|
|
|
38
|
|
|
return $sidebarWidgetManager->getWidgets(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function getFooterWidgets() |
|
42
|
|
|
{ |
|
43
|
|
|
$footerWidgetManager = new WidgetManager(); |
|
44
|
|
|
$footerWidgetManager->add(new RecentPosts($this->posts, $this->twig, 5)); |
|
45
|
|
|
$footerWidgetManager->add(new RecentCategories($this->categories, $this->twig, 5)); |
|
46
|
|
|
$footerWidgetManager->add(new HTML('Polecane strony', '<ul> |
|
47
|
|
|
<li><a href="#">Lorem ipsum.</a></li> |
|
48
|
|
|
<li><a href="#">Amet, ipsam?</a></li> |
|
49
|
|
|
<li><a href="#">Animi, alias.</a></li> |
|
50
|
|
|
<li><a href="#">Sed, non.</a></li> |
|
51
|
|
|
<li><a href="#">Officiis, harum.</a></li> |
|
52
|
|
|
</ul>')); |
|
53
|
|
|
|
|
54
|
|
|
return $footerWidgetManager->getWidgets(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getGlobals() |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
'sidebarWidgets' => $this->getSidebarWidgets(), |
|
64
|
|
|
'footerWidgets' => $this->getFooterWidgets() |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getName() |
|
72
|
|
|
{ |
|
73
|
|
|
return 'widgets'; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|