Skip to content

Instantly share code, notes, and snippets.

@seriousManual
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seriousManual/50e74306f91c37b25135 to your computer and use it in GitHub Desktop.
Save seriousManual/50e74306f91c37b25135 to your computer and use it in GitHub Desktop.
shopware sitemap
<?php
/**
* Shopware 4
* Copyright © shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/
/**
* Sitemap controller
*
* @category Shopware
* @package Shopware\Controllers\Frontend
* @copyright Copyright (c) shopware AG (http://www.shopware.de)
*/
class Shopware_Controllers_Frontend_SitemapXml extends Enlight_Controller_Action
{
/**
* @var \Shopware\Models\Category\Repository
*/
protected $repository;
/**
* Init controller method
*/
public function init()
{
$this->Front()->Plugins()->ViewRenderer()->setNoRender();
$this->Front()->setParam('disableOutputBuffering', true);
$this->Front()->returnResponse(true);
$this->Response()->setHeader('Content-Type', 'text/xml; charset=utf-8');
$this->Response()->sendResponse();
$this->repository = Shopware()->Models()->getRepository(
'Shopware\Models\Category\Category'
);
set_time_limit(0);
}
/**
* Index action method
*/
public function indexAction()
{
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";
$parentId = Shopware()->Shop()->get('parentID');
$this->readCategoryUrls($parentId);
$this->readArticleUrls($parentId);
$this->readBlogUrls($parentId);
$this->readCustomUrls('gTop');
echo "</urlset>\r\n";
}
/**
* Print category urls
*
* @param int $parentId
*/
public function readCategoryUrls($parentId)
{
$categories = $this->repository->getActiveChildrenList($parentId);
foreach ($categories as $category) {
if (!empty($category['external'])) {
continue;
}
//use a different link if it is a blog category
if (!empty($category['blog'])) {
$link = $this->Front()->Router()->assemble(array(
'sViewport' => 'blog',
'sCategory' => $category['id'],
'title' => $category['name']
));
$this->printEntry($link, $category['changed'], 0.5);
} else {
$link = $this->Front()->Router()->assemble(array(
'sViewport' => 'cat',
'sCategory' => $category['id'],
'title' => $category['name']
));
$this->printEntry($link, $category['changed'], 0.8);
}
}
}
/**
* Read article urls
*
* @param int $parentId
*/
public function readArticleUrls($parentId)
{
$sql = "
SELECT
a.id,
DATE(a.changetime) as changed
FROM s_articles a
INNER JOIN s_articles_categories_ro ac
ON ac.articleID = a.id
AND ac.categoryID = ?
INNER JOIN s_categories c
ON c.id = ac.categoryID
AND c.active = 1
WHERE a.active = 1
GROUP BY a.id
";
$result = Shopware()->Db()->query($sql, array($parentId));
if (!$result->rowCount()) {
return;
}
while ($url = $result->fetch()) {
$link = $this->Front()->Router()->assemble(array(
'sViewport' => 'detail',
'sArticle' => $url['id']
));
$this->printEntry($link, new DateTime($url['changed']), 1);
}
}
/**
* Reads the blog item urls
*
* @param $parentId
*/
public function readBlogUrls($parentId)
{
$query = $this->repository->getBlogCategoriesByParentQuery($parentId);
$blogCategories = $query->getArrayResult();
$blogIds = array();
foreach ($blogCategories as $blogCategory) {
$blogIds[] = $blogCategory["id"];
}
if (empty($blogIds)) {
return;
}
$blogIds = Shopware()->Db()->quote($blogIds);
$sql = "
SELECT id, category_id, DATE(display_date) as changed
FROM s_blog
WHERE active = 1 AND category_id IN($blogIds)
";
$result = Shopware()->Db()->query($sql);
if (!$result->rowCount()) {
return;
}
while ($blogUrlData = $result->fetch()) {
$link = $this->Front()->Router()->assemble(array(
'sViewport' => 'blog',
'sAction' => 'detail',
'sCategory' => $blogUrlData['category_id'],
'blogArticle' => $blogUrlData['id']
));
$this->printEntry($link, new DateTime($blogUrlData['changed']), 0.5);
}
}
/**
* Read certain custom urls
*
*
*/
public function readCustomUrls($group)
{
$sql = "SELECT c.id FROM s_cms_static c WHERE c.grouping = \"$group\"";
$result = Shopware()->Db()->query($sql);
if (!$result->rowCount()) {
return;
}
while ($url = $result->fetch()) {
$link = $this->Front()->Router()->assemble(array(
'sViewport' => 'custom',
'sCustom' => $url['id']
));
$this->printEntry($link, null, 0.3);
}
}
/**
* @param $link
* @param DateTime $changed
* @param $priority
*/
function printEntry($link, DateTime $changed = null, $priority) {
$line = '<url>';
$line .= '<loc>' . $link . '</loc>';
if ($changed) {
$line .= '<lastmod>' . $changed->format('Y-m-d') . '</lastmod>';
}
$line .= '<changefreq>weekly</changefreq>';
$line .= '<priority>' . $priority . '</priority>';
$line .= '</url>';
$line .= "\r\n";
echo $line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment