<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_node_group_model extends SDF_Model{
var $title = '';
var $content = '';
var $date = '';
function __construct(){
$this->load->database();
parent::__construct();
}
public function get_admin_node_group_tree($r_cache = false){
$S_KEY = __CLASS__.'-'.__FUNCTION__;
if($r_cache===true){
$tree = $this->S($S_KEY);
if(!empty($tree)){
return $tree;
}
}
$list_arr = $tree = array();
$list = $this->db->select('id,name,pid')->order_by('sort_order','desc')->get('admin_node_group')->result_array();
foreach($list as $row){
$list_arr[$row['id']] = $row;
}
foreach($list_arr as $vo){
if(isset($list_arr[$vo['pid']] )){
$list_arr[$vo['pid']]['son'][$vo['id']] = &$list_arr[$vo['id']];
}else{
$tree[$vo['id']] = &$list_arr[$vo['id']];
}
}
$this->S($S_KEY,$tree);
return $tree;
}
public function get_admin_node_group_option_tree($select_id=0,$r_cache=false){
$this->option_list_str = '';
$tree = $this->get_admin_node_group_tree($r_cache);
$this->get_child_option_tree($tree,$select_id);
return $this->option_list_str;
/*foreach($tree as $t){
$option_list_str .="<option value=\"{$t['id']}\">{$t['name']}</option>";
if(empty($t['son'])){
$option_list_str .=$this->get_child tree($tree,$nbsp = ' ');
}
}*/
}
private function get_child_option_tree(&$tree,$select_id=0,$nbsp=' '){
foreach($tree as $t){
$selected_str = ($select_id == $t['id'])?'selected="selected"':'';
$this->option_list_str .= "<option {$selected_str} value=\"{$t['id']}\">{$nbsp}{$t['name']}({$t['id']})</option>";
if(isset($t['son']) and is_array($t['son'])){
$this->get_child_option_tree($t['son'],$select_id,$nbsp.$nbsp);
}
}
}
public function get_admin_node_group_menu($node_list){
$node_groupid_arr = array();
foreach($node_list as &$node){
$node_groupid_arr[$node['node_groupid']][] = $node;
}
$this->db->where('status',1);
$tree = $this->get_admin_node_group_tree();
$this->get_child_node_num($tree,$node_groupid_arr);
return $tree;
}
private function get_child_node_num(&$tree,&$node_groupid_arr){
$child_node_num = 0;
foreach($tree as &$t){
$t['child_node_num'] = 0;
if(isset($node_groupid_arr[$t['id']])){
$t['nodes'] = $node_groupid_arr[$t['id']];
$t['child_node_num'] += count($t['nodes']);
}
if(isset($t['son']) and is_array($t['son'])){
$t['child_node_num'] += $this->get_child_node_num($t['son'],$node_groupid_arr);
}
$child_node_num += $t['child_node_num'];
}
return $child_node_num;
}
public function get_group_id($modules,$controller,$title=''){
$code = $modules.$controller;
$this->db->where("FIND_IN_SET('{$code}',`code`)<>",0);
$query = $this->db->get('admin_node_group',1);
if ($query->num_rows() > 0){
$row = $query->row();
return $row->id;
}
$data = array(
'name' => empty($title)?$code:$title ,
'status' => 1,
'code' => $code
);
$this->db->insert('admin_node_group', $data);
return $this->db->insert_id();
}
}