PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/var/Widget/Menu.php

http://typecho.googlecode.com/
PHP | 298 lines | 170 code | 39 blank | 89 comment | 24 complexity | e0686886a32969db8695c5f014725902 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Typecho Blog Platform
  4. *
  5. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  6. * @license GNU General Public License 2.0
  7. * @version $Id$
  8. */
  9. /**
  10. * ??????
  11. *
  12. * @package Widget
  13. */
  14. class Widget_Menu extends Typecho_Widget
  15. {
  16. /**
  17. * ?????
  18. *
  19. * @access private
  20. * @var array
  21. */
  22. private $_menu = array();
  23. /**
  24. * ?????
  25. *
  26. * @access private
  27. * @var integer
  28. */
  29. private $_currentParent = 1;
  30. /**
  31. * ?????
  32. *
  33. * @access private
  34. * @var integer
  35. */
  36. private $_currentChild = 0;
  37. /**
  38. * ????
  39. *
  40. * @access private
  41. * @var string
  42. */
  43. private $_currentUrl;
  44. /**
  45. * ????
  46. *
  47. * @access protected
  48. * @var Widget_Options
  49. */
  50. protected $options;
  51. /**
  52. * ????
  53. *
  54. * @access protected
  55. * @var Widget_User
  56. */
  57. protected $user;
  58. /**
  59. * ??????
  60. * @var string
  61. */
  62. public $title;
  63. /**
  64. * ????????
  65. * @var string
  66. */
  67. public $addLink;
  68. /**
  69. * ????,?????
  70. *
  71. * @access public
  72. * @param mixed $request request??
  73. * @param mixed $response response??
  74. * @param mixed $params ????
  75. * @return void
  76. */
  77. public function __construct($request, $response, $params = NULL)
  78. {
  79. parent::__construct($request, $response, $params);
  80. /** ??????? */
  81. $this->options = $this->widget('Widget_Options');
  82. $this->user = $this->widget('Widget_User');
  83. }
  84. /**
  85. * ????,?????
  86. *
  87. * @access public
  88. * @return void
  89. */
  90. public function execute()
  91. {
  92. $parentNodes = array(NULL, _t('???'), _t('??'), _t('??'), _t('??'));
  93. $childNodes = array(
  94. array(
  95. array(_t('??'), _t('???%s', $this->options->title), 'login.php', 'visitor'),
  96. array(_t('??'), _t('???%s', $this->options->title), 'register.php', 'visitor')
  97. ),
  98. array(
  99. array(_t('??'), _t('????'), 'index.php', 'subscriber'),
  100. array(_t('????'), _t('????'), 'profile.php', 'subscriber'),
  101. array(_t('??'), _t('????'), 'plugins.php', 'administrator'),
  102. array(array('Widget_Plugins_Config', 'getMenuTitle'), array('Widget_Plugins_Config', 'getMenuTitle'), 'options-plugin.php?config=', 'administrator', true),
  103. array(_t('??'), _t('????'), 'themes.php', 'administrator'),
  104. array(array('Widget_Themes_Files', 'getMenuTitle'), array('Widget_Themes_Files', 'getMenuTitle'), 'theme-editor.php', 'administrator', true),
  105. array(array('Widget_Themes_Config', 'getMenuTitle'), array('Widget_Themes_Config', 'getMenuTitle'), 'options-theme.php', 'administrator', true),
  106. array(_t('??'), _t('????'), 'upgrade.php', 'administrator', true),
  107. array(_t('??'), _t('????'), 'welcome.php', 'subscriber', true)
  108. ),
  109. array(
  110. array(_t('????'), _t('?????'), 'write-post.php', 'contributor'),
  111. array(array('Widget_Contents_Post_Edit', 'getMenuTitle'), array('Widget_Contents_Post_Edit', 'getMenuTitle'), 'write-post.php?cid=', 'contributor', true),
  112. array(_t('????'), _t('?????'), 'write-page.php', 'editor'),
  113. array(array('Widget_Contents_Page_Edit', 'getMenuTitle'), array('Widget_Contents_Page_Edit', 'getMenuTitle'), 'write-page.php?cid=', 'editor', true),
  114. ),
  115. array(
  116. array(_t('??'), _t('????'), 'manage-posts.php', 'contributor', false, 'write-post.php'),
  117. array(array('Widget_Contents_Post_Admin', 'getMenuTitle'), array('Widget_Contents_Post_Admin', 'getMenuTitle'), 'manage-posts.php?uid=', 'contributor', true),
  118. array(_t('????'), _t('??????'), 'manage-pages.php', 'editor', false, 'write-page.php'),
  119. array(_t('??'), _t('????'), 'manage-comments.php', 'contributor'),
  120. array(array('Widget_Comments_Admin', 'getMenuTitle'), array('Widget_Comments_Admin', 'getMenuTitle'), 'manage-comments.php?cid=', 'contributor', true),
  121. array(_t('?????'), _t('?????'), 'manage-metas.php', 'editor'),
  122. array(_t('??'), _t('????'), 'manage-medias.php', 'editor'),
  123. array(array('Widget_Contents_Attachment_Edit', 'getMenuTitle'), array('Widget_Contents_Attachment_Edit', 'getMenuTitle'), 'media.php?cid=', 'contributor', true),
  124. array(_t('??'), _t('????'), 'manage-users.php', 'administrator', false, 'user.php'),
  125. array(_t('????'), _t('????'), 'user.php', 'administrator', true),
  126. array(array('Widget_Users_Edit', 'getMenuTitle'), array('Widget_Users_Edit', 'getMenuTitle'), 'user.php?uid=', 'administrator', true),
  127. ),
  128. array(
  129. array(_t('??'), _t('????'), 'options-general.php', 'administrator'),
  130. array(_t('??'), _t('????'), 'options-discussion.php', 'administrator'),
  131. array(_t('??'), _t('????'), 'options-reading.php', 'administrator'),
  132. array(_t('????'), _t('??????'), 'options-permalink.php', 'administrator'),
  133. ));
  134. /** ?????? */
  135. $panelTable = unserialize($this->options->panelTable);
  136. $extendingParentMenu = empty($panelTable['parent']) ? array() : $panelTable['parent'];
  137. $extendingChildMenu = empty($panelTable['child']) ? array() : $panelTable['child'];
  138. $currentUrl = $this->request->makeUriByRequest();
  139. $adminUrl = $this->options->adminUrl;
  140. $menu = array();
  141. $defaultChildeNode = array(NULL, NULL, NULL, 'administrator', false, NULL);
  142. $currentUrlParts = parse_url($currentUrl);
  143. $currentUrlParams = array();
  144. if (!empty($currentUrlParts['query'])) {
  145. parse_str($currentUrlParts['query'], $currentUrlParams);
  146. }
  147. foreach ($extendingParentMenu as $key => $val) {
  148. $parentNodes[10 + $key] = $val;
  149. }
  150. foreach ($extendingChildMenu as $key => $val) {
  151. $childNodes[$key] = array_merge(isset($childNodes[$key]) ? $childNodes[$key] : array(), $val);
  152. }
  153. foreach ($parentNodes as $key => $parentNode) {
  154. // this is a simple struct than before
  155. $children = array();
  156. $showedChildrenCount = 0;
  157. $firstUrl = NULL;
  158. foreach ($childNodes[$key] as $inKey => $childNode) {
  159. // magic merge
  160. $childNode += $defaultChildeNode;
  161. list ($name, $title, $url, $access, $hidden, $addLink) = $childNode;
  162. // parse url
  163. $url = Typecho_Common::url($url, $adminUrl);
  164. // compare url
  165. $urlParts = parse_url($url);
  166. $urlParams = array();
  167. if (!empty($urlParts['query'])) {
  168. parse_str($urlParts['query'], $urlParams);
  169. }
  170. $validate = true;
  171. if ($urlParts['path'] != $currentUrlParts['path']) {
  172. $validate = false;
  173. } else {
  174. foreach ($urlParams as $paramName => $paramValue) {
  175. if (!isset($currentUrlParams[$paramName])) {
  176. $validate = false;
  177. break;
  178. }
  179. }
  180. }
  181. if ($hidden && $validate) {
  182. $hidden = false;
  183. }
  184. if (!$hidden && !$this->user->pass($access, true)) {
  185. $hidden = true;
  186. }
  187. if (!$hidden) {
  188. $showedChildrenCount ++;
  189. if (empty($firstUrl)) {
  190. $firstUrl = $url;
  191. }
  192. if (is_array($name)) {
  193. list($widget, $method) = $name;
  194. $name = Typecho_Widget::widget($widget)->$method();
  195. }
  196. if (is_array($title)) {
  197. list($widget, $method) = $title;
  198. $title = Typecho_Widget::widget($widget)->$method();
  199. }
  200. }
  201. if ($validate) {
  202. if ('visitor' != $access) {
  203. $this->user->pass($access);
  204. }
  205. $this->_currentParent = $key;
  206. $this->_currentChild = $inKey;
  207. $this->title = $title;
  208. $this->addLink = $addLink;
  209. }
  210. $children[$inKey] = array(
  211. $name,
  212. $title,
  213. $url,
  214. $access,
  215. $hidden,
  216. $addLink
  217. );
  218. }
  219. $menu[$key] = array($parentNode, $showedChildrenCount > 0, $firstUrl,$children);
  220. }
  221. $this->_menu = $menu;
  222. $this->_currentUrl = $currentUrl;
  223. }
  224. /**
  225. * ??????
  226. *
  227. * @access public
  228. * @return array
  229. */
  230. public function getCurrentMenu()
  231. {
  232. return $this->_currentParent > 0 ? $this->_menu[$this->_currentParent][3][$this->_currentChild] : NULL;
  233. }
  234. /**
  235. * ??????
  236. *
  237. * @access public
  238. * @return string
  239. */
  240. public function output($class = 'focus', $childClass = 'focus')
  241. {
  242. foreach ($this->_menu as $key => $node) {
  243. if (!$node[1] || !$key) {
  244. continue;
  245. }
  246. echo "<dt" . ($key == $this->_currentParent ? ' class="' . $class . '"' : NULL)
  247. . "><a href=\"{$node[2]}\" title=\"{$node[0]}\">{$node[0]}</a></dt>"
  248. . "<dd><ul>";
  249. foreach ($node[3] as $inKey => $inNode) {
  250. if ($inNode[4]) {
  251. continue;
  252. }
  253. echo "<li" . ($key == $this->_currentParent && $inKey == $this->_currentChild ? ' class="' . $childClass . '"' : NULL) .
  254. "><a href=\"" . ($key == $this->_currentParent && $inKey == $this->_currentChild ? $this->_currentUrl : $inNode[2]) . "\" title=\"{$inNode[0]}\">{$inNode[0]}</a></li>";
  255. }
  256. echo "</ul></dd>";
  257. }
  258. }
  259. }