1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sitemap\Controller; |
4
|
|
|
|
5
|
|
|
use Application\Mvc\Controller; |
6
|
|
|
use Cms\Model\Language; |
7
|
|
|
use Application\Mvc\Router\DefaultRouter; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class IndexController extends Controller |
11
|
|
|
{ |
12
|
|
|
private $cacheViewKey; |
13
|
|
|
private $models; |
14
|
|
|
private $links = []; |
15
|
|
|
|
16
|
|
|
public function initialize() |
17
|
|
|
{ |
18
|
|
|
$this->cacheViewKey = HOST_HASH . md5('Sitemap\Model\Sitemap'); |
19
|
|
|
|
20
|
|
|
$this->models = [ |
21
|
|
|
[ |
22
|
|
|
'class' => 'Publication', |
23
|
|
|
'model' => 'Publication', |
24
|
|
|
'where' => "", // preview_inner='0' , etc. |
25
|
|
|
'getLink' => function($model, $lang){ |
26
|
|
|
return $this->langUrlCustom([ |
27
|
|
|
'for' => 'publication', |
28
|
|
|
'type' => $model->getTypeSlug(), |
29
|
|
|
'slug' => $model->getSlug()], $lang); |
30
|
|
|
} |
31
|
|
|
],[ |
32
|
|
|
'class' => 'Page', |
33
|
|
|
'model' => 'Page', |
34
|
|
|
'getLink' => function($model, $lang){ |
35
|
|
|
return $this->langUrlCustom([ |
36
|
|
|
'for' => 'page', |
37
|
|
|
'slug' => $model->getSlug()], $lang); |
38
|
|
|
}, |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
public function indexAction() |
45
|
|
|
{ |
46
|
|
|
$this->view->setRenderLevel( \Phalcon\Mvc\View::LEVEL_NO_RENDER ); |
47
|
|
|
$cache = $this->getDi()->get('cache'); |
|
|
|
|
48
|
|
|
$sitemap_xml = $cache->get($this->cacheViewKey); |
49
|
|
|
|
50
|
|
|
if(!$sitemap_xml){ |
51
|
|
|
$langs = Language::find(['columns' => 'iso,primary']); |
|
|
|
|
52
|
|
|
|
53
|
|
|
//link(s) for main-page(s) |
54
|
|
|
foreach ($langs as $lang){ |
|
|
|
|
55
|
|
|
$suffix = !$lang['primary'] ? $lang['iso'] . '/' : ''; |
56
|
|
|
$this->links[] = [ |
57
|
|
|
'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/' . $suffix, |
58
|
|
|
'updated_at' => date('c',time()), |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($this->models as $m) { |
63
|
|
|
$class_name = '\\' . $m['class'] . '\Model\\' . $m['model']; |
64
|
|
|
$where = !empty($m['where']) ? $m['where'] : ''; |
65
|
|
|
|
66
|
|
|
$rows = $class_name::find($where); |
67
|
|
|
|
68
|
|
|
foreach ($langs as $lang) { |
|
|
|
|
69
|
|
|
foreach ($rows as $row) { |
70
|
|
|
$row::setCustomLang($lang->iso); |
71
|
|
|
if($row->getSlug() !== 'index' && $row->getTitle()){ |
72
|
|
|
$this->links[] = [ |
73
|
|
|
'url' => 'http://' . $_SERVER['HTTP_HOST'] . $m['getLink']($row, $lang->iso), |
74
|
|
|
'updated_at' => date('c', strtotime($row->getUpdatedAt())), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$sitemap_xml = $this->getRawXml(); |
81
|
|
|
$cache->save($this->cacheViewKey, $sitemap_xml); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->response->setHeader("Content-Type", "text/xml"); |
86
|
|
|
$this->response->setContent($sitemap_xml); |
87
|
|
|
return $this->response->send(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function langUrlCustom($params, $lang) |
91
|
|
|
{ |
92
|
|
|
$routeName = $params['for']; |
93
|
|
|
$routeName = DefaultRouter::ML_PREFIX . $routeName . '_' . $lang; |
94
|
|
|
$params['for'] = $routeName; |
95
|
|
|
return $this->url->get($params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getRawXml() |
99
|
|
|
{ |
100
|
|
|
$this->view->setRenderLevel( \Phalcon\Mvc\View::LEVEL_ACTION_VIEW ); |
101
|
|
|
$this->view->links = $this->links; |
|
|
|
|
102
|
|
|
$this->view->start(); |
103
|
|
|
$this->view->setLayoutsDir('../views/'); |
104
|
|
|
$this->view->render('index', 'index'); |
105
|
|
|
$this->view->finish(); |
106
|
|
|
return $this->view->getContent(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.