Backend for songs.zachdecook.com
* Allow sorting by bible book and by key
| -rwxr-xr-x | index.php | 4 | ||||
| -rw-r--r-- | page.php | 11 | ||||
| -rw-r--r-- | sort.php | 66 |
3 files changed, 79 insertions, 2 deletions
@@ -85,7 +85,9 @@ include 'page.php'; $song_number = isset( $_GET['song'] ) ? $_GET['song'] : ''; if( ! $song_number ) { - echo toc(); + $sort = ''; + if ( isset( $_GET['sort'] ) ) $sort = $_GET['sort']; + echo toc($sort); } else { @@ -8,7 +8,7 @@ /** * @brief Go through the file inputfile.txt and return each song with a link to it. */ -function toc(){ +function toc( $sort ){ $handle = fopen("inputfile.txt", "r"); $entries = array(); $number = 0; @@ -42,6 +42,15 @@ function toc(){ } $toc = '<form><input type="text" id="toc-filter" placeholder="Filter by song title"/></form>'; $toc .= '<ul id="toc">'; + if ( $sort ){ + include 'sort.php'; + if ( $sort == 'key' ){ + usort( $entries, 'tocKeysort' ); + } + else if ( $sort == 'verse' ){ + usort( $entries, 'tocBooksort' ); + } + } foreach( $entries as $item ){ $toc .= tocentry($item); } diff --git a/sort.php b/sort.php new file mode 100644 index 0000000..887178d --- /dev/null +++ b/sort.php @@ -0,0 +1,66 @@ +<?php + +function tocKeysort( $a, $b ){ + $aKey = $a['key'] ?: 'H'; + $bKey = $b['key'] ?: 'H'; + return ord($aKey[0]) - ord($bKey[0]); +} + + +function tocBooksort( $a, $b ){ +$bookOrder = array( +'Genesis', +'Exodus', +'Numbers', +'Deuteronomy', +'Joshua', +'I Samuel', +'I Chronicles', +'II Chronicles', +'Psalm', +'Proverbs', +'Isaiah', +'Jeremiah', +'Lamentations', +'Micah', +'Habakkuk', +'Zephaniah', +'Zepheniah', +'Matthew', +'Matt.', +'John', +'Romans', +'I Cor.', +'II Corinthians', +'Galatians', +'Ephesians', +'Philippians', +'Phil.', +'I Thessalonians', +'II Timothy', +'Hebrews', +'James', +'Psalm', +'I Peter', +'I John', +'Jude', +'Revelation', +'Revelations', +'Rev.', +); + $aVerse = $a['verse'] ?: ''; + $bVerse = $b['verse'] ?: ''; + $matches = array(); + $aBookKey = 3000; + $bBookKey = 3000; + if ( preg_match( '/^[0-9i]*\ ?[A-Z.]+/i', $aVerse, $matches ) ){ + $aBook = $matches[0]; + $aBookKey = array_search( $aBook, $bookOrder ); + } + if ( preg_match( '/^[0-9i]*\ ?[A-Z.]+/i', $bVerse, $matches ) ){ + $bBook = $matches[0]; + $bBookKey = array_search( $bBook, $bookOrder ); + } + + return $aBookKey - $bBookKey; +} |