diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index 7f77852..243de46 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -753,12 +753,12 @@ eof; foreach($tags as $index => $tag) { $tag = Common::cleanSpecialChars($tag); if (!empty($tag) && !is_numeric($tag)) { - array_push($tags_ok, mb_substr($tag, 0, 5, 'utf-8')); + array_push($tags_ok, mb_substr($tag, 0, 15, 'utf-8')); } } if (empty($tags_ok)) { - $err = "请按规则填写分类:2 - 5 个汉字、数字、英文字符"; + $err = "请按规则填写分类:2 - 15 个汉字、数字、英文字符"; }else { $tags = $tags_ok; } @@ -804,7 +804,7 @@ eof; return $this->renderJson(compact('code', 'msg', 'err')); } - //删除管理 + //删除分类 public function actionDeletetag() { $ip = $this->getUserIp(); $check_time = 120; //2 分钟内 @@ -857,6 +857,94 @@ eof; return $this->renderJson(compact('code', 'msg', 'err')); } + //添加分类 + public function actionAddtag() { + $ip = $this->getUserIp(); + $check_time = 120; //2 分钟内 + $max_time_in_minutes = 15; //最多 15 次 + + $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time); + if ($isUserGotRequestLimit) { + $this->logError("Request limit got, ip: {$ip}"); + throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...'); + } + + //只允许修改自己的数据 + $loginedUser = Common::getUserFromSession(); + if (empty($loginedUser['username'])) { + throw new Exception('Oops,你还没登录哦'); + }else if ( + !empty(FSC::$app['config']['multipleUserUriParse']) + && (empty(FSC::$app['user_id']) || FSC::$app['user_id'] != $loginedUser['username']) + ) { + throw new Exception('Oops,请求地址有误'); + } + + + //返回给视图的变量 + $code = 0; + $msg = ''; + $err = ''; + + //用户提交的数据检查 + $postParams = $this->post(); + if (!empty($postParams)) { + $tag_to_add = $this->post('tag', ''); + + if (empty($tag_to_add)) { + $err = "参数错误,缺少tag传参"; + }else { + $tagLen = mb_strlen($tag_to_add, 'utf-8'); + if ($tagLen < 2 || $tagLen > 15) { + $err = '分类名长度不符合规则,请填写 2 - 15 个汉字、数字、英文字符'; + } + } + + if (empty($err)) { //如果数据检查通过,尝试保存 + //获取已有的分类 + $scanner = new DirScanner(); + $scanner->setWebRoot(FSC::$app['config']['content_directory']); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3); + + $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $readmeFile = $scanner->getDefaultReadme(); + if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + } + + //获取tags分类 + $tags_current = $this->getTags($dirTree); + //排序 + if (!empty($menus_sorted) && !empty($tags_current)) { + $tags_current = $this->sortTags($menus_sorted, $tags_current); + } + //获取只包含分类名的数组 + $tmp_arr = array(); + foreach ($tags_current as $id => $tag) { + array_push($tmp_arr, $tag['name']); + } + + //最多添加 20 个分类 + if (count($tmp_arr) >= 20) { + $err = '最多添加 20 个分类,请合理规划视频分类哦'; + }else { + //保存 + $saved = $this->addTag(ucfirst($tag_to_add)); + if (!empty($saved)) { + $msg = "分类已添加"; + $code = 1; + }else { + $err = '添加分类失败,请稍后重试'; + } + } + } + } + + return $this->renderJson(compact('code', 'msg', 'err')); + } + //TODO: 视频管理 diff --git a/themes/tajian/controller/MyController.php b/themes/tajian/controller/MyController.php index daf54a4..7740163 100644 --- a/themes/tajian/controller/MyController.php +++ b/themes/tajian/controller/MyController.php @@ -73,4 +73,11 @@ Class MyController extends SiteController { return $this->actionIndex($viewName, $defaultTitle); } + //添加分类 + public function actionAddtag() { + $defaultTitle = "添加分类"; + $viewName = 'tag_new'; + return $this->actionIndex($viewName, $defaultTitle); + } + } \ No newline at end of file diff --git a/themes/tajian/controller/SiteController.php b/themes/tajian/controller/SiteController.php index cb2ebec..825cae6 100644 --- a/themes/tajian/controller/SiteController.php +++ b/themes/tajian/controller/SiteController.php @@ -247,6 +247,37 @@ Class SiteController extends Controller { return $done; } + //添加分类 + protected function addTag($tag) { + $done = false; + + try { + $rootDir = FSC::$app['config']['content_directory']; + $tagSaveDirName = str_replace('/', '', FSC::$app['config']['tajian']['tag_dir']); + + $tagFile = "{$rootDir}{$tagSaveDirName}/{$tag}.txt"; + if (file_exists($tagFile) == false) { + touch($tagFile); + } + + //更新排序文件 + $sortFile = "{$rootDir}README_sort.txt"; + if (file_exists($sortFile)) { + $content = file_get_contents($sortFile); + $content = "{$content}\n{$tag}"; + file_put_contents($sortFile, $content); + }else { + file_put_contents($sortFile, $tag); + } + + $done = true; + }catch(Exception $e) { + $this->logError("Add tag {$tag} failed: " . $e->getMessage()); + } + + return $done; + } + protected function getNickname($readmeFile) { $nickname = ''; diff --git a/themes/tajian/views/my/index.php b/themes/tajian/views/my/index.php index 4bdb038..250bcb6 100644 --- a/themes/tajian/views/my/index.php +++ b/themes/tajian/views/my/index.php @@ -22,7 +22,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use
diff --git a/themes/tajian/views/my/tag_new.php b/themes/tajian/views/my/tag_new.php new file mode 100644 index 0000000..e15a6ae --- /dev/null +++ b/themes/tajian/views/my/tag_new.php @@ -0,0 +1,30 @@ +
说明:
- 分类名请填 2 - 5 个汉字、数字、英文字符;
+ 分类名请填 2 - 15 个汉字、数字、英文字符;
点击上下箭头图标改变分类顺序,删除某个分类并不会删除这个分类里的视频。