From 2acbd7b7c397f82aef9ccfe29e0b41327a9610c2 Mon Sep 17 00:00:00 2001 From: Johan Zandstra Date: Mon, 18 Dec 2017 10:22:33 +0100 Subject: [PATCH] Bugifx if there are no categories or sections in the website --- CHANGELOG.md | 4 + composer.json | 2 +- src/controllers/SettingsController.php | 121 +++++++++++++------------ 3 files changed, 69 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 911d25d..a7cebdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 column to the XML diff --git a/composer.json b/composer.json index 2d3926c..b193cd6 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 428ebf8..ab5cc51 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -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, @@ -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); } } } @@ -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); } } }