Commit 7a21d0d4 authored by Sean Madsen's avatar Sean Madsen

Adding book sorting functionality to close #31

parent dac7975d
name: Developer guide
weight: 10
description: For CiviCRM developers
langs:
en:
repo: 'https://github.com/civicrm/civicrm-dev-docs'
latest: master
#stable: master
stable: master
name: User guide
weight: -100
description: Aimed at day to day users of CiviCRM.
langs:
en:
......
......@@ -26,6 +26,31 @@ class BookLoader
$this->configDir = $configDir;
}
/**
* Compares 2 books, side by side, for the purpose of sorting an array of
* books with uasort()
* @param array $bookA
* @param array $bookB
* @return int
*/
private function compareBooksBySortOrder($bookA, $bookB) {
$aWeight = isset($bookA['weight']) ? $bookA['weight'] : 0;
$bWeight = isset($bookB['weight']) ? $bookB['weight'] : 0;
if ( $aWeight == $bWeight ) {
return strnatcmp($bookA['name'], $bookB['name']);
}
return $aWeight - $bWeight;
}
/**
* Modifies the internal book cache by sorting the array of books correctly
*/
private function cacheSort() {
if( isset($this->cache) ) {
uasort($this->cache, [$this, 'compareBooksBySortOrder']);
}
}
/**
* @return array
*/
......@@ -40,6 +65,7 @@ class BookLoader
$books[basename($file, '.yml')] = $yaml->parse(file_get_contents("$file"));
}
$this->cache = $books;
$this->cacheSort();
}
return $this->cache;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment