php extjsドロップダウンツリーjsonデータフォーマットの生成

16719 ワード

メニュー項目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:     <[email protected]> 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:     <[email protected]> 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;
    }

    
}

?>