1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Webcms admin module package. |
5
|
|
|
*/ |
6
|
|
|
namespace WebCMS2\Common; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
abstract class BasePresenter extends \Nette\Application\UI\Presenter |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @var [type] |
16
|
|
|
*/ |
17
|
|
|
protected $em; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* |
22
|
|
|
* @return [type] [description] |
23
|
|
|
*/ |
24
|
|
|
abstract protected function getLanguageId(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* |
29
|
|
|
* @return [type] [description] |
30
|
|
|
*/ |
31
|
|
|
protected function startUp() |
32
|
|
|
{ |
33
|
|
|
parent::startUp(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* |
39
|
|
|
* @return [type] [description] |
40
|
|
|
*/ |
41
|
|
|
protected function getSettings() |
42
|
|
|
{ |
43
|
|
|
$query = $this->em->createQuery('SELECT s FROM WebCMS\Entity\Setting s WHERE s.language = '.$this->getLanguageId().' OR s.language IS NULL'); |
44
|
|
|
$tmp = $query->getResult(); |
45
|
|
|
|
46
|
|
|
$settings = array(); |
47
|
|
|
foreach ($tmp as $s) { |
48
|
|
|
$settings[$s->getSection()][$s->getKey()] = $s; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $settings; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Injects entity manager. |
56
|
|
|
* |
57
|
|
|
* @param \Doctrine\ORM\EntityManager $em |
58
|
|
|
* |
59
|
|
|
* @return BasePresenter |
60
|
|
|
* @throws \Nette\InvalidStateException |
61
|
|
|
*/ |
62
|
|
|
public function injectEntityManager(\Doctrine\ORM\EntityManager $em) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if ($this->em) { |
65
|
|
|
throw new \Nette\InvalidStateException('Entity manager has been already set.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->em = $em; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Generate sitemap.xml file in www (public) directory. |
75
|
|
|
* |
76
|
|
|
* @return XML sitemap |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function generateSitemap() |
79
|
|
|
{ |
80
|
|
|
$sitemapXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"; |
81
|
|
|
|
82
|
|
|
$repository = $this->em->getRepository('WebCMS\Entity\Page'); |
83
|
|
|
$pages = $repository->findAll(); |
84
|
|
|
|
85
|
|
|
foreach ($pages as $page) { |
86
|
|
|
if ($page->getParent() !== null && $page->getVisible()) { |
87
|
|
|
$sitemapXml .= "<url>\n\t<loc>".$this->getSitemapLink($page)."</loc>\n</url>\n"; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$sitemapXml .= '</urlset>'; |
92
|
|
|
|
93
|
|
|
file_put_contents('./sitemap.xml', $sitemapXml); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get single sitemap link url address. |
98
|
|
|
* |
99
|
|
|
* @param \WebCMS2\Entity\Page $page Page entity object. |
100
|
|
|
* @return string Url address of the link. |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
private function getSitemapLink($page) |
103
|
|
|
{ |
104
|
|
|
$url = $this->context->httpRequest->url->baseUrl; |
105
|
|
|
$url .= !$page->getLanguage()->getDefaultFrontend() ? $page->getLanguage()->getAbbr().'/' : ''; |
106
|
|
|
$url .= $page->getPath(); |
107
|
|
|
|
108
|
|
|
return $url; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* |
114
|
|
|
* @return [type] [description] |
115
|
|
|
*/ |
116
|
|
|
protected function getAllLanguages() |
117
|
|
|
{ |
118
|
|
|
$languages = $this->em->getRepository('WebCMS\Entity\Language')->findAll(); |
119
|
|
|
|
120
|
|
|
$langs = array('' => $this->translation['Pick a language']); |
121
|
|
|
foreach ($languages as $l) { |
122
|
|
|
$langs[$l->getId()] = $l->getName(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $langs; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* |
131
|
|
|
* @param [type] $name [description] |
132
|
|
|
* @return [type] [description] |
133
|
|
|
*/ |
134
|
|
|
protected function createObject($name) |
135
|
|
|
{ |
136
|
|
|
$expl = explode('-', $name); |
137
|
|
|
|
138
|
|
|
$objectName = ucfirst($expl[0]); |
139
|
|
|
$objectName = "\\WebCMS\\$objectName"."Module\\".$objectName; |
140
|
|
|
|
141
|
|
|
return new $objectName(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.