Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from Dolphiq/development
Browse files Browse the repository at this point in the history
Bugifx if there are no categories or sections in the website
  • Loading branch information
johanzandstra authored Dec 18, 2017
2 parents 5a141c0 + 2acbd7b commit 1439836
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# sitemap Changelog

## 1.0.5 - 2017-12-18
### Fixed
- Fixed bug if there are no sections or categories in the website

## 1.0.4 - 2017-12-18
### Added
- Added <lastmod> column to the XML
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dolphiq/sitemap",
"description": "Craft 3 plugin that provides an easy way to provide and manage a XML sitemap for search engines like Google and Bing",
"type": "craft-plugin",
"version": "1.0.4",
"version": "1.0.5",
"keywords": [
"craft",
"cms",
Expand Down
121 changes: 64 additions & 57 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,37 @@ public function actionIndex(): craft\web\Response
// $allSections = Craft::$app->getSections()->getAllSections();
$allSections = $this->_createEntrySectionQuery()->all();
$allStructures = [];
// print_r($allSections);
foreach($allSections as $section) {
$allStructures[] = [
'id' => $section['id'],
'type'=> $section['type'],
'heading' => $section['name'],
'enabled' => ($section['sitemapEntryId']>0 ? true : false ),
'elementCount' => $section['elementCount'],
'changefreq' => ($section['sitemapEntryId']>0 ? $section['changefreq'] : 'weekly' ),
'priority' => ($section['sitemapEntryId']>0 ? $section['priority'] : 0.5 ),
];

if (is_array($allSections)) {
foreach ($allSections as $section) {
$allStructures[] = [
'id' => $section['id'],
'type' => $section['type'],
'heading' => $section['name'],
'enabled' => ($section['sitemapEntryId'] > 0 ? true : false),
'elementCount' => $section['elementCount'],
'changefreq' => ($section['sitemapEntryId'] > 0 ? $section['changefreq'] : 'weekly'),
'priority' => ($section['sitemapEntryId'] > 0 ? $section['priority'] : 0.5),
];
}
}

$allCategories = $this->_createCategoryQuery()->all();
$allCategoryStructures = [];
// print_r($allSections);
foreach($allCategories as $category) {
$allCategoryStructures[] = [
'id' => $category['id'],
'type'=> 'category',
'heading' => $category['name'],
'enabled' => ($category['sitemapEntryId']>0 ? true : false ),
'elementCount' => $category['elementCount'],
'changefreq' => ($category['sitemapEntryId']>0 ? $category['changefreq'] : 'weekly' ),
'priority' => ($category['sitemapEntryId']>0 ? $category['priority'] : 0.5 ),
];
if (is_array($allCategories)) {
// print_r($allSections);
foreach ($allCategories as $category) {
$allCategoryStructures[] = [
'id' => $category['id'],
'type' => 'category',
'heading' => $category['name'],
'enabled' => ($category['sitemapEntryId'] > 0 ? true : false),
'elementCount' => $category['elementCount'],
'changefreq' => ($category['sitemapEntryId'] > 0 ? $category['changefreq'] : 'weekly'),
'priority' => ($category['sitemapEntryId'] > 0 ? $category['priority'] : 0.5),
];
}
}

$variables = [
'settings' => Sitemap::$plugin->getSettings(),
'source' => $source,
Expand All @@ -173,24 +176,26 @@ public function actionSaveSitemap(): craft\web\Response
$sitemapSections = $request->getBodyParam('sitemapSections');
// filter the enabled sections
$allSectionIds = [];
foreach($sitemapSections as $key => $entry) {
if($entry['enabled']) {
// filter section id from key

$id = (int) str_replace('id:', '', $key);
if($id > 0) {
// find the entry, else add one
$sitemapEntry = SitemapEntry::find()->where(['linkId' => $id, 'type' => 'section'])->one();
if(!$sitemapEntry) {
// insert / update this section
$sitemapEntry = new SitemapEntry();
if (is_array($sitemapSections)) {
foreach ($sitemapSections as $key => $entry) {
if ($entry['enabled']) {
// filter section id from key

$id = (int)str_replace('id:', '', $key);
if ($id > 0) {
// find the entry, else add one
$sitemapEntry = SitemapEntry::find()->where(['linkId' => $id, 'type' => 'section'])->one();
if (!$sitemapEntry) {
// insert / update this section
$sitemapEntry = new SitemapEntry();
}
$sitemapEntry->linkId = $id;
$sitemapEntry->type = 'section';
$sitemapEntry->priority = $entry['priority'];
$sitemapEntry->changefreq = $entry['changefreq'];
$sitemapEntry->save();
array_push($allSectionIds, $id);
}
$sitemapEntry->linkId = $id;
$sitemapEntry->type = 'section';
$sitemapEntry->priority = $entry['priority'];
$sitemapEntry->changefreq = $entry['changefreq'];
$sitemapEntry->save();
array_push($allSectionIds, $id);
}
}
}
Expand All @@ -207,24 +212,26 @@ public function actionSaveSitemap(): craft\web\Response
$sitemapCategories = $request->getBodyParam('sitemapCategories');
// filter the enabled sections
$allCategoryIds = [];
foreach($sitemapCategories as $key => $entry) {
if($entry['enabled']) {
// filter section id from key

$id = (int) str_replace('id:', '', $key);
if($id > 0) {
// find the entry, else add one
$sitemapEntry = SitemapEntry::find()->where(['linkId' => $id, 'type' => 'category'])->one();
if(!$sitemapEntry) {
// insert / update this section
$sitemapEntry = new SitemapEntry();
if (is_array($sitemapCategories)) {
foreach ($sitemapCategories as $key => $entry) {
if ($entry['enabled']) {
// filter section id from key

$id = (int)str_replace('id:', '', $key);
if ($id > 0) {
// find the entry, else add one
$sitemapEntry = SitemapEntry::find()->where(['linkId' => $id, 'type' => 'category'])->one();
if (!$sitemapEntry) {
// insert / update this section
$sitemapEntry = new SitemapEntry();
}
$sitemapEntry->linkId = $id;
$sitemapEntry->type = 'category';
$sitemapEntry->priority = $entry['priority'];
$sitemapEntry->changefreq = $entry['changefreq'];
$sitemapEntry->save();
array_push($allCategoryIds, $id);
}
$sitemapEntry->linkId = $id;
$sitemapEntry->type = 'category';
$sitemapEntry->priority = $entry['priority'];
$sitemapEntry->changefreq = $entry['changefreq'];
$sitemapEntry->save();
array_push($allCategoryIds, $id);
}
}
}
Expand Down

0 comments on commit 1439836

Please sign in to comment.