From 51d40d8ca40d51096e9b25550daf14eac40b0493 Mon Sep 17 00:00:00 2001 From: filesite Date: Sat, 27 Aug 2022 23:07:50 +0800 Subject: [PATCH] add menus and dir tree sort function --- controller/Controller.php | 40 +++++++++++++++++++ .../googleimage/controller/ListController.php | 20 ++++++++-- .../googleimage/controller/SiteController.php | 20 ++++++++-- .../videoblog/controller/ListController.php | 21 ++++++++-- .../videoblog/controller/SiteController.php | 24 ++++++++++- .../controller/SiteController.php | 37 ++--------------- www/content/README.md | 4 +- 7 files changed, 120 insertions(+), 46 deletions(-) diff --git a/controller/Controller.php b/controller/Controller.php index b7e635b..e02cc48 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -188,4 +188,44 @@ Class Controller { setcookie($cookieKey, $val, $expires, $path); } + //sort menus and dirTree + protected function sortMenusAndDirTree($menus_sorted, $menus, $dirTree) { + if (empty($menus_sorted) || empty($menus) || empty($dirTree)) {return false;} + + //一级目录菜单排序 + $menu_dirs = array_column($menus, 'directory'); + $names = array_replace(array_flip($menus_sorted), array_flip($menu_dirs)); + if (!empty($names)) { + $menus_sorted = array_keys($names); + + $arr = []; + foreach($menus_sorted as $name) { + $index = array_search($name, $menu_dirs); + array_push($arr, $menus[$index]); + } + $menus = $arr; + } + + //dirTree一级目录排序 + $sorted_dirs = array_column($menus, 'directory'); + $tree_dirs = array_column($dirTree, 'directory'); + $names = array_replace(array_flip($sorted_dirs), array_flip($tree_dirs)); + if (!empty($names)) { + $sorted_dirs = array_keys($names); + + $arr = []; + foreach($sorted_dirs as $name) { + foreach($dirTree as $index => $item) { + if (!empty($item['directory']) && $item['directory'] == $name) { + array_push($arr, $item); + break; + } + } + } + $dirTree = $arr; + } + + return compact('menus', 'dirTree'); + } + } diff --git a/themes/googleimage/controller/ListController.php b/themes/googleimage/controller/ListController.php index 8819e16..bfc7d18 100644 --- a/themes/googleimage/controller/ListController.php +++ b/themes/googleimage/controller/ListController.php @@ -9,6 +9,11 @@ Class ListController extends Controller { public function actionIndex() { //获取数据 + $menus = []; //菜单,一级目录 + $htmlReadme = ''; //Readme.md 内容,底部网站详细介绍 + $htmlCateReadme = ''; //当前目录下的Readme.md 内容 + $menus_sorted = []; //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); @@ -16,12 +21,14 @@ Class ListController extends Controller { //获取目录 $menus = $scanner->getMenus(); - $cateId = $this->get('id', $menus[0]['id']); $titles = []; - $htmlReadme = ''; $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + $titles = $scanner->getMDTitles($readmeFile['id']); $Parsedown = new Parsedown(); @@ -30,12 +37,19 @@ Class ListController extends Controller { $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } + //排序 + $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); + if (!empty($sortedTree)) { + $menus = $sortedTree['menus']; + $dirTree = $sortedTree['dirTree']; + } + //获取目录面包屑 + $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId]; $breadcrumbs = $this->getBreadcrumbs($menus, $subcate); //获取当前目录下的readme - $htmlCateReadme = ''; $cateReadmeFile = $scanner->getDefaultReadme($cateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); diff --git a/themes/googleimage/controller/SiteController.php b/themes/googleimage/controller/SiteController.php index a278b09..3f9e268 100644 --- a/themes/googleimage/controller/SiteController.php +++ b/themes/googleimage/controller/SiteController.php @@ -9,6 +9,11 @@ Class SiteController extends Controller { public function actionIndex() { //获取数据 + $menus = []; //菜单,一级目录 + $htmlReadme = ''; //Readme.md 内容,底部网站详细介绍 + $htmlCateReadme = ''; //当前目录下的Readme.md 内容 + $menus_sorted = []; //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); @@ -16,12 +21,14 @@ Class SiteController extends Controller { //获取目录 $menus = $scanner->getMenus(); - $cateId = $this->get('id', $menus[0]['id']); $titles = []; - $htmlReadme = ''; $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + $titles = $scanner->getMDTitles($readmeFile['id']); $Parsedown = new Parsedown(); @@ -30,10 +37,17 @@ Class SiteController extends Controller { $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } + //排序 + $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); + if (!empty($sortedTree)) { + $menus = $sortedTree['menus']; + $dirTree = $sortedTree['dirTree']; + } + + $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId]; //获取当前目录下的readme - $htmlCateReadme = ''; $cateReadmeFile = $scanner->getDefaultReadme($cateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); diff --git a/themes/videoblog/controller/ListController.php b/themes/videoblog/controller/ListController.php index b4c87b8..505a15a 100644 --- a/themes/videoblog/controller/ListController.php +++ b/themes/videoblog/controller/ListController.php @@ -9,6 +9,11 @@ Class ListController extends Controller { public function actionIndex() { //获取数据 + $menus = []; //菜单,一级目录 + $htmlReadme = ''; //Readme.md 内容,底部网站详细介绍 + $htmlCateReadme = ''; //当前目录下的Readme.md 内容 + $menus_sorted = []; //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3); @@ -16,13 +21,14 @@ Class ListController extends Controller { //获取目录 $menus = $scanner->getMenus(); - $cateId = $this->get('id', $menus[0]['id']); - //print_r($menus);exit; $titles = []; - $htmlReadme = ''; $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + $titles = $scanner->getMDTitles($readmeFile['id']); $Parsedown = new Parsedown(); @@ -31,12 +37,19 @@ Class ListController extends Controller { $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } + //排序 + $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); + if (!empty($sortedTree)) { + $menus = $sortedTree['menus']; + $dirTree = $sortedTree['dirTree']; + } + //获取目录面包屑 + $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId]; $breadcrumbs = $this->getBreadcrumbs($scanResults, $subcate); //获取当前目录下的readme - $htmlCateReadme = ''; $cateReadmeFile = $scanner->getDefaultReadme($cateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); diff --git a/themes/videoblog/controller/SiteController.php b/themes/videoblog/controller/SiteController.php index 76dbf88..740b922 100644 --- a/themes/videoblog/controller/SiteController.php +++ b/themes/videoblog/controller/SiteController.php @@ -9,6 +9,11 @@ Class SiteController extends Controller { public function actionIndex() { //获取数据 + $menus = []; //菜单,一级目录 + $htmlReadme = ''; //Readme.md 内容,底部网站详细介绍 + $htmlCateReadme = ''; //当前目录下的Readme.md 内容 + $menus_sorted = []; //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); @@ -16,12 +21,15 @@ Class SiteController extends Controller { //获取目录 $menus = $scanner->getMenus(); - $cateId = $this->get('id', $menus[0]['id']); $titles = []; $htmlReadme = ''; $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + $titles = $scanner->getMDTitles($readmeFile['id']); $Parsedown = new Parsedown(); @@ -30,10 +38,18 @@ Class SiteController extends Controller { $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } + //排序 + $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); + if (!empty($sortedTree)) { + $menus = $sortedTree['menus']; + $dirTree = $sortedTree['dirTree']; + } + + //默认显示的目录 + $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId]; //获取当前目录下的readme - $htmlCateReadme = ''; $cateReadmeFile = $scanner->getDefaultReadme($cateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); @@ -42,6 +58,10 @@ Class SiteController extends Controller { $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); } + + + + $pageTitle = !empty($titles) ? $titles[0]['name'] : "FileSite.io - 无数据库、基于文件和目录的Markdown文档、网址导航、图书、图片、视频网站PHP开源系统"; if (!empty($readmeFile['title'])) { $pageTitle = $readmeFile['title']; diff --git a/themes/webdirectory/controller/SiteController.php b/themes/webdirectory/controller/SiteController.php index 098337d..26d2721 100644 --- a/themes/webdirectory/controller/SiteController.php +++ b/themes/webdirectory/controller/SiteController.php @@ -43,39 +43,10 @@ Class SiteController extends Controller { } //排序 - if (!empty($menus_sorted) && !empty($menus)) { - //一级目录菜单排序 - $menu_dirs = array_column($menus, 'directory'); - $names = array_replace(array_flip($menus_sorted), array_flip($menu_dirs)); - if (!empty($names)) { - $menus_sorted = array_keys($names); - - $arr = []; - foreach($menus_sorted as $name) { - $index = array_search($name, $menu_dirs); - array_push($arr, $menus[$index]); - } - $menus = $arr; - } - - //dirTree一级目录排序 - $sorted_dirs = array_column($menus, 'directory'); - $tree_dirs = array_column($dirTree, 'directory'); - $names = array_replace(array_flip($sorted_dirs), array_flip($tree_dirs)); - if (!empty($names)) { - $sorted_dirs = array_keys($names); - - $arr = []; - foreach($sorted_dirs as $name) { - foreach($dirTree as $index => $item) { - if (!empty($item['directory']) && $item['directory'] == $name) { - array_push($arr, $item); - break; - } - } - } - $dirTree = $arr; - } + $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); + if (!empty($sortedTree)) { + $menus = $sortedTree['menus']; + $dirTree = $sortedTree['dirTree']; } diff --git a/www/content/README.md b/www/content/README.md index cb22190..4de198f 100644 --- a/www/content/README.md +++ b/www/content/README.md @@ -81,7 +81,9 @@ https://git.filesite.io/filesite/machete/archive/master.zip 5. 视频站 - - + | 名称 | 网址 | +| ---- | ---- | +| 在线学 | Duan.Filesite.io | ## 源码下载