菜单项curd以及extjs下拉树json数据构造
<?php
// +----------------------------------------------------------------------
// | thinkphp+extjs +jquery CMS管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://blog.aigouw.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 殷志朋 <601046124@qq.com> 7391390
// +----------------------------------------------------------------------
// | Q&A: 群 7391390
// +-----
class MenuItemAction extends Action {
/**
* 取出所有菜单项
*/
function ListAll() {
$keyword = $_POST['searchstring'];
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
if (!empty($keyword)) {
$where['name'] = array('like', '%' . $keyword . '%');
$_SESSION['keyword'] = $where;
} else {
if (empty($keyword)) {
unset($_SESSION['keyword']);
} else
if (!empty($_SESSION['keyword'])) {
$where = $_SESSION['keyword'];
}
}
$menuitem = new MenuItemModel();
$keyword = $_POST['keyword'];
$ftype = $_POST['ftype'];
if (!empty($keyword) && !empty($ftype)) {
$where[$ftype] = array('like', '%' . $keyword . '%');
$_SESSION['keyword'] = $where;
} else {
if (empty($keyword) && !empty($ftype)) {
unset($_SESSION['keyword']);
} else
if (!empty($_SESSION['keyword'])) {
$where = $_SESSION['keyword'];
}
}
if (!empty($_REQUEST['id'])) {
$_SESSION['menuid'] = $_REQUEST['id'];
$where['menuid'] = $_SESSION['menuid'];
} elseif ($_SESSION['menuid']) {
$where['menuid'] = $_SESSION['menuid'];
} else {
echo('请选择菜单');
}
$count = $menuitem->where($where)->count();
$list = $menuitem->field("id,name,pid,type,componentid,`order`,published,access,concat(path,'-',id) as bpath")->
order("bpath,id")->limit($limits)->where($where)->select();
$role = new Model('Role');
$rlist = $role->getField('id,name');
foreach ($list as $key => $value) {
if ($list[$key]['access'] != 0) {
$list[$key]['access'] = $rlist[$list[$key]['access']];
}
$list[$key]['signnum'] = count(explode('-', $value['bpath'])) - 1;
$list[$key]['marginnum'] = (count(explode('-', $value['bpath'])) - 1) * 20;
}
if ($list) {
$result = json_encode($list);
echo '{"totalCount":' . $count . ',"data":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"data":[{"id":"0","name":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
/**
* 构造路径
*/
function GetPath() {
$menu = new MenuItemModel();
$pid = $_POST['pid'];
$mi = $menu->field('id,path')->getById($pid);
$path = $pid != 0 ? $mi['path'] . '-' . $mi['id'] : 0;
return $path;
}
/**
* 添加
*/
function Add() {
$menu = new MenuItemModel();
switch ($_POST['fenlei']) {
case '1':$_POST['fenlei'] = 'Article';
case '2':$_POST['fenlei'] = 'Section';
case '3':$_POST['fenlei'] = 'Category';
}
$data['link'] = 'index.php/' . $_POST['fenlei'] . '/view/id/' . $_POST['ctype'];
$data['access'] = $_POST['access'];
$data['published'] = $_POST['published'];
$data['browserNav'] = $_POST['nav'];
$data['name'] = $_POST['name'];
$data['type'] = $_POST['fenlei'];
$data['menuid'] = $_POST['menuid'];
$data['path'] = $this->GetPath();
$data['pid'] = $_POST['pid'];
if (false !== $menu->data($data)->Add()) {
echo '{"success":true}';
} else {
echo '{"success":false}';
}
}
/**
* 保存
*/
function Save() {
$menu = new MenuItemModel();
switch ($_POST['fenlei']) {
case '1':$_POST['fenlei'] = 'Article';
case '2':$_POST['fenlei'] = 'Section';
case '3':$_POST['fenlei'] = 'Category';
}
$data['link'] = 'index.php/' . $_POST['fenlei'] . '/view/id/' . $_POST['ctype'];
$data['access'] = $_POST['access'];
$data['published'] = $_POST['published'];
$data['browserNav'] = $_POST['nav'];
$data['name'] = $_POST['name'];
$data['type'] = $_POST['fenlei'];
$data['menuid'] = $_POST['pid'];
$data['path'] = $this->getPath();
$data['id'] = $_POST['id'];
if (false !== $menu->data($data)->Save()) {
echo '{"success":true}';
} else {
echo '{"success":false}';
}
}
/**
* 移除
*/
function Remove() {
$did = $_POST['deleteIds'];
if (!empty($did)) {
$user = new MenuItemModel();
if (false !== $user->where('id in(' . $did . ')')->delete()) {
echo "{'success':true}";
} else {
echo "{'success':true}";
}
} else {
echo "{'success':true}";
}
}
/**
* 构造路径
*/
function ParentMenu() {
$menuitem = new MenuItemModel();
$result = $menuitem->field("id,name,pid,type,componentid,`order`,published,access,concat(path,'-',id) as bpath")->
order("bpath,id")->where('pid=' . $_SESSION['menuid'])->select();
$json = array();
$ids = array();
foreach ($result as $val) {
if (in_array($ids, $val['id'])) {
return;
}
;
array_puah($val['id'], $ids);
$json['id'] = $val["id"];
$json['text'] = $val["name"];
$json['leaf'] = 'true';
foreach ($resultc as $cval) {
if ($cval['pid'] = $val['id']) {
$json['children']['id'] = $cval['id'];
$json['children']['text'] = $cval['name'];
$json['children']['leaf'] = 'false';
}
}
}
if ($result) {
$result = json_encode($result);
echo $result;
} else {
echo "{'success':false}";
}
}
/**
* 构造extjs下拉树内层数据
*/
function InsideTree($list) {
$str = '';
foreach ($list as $key => $value) { //遍历列表
if ($str != '')
$str .= ',';
$in_str = '';
foreach ($value as $n => &$v) { //遍历数组
if ($in_str != '')
$in_str .= ',';
if ($n == 'leaf') {
$in_str .= $n . ":" . $v;
} else {
if ($n == 'children' && is_array($v)) {
$v = $this->InsideTree($v);
$in_str .= $n . ":" . $v;
} else {
$in_str .= $n . ":'" . $v . "'";
}
}
}
$str .= '{' . $in_str . '}';
}
return $str = '[' . $str . ']';
}
/**
* 完成extjs下拉树内层数据构造
*/
function Tree() {
$menuitem = new MenuItemModel();
$result = $menuitem->field("id,name as text,pid,type")->select();
$tree = new TreeAction();
$tree->input_arr = $result;
$data = $tree->GetTreeData(0);
echo "[{id:'0',text:'顶层菜单',children:" . $this->InsideTree($data) . "}]";
}
function Art() {
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
$role = new ArticleViewModel();
$where['Article.published'] = array('gt', 0);
$count = $role->where($where)->count();
$result = $role->order('id')->where($where)->limit($limits)->select();
if ($result) {
$result = json_encode($result);
echo '{"totalCount":' . $count . ',"root":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"root":[{"id":"0","atitle":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
function Car() {
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
$role = new CategoryViewModel();
$where['Category.published'] = array('gt', 0);
$count = $role->where($where)->count();
$result = $role->order('id')->where($where)->limit($limits)->select();
foreach ($result as & $val) {
$val['atitle'] = $val['ctitle'];
}
if ($result) {
$result = json_encode($result);
echo '{"totalCount":' . $count . ',"root":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"root":[{"id":"0","atitle":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
function Sec() {
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
$role = new SectionModel();
$where['published'] = array('gt', 0);
$count = $role->where($where)->count();
$result = $role->order('id')->where($where)->limit($limits)->select();
foreach ($result as & $val) {
$val['atitle'] = $val['title'];
}
if ($result) {
$result = json_encode($result);
echo '{"totalCount":' . $count . ',"root":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"root":[{"id":"0","atitle":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
}
?>
<?php
// +----------------------------------------------------------------------
// | thinkphp+extjs +jquery CMS管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://blog.aigouw.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 殷志朋 <601046124@qq.com> 7391390
// +----------------------------------------------------------------------
// | Q&A:群 7391390
// +----------------------------------------------------------------------
//
class TreeAction {
/**
* 生成数所需要的二维数组
*/
public $input_arr;
/**
* 数组父键值
*/
public $parent_tag = 'pid';
public function __construct($arr = array()) {
$this->input_arr = $arr;
return is_array($this->input_arr);
}
/**
* 递归取树形纯数组
* @param int $start_id 从哪个id开始遍历,0为根节点
* @return array 排序好的数组
*/
public function GetTreeData($start_id = 0, $leaf = false) {
$tree_arr = array();
$child = $this->GetChild($start_id, $leaf);
if (is_array($child) && $child) {
foreach ($child as &$child_value) {
$tree_arr[$child_value['id']] = $child_value;
$data = $this->GetChild($child_value['id']);
if (is_array($data) && count($data) >= 1) {
$leaf = 'false';
} else {
$leaf = 'true';
}
$tree_arr[$child_value['id']]['leaf'] = $leaf;
$tree_arr[$child_value['id']]['children'] = $this->GetTreeData($child_value['id']);
}
}
return $tree_arr;
}
/**
* 获取子元素
* @param string $my_id 其id
* @return array $data 子元素数组
*/
private function GetChild($my_id, $leaf) {
$data = array();
foreach ($this->input_arr as $key => $value) {
if ($value[$this->parent_tag] == $my_id) {
$data[$key] = $value;
}
}
return $data;
}
}
?>