Skip to content

Commit

Permalink
Avoid running a query for every childless page
Browse files Browse the repository at this point in the history
Previously, an extra query would be run for every leaf node in
the SimplePage navigation tree. Here we avoid those queries by
fetching all of the SimplePages from the outset and using the same
result to build the navigation tree.

After fetching all SimplePages, pages are binned in a 2-D array
keyed by their parent_id.
  • Loading branch information
zploskey committed Aug 10, 2017
1 parent 24370b8 commit 83d21eb
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions helpers/SimplePageFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
* @param integer|null The id of the parent page. If null, it uses the current simple page
* @param string The method by which you sort pages. Options are 'order' (default) and 'alpha'.
* @param boolean Whether to return only published pages.
* @param array Array with keys of parent_id and values = array(SimplePages)
* that have that parent_id. Used internally.
* @return array The navigation links.
*/
function simple_pages_get_links_for_children_pages($parentId = null, $sort = 'order', $requiresIsPublished = false)
function simple_pages_get_links_for_children_pages($parentId = null, $sort = 'order', $requiresIsPublished = false,
$pages = null)
{
if ($parentId === null) {
$parentPage = get_current_record('simple_pages_page', false);
Expand All @@ -26,32 +29,47 @@ function simple_pages_get_links_for_children_pages($parentId = null, $sort = 'or
}
}

$findBy = array('parent_id' => $parentId, 'sort' => $sort);
if ($requiresIsPublished) {
$findBy['is_published'] = $requiresIsPublished ? 1 : 0;
}
if (! isset($pages)) {
$findBy = array('sort' => $sort);
if ($requiresIsPublished) {
$findBy['is_published'] = $requiresIsPublished ? 1 : 0;
}
$pages = get_db()->getTable('SimplePagesPage')->findBy($findBy);

$pages = get_db()->getTable('SimplePagesPage')->findBy($findBy);
$parentIdToPages = array();
foreach ($pages as $page) {
if (! isset($parentIdToPages[$page->parent_id])) {
$parentIdToPages[$page->parent_id] = array();
}
$parentIdToPages[$page->parent_id][] = $page;
}
$pages = $parentIdToPages;
}

$navLinks = array();

foreach ($pages as $page) {
$uri = public_url($page->slug);

$subNavLinks = simple_pages_get_links_for_children_pages($page->id, $sort, $requiresIsPublished);
if (count($subNavLinks) > 0) {
$navLinks[] = array(
'label' => $page->title,
'uri' => $uri,
'pages' => $subNavLinks
);
} else {
$navLinks[] = array(
'label' => $page->title,
'uri' => $uri,
if (isset($pages[$parentId])) {
foreach ($pages[$parentId] as $page) {
$uri = public_url($page->slug);
$subNavLinks = simple_pages_get_links_for_children_pages(
$page->id, $sort, $requiresIsPublished, $pages
);

if (count($subNavLinks) > 0) {
$navLinks[] = array(
'label' => $page->title,
'uri' => $uri,
'pages' => $subNavLinks
);
} else {
$navLinks[] = array(
'label' => $page->title,
'uri' => $uri,
);
}
}
}

return $navLinks;
}

Expand Down Expand Up @@ -171,4 +189,4 @@ function simple_pages_get_parent_options($page)
}
}
return $valuePairs;
}
}

0 comments on commit 83d21eb

Please sign in to comment.