1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 03:32:47 +08:00
2020-07-27 21:00:25 +08:00

84 lines
1.9 KiB
PHP

<?php
namespace App\Builders;
use App\Models\Nav as NavModel;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\ResultsetInterface;
class NavTreeList extends Builder
{
public function handle($position = 'top')
{
$topNavs = $this->findTopNavs($position);
if ($topNavs->count() == 0) {
return [];
}
$list = [];
foreach ($topNavs as $nav) {
$list[] = [
'id' => $nav->id,
'name' => $nav->name,
'target' => $nav->target,
'url' => $nav->url,
'children' => $this->handleChildren($nav),
];
}
return $list;
}
protected function handleChildren(NavModel $nav)
{
$subNavs = $this->findSubNavs($nav->id);
if ($subNavs->count() == 0) {
return [];
}
$list = [];
foreach ($subNavs as $nav) {
$list[] = [
'id' => $nav->id,
'name' => $nav->name,
'target' => $nav->target,
'url' => $nav->url,
];
}
return $list;
}
/**
* @param int $navId
* @return ResultsetInterface|Resultset|NavModel[]
*/
protected function findSubNavs($navId)
{
return NavModel::query()
->where('parent_id = :parent_id:', ['parent_id' => $navId])
->andWhere('published = 1')
->orderBy('priority ASC')
->execute();
}
/**
* @param string $position
* @return ResultsetInterface|Resultset|NavModel[]
*/
protected function findTopNavs($position)
{
return NavModel::query()
->where('position = :position:', ['position' => $position])
->andWhere('level = 1 AND published = 1')
->orderBy('priority ASC')
->execute();
}
}