7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
9 * @package Magic3 Framework
10 * @author 平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11 * @copyright Copyright 2006-2013 Magic3 Project.
12 * @license http://www.gnu.org/copyleft/gpl.html GPL License
14 * @link http://www.magic3.org
16 require_once($gEnvManager->getContainerPath() . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/menuDb.php');
19 class m_menuWidgetContainer extends BaseWidgetContainer
21 private $db; // DB接続オブジェクト
22 private $langId; // 現在の言語
23 private $paramObj; // 定義取得用
24 private $templateType; // テンプレートのタイプ
25 private $currentUserLogined; // 現在のユーザはログイン中かどうか
26 const DEFAULT_CONFIG_ID = 0;
27 const MAX_MENU_TREE_LEVEL = 5; // メニュー階層最大数
32 function __construct()
35 parent::__construct();
38 $this->db = new menuDb();
43 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
44 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
46 * @param RequestManager $request HTTPリクエスト処理クラス
47 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
48 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
50 function _setTemplate($request, &$param)
52 return 'index.tmpl.html';
57 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
59 * @param RequestManager $request HTTPリクエスト処理クラス
60 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
63 function _assign($request, &$param)
65 $this->langId = $this->gEnv->getCurrentLanguage();
66 $this->currentUserLogined = $this->gEnv->isCurrentUserLogined(); // 現在のユーザはログイン中かどうか
69 $configId = $this->gEnv->getCurrentWidgetConfigId();
70 if (empty($configId)) $configId = self::DEFAULT_CONFIG_ID;
73 $targetObj = $this->getWidgetParamObjByConfigId($configId);
74 if (empty($targetObj)){ // 定義データが取得できないとき
78 $menuId = $targetObj->menuId; // メニューID
79 $name = $targetObj->name;// 定義名
80 $limitUser = $targetObj->limitUser;// ユーザを制限するかどうか
82 // ユーザ制限があるときはログイン時のみ表示
83 if (!$limitUser || $this->currentUserLogined){
85 $parentTree = array(); // 選択されている項目までの階層パス
86 $menuHtml = $this->createMenu($menuId, 0, 0, $tmp, $parentTree);
88 if (!empty($menuHtml)) $this->tmpl->addVar("_widget", "menu_html", $menuHtml);
98 * @param string $menuId メニューID
99 * @param int $parantId 親メニュー項目ID
100 * @param int $level 階層数
101 * @param bool $hasSelectedChild 現在選択状態の子項目があるかどうか
102 * @param array $parentTree 現在の階層パス
103 * @return string ツリーメニュータグ
105 function createMenu($menuId, $parantId, $level, &$hasSelectedChild, &$parentTree)
107 static $index = 0; // インデックス番号
108 $hasSelectedChild = false;
111 if ($level >= self::MAX_MENU_TREE_LEVEL) return '';
114 if ($this->db->getChildMenuItems($menuId, $parantId, $this->langId, $rows)){
115 $itemCount = count($rows);
116 for ($i = 0; $i < $itemCount; $i++){
120 if (!$row['md_visible']) continue;
122 // ユーザ制限がある場合はログイン状態をチェック
123 if ($row['md_user_limited'] && !$this->currentUserLogined) continue;
125 // リンク先のコンテンツの表示状況に合わせる
126 if ($row['md_content_type'] == M3_VIEW_TYPE_CONTENT){ // 汎用コンテンツの場合
127 // ログインユーザに表示制限されている場合はメニューを追加しない
128 if (!empty($row['cn_user_limited']) && !$this->currentUserLogined) continue;
132 $linkUrl = $row['md_link_url'];
133 $linkUrl = $this->getUrl($linkUrl, true/*リンク用*/);
134 if (empty($linkUrl)) $linkUrl = '#';
137 if ($this->checkMenuItemUrl($linkUrl)){
138 $hasSelectedChild = true;
142 $name = $this->getCurrentLangString($row['md_name']);
143 $title = $this->getCurrentLangString($row['md_title']); // タイトル(HTML可)
144 if (empty($title)) $title = $name;
145 if (empty($title)) continue;
147 // メニュータイトルの処理。タグが含まれていない場合は文字をエスケープする。
148 $stripName = strip_tags($title);
149 if (strlen($stripName) == strlen($title)) $title = $this->convertToDispString($title); // 文字列長が同じとき
151 $index++; // インデックス番号更新
153 switch ($row['md_type']){
156 $treeHtml .= '<a href="' . $this->convertUrlToHtmlEntity($linkUrl) . '" >' . $title . '</a><br />' . M3_NL;
159 $treeHtml .= $title . '<br />' . M3_NL;
163 $treeHtml .= '<hr />' . M3_NL;
173 * @param string $url チェック対象のURL
174 * @return bool true=アクティブ、false=非アクティブ
176 function checkMenuItemUrl($url)
178 $currentUrl = $this->gEnv->getCurrentRequestUri();
181 if ($url == $currentUrl) return true;
184 $queryArray = array();
185 $parsedUrl = parse_url($url);
186 if (!empty($parsedUrl['query'])) parse_str($parsedUrl['query'], $queryArray); // クエリーの解析
188 // ルートかどうかチェック(クエリー文字列なし)
189 if ($this->isRootUrl($url)){
191 if ($this->gEnv->getCurrentPageSubId() == $this->gEnv->getDefaultPageSubId()) return true;
194 // パラメータがサブページIDだけの場合はページサブIDで比較
195 if (count($queryArray) == 1 && isset($queryArray[M3_REQUEST_PARAM_PAGE_SUB_ID])){
196 if ($this->gEnv->getCurrentPageSubId() == $queryArray[M3_REQUEST_PARAM_PAGE_SUB_ID]) return true;
201 * URLがルートを指しているかどうか取得
203 * @param string $url チェック対象のURL
204 * @return bool true=ルート、false=ルート以外
206 function isRootUrl($url)
208 $url = str_replace('https://', 'http://', $url); // 一旦httpに統一
209 $systemUrl = str_replace('https://', 'http://', $this->gEnv->getRootUrl()); // 一旦httpに統一
210 $systemSslUrl = str_replace('https://', 'http://', $this->gEnv->getSslRootUrl()); // 一旦httpに統一
212 $parsedUrl = parse_url($url);
213 if (empty($parsedUrl['query'])){ // クエリ文字列がないことが条件。「#」はあっても良い。
215 $relativePath = str_replace($systemUrl, '', $url); // ルートURLからの相対パスを取得
216 if (empty($relativePath)){ // Magic3のルートURLの場合
218 } else if (strStartsWith($relativePath, '/') || strStartsWith($relativePath, '/' . M3_FILENAME_INDEX)){ // ルートURL配下のとき
220 } else { // ルートURL以外のURLのとき(SSL用のURL以下かどうかチェック)
221 $relativePath = str_replace($systemSslUrl, '', $url); // ルートURLからの相対パスを取得
222 if (empty($relativePath)){ // Magic3のルートURLの場合
224 } else if (strStartsWith($relativePath, '/') || strStartsWith($relativePath, '/' . M3_FILENAME_INDEX)){ // ルートURL配下のとき