| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 | <?php// +—————————————————————————————————————————————————————————————————————// | Created by Yunbao// +—————————————————————————————————————————————————————————————————————// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.// +—————————————————————————————————————————————————————————————————————// | Author: https://gitee.com/yunbaokeji// +—————————————————————————————————————————————————————————————————————// | Date: 2022-04-30// +—————————————————————————————————————————————————————————————————————namespace app\admin\controller;use app\admin\model\NavMenuModel;use cmf\controller\AdminBaseController;use tree\Tree;/** * Class NavMenuController 前台菜单管理控制器 * @package app\admin\controller */class NavMenuController extends AdminBaseController{    /**     * 导航菜单     * @adminMenu(     *     'name'   => '导航菜单',     *     'parent' => 'admin/Nav/index',     *     'display'=> false,     *     'hasView'=> true,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '导航菜单',     *     'param'  => ''     * )     */    public function index()    {        $intNavId     = $this->request->param("nav_id", 0, 'intval');        $navMenuModel = new NavMenuModel();        if (empty($intNavId)) {            $this->error("请指定导航!");        }        $objResult = $navMenuModel->where("nav_id", $intNavId)->order(["list_order" => "ASC"])->select();        $arrResult = $objResult ? $objResult->toArray() : [];        $tree       = new Tree();        $tree->icon = ['   │ ', '   ├─ ', '   └─ '];        $tree->nbsp = '   ';        $array = [];        foreach ($arrResult as $r) {            $r['str_manage'] = '<a class="btn btn-xs btn-primary" href="' . url("NavMenu/add", ["parent_id" => $r['id'], "nav_id" => $r['nav_id']]) . '">添加子菜单</a>            <a class="btn btn-xs btn-primary" href="' . url("NavMenu/edit", ["id" => $r['id'], "parent_id" => $r['parent_id'], "nav_id" => $r['nav_id']]) . '">编辑</a>             <a class="btn btn-xs btn-danger js-ajax-delete" href="' . url("NavMenu/delete", ["id" => $r['id'], 'nav_id' => $r['nav_id']]) . '">删除</a> ';            $r['status']     = $r['status'] ? "显示" : "隐藏";            $array[]         = $r;        }        $tree->init($array);        $str = "<tr>            <td><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input input-order'></td>            <td>\$id</td>            <td >\$spacer\$name</td>            <td>\$status</td>            <td>\$str_manage</td>        </tr>";        $categories = $tree->getTree(0, $str);        $this->assign("categories", $categories);        $this->assign('nav_id', $intNavId);        return $this->fetch();    }    /**     * 添加导航菜单     * @adminMenu(     *     'name'   => '添加导航菜单',     *     'parent' => 'index',     *     'display'=> false,     *     'order'  => 10000,     *     'hasView'=> true,     *     'icon'   => '',     *     'remark' => '添加导航菜单',     *     'param'  => ''     * )     */    public function add()    {        $navMenuModel = new NavMenuModel();        $intNavId     = $this->request->param("nav_id", 0, 'intval');        $intParentId  = $this->request->param("parent_id", 0, 'intval');        $objResult    = $navMenuModel->where("nav_id", $intNavId)->order(["list_order" => "ASC"])->select();        $arrResult    = $objResult ? $objResult->toArray() : [];        $tree       = new Tree();        $tree->icon = [' │ ', ' ├─ ', ' └─ '];        $tree->nbsp = ' ';        $array      = [];        foreach ($arrResult as $r) {            $r['str_manage'] = '<a href="' . url("NavMenu/add", ["parent_id" => $r['id']]) . '">添加子菜单</a> | <a href="'                . url("NavMenu/edit", ["id" => $r['id']]) . '">编辑</a> | <a class="J_ajax_del" href="'                . url("NavMenu/delete", ["id" => $r['id']]) . '">删除</a> ';            $r['status']     = $r['status'] ? "显示" : "隐藏";            $r['selected']   = $r['id'] == $intParentId ? "selected" : "";            $array[]         = $r;        }        $tree->init($array);        $str      = "<option value='\$id' \$selected>\$spacer\$name</option>";        $navTrees = $tree->getTree(0, $str);        $this->assign("nav_trees", $navTrees);        $navs = $navMenuModel->selectNavs();        $this->assign('navs', $navs);        $this->assign("nav_id", $intNavId);        return $this->fetch();    }    /**     * 添加导航菜单提交保存     * @adminMenu(     *     'name'   => '添加导航菜单提交保存',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '添加导航菜单提交保存',     *     'param'  => ''     * )     */    public function addPost()    {        $navMenuModel = new NavMenuModel();        $arrData      = $this->request->post();        if (isset($arrData['external_href'])) {            $arrData['href'] = htmlspecialchars_decode($arrData['external_href']);        } else {            $arrData['href'] = htmlspecialchars_decode($arrData['href']);            $arrData['href'] = base64_decode($arrData['href']);        }        $navMenuModel->allowField(true)->isUpdate(false)->save($arrData);        $this->success(lang("EDIT_SUCCESS"), url("NavMenu/index", ['nav_id' => $arrData['nav_id']]));    }    /**     * 编辑导航菜单     * @adminMenu(     *     'name'   => '编辑导航菜单',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> true,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '编辑导航菜单',     *     'param'  => ''     * )     */    public function edit()    {        $navMenuModel = new NavMenuModel();        $intNavId     = $this->request->param("nav_id", 0, 'intval');        $intId        = $this->request->param("id", 0, 'intval');        $intParentId  = $this->request->param("parent_id", 0, 'intval');        $objResult    = $navMenuModel            ->where("nav_id", $intNavId)            ->where("id", "neq", $intId)            ->order(["list_order" => "ASC"])            ->select();        $arrResult    = $objResult ? $objResult->toArray() : [];        $tree       = new Tree();        $tree->icon = [' │ ', ' ├─ ', ' └─ '];        $tree->nbsp = '       ';        $array      = [];        foreach ($arrResult as $r) {            $r['str_manage'] = '<a href="' . url("NavMenu/add", ["parent_id" => $r['id'], "nav_id" => $intNavId]) . '">添加子菜单</a> | <a href="'                . url("NavMenu/edit", ["id" => $r['id'], "nav_id" => $intNavId]) . '">编辑</a> | <a class="js-ajax-delete" href="'                . url("NavMenu/delete", ["id" => $r['id'], "nav_id" => $intNavId]) . '">删除</a> ';            $r['status']     = $r['status'] ? "显示" : "隐藏";            $r['selected']   = $r['id'] == $intParentId ? "selected" : "";            $array[]         = $r;        }        $tree->init($array);        $str       = "<option value='\$id' \$selected>\$spacer\$name</option>";        $nav_trees = $tree->getTree(0, $str);        $this->assign("nav_trees", $nav_trees);        $objNav = $navMenuModel->where("id", $intId)->find();        $arrNav = $objNav ? $objNav->toArray() : [];        $arrNav['href_old'] = $arrNav['href'];        if (strpos($arrNav['href'], "{") === 0 || $arrNav['href'] == 'home') {            $arrNav['href'] = base64_encode($arrNav['href']);        }        $this->assign($arrNav);        $navs = $navMenuModel->selectNavs();        $this->assign('navs', $navs);        $this->assign("nav_id", $intNavId);        $this->assign("parent_id", $intParentId);        return $this->fetch();    }    /**     * 编辑导航菜单提交保存     * @adminMenu(     *     'name'   => '编辑导航菜单提交保存',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '编辑导航菜单提交保存',     *     'param'  => ''     * )     */    public function editPost()    {        $navMenuModel = new NavMenuModel();        $intId        = $this->request->param('id', 0, 'intval');        $arrData      = $this->request->post();        if (isset($arrData['external_href'])) {            $arrData['href'] = htmlspecialchars_decode($arrData['external_href']);        } else {            $arrData['href'] = htmlspecialchars_decode($arrData['href']);            $arrData['href'] = base64_decode($arrData['href']);        }        $navMenuModel->update($arrData, ["id" => $intId], true);        $this->success(lang("EDIT_SUCCESS"), url("NavMenu/index", ['nav_id' => $arrData['nav_id']]));    }    /**     * 删除导航菜单     * @adminMenu(     *     'name'   => '删除导航菜单',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '删除导航菜单',     *     'param'  => ''     * )     */    public function delete()    {        $navMenuModel = new NavMenuModel();        $intId    = $this->request->param("id", 0, "intval");        $intNavId = $this->request->param("nav_id", 0, "intval");        if (empty($intId)) {            $this->error(lang("NO_ID"));        }        $count = $navMenuModel->where("parent_id", $intId)->count();        if ($count > 0) {            $this->error("该菜单下还有子菜单,无法删除!");        }        $navMenuModel->where("id", $intId)->delete();        $this->success(lang("DELETE_SUCCESS"), url("NavMenu/index", ['nav_id' => $intNavId]));    }    /**     * 导航菜单排序     * @adminMenu(     *     'name'   => '导航菜单排序',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '导航菜单排序',     *     'param'  => ''     * )     */    public function listOrder()    {        $navMenuModel = new NavMenuModel();        $status       = parent::listOrders($navMenuModel);        if ($status) {            $this->success("排序更新成功!");        } else {            $this->error("排序更新失败!");        }    }}
 |