142 lines
4.0 KiB
PHP
142 lines
4.0 KiB
PHP
<?php
|
||
namespace app\model;
|
||
|
||
class InvitationTemplateClass extends Base
|
||
{
|
||
|
||
|
||
|
||
//根据上级ID获取下级列表
|
||
public static function getListTree($forMenu = 0)
|
||
{
|
||
$items = self::getList($forMenu);
|
||
return self::getChildren($items);
|
||
}
|
||
/**
|
||
* @param integer $forMenu 大于0表示获取可显示的权限
|
||
* @return void
|
||
*/
|
||
private static function getList($forMenu = 0)
|
||
{
|
||
if($forMenu){
|
||
return self::where('show', 'yes')->order('sort', 'asc')->select()->toArray();
|
||
}else{
|
||
return self::order('sort', 'asc')->select()->toArray();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* @param array $items
|
||
* @param integer $parent_id
|
||
* @return array
|
||
*/
|
||
public static function getChildren($items, $parentId = 0)
|
||
{
|
||
$data = [];
|
||
foreach($items as $item){
|
||
if($item['parent_id'] == $parentId){
|
||
$childern = self::getChildren($items, $item['id']);
|
||
if(!empty($childern)){
|
||
$item['hasChildren'] = true;
|
||
$item['children'] = $childern;
|
||
}
|
||
$data[] = $item;
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public static function onAfterInsert($rule)
|
||
{
|
||
$rule->sort = $rule->id;
|
||
$rule->save();
|
||
}
|
||
|
||
|
||
/**
|
||
* 分组排序(不拆分为子集)
|
||
*
|
||
* @param $items 数据源
|
||
* @param integer $pid
|
||
* @param integer $level
|
||
* @param bool $clear 是否释放局部变量(外部调用时必须先释放,避免数据被累加;内部不用,需要累加)
|
||
* @param bool $isArr 传入的$items是否为数组,默认否(数据集)
|
||
* @param string $levelSpare 分层符
|
||
* @return void
|
||
*/
|
||
public static function groupSort($items,$pid = 0, $level = 1, $clear = true, $isArr = false, $levelSpare = '_')
|
||
{
|
||
static $data = [];
|
||
if ($clear) {
|
||
$data = [];
|
||
static $data = [];
|
||
}
|
||
if(!empty($levelSpare) && $level > 1) {
|
||
$levelSpare = str_repeat($levelSpare, $level-1);
|
||
}
|
||
if (count($items) > 0) {
|
||
if ($isArr) {
|
||
foreach ($items as $key => $item) {
|
||
if ($item['parent_id'] == $pid) {
|
||
$item['level'] = $level;
|
||
$item['title'] = $levelSpare.$item['title'];
|
||
$data[] = $item;
|
||
self::groupSort($items, $item['id'], $level+1, false, $isArr);
|
||
}
|
||
}
|
||
} else {
|
||
foreach ($items as $key => $item) {
|
||
if ($item->parent_id == $pid) {
|
||
$item->level = $level;
|
||
$item->title = $levelSpare.$item->title;
|
||
$data[] = $item;
|
||
self::groupSort($items, $item->id, $level+1, false, $isArr);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 树形排序 (拆分子集)
|
||
*
|
||
* @param Collection $items 数据集(非数组)
|
||
* @param integer $pid
|
||
* @param integer $level
|
||
* @return void
|
||
*/
|
||
public static function treeSort($items,$pid = 0, $level = 1)
|
||
{
|
||
$data = [];
|
||
if (count($items) > 0) {
|
||
foreach ($items as $key => $item) {
|
||
if ($item->parent_id == $pid) {
|
||
$item->level = $level;
|
||
$children = self::treeSort($items, $item->id, $level+1);
|
||
$item->children = $children;
|
||
$data[] = $item;
|
||
}
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
//根据上级ID获取下级列表
|
||
public static function getListByParentId($parentId)
|
||
{
|
||
return self::where('parent_id', $parentId)
|
||
->order('sort', 'asc')
|
||
->select()
|
||
->toArray();
|
||
}
|
||
|
||
}
|