/**
  * 模块入库
  *
  * @param	string	$dir
  * @return	intval
  */
 public function add($dir, $config, $mydb = '')
 {
     if (!$dir) {
         return NULL;
     }
     // 判断重复安装
     if ($this->db->where('dirname', $dir)->count_all_results('module')) {
         return NULL;
     }
     $extend = (int) $config['extend'];
     $this->db->replace('module', array('site' => '', 'extend' => $extend, 'dirname' => $dir, 'setting' => '', 'sitemap' => 1, 'disabled' => 0, 'displayorder' => 0));
     $id = $this->db->insert_id();
     if (!$id) {
         return NULL;
     }
     // 非自定义表时
     if (!$mydb) {
         // 字段入库
         $main = (require FCPATH . $dir . '/config/main.table.php');
         // 主表信息
         foreach ($main['field'] as $field) {
             $this->add_field($id, $field, 1);
         }
         $data = (require FCPATH . $dir . '/config/data.table.php');
         // 附表信息
         if ($data['field']) {
             foreach ($data['field'] as $field) {
                 $this->add_field($id, $field, 0);
             }
         }
         //扩展内容表
         if ($extend) {
             // 字段入库
             $main = (require FCPATH . $dir . '/config/extend.main.table.php');
             // 主表信息
             foreach ($main['field'] as $field) {
                 $this->add_field($id, $field, 1, 1);
             }
             $data = (require FCPATH . $dir . '/config/extend.data.table.php');
             // 附表信息
             if ($data['field']) {
                 foreach ($data['field'] as $field) {
                     $this->add_field($id, $field, 0, 1);
                 }
             }
         }
     }
     // 菜单
     if (is_file(FCPATH . $dir . '/config/menu.php')) {
         $name = $config['name'];
         // 顶部菜单名称
         $menu = (require FCPATH . $dir . '/config/menu.php');
         // 插入后台的顶级菜单
         $this->db->insert('admin_menu', array('uri' => '', 'pid' => 0, 'mark' => 'module-' . $dir, 'name' => $name, 'icon' => $menu['icon'] ? $menu['icon'] : dr_get_icon_m($dir), 'hidden' => 0, 'displayorder' => 0));
         $topid = $this->db->insert_id();
         if (isset($menu['admin']) && $menu['admin']) {
             foreach ($menu['admin'] as $left) {
                 // 分组菜单名称
                 $this->db->insert('admin_menu', array('uri' => '', 'pid' => $topid, 'mark' => 'module-' . $dir, 'name' => $left['name'], 'hidden' => 0, 'displayorder' => 0));
                 $leftid = $this->db->insert_id();
                 foreach ($left['menu'] as $link) {
                     // 链接菜单
                     $this->db->insert('admin_menu', array('pid' => $leftid, 'uri' => strpos($link['uri'], '{id}') === FALSE ? trim($dir . '/' . $link['uri'], '/') : str_replace('{id}', $id, $link['uri']), 'mark' => 'module-' . $dir, 'name' => $link['name'], 'icon' => $link['icon'] ? $link['icon'] : dr_get_icon($link['uri']), 'hidden' => 0, 'displayorder' => 0));
                 }
             }
         }
         // 会员菜单
         if (isset($menu['member']) && $menu['member']) {
             // 查询内容的顶级菜单
             $top = $this->db->where('mark', 'm_mod')->get('member_menu')->row_array();
             if (!$top) {
                 $this->db->insert('member_menu', array('uri' => '', 'url' => '', 'pid' => 0, 'mark' => 'm_mod', 'name' => '内容', 'target' => 0, 'hidden' => 0, 'displayorder' => 0));
                 $top['id'] = $this->db->insert_id();
             }
             $topid = $top['id'];
             // 分组菜单名称
             $this->db->insert('member_menu', array('uri' => '', 'url' => '', 'pid' => $topid, 'mark' => 'left-' . $dir, 'name' => $name, 'target' => 0, 'hidden' => 0, 'displayorder' => 0));
             $leftid = $this->db->insert_id();
             foreach ($menu['member'] as $left) {
                 if ($left['menu']) {
                     foreach ($left['menu'] as $link) {
                         $this->db->insert('member_menu', array('pid' => $leftid, 'url' => '', 'uri' => strpos($link['uri'], '{id}') === FALSE ? trim($dir . '/' . $link['uri'], '/') : str_replace('{id}', $id, $link['uri']), 'mark' => 'module-' . $dir, 'name' => $link['name'], 'target' => 0, 'hidden' => 0, 'displayorder' => 0));
                     }
                 }
             }
         }
     }
     return $id;
 }
 /**
  * 初始化菜单
  *
  * @return	array
  */
 public function init()
 {
     // 清空菜单
     $this->db->query('TRUNCATE `' . $this->db->dbprefix('admin_menu') . '`');
     // 导入初始化菜单数据
     $this->ci->sql_query(str_replace('{dbprefix}', $this->db->dbprefix, file_get_contents(FCPATH . 'cache/install/admin_menu.sql')));
     // 按模块安装菜单
     $module = $this->db->get('module')->result_array();
     if ($module) {
         foreach ($module as $m) {
             $id = $m['id'];
             $dir = $m['dirname'];
             // 菜单
             if (is_file(FCPATH . $dir . '/config/menu.php')) {
                 $config = (require FCPATH . $dir . '/config/module.php');
                 $name = $config['name'];
                 // 顶部菜单名称
                 $menu = (require FCPATH . $dir . '/config/menu.php');
                 // 插入后台的顶级菜单
                 $this->db->insert('admin_menu', array('uri' => '', 'pid' => 0, 'mark' => 'module-' . $dir, 'name' => $name, 'icon' => $menu['icon'] ? $menu['icon'] : dr_get_icon_m($dir), 'hidden' => 0, 'displayorder' => 0));
                 $topid = $this->db->insert_id();
                 $left_id = 0;
                 foreach ($menu['admin'] as $left) {
                     // 分组菜单名称
                     $this->db->insert('admin_menu', array('uri' => '', 'pid' => $topid, 'mark' => 'module-' . $dir, 'name' => $left['name'], 'icon' => '', 'hidden' => 0, 'displayorder' => 0));
                     $leftid = $this->db->insert_id();
                     $left_id = $left_id ? $left_id : $leftid;
                     foreach ($left['menu'] as $link) {
                         // 链接菜单
                         $this->db->insert('admin_menu', array('pid' => $leftid, 'uri' => strpos($link['uri'], '{id}') === FALSE ? trim($dir . '/' . $link['uri'], '/') : str_replace('{id}', $id, $link['uri']), 'mark' => 'module-' . $dir, 'name' => $link['name'], 'icon' => $link['icon'] ? $link['icon'] : dr_get_icon($link['uri']), 'hidden' => 0, 'displayorder' => 0));
                     }
                 }
                 // 查询表单
                 $form = $this->db->where('module', $dir)->get('module_form')->result_array();
                 if ($form && $left_id) {
                     // 将此表单放在模块菜单中
                     foreach ($form as $f) {
                         $this->db->insert('admin_menu', array('uri' => $dir . '/admin/form_' . $f['table'] . '/index', 'url' => '', 'pid' => $left_id, 'name' => $f['name'] . '管理', 'icon' => 'icon-table', 'mark' => 'module-' . $dir . '-' . $f['id'], 'hidden' => 0, 'displayorder' => 0));
                     }
                 }
             }
         }
     }
     // 按应用安装菜单
     $app = $this->db->get('application')->result_array();
     if ($app) {
         foreach ($app as $a) {
             $dir = $a['dirname'];
             if (is_file(FCPATH . 'app/' . $dir . '/models/' . ucfirst($dir) . '_model.php')) {
                 $this->load->add_package_path(FCPATH . 'app/' . $dir . '/');
                 $this->load->model($dir . '_model', 'app_model');
                 $this->app_model->install_admin_menu($dir, $a['id']);
                 $this->load->remove_package_path(FCPATH . 'app/' . $dir . '/');
                 $menu = $this->db->where('pid', 75)->where('mark', 'app-' . $dir)->get('admin_menu')->row_array();
                 if ($menu['hidden'] !== 0) {
                     $this->db->where('id', $menu['id'])->update('admin_menu', array('hidden' => 0));
                 }
             }
         }
     }
     // 安装空间模型
     $space = $this->db->get('space_model')->result_array();
     if ($space) {
         foreach ($space as $t) {
             $id = $t['id'];
             $uri = 'member/content/index/mid/' . $id;
             if (!$this->db->where('mark', 'space-' . $id)->count_all_results('admin_menu')) {
                 $this->db->insert('admin_menu', array('pid' => 80, 'uri' => $uri, 'url' => '', 'mark' => 'space-' . $id, 'name' => $t['name'] . '管理', 'icon' => 'icon-table', 'hidden' => 0, 'displayorder' => $id + 5));
             }
         }
     }
 }