From 527a37984bef04261b8fe379b4f928d1fa1d5b23 Mon Sep 17 00:00:00 2001 From: filesite Date: Sun, 17 Sep 2023 12:19:46 +0800 Subject: [PATCH] get tags done, api for tags ready --- themes/tajian/controller/ApiController.php | 34 +++++++++++++++ themes/tajian/controller/ListController.php | 11 ++++- themes/tajian/controller/SiteController.php | 48 ++++++++++++++++++++- themes/tajian/views/site/index.php | 21 +++++---- 4 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 themes/tajian/controller/ApiController.php diff --git a/themes/tajian/controller/ApiController.php b/themes/tajian/controller/ApiController.php new file mode 100644 index 0000000..583f9e0 --- /dev/null +++ b/themes/tajian/controller/ApiController.php @@ -0,0 +1,34 @@ +setWebRoot(FSC::$app['config']['content_directory']); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3); + //$scanResults = $scanner->getScanResults(); + + $code = 1; + $msg = ''; + $err = ''; + + //获取tags分类 + $data = $this->getTags($dirTree); + + + return $this->renderJson(compact('code', 'msg', 'err', 'data')); + } + +} diff --git a/themes/tajian/controller/ListController.php b/themes/tajian/controller/ListController.php index cae0e21..2491fbd 100644 --- a/themes/tajian/controller/ListController.php +++ b/themes/tajian/controller/ListController.php @@ -4,8 +4,9 @@ */ require_once __DIR__ . '/../../../lib/DirScanner.php'; require_once __DIR__ . '/../../../plugins/Parsedown.php'; +require_once __DIR__ . '/SiteController.php'; -Class ListController extends Controller { +Class ListController extends SiteController { public function actionIndex() { //获取数据 @@ -58,6 +59,12 @@ Class ListController extends Controller { $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); } + //获取tags分类 + $tags = $this->getTags($dirTree); + + //TODO: 根据tag获取相关数据,并传给视图;调整视图兼容tag的数据结构 + + $pageTitle = $defaultTitle = !empty($titles) ? $titles[0]['name'] : FSC::$app['config']['site_name']; if (!empty($subcate)) { $pageTitle = "{$subcate['directory']}相关视频,来自{$defaultTitle}"; @@ -66,7 +73,7 @@ Class ListController extends Controller { } } $viewName = '//site/index'; //共享视图 - $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme'); + $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', 'tags'); return $this->render($viewName, $params, $pageTitle); } diff --git a/themes/tajian/controller/SiteController.php b/themes/tajian/controller/SiteController.php index 13b8c90..9bdd772 100644 --- a/themes/tajian/controller/SiteController.php +++ b/themes/tajian/controller/SiteController.php @@ -58,6 +58,10 @@ Class SiteController extends Controller { $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); } + //获取tags分类 + $tags = $this->getTags($dirTree); + + $pageTitle = $defaultTitle = !empty($titles) ? $titles[0]['name'] : FSC::$app['config']['site_name']; if (!empty($subcate)) { $pageTitle = "{$subcate['directory']},来自{$defaultTitle}"; @@ -65,10 +69,50 @@ Class SiteController extends Controller { if (!empty($readmeFile['title'])) { $pageTitle = "{$readmeFile['title']},来自{$defaultTitle}"; } - + $viewName = 'index'; - $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme'); + $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'tags'); return $this->render($viewName, $params, $pageTitle); } + //获取tag分类 + protected function getTags($dirTree) { + $tags = array(); + + $tagDir = null; + $tagSaveDirName = str_replace('/', '', FSC::$app['config']['tajian']['tag_dir']); + foreach($dirTree as $id => $item) { + if (!empty($item['directory']) && $item['directory'] == $tagSaveDirName) { + $tagDir = $item; + break; + } + } + + if (!empty($tagDir) && !empty($tagDir['files'])) { + foreach($tagDir['files'] as $id => $item) { + if (empty($item['realpath'])) { //如果是txt描述文件 + $tag = $this->getTagItem($item); + $tags[$tag['id']] = $tag; + } + } + } + + return $tags; + } + + protected function getTagItem($tagFile) { + $tag = array(); + + foreach($tagFile as $name => $item) { + if ($name == 'id') { + $tag['id'] = $item; + }else { + $tag['name'] = $name; + $tag['files'] = explode("\n", $item); + } + } + + return $tag; + } + } diff --git a/themes/tajian/views/site/index.php b/themes/tajian/views/site/index.php index 1fe7a12..9082ff1 100644 --- a/themes/tajian/views/site/index.php +++ b/themes/tajian/views/site/index.php @@ -7,11 +7,10 @@ $imgPreffix = FSC::$app['config']['content_directory'] . FSC::$app['config']['ta $item) { - $selected = $item['id'] == $selectedId || (!empty($breadcrumbs) && $item['id'] == $breadcrumbs[0]['id']) ? 'selected' : ''; +if (!empty($viewData['tags'])) { //显示tags分类 + foreach($viewData['tags'] as $id => $item) { echo <<{$item['directory']} + {$item['name']} eof; } } @@ -58,13 +57,6 @@ eof; eof; } - //当前目录的readme详细介绍 - if (!empty($viewData['htmlCateReadme'])) { - echo <<{$viewData['htmlCateReadme']} -eof; - } - if (!empty($category['files'])) { //一级目录支持,目录下直接存放视频文件 foreach($category['files'] as $file) { @@ -93,5 +85,12 @@ eof; eof; } } + + //当前目录的readme详细介绍 + if (!empty($viewData['htmlCateReadme'])) { + echo <<{$viewData['htmlCateReadme']} +eof; + } ?>