/var/Widget/Menu.php
PHP | 298 lines | 170 code | 39 blank | 89 comment | 24 complexity | e0686886a32969db8695c5f014725902 MD5 | raw file
Possible License(s): AGPL-1.0
- <?php
- /**
- * Typecho Blog Platform
- *
- * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
- * @license GNU General Public License 2.0
- * @version $Id$
- */
- /**
- * ??????
- *
- * @package Widget
- */
- class Widget_Menu extends Typecho_Widget
- {
- /**
- * ?????
- *
- * @access private
- * @var array
- */
- private $_menu = array();
- /**
- * ?????
- *
- * @access private
- * @var integer
- */
- private $_currentParent = 1;
- /**
- * ?????
- *
- * @access private
- * @var integer
- */
- private $_currentChild = 0;
- /**
- * ????
- *
- * @access private
- * @var string
- */
- private $_currentUrl;
- /**
- * ????
- *
- * @access protected
- * @var Widget_Options
- */
- protected $options;
- /**
- * ????
- *
- * @access protected
- * @var Widget_User
- */
- protected $user;
- /**
- * ??????
- * @var string
- */
- public $title;
-
- /**
- * ????????
- * @var string
- */
- public $addLink;
- /**
- * ????,?????
- *
- * @access public
- * @param mixed $request request??
- * @param mixed $response response??
- * @param mixed $params ????
- * @return void
- */
- public function __construct($request, $response, $params = NULL)
- {
- parent::__construct($request, $response, $params);
- /** ??????? */
- $this->options = $this->widget('Widget_Options');
- $this->user = $this->widget('Widget_User');
- }
- /**
- * ????,?????
- *
- * @access public
- * @return void
- */
- public function execute()
- {
- $parentNodes = array(NULL, _t('???'), _t('??'), _t('??'), _t('??'));
- $childNodes = array(
- array(
- array(_t('??'), _t('???%s', $this->options->title), 'login.php', 'visitor'),
- array(_t('??'), _t('???%s', $this->options->title), 'register.php', 'visitor')
- ),
- array(
- array(_t('??'), _t('????'), 'index.php', 'subscriber'),
- array(_t('????'), _t('????'), 'profile.php', 'subscriber'),
- array(_t('??'), _t('????'), 'plugins.php', 'administrator'),
- array(array('Widget_Plugins_Config', 'getMenuTitle'), array('Widget_Plugins_Config', 'getMenuTitle'), 'options-plugin.php?config=', 'administrator', true),
- array(_t('??'), _t('????'), 'themes.php', 'administrator'),
- array(array('Widget_Themes_Files', 'getMenuTitle'), array('Widget_Themes_Files', 'getMenuTitle'), 'theme-editor.php', 'administrator', true),
- array(array('Widget_Themes_Config', 'getMenuTitle'), array('Widget_Themes_Config', 'getMenuTitle'), 'options-theme.php', 'administrator', true),
- array(_t('??'), _t('????'), 'upgrade.php', 'administrator', true),
- array(_t('??'), _t('????'), 'welcome.php', 'subscriber', true)
- ),
- array(
- array(_t('????'), _t('?????'), 'write-post.php', 'contributor'),
- array(array('Widget_Contents_Post_Edit', 'getMenuTitle'), array('Widget_Contents_Post_Edit', 'getMenuTitle'), 'write-post.php?cid=', 'contributor', true),
- array(_t('????'), _t('?????'), 'write-page.php', 'editor'),
- array(array('Widget_Contents_Page_Edit', 'getMenuTitle'), array('Widget_Contents_Page_Edit', 'getMenuTitle'), 'write-page.php?cid=', 'editor', true),
- ),
- array(
- array(_t('??'), _t('????'), 'manage-posts.php', 'contributor', false, 'write-post.php'),
- array(array('Widget_Contents_Post_Admin', 'getMenuTitle'), array('Widget_Contents_Post_Admin', 'getMenuTitle'), 'manage-posts.php?uid=', 'contributor', true),
- array(_t('????'), _t('??????'), 'manage-pages.php', 'editor', false, 'write-page.php'),
- array(_t('??'), _t('????'), 'manage-comments.php', 'contributor'),
- array(array('Widget_Comments_Admin', 'getMenuTitle'), array('Widget_Comments_Admin', 'getMenuTitle'), 'manage-comments.php?cid=', 'contributor', true),
- array(_t('?????'), _t('?????'), 'manage-metas.php', 'editor'),
- array(_t('??'), _t('????'), 'manage-medias.php', 'editor'),
- array(array('Widget_Contents_Attachment_Edit', 'getMenuTitle'), array('Widget_Contents_Attachment_Edit', 'getMenuTitle'), 'media.php?cid=', 'contributor', true),
- array(_t('??'), _t('????'), 'manage-users.php', 'administrator', false, 'user.php'),
- array(_t('????'), _t('????'), 'user.php', 'administrator', true),
- array(array('Widget_Users_Edit', 'getMenuTitle'), array('Widget_Users_Edit', 'getMenuTitle'), 'user.php?uid=', 'administrator', true),
- ),
- array(
- array(_t('??'), _t('????'), 'options-general.php', 'administrator'),
- array(_t('??'), _t('????'), 'options-discussion.php', 'administrator'),
- array(_t('??'), _t('????'), 'options-reading.php', 'administrator'),
- array(_t('????'), _t('??????'), 'options-permalink.php', 'administrator'),
- ));
- /** ?????? */
- $panelTable = unserialize($this->options->panelTable);
- $extendingParentMenu = empty($panelTable['parent']) ? array() : $panelTable['parent'];
- $extendingChildMenu = empty($panelTable['child']) ? array() : $panelTable['child'];
- $currentUrl = $this->request->makeUriByRequest();
- $adminUrl = $this->options->adminUrl;
- $menu = array();
- $defaultChildeNode = array(NULL, NULL, NULL, 'administrator', false, NULL);
- $currentUrlParts = parse_url($currentUrl);
- $currentUrlParams = array();
- if (!empty($currentUrlParts['query'])) {
- parse_str($currentUrlParts['query'], $currentUrlParams);
- }
- foreach ($extendingParentMenu as $key => $val) {
- $parentNodes[10 + $key] = $val;
- }
- foreach ($extendingChildMenu as $key => $val) {
- $childNodes[$key] = array_merge(isset($childNodes[$key]) ? $childNodes[$key] : array(), $val);
- }
- foreach ($parentNodes as $key => $parentNode) {
- // this is a simple struct than before
- $children = array();
- $showedChildrenCount = 0;
- $firstUrl = NULL;
-
- foreach ($childNodes[$key] as $inKey => $childNode) {
- // magic merge
- $childNode += $defaultChildeNode;
- list ($name, $title, $url, $access, $hidden, $addLink) = $childNode;
- // parse url
- $url = Typecho_Common::url($url, $adminUrl);
- // compare url
- $urlParts = parse_url($url);
- $urlParams = array();
- if (!empty($urlParts['query'])) {
- parse_str($urlParts['query'], $urlParams);
- }
- $validate = true;
- if ($urlParts['path'] != $currentUrlParts['path']) {
- $validate = false;
- } else {
- foreach ($urlParams as $paramName => $paramValue) {
- if (!isset($currentUrlParams[$paramName])) {
- $validate = false;
- break;
- }
- }
- }
- if ($hidden && $validate) {
- $hidden = false;
- }
- if (!$hidden && !$this->user->pass($access, true)) {
- $hidden = true;
- }
- if (!$hidden) {
- $showedChildrenCount ++;
- if (empty($firstUrl)) {
- $firstUrl = $url;
- }
- if (is_array($name)) {
- list($widget, $method) = $name;
- $name = Typecho_Widget::widget($widget)->$method();
- }
-
- if (is_array($title)) {
- list($widget, $method) = $title;
- $title = Typecho_Widget::widget($widget)->$method();
- }
- }
- if ($validate) {
- if ('visitor' != $access) {
- $this->user->pass($access);
- }
-
- $this->_currentParent = $key;
- $this->_currentChild = $inKey;
- $this->title = $title;
- $this->addLink = $addLink;
- }
- $children[$inKey] = array(
- $name,
- $title,
- $url,
- $access,
- $hidden,
- $addLink
- );
- }
- $menu[$key] = array($parentNode, $showedChildrenCount > 0, $firstUrl,$children);
- }
- $this->_menu = $menu;
- $this->_currentUrl = $currentUrl;
- }
- /**
- * ??????
- *
- * @access public
- * @return array
- */
- public function getCurrentMenu()
- {
- return $this->_currentParent > 0 ? $this->_menu[$this->_currentParent][3][$this->_currentChild] : NULL;
- }
- /**
- * ??????
- *
- * @access public
- * @return string
- */
- public function output($class = 'focus', $childClass = 'focus')
- {
- foreach ($this->_menu as $key => $node) {
- if (!$node[1] || !$key) {
- continue;
- }
- echo "<dt" . ($key == $this->_currentParent ? ' class="' . $class . '"' : NULL)
- . "><a href=\"{$node[2]}\" title=\"{$node[0]}\">{$node[0]}</a></dt>"
- . "<dd><ul>";
-
- foreach ($node[3] as $inKey => $inNode) {
- if ($inNode[4]) {
- continue;
- }
- echo "<li" . ($key == $this->_currentParent && $inKey == $this->_currentChild ? ' class="' . $childClass . '"' : NULL) .
- "><a href=\"" . ($key == $this->_currentParent && $inKey == $this->_currentChild ? $this->_currentUrl : $inNode[2]) . "\" title=\"{$inNode[0]}\">{$inNode[0]}</a></li>";
- }
- echo "</ul></dd>";
- }
- }
- }