diff --git a/module/doc/control.php b/module/doc/control.php index 6f6416d8fa..d8213ddc04 100755 --- a/module/doc/control.php +++ b/module/doc/control.php @@ -1948,8 +1948,8 @@ class doc extends control if($docID) $doc = $this->doc->getByID($docID); if($modalType == 'chapter' || $modalType == 'subDoc' || $parentID || (isset($doc) && $doc->parent)) { - $chapterAndDocs = $this->doc->getDocsOfLibs(array($libID), $objectType); - $chapterAndDocs = array_column($chapterAndDocs, 'title', 'id'); + $chapterAndDocs = $this->doc->getDocsOfLibs(array($libID), $objectType, $docID); + $chapterAndDocs = $this->doc->buildNestedDocs($chapterAndDocs); $this->view->chapterAndDocs = $chapterAndDocs; } diff --git a/module/doc/model.php b/module/doc/model.php index a882cbbb9e..c1f5c329df 100644 --- a/module/doc/model.php +++ b/module/doc/model.php @@ -730,10 +730,11 @@ class docModel extends model * * @param array $libs * @param string $spaceType + * @param int $excludeID * @access public * @return array */ - public function getDocsOfLibs(array $libs, string $spaceType): array + public function getDocsOfLibs(array $libs, string $spaceType, int $excludeID = 0): array { $docs = $this->dao->select('t1.*')->from(TABLE_DOC)->alias('t1') ->leftJoin(TABLE_MODULE)->alias('t2')->on('t1.module=t2.id') @@ -743,6 +744,7 @@ class docModel extends model ->andWhere("(t1.status = 'normal' or (t1.status = 'draft' and t1.addedBy='{$this->app->user->account}'))") ->andWhere('t2.type')->eq('doc') ->andWhere('t2.deleted')->eq('0') + ->beginIF(!empty($excludeID))->andWhere('t1.parent')->ne($excludeID)->andWhere('t1.id')->ne($excludeID)->fi() ->orderBy('t1.`order` asc, t1.id asc') ->fetchAll('id', false); @@ -752,6 +754,7 @@ class docModel extends model ->andWhere('templateType')->eq('') ->andWhere("(status = 'normal' or (status = 'draft' and addedBy='{$this->app->user->account}'))") ->andWhere('module')->in(array('0', '')) + ->beginIF(!empty($excludeID))->andWhere('parent')->ne($excludeID)->andWhere('id')->ne($excludeID)->fi() ->orderBy('`order` asc, id_asc') ->fetchAll('id', false); @@ -3965,4 +3968,56 @@ class docModel extends model ->andWhere('t2.deleted')->eq('0') ->fetch(); } + + /** + * 构建文档层级。 + * + * @param array $docs + * @access public + * @return array + */ + public function buildNestedDocs(array $docs): array + { + $children = array(); + foreach($docs as $doc) $children[$doc->parent][] = $doc; + + /* 找到所有根节点,如果没有parent为0的文档,尝试找到父节点不在docs中的文档。*/ + $rootDocs = isset($children[0]) ? $children[0] : array(); + if(empty($rootDocs)) + { + foreach($docs as $doc) + { + if(!isset($docs[$doc->parent])) $rootDocs[$doc->id] = $doc; + } + } + + $result = array(); + foreach($rootDocs as $rootDoc) $result[$rootDoc->id] = $this->buildDocItems($rootDoc->id, $rootDoc->title, $children); + + return $result; + } + + /** + * 构建文档items。 + * + * @param int $docID + * @param string $docTitle + * @param array $children + * @access public + * @return array + */ + public function buildDocItems(int $docID, string $docTitle, array $children): array + { + $items = array('value' => $docID, 'text' => $docTitle); + + if(isset($children[$docID])) + { + foreach($children[$docID] as $childDoc) + { + $items['items'][] = $this->buildDocItems($childDoc->id, $childDoc->title, $children); + } + } + + return $items; + } }