-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sitemap.class.php
executable file
·175 lines (143 loc) · 4.13 KB
/
Sitemap.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?PHP
/**
* Simpla CMS
*
* @copyright 2009 Denis Pikusov
* @link http://simp.la
* @author Denis Pikusov
*
* Генерация google sitemap
*
*/
require_once('Widget.class.php');
require_once('Storefront.class.php');
class Sitemap extends Widget
{
var $single = false;
var $title = 'Карта сайта';
var $sections = array();
var $news = array();
var $articles = array();
var $catalog = array();
/**
*
* Конструктор
*
*/
function Sitemap(&$parent)
{
Widget::Widget($parent);
}
/**
*
* Отображение
*
*/
function fetch()
{
// Разделы меню
$this->db->query("SELECT url, DATE_FORMAT(modified, '%Y-%m-%d') as lastmod FROM sections WHERE enabled=1 AND menu_id>0 AND url!='404' ORDER BY order_num");
$this->sections = $this->db->results();
// Новости
$this->db->query("SELECT url, DATE_FORMAT(modified, '%Y-%m-%d') as lastmod FROM news WHERE enabled=1 ORDER BY news_id DESC");
$this->news = $this->db->results();
// Статьи
$this->db->query("SELECT url, DATE_FORMAT(modified, '%Y-%m-%d') as lastmod FROM articles WHERE enabled=1 ORDER BY order_num DESC");
$this->articles = $this->db->results();
// Каталог
$this->catalog = Storefront::get_catalog();
if($this->param('format')=='google')
{
$this->single = true;
return $this->google_site_map();
}
else
{
return $this->site_map();
}
}
function google_site_map()
{
header('Content-Type: text/xml');
$map = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$map.= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
// Главная страница
$url = "http://$this->root_url";
$lastmod = date("Y-m-d");
$map.= "\t<url>"."\n";
$map.= "\t\t<loc>$url</loc>"."\n";
$map.= "\t\t<lastmod>$lastmod</lastmod>"."\n";
$map.= "\t</url>"."\n";
// Разделы меню
if(!empty($this->sections))
foreach($this->sections as $section)
{
$url = "http://$this->root_url/sections/".$this->esc($section->url);
$map.= "\t<url>"."\n";
$map.= "\t\t<loc>$url</loc>"."\n";
$map.= "\t\t<lastmod>$section->lastmod</lastmod>"."\n";
$map.= "\t</url>"."\n";
}
// Новости
if(!empty($this->news))
foreach($this->news as $n)
{
$url = "http://$this->root_url/news/".$this->esc($n->url);
$map.= "\t<url>"."\n";
$map.= "\t\t<loc>$url</loc>"."\n";
$map.= "\t\t<lastmod>$n->lastmod</lastmod>"."\n";
$map.= "\t</url>"."\n";
}
// Статьи
if(!empty($this->articles))
foreach($this->articles as $article)
{
$url = "http://$this->root_url/articles/".$this->esc($article->url);
$map.= "\t<url>"."\n";
$map.= "\t\t<loc>$url</loc>"."\n";
$map.= "\t\t<lastmod>$article->lastmod</lastmod>"."\n";
$map.= "\t</url>"."\n";
}
// Товары
$map.= $this->category_map_recursive($this->catalog);
$map.= '</urlset>'."\n";
$this->body = $map;
return $map;
}
function category_map_recursive($categories)
{
foreach($categories as $category)
{
if($category->products)
{
$url = "http://$this->root_url/catalog/".$this->esc($category->url);
$map.= "\t<url>"."\n";
$map.= "\t\t<loc>$url</loc>"."\n";
$map.= "\t\t<lastmod>".date("Y-m-d")."</lastmod>"."\n";
$map.= "\t</url>"."\n";
foreach($category->products as $product)
{
$url = "http://$this->root_url/products/".$this->esc($product->url);
$map.= "\t<url>"."\n";
$map.= "\t\t<loc>$url</loc>"."\n";
$map.= "\t\t<lastmod>$product->modified</lastmod>"."\n";
$map.= "\t</url>"."\n";
}
}
if($category->subcategories)
$map .= $this->category_map_recursive($category->subcategories);
}
return $map;
}
function site_map()
{
// Передаем в шаблон
$this->smarty->assign('catalog', $this->catalog);
$this->body = $this->smarty->fetch('sitemap.tpl');
return $this->body;
}
function esc($s)
{
return(htmlspecialchars($s, ENT_QUOTES, 'UTF-8'));
}
}