Skip to content

Commit

Permalink
Fix the method used for combining Content SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed Jun 2, 2020
1 parent 47a9237 commit ab67fa8
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/controllers/ContentSeoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace nystudio107\seomatic\controllers;

use nystudio107\seomatic\base\SeoElementInterface;
use nystudio107\seomatic\helpers\ArrayHelper;
use nystudio107\seomatic\models\MetaBundle;
use nystudio107\seomatic\Seomatic;
use nystudio107\seomatic\services\SeoElements;
Expand Down Expand Up @@ -73,6 +74,7 @@ public function actionMetaBundles(
$filter = '',
$siteId = 0
): Response {
$per_page = 2;
$data = [];
$sortField = 'sourceName';
$sortType = 'ASC';
Expand Down Expand Up @@ -101,46 +103,45 @@ public function actionMetaBundles(

// Query the db table
$offset = ($page - 1) * $per_page;

$currentSiteHandle = '';
if ((int)$siteId !== 0) {
$site = Craft::$app->getSites()->getSiteById($siteId);
if ($site !== null) {
$currentSiteHandle = $site->handle;
}
}
$bundleQuery = (new Query())
->select(['mb.*'])
->offset($offset)
->limit($per_page)
->orderBy($sortParams)
;

// Since sectionIds, CategoryIds, etc. are not unique, we need to special case for them
$bundles = [];
// Since sectionIds, CategoryIds, etc. are not unique, we need to do separate queries and combine them
$seoElements = Seomatic::$plugin->seoElements->getAllSeoElementTypes();
$tableIndex = 1;
foreach ($seoElements as $seoElement) {
$tableIndex++;

$subQuery = (new Query())
->from(['{{%seomatic_metabundles}}'])
->where(['=', 'sourceBundleType', $seoElement::META_BUNDLE_TYPE]);

if ((int)$siteId !== 0) {
$subQuery->andWhere(['sourceSiteId' => $siteId]);
}
if ($filter !== '') {
$subQuery->andWhere(['like', 'sourceName', $filter]);
}
$bundleQuery
->where(['mb'.$tableIndex.'.id' => null])
$bundleQuery = (new Query())
->select(['mb.*'])
->from(['mb' => $subQuery])
->leftJoin(['mb'.$tableIndex => $subQuery], [
->leftJoin(['mb2' => $subQuery], [
'and',
'[[mb.sourceId]] = [[mb'.$tableIndex.'.sourceId]]',
'[[mb.id]] < [[mb'.$tableIndex.'.id]]'
]);
'[[mb.sourceId]] = [[mb2.sourceId]]',
'[[mb.id]] < [[mb2.id]]'
])
->where(['mb2.id' => null])
;
$bundles = array_merge($bundles, $bundleQuery->all());
}
$bundles = $bundleQuery->all();
if ($bundles) {
// Sort it manually
ArrayHelper::multisort($bundles, $sortField, $sortType);
$bundles = array_slice($bundles, $offset, $per_page);
$dataArray = [];
// Add in the `addLink` field
foreach ($bundles as $bundle) {
Expand Down

0 comments on commit ab67fa8

Please sign in to comment.