NavModel.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Admin\Model;
  3. use Common\Model\ModelModel;
  4. use Util\Tree;
  5. /**
  6. * 导航模型
  7. *
  8. */
  9. class NavModel extends ModelModel
  10. {
  11. /**
  12. * 数据库表名
  13. *
  14. */
  15. protected $tableName = 'admin_nav';
  16. /**
  17. * 自动验证规则
  18. *
  19. */
  20. protected $_validate = array(
  21. array('group', 'require', '导航分组必须', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
  22. array('title', 'require', '导航标题不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
  23. array('type', 'require', '导航类型不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
  24. array('url', '0,255', '链接长度为0-25个字符', self::EXISTS_VALIDATE, 'length', self::MODEL_BOTH),
  25. );
  26. /**
  27. * 自动完成规则
  28. *
  29. */
  30. protected $_auto = array(
  31. array('value', 'get_value_by_type', self::MODEL_BOTH, 'callback'),
  32. array('create_time', 'time', self::MODEL_INSERT, 'function'),
  33. array('update_time', 'time', self::MODEL_BOTH, 'function'),
  34. array('status', '1', self::MODEL_INSERT),
  35. );
  36. /**
  37. * 查找后置操作
  38. *
  39. */
  40. protected function _after_find(&$result, $options)
  41. {
  42. // 处理不同导航类型
  43. switch ($result['type']) {
  44. case 'link':
  45. $result['url'] = $result['value'];
  46. if (!$result['url']) {
  47. $result['href'] = C('HOME_PAGE');
  48. } else {
  49. if (stristr($result['url'], (is_ssl() ? 'https://' : 'http://'))) {
  50. $result['href'] = $result['url'];
  51. } else {
  52. $result['href'] = U($result['url'], '', true, true);
  53. }
  54. }
  55. break;
  56. case 'module':
  57. $result['module_name'] = $result['value'];
  58. $result['href'] = U('/' . ucfirst($result['value']), '', true, true);
  59. break;
  60. case 'page':
  61. $result['content'] = $result['value'];
  62. $result['href'] = U('Home/Nav/page', array('id' => $result['id']), true, true);
  63. break;
  64. case 'post':
  65. $result['href'] = U('Home/Nav/lists', array('id' => $result['id']), true, true);
  66. break;
  67. }
  68. }
  69. /**
  70. * 查找后置操作
  71. *
  72. */
  73. protected function _after_select(&$result, $options)
  74. {
  75. foreach ($result as &$record) {
  76. $this->_after_find($record, $options);
  77. }
  78. }
  79. /**
  80. * 导航类型
  81. *
  82. */
  83. public function nav_type($id = 0)
  84. {
  85. $list['link'] = '链接';
  86. $list['module'] = '模块';
  87. $list['page'] = '单页';
  88. $list['post'] = '文章列表';
  89. return $id ? $list[$id] : $list;
  90. }
  91. /**
  92. * 根据导航类型获取值
  93. *
  94. */
  95. public function get_value_by_type($value)
  96. {
  97. if (!$value) {
  98. switch (I('post.type')) {
  99. case 'link':
  100. return I('post.url');
  101. break;
  102. case 'module':
  103. return I('post.module_name');
  104. break;
  105. case 'page':
  106. return I('post.content');
  107. break;
  108. }
  109. } else {
  110. return $value;
  111. }
  112. }
  113. /**
  114. * 获取参数的所有父级导航
  115. * @param int $id 导航id
  116. * @return array 参数导航和父类的信息集合
  117. *
  118. */
  119. public function getParentNav($id, $group = 'main')
  120. {
  121. if (empty($id)) {
  122. return false;
  123. }
  124. $con['status'] = 1;
  125. $con['group'] = array('eq', $group);
  126. $nav_list = $this->where($con)->field(true)->order('sort asc,id asc')->select();
  127. $current_nav = $this->field(true)->find($cid); //获取当前导航的信息
  128. $result[] = $current_nav;
  129. $pid = $current_nav['pid'];
  130. while (true) {
  131. foreach ($nav_list as $key => $val) {
  132. if ($val['id'] == $pid) {
  133. $pid = $val['pid'];
  134. array_unshift($result, $val); //将父导航插入到第一个元素前
  135. }
  136. }
  137. // 已找到顶级导航或者没有任何父导航
  138. if ($pid == 0 || count($result) == 1) {
  139. break;
  140. }
  141. }
  142. return $result;
  143. }
  144. /**
  145. * 获取导航树,指定导航则返回指定导航的子导航树,不指定则返回所有导航树,指定导航若无子导航则返回同级导航
  146. * @param integer $id 导航ID
  147. * @param boolean $field 查询字段
  148. * @return array 导航树
  149. *
  150. */
  151. public function getNavTree($id = 0, $group = 'main', $field = true)
  152. {
  153. if (!in_array(MODULE_NAME, array('Admin', 'Common')) && is_file(APP_PATH . MODULE_NAME . '/Model/NavModel.class.php')) {
  154. $module_nav_tree = D(MODULE_NAME . '/Nav')->getNavTree($id, $group, $field);
  155. if ($module_nav_tree) {
  156. return $module_nav_tree;
  157. }
  158. }
  159. // 开启默认模块并且使用默认模块布局
  160. if (C('DEFAULT_MODULE_LAYOUT') && C('DEFAULT_PUBLIC_LAYOUT') && is_file(APP_PATH . C('DEFAULT_MODULE') . '/Model/NavModel.class.php')) {
  161. $module_nav_tree = D(C('DEFAULT_MODULE') . '/Nav')->getNavTree($id, $group, $field);
  162. if ($module_nav_tree) {
  163. return $module_nav_tree;
  164. }
  165. }
  166. // 获取当前导航信息
  167. if ((int) $id > 0) {
  168. $info = $this->find($id);
  169. $id = $info['id'];
  170. }
  171. // 获取所有导航
  172. $map['status'] = array('eq', 1);
  173. $map['group'] = array('eq', $group);
  174. $tree = new Tree();
  175. $list = $this->field($field)->where($map)->order('sort asc,id asc')->select();
  176. // 返回当前导航的子导航树
  177. $list = $tree->list2tree(
  178. $list,
  179. $pk = 'id',
  180. $pid = 'pid',
  181. $child = '_child',
  182. $root = (int) $id
  183. );
  184. if (!$list) {
  185. return $this->getSameLevelNavTree($id);
  186. }
  187. return $list;
  188. }
  189. /**
  190. * 获取同级导航树
  191. * @param integer $id 导航ID
  192. * @return array 导航树
  193. *
  194. */
  195. public function getSameLevelNavTree($id = 0, $group = 'main')
  196. {
  197. //获取当前导航信息
  198. if ((int) $id > 0) {
  199. $nav_info = $this->find($id);
  200. $parent_info = $this->find($nav_info['pid']);
  201. $id = $info['id'];
  202. }
  203. //获取所有导航
  204. $map['status'] = array('eq', 1);
  205. $map['group'] = array('eq', $group);
  206. $map['pid'] = array('eq', $nav_info['pid']);
  207. $tree = new Tree();
  208. $list = $this->field($field)->where($map)->order('sort asc,id asc')->select();
  209. return $list;
  210. }
  211. }