PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/var/Widget/Menu.php

https://github.com/zihuxinyu/typecho
PHP | 343 lines | 207 code | 46 blank | 90 comment | 40 complexity | db19eecfcd791e5601de2643d62e8269 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. if (!defined('__TYPECHO_ROOT_DIR__')) exit;
  3. /**
  4. * Typecho Blog Platform
  5. *
  6. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  7. * @license GNU General Public License 2.0
  8. * @version $Id$
  9. */
  10. /**
  11. * 后台菜单显示
  12. *
  13. * @package Widget
  14. */
  15. class Widget_Menu extends Typecho_Widget
  16. {
  17. /**
  18. * 父菜单列表
  19. *
  20. * @access private
  21. * @var array
  22. */
  23. private $_menu = array();
  24. /**
  25. * 当前父菜单
  26. *
  27. * @access private
  28. * @var integer
  29. */
  30. private $_currentParent = 1;
  31. /**
  32. * 当前子菜单
  33. *
  34. * @access private
  35. * @var integer
  36. */
  37. private $_currentChild = 0;
  38. /**
  39. * 当前页面
  40. *
  41. * @access private
  42. * @var string
  43. */
  44. private $_currentUrl;
  45. /**
  46. * 全局选项
  47. *
  48. * @access protected
  49. * @var Widget_Options
  50. */
  51. protected $options;
  52. /**
  53. * 用户对象
  54. *
  55. * @access protected
  56. * @var Widget_User
  57. */
  58. protected $user;
  59. /**
  60. * 当前菜单标题
  61. * @var string
  62. */
  63. public $title;
  64. /**
  65. * 当前增加项目链接
  66. * @var string
  67. */
  68. public $addLink;
  69. /**
  70. * 构造函数,初始化组件
  71. *
  72. * @access public
  73. * @param mixed $request request对象
  74. * @param mixed $response response对象
  75. * @param mixed $params 参数列表
  76. * @return void
  77. */
  78. public function __construct($request, $response, $params = NULL)
  79. {
  80. parent::__construct($request, $response, $params);
  81. /** 初始化常用组件 */
  82. $this->options = $this->widget('Widget_Options');
  83. $this->user = $this->widget('Widget_User');
  84. }
  85. /**
  86. * 执行函数,初始化菜单
  87. *
  88. * @access public
  89. * @return void
  90. */
  91. public function execute()
  92. {
  93. $parentNodes = array(NULL, _t('控制台'), _t('撰写'), _t('管理'), _t('设置'));
  94. $childNodes = array(
  95. array(
  96. array(_t('登录'), _t('登录到%s', $this->options->title), 'login.php', 'visitor'),
  97. array(_t('注册'), _t('注册到%s', $this->options->title), 'register.php', 'visitor')
  98. ),
  99. array(
  100. array(_t('概要'), _t('网站概要'), 'index.php', 'subscriber'),
  101. array(_t('个人设置'), _t('个人设置'), 'profile.php', 'subscriber'),
  102. array(_t('插件'), _t('插件管理'), 'plugins.php', 'administrator'),
  103. array(array('Widget_Plugins_Config', 'getMenuTitle'), array('Widget_Plugins_Config', 'getMenuTitle'), 'options-plugin.php?config=', 'administrator', true),
  104. array(_t('外观'), _t('网站外观'), 'themes.php', 'administrator'),
  105. array(array('Widget_Themes_Files', 'getMenuTitle'), array('Widget_Themes_Files', 'getMenuTitle'), 'theme-editor.php', 'administrator', true),
  106. array(_t('设置外观'), _t('设置外观'), 'options-theme.php', 'administrator', true),
  107. array(_t('升级'), _t('升级程序'), 'upgrade.php', 'administrator', true),
  108. array(_t('欢迎'), _t('欢迎使用'), 'welcome.php', 'subscriber', true)
  109. ),
  110. array(
  111. array(_t('撰写文章'), _t('撰写新文章'), 'write-post.php', 'contributor'),
  112. array(array('Widget_Contents_Post_Edit', 'getMenuTitle'), array('Widget_Contents_Post_Edit', 'getMenuTitle'), 'write-post.php?cid=', 'contributor', true),
  113. array(_t('创建页面'), _t('创建新页面'), 'write-page.php', 'editor'),
  114. array(array('Widget_Contents_Page_Edit', 'getMenuTitle'), array('Widget_Contents_Page_Edit', 'getMenuTitle'), 'write-page.php?cid=', 'editor', true),
  115. ),
  116. array(
  117. array(_t('文章'), _t('管理文章'), 'manage-posts.php', 'contributor', false, 'write-post.php'),
  118. array(array('Widget_Contents_Post_Admin', 'getMenuTitle'), array('Widget_Contents_Post_Admin', 'getMenuTitle'), 'manage-posts.php?uid=', 'contributor', true),
  119. array(_t('独立页面'), _t('管理独立页面'), 'manage-pages.php', 'editor', false, 'write-page.php'),
  120. array(_t('评论'), _t('管理评论'), 'manage-comments.php', 'contributor'),
  121. array(array('Widget_Comments_Admin', 'getMenuTitle'), array('Widget_Comments_Admin', 'getMenuTitle'), 'manage-comments.php?cid=', 'contributor', true),
  122. array(_t('分类'), _t('管理分类'), 'manage-categories.php', 'editor', false, 'category.php'),
  123. array(_t('新增分类'), _t('新增分类'), 'category.php', 'editor', true),
  124. array(array('Widget_Metas_Category_Admin', 'getMenuTitle'), array('Widget_Metas_Category_Admin', 'getMenuTitle'), 'manage-categories.php?parent=', 'editor', true, array('Widget_Metas_Category_Admin', 'getAddLink')),
  125. array(array('Widget_Metas_Category_Edit', 'getMenuTitle'), array('Widget_Metas_Category_Edit', 'getMenuTitle'), 'category.php?mid=', 'editor', true),
  126. array(array('Widget_Metas_Category_Edit', 'getMenuTitle'), array('Widget_Metas_Category_Edit', 'getMenuTitle'), 'category.php?parent=', 'editor', true),
  127. array(_t('标签'), _t('管理标签'), 'manage-tags.php', 'editor'),
  128. array(array('Widget_Metas_Tag_Admin', 'getMenuTitle'), array('Widget_Metas_Tag_Admin', 'getMenuTitle'), 'manage-tags.php?mid=', 'editor', true),
  129. array(_t('文件'), _t('管理文件'), 'manage-medias.php', 'editor'),
  130. array(array('Widget_Contents_Attachment_Edit', 'getMenuTitle'), array('Widget_Contents_Attachment_Edit', 'getMenuTitle'), 'media.php?cid=', 'contributor', true),
  131. array(_t('用户'), _t('管理用户'), 'manage-users.php', 'administrator', false, 'user.php'),
  132. array(_t('新增用户'), _t('新增用户'), 'user.php', 'administrator', true),
  133. array(array('Widget_Users_Edit', 'getMenuTitle'), array('Widget_Users_Edit', 'getMenuTitle'), 'user.php?uid=', 'administrator', true),
  134. ),
  135. array(
  136. array(_t('基本'), _t('基本设置'), 'options-general.php', 'administrator'),
  137. array(_t('评论'), _t('评论设置'), 'options-discussion.php', 'administrator'),
  138. array(_t('阅读'), _t('阅读设置'), 'options-reading.php', 'administrator'),
  139. array(_t('永久链接'), _t('永久链接设置'), 'options-permalink.php', 'administrator'),
  140. ));
  141. /** 获取扩展菜单 */
  142. $panelTable = unserialize($this->options->panelTable);
  143. $extendingParentMenu = empty($panelTable['parent']) ? array() : $panelTable['parent'];
  144. $extendingChildMenu = empty($panelTable['child']) ? array() : $panelTable['child'];
  145. $currentUrl = $this->request->makeUriByRequest();
  146. $adminUrl = $this->options->adminUrl;
  147. $menu = array();
  148. $defaultChildeNode = array(NULL, NULL, NULL, 'administrator', false, NULL);
  149. $currentUrlParts = parse_url($currentUrl);
  150. $currentUrlParams = array();
  151. if (!empty($currentUrlParts['query'])) {
  152. parse_str($currentUrlParts['query'], $currentUrlParams);
  153. }
  154. if ('/' == $currentUrlParts['path'][strlen($currentUrlParts['path']) - 1]) {
  155. $currentUrlParts['path'] .= 'index.php';
  156. }
  157. foreach ($extendingParentMenu as $key => $val) {
  158. $parentNodes[10 + $key] = $val;
  159. }
  160. foreach ($extendingChildMenu as $key => $val) {
  161. $childNodes[$key] = array_merge(isset($childNodes[$key]) ? $childNodes[$key] : array(), $val);
  162. }
  163. foreach ($parentNodes as $key => $parentNode) {
  164. // this is a simple struct than before
  165. $children = array();
  166. $showedChildrenCount = 0;
  167. $firstUrl = NULL;
  168. foreach ($childNodes[$key] as $inKey => $childNode) {
  169. // magic merge
  170. $childNode += $defaultChildeNode;
  171. list ($name, $title, $url, $access, $hidden, $addLink) = $childNode;
  172. // 保存最原始的hidden信息
  173. $orgHidden = $hidden;
  174. // parse url
  175. $url = Typecho_Common::url($url, $adminUrl);
  176. // compare url
  177. $urlParts = parse_url($url);
  178. $urlParams = array();
  179. if (!empty($urlParts['query'])) {
  180. parse_str($urlParts['query'], $urlParams);
  181. }
  182. $validate = true;
  183. if ($urlParts['path'] != $currentUrlParts['path']) {
  184. $validate = false;
  185. } else {
  186. foreach ($urlParams as $paramName => $paramValue) {
  187. if (!isset($currentUrlParams[$paramName])) {
  188. $validate = false;
  189. break;
  190. }
  191. }
  192. }
  193. if ($validate
  194. && basename($urlParts['path']) == 'extending.php'
  195. && !empty($currentUrlParams['panel']) && !empty($urlParams['panel'])
  196. && $urlParams['panel'] != $currentUrlParams['panel']){
  197. $validate = false;
  198. }
  199. if ($hidden && $validate) {
  200. $hidden = false;
  201. }
  202. if (!$hidden && !$this->user->pass($access, true)) {
  203. $hidden = true;
  204. }
  205. if (!$hidden) {
  206. $showedChildrenCount ++;
  207. if (empty($firstUrl)) {
  208. $firstUrl = $url;
  209. }
  210. if (is_array($name)) {
  211. list($widget, $method) = $name;
  212. $name = Typecho_Widget::widget($widget)->$method();
  213. }
  214. if (is_array($title)) {
  215. list($widget, $method) = $title;
  216. $title = Typecho_Widget::widget($widget)->$method();
  217. }
  218. if (is_array($addLink)) {
  219. list($widget, $method) = $addLink;
  220. $addLink = Typecho_Widget::widget($widget)->$method();
  221. }
  222. }
  223. if ($validate) {
  224. if ('visitor' != $access) {
  225. $this->user->pass($access);
  226. }
  227. $this->_currentParent = $key;
  228. $this->_currentChild = $inKey;
  229. $this->title = $title;
  230. $this->addLink = $addLink ? Typecho_Common::url($addLink, $adminUrl) : NULL;
  231. }
  232. $children[$inKey] = array(
  233. $name,
  234. $title,
  235. $url,
  236. $access,
  237. $hidden,
  238. $addLink,
  239. $orgHidden
  240. );
  241. }
  242. $menu[$key] = array($parentNode, $showedChildrenCount > 0, $firstUrl,$children);
  243. }
  244. $this->_menu = $menu;
  245. $this->_currentUrl = $currentUrl;
  246. }
  247. /**
  248. * 获取当前菜单
  249. *
  250. * @access public
  251. * @return array
  252. */
  253. public function getCurrentMenu()
  254. {
  255. return $this->_currentParent > 0 ? $this->_menu[$this->_currentParent][3][$this->_currentChild] : NULL;
  256. }
  257. /**
  258. * 输出父级菜单
  259. *
  260. * @access public
  261. * @return string
  262. */
  263. public function output($class = 'focus', $childClass = 'focus')
  264. {
  265. foreach ($this->_menu as $key => $node) {
  266. if (!$node[1] || !$key) {
  267. continue;
  268. }
  269. echo "<ul class=\"root" . ($key == $this->_currentParent ? ' ' . $class : NULL)
  270. . "\"><li class=\"parent\"><a href=\"{$node[2]}\">{$node[0]}</a></dt>"
  271. . "</li><ul class=\"child\">";
  272. $last = 0;
  273. foreach ($node[3] as $inKey => $inNode) {
  274. if (!$inNode[4]) {
  275. $last = $inKey;
  276. }
  277. }
  278. foreach ($node[3] as $inKey => $inNode) {
  279. if ($inNode[4]) {
  280. continue;
  281. }
  282. $classes = array();
  283. if ($key == $this->_currentParent && $inKey == $this->_currentChild) {
  284. $classes[] = $childClass;
  285. } else if ($inNode[6]) {
  286. continue;
  287. }
  288. if ($inKey == $last) {
  289. $classes[] = 'last';
  290. }
  291. echo "<li" . (!empty($classes) ? ' class="' . implode(' ', $classes) . '"' : NULL) .
  292. "><a href=\"" . ($key == $this->_currentParent && $inKey == $this->_currentChild ? $this->_currentUrl : $inNode[2]) . "\">{$inNode[0]}</a></li>";
  293. }
  294. echo "</ul></ul>";
  295. }
  296. }
  297. }