api/App/Controllers/ArticleTree.php

272 lines
9.8 KiB
PHP

<?php
namespace App\Controllers;
use App\Models;
use Core\Controller;
use Core\View;
use Parsedown;
use App\Config;
class ArticleTree extends Controller
{
function indexAction() {
$docModel = new Models\Doc();
$limit = 5;
$offset = 0;
$firstpagedocs = [];
$typedocs = $docModel->getAllTypeDocs();
$titles = $docModel->AllTitle();
$doc_all = $docModel->getArticlesTree(-1);
$mapforbuildtree = [];
$articletree = [];
foreach ($doc_all as $key => $value){
$tmp = array(
'name' => $value['title'],
'id' =>$value['id'],
'isParent'=> false,
'url' => \App\Config::Url() . "ArticleTree/article/" . $value['id'],
'level' => $value['level']
);
$mapforbuildtree[$value['id']] = $tmp;
}
foreach ($doc_all as $key => $value){
if($value['father'] != 0){
if(!isset($mapforbuildtree[$value['father']]['children'])){
$mapforbuildtree[$value['father']]['children'] = array();
}
array_push($mapforbuildtree[$value['father']]['children'],
$mapforbuildtree[$value['id']]);
$mapforbuildtree[$value['father']]["isParent"] = true;
}
}
foreach ($doc_all as $key => $value){
if($value['level'] == 0){
array_push($articletree,$mapforbuildtree[$value['id']]);
}
}
$dat = json_encode ( $articletree ,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE );
$groupstype = $docModel->GetAllGroupType();
if(sizeof($this->params) == 2){
$limit = $this->params[0];
$offset = $this->params[1];
}
$fisrtpage = $docModel->pageDoc($limit,$offset,55);
$top5 = $docModel->top5Doc();
foreach ($fisrtpage as $key => $value) {
$obj['title'] = $value['title'];
$obj['id'] = $value['id'];
$obj['content'] = $value['content'];
$obj['type'] = $value['type'];
//数据加工
$markdown = new Parsedown;
$content = $markdown->text($value['content']);
$obj['content'] = $content;
array_push($firstpagedocs,$obj);
}
if(\App\Config::$DEBUG)
View::renderTemplate("/blog/index.html",[
"url"=>\App\Config::Url(),
'route'=>'article',
'docs'=>$firstpagedocs,
'index'=>true,
'debug'=>true,
"typedocs" => $typedocs,
"alltitle" => $titles,
"groups" => $groups,
"group_types"=>$groupstype,
"topdoc"=>$top5,
"title"=>"博客",
"openapi"=>\App\Config::OpenApiUrl(),
"treedoc"=>$dat,
]);
else{
View::renderTemplate("/blog/index.html",[
"url"=>\App\Config::Url(),
'route'=>'article',
'docs'=>$firstpagedocs,
'index'=>true,
'debug'=>false,
"typedocs" => $typedocs,
"alltitle" => $titles,
"groups" => $groups,
"group_types"=>$groupstype,
"topdoc"=>$top5,
"title"=>"博客",
"openapi"=>\App\Config::OpenApiUrl(),
"treedoc"=>$dat,
]);
}
}
function articleAction(){
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
//查看服务是否运行
$commentModel = new Models\DocComment();
if(sizeof($this->params) > 1)
$vertime = urldecode((string)$this->params[1]);
$doccoment = $commentModel->DocComments((int)$this->params[0]);
if(sizeof($this->params) > 0){
}
$docModel = new Models\Doc();
$doc_all = $docModel->getArticlesTree((int)$this->params[0]);
$doc_vers = $docModel->DocVersTime((int)$this->params[0]);
$firstpagedocs = [];
$child_docs = $docModel->getChildrenDocs($this->params[0]);
$doc = [];
if(sizeof($this->params) > 1){
if(\strstr($vertime,"20") == 0){
$doc = $docModel->docHistoryTree((int)$this->params[0],$vertime);
$doc = array_merge($doc,$child_docs);
}else{
$doc = $docModel->docTree((int)$this->params[0]);
$doc = array_merge($doc,$child_docs);
}
}else{
$doc = $docModel->docTree((int)$this->params[0]);
$doc = array_merge($doc,$child_docs);
}
$top5 = $docModel->top5Doc();
$markdowntoc = new \ParsedownToC();
$doc[0]['content'] = $markdowntoc->body($doc[0]['content']);
$doc[0]['toc'] = $markdowntoc->contentsList();
// $doc_history = $docModel->doc_history((int)$this->params[0]);
$markdown = new Parsedown;
foreach ($doc as $key => $value) {
$content = $markdown->text($value['content']);
$doc[$key]['content'] = $content;
}
$titles = $docModel->AllTitle();
$groups = $docModel->getGroups();
$groupstype = $docModel->GetAllGroupType();
if(\App\Config::$DEBUG){
View::renderTemplate("/blog/article.html",[
"url"=>\App\Config::ServerUrl(),
'route'=>'article',
'index'=>false,
'debug'=>true,
"alltitle" => $titles,
"groups" => $groups,
"group_types"=>$groupstype,
"title"=>urldecode ($doc[0]['title']),
"doc"=>$doc[0],
"docs"=>$doc,
"comments" => $doccoment,
"comment_count" => count($doccoment),
// "doc_type"=>$doc[0]["type"],
"openapi"=>\App\Config::OpenApiUrl(),
"treedoc"=>$doc_all,
"topdoc"=>$top5,
"doc_toc" => $doc[0]['toc'],
"id"=>$doc[0]['id'],
"vestimes" => $doc_vers,
]);
}
else{
View::renderTemplate("/blog/article.html",[
"url"=>\App\Config::ServerUrl(),
'route'=>'article',
'index'=>true,
'debug'=>false,
"alltitle" => $titles,
"groups" => $groups,
"group_types"=>$groupstype,
"title"=>urldecode ($this->params[1]),
"doc"=>$doc[0],
"docs"=>$doc,
"comments" => $doccoment,
"comment_count" => count($doccoment),
"doc_type"=>$doc[0]["type"],
"openapi"=>\App\Config::OpenApiUrl(),
"treedoc"=>$doc_all,
"topdoc"=>$top5,
"doc_toc" => $doc[0]['toc'],
"id"=>$doc[0]['id'],
"vestimes" => $doc_vers,
]);
}
}
function searchAction()
{
if (sizeof($this->querys) != 0) {
$docModel = new Models\Doc();
$commentModel = new Models\DocComment();
$doc = $docModel->titleDoc(rawurldecode($this->querys['title']));
$id = $doc[0]["id"];
$markdown = new \ParsedownToC();
$doc[0]['content'] = $markdown->body($doc[0]['content']);
$typedocs = $docModel->getAllTypeDocs();
$doccoment = $commentModel->DocComments($id);
$titles = $docModel->AllTitle();
$top5 = $docModel->top5Doc();
$groups = $docModel->getGroups();
$groupstype = $docModel->GetAllGroupType();
if (\App\Config::$DEBUG)
View::renderTemplate("/blog/article.html",
[
"url"=>\App\Config::ServerUrl(),
'route'=>'article',
'index'=>true,
'debug'=>true,
"typedocs" => $typedocs,
"alltitle" => $titles,
"groups" => $groups,
"group_types"=>$groupstype,
"title"=>urldecode ($this->querys['title']),
"doc"=>$doc[0],
"comments" => $doccoment,
"comment_count" => count($doccoment),
"doc_type"=>$doc[0]["type"],
"group_type"=>$docModel->TypeGroup($doc[0]["type"])[0][0],
"topdoc"=>$top5,
"openapi"=>\App\Config::OpenApiUrl(),
"vestimes" => $doc_vers,
]);
else
View::renderTemplate("/blog/article.html", [
"url"=>\App\Config::ServerUrl(),
'route'=>'article',
'index'=>true,
'debug'=>true,
"typedocs" => $typedocs,
"alltitle" => $titles,
"groups" => $groups,
"group_types"=>$groupstype,
"title"=>urldecode ($this->querys['title']),
"doc"=>$doc[0],
"comments" => $doccoment,
"comment_count" => count($doccoment),
"doc_type"=>$doc[0]["type"],
"group_type"=>$docModel->TypeGroup($doc[0]["type"])[0][0],
"topdoc"=>$top5,
"openapi"=>\App\Config::OpenApiUrl(),
"vestimes" => $doc_vers,
]);
} else {
View::renderTemplate("/blog/article.html", ['title' => 'test', 'index' => false]);
}
}
}