OSDN Git Service

DB更新スクリプト更新。
[magic3/magic3.git] / widgets / m / menu / include / container / m_menuWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
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
13  * @version    SVN: $Id$
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/menuDb.php');
18
19 class m_menuWidgetContainer extends BaseWidgetContainer
20 {
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;                  // メニュー階層最大数
28         
29         /**
30          * コンストラクタ
31          */
32         function __construct()
33         {
34                 // 親クラスを呼び出す
35                 parent::__construct();
36                 
37                 // DBオブジェクト作成
38                 $this->db = new menuDb();
39         }
40         /**
41          * テンプレートファイルを設定
42          *
43          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
44          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
45          *
46          * @param RequestManager $request               HTTPリクエスト処理クラス
47          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
48          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
49          */
50         function _setTemplate($request, &$param)
51         {
52                 return 'index.tmpl.html';
53         }
54         /**
55          * テンプレートにデータ埋め込む
56          *
57          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
58          *
59          * @param RequestManager $request               HTTPリクエスト処理クラス
60          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
61          * @param                                                               なし
62          */
63         function _assign($request, &$param)
64         {
65                 $this->langId = $this->gEnv->getCurrentLanguage();
66                 $this->currentUserLogined = $this->gEnv->isCurrentUserLogined();        // 現在のユーザはログイン中かどうか
67                 
68                 // 定義ID取得
69                 $configId = $this->gEnv->getCurrentWidgetConfigId();
70                 if (empty($configId)) $configId = self::DEFAULT_CONFIG_ID;
71                 
72                 // パラメータオブジェクトを取得
73                 $targetObj = $this->getWidgetParamObjByConfigId($configId);
74                 if (empty($targetObj)){         // 定義データが取得できないとき
75                         // 出力抑止
76                         $this->cancelParse();
77                 } else {
78                         $menuId         = $targetObj->menuId;   // メニューID
79                         $name           = $targetObj->name;// 定義名
80                         $limitUser      = $targetObj->limitUser;// ユーザを制限するかどうか
81
82                         // ユーザ制限があるときはログイン時のみ表示
83                         if (!$limitUser || $this->currentUserLogined){
84                                 // メニュー作成
85                                 $parentTree = array();                  // 選択されている項目までの階層パス
86                                 $menuHtml = $this->createMenu($menuId, 0, 0, $tmp, $parentTree);
87                                 
88                                 if (!empty($menuHtml)) $this->tmpl->addVar("_widget", "menu_html", $menuHtml);
89                         } else {
90                                 // 出力抑止
91                                 $this->cancelParse();
92                         }
93                 }
94         }
95         /**
96          * メニューツリー作成
97          *
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                               ツリーメニュータグ
104          */
105         function createMenu($menuId, $parantId, $level, &$hasSelectedChild, &$parentTree)
106         {
107                 static $index = 0;              // インデックス番号
108                 $hasSelectedChild = false;
109
110                 // メニューの階層を制限
111                 if ($level >= self::MAX_MENU_TREE_LEVEL) return '';
112                 
113                 $treeHtml = '';
114                 if ($this->db->getChildMenuItems($menuId, $parantId, $this->langId, $rows)){
115                         $itemCount = count($rows);
116                         for ($i = 0; $i < $itemCount; $i++){
117                                 $row = $rows[$i];
118                                                                         
119                                 // 非表示のときは処理を飛ばす
120                                 if (!$row['md_visible']) continue;
121                                 
122                                 // ユーザ制限がある場合はログイン状態をチェック
123                                 if ($row['md_user_limited'] && !$this->currentUserLogined) continue;
124                 
125                                 // リンク先のコンテンツの表示状況に合わせる
126                                 if ($row['md_content_type'] == M3_VIEW_TYPE_CONTENT){           // 汎用コンテンツの場合
127                                         // ログインユーザに表示制限されている場合はメニューを追加しない
128                                         if (!empty($row['cn_user_limited']) && !$this->currentUserLogined) continue;
129                                 }
130                                 
131                                 // リンク先の作成
132                                 $linkUrl = $row['md_link_url'];
133                                 $linkUrl = $this->getUrl($linkUrl, true/*リンク用*/);
134                                 if (empty($linkUrl)) $linkUrl = '#';
135                                 
136                                 // 選択状態の設定
137                                 if ($this->checkMenuItemUrl($linkUrl)){
138                                         $hasSelectedChild = true;
139                                 }
140                                 
141                                 // メニュー項目を作成
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;
146                                 
147                                 // メニュータイトルの処理。タグが含まれていない場合は文字をエスケープする。
148                                 $stripName = strip_tags($title);
149                                 if (strlen($stripName) == strlen($title)) $title = $this->convertToDispString($title);          // 文字列長が同じとき
150                                 
151                                 $index++;               // インデックス番号更新
152                                                                 
153                                 switch ($row['md_type']){
154                                         case 0:                 // リンク項目のとき
155                                         default:                // フォルダ等
156                                                 $treeHtml .= '<a href="' . $this->convertUrlToHtmlEntity($linkUrl) . '" >' . $title . '</a><br />' . M3_NL;
157                                                 break;
158                                         case 2:                 // テキストのとき
159                                                 $treeHtml .= $title . '<br />' . M3_NL;
160                                                 break;
161                                         case 3:                 // セパレータのとき
162                                                 // ##### タグ作成 #####
163                                                 $treeHtml .= '<hr />' . M3_NL;
164                                                 break;
165                                 }
166                         }
167                 }
168                 return $treeHtml;
169         }
170         /**
171          * メニュー項目の選択条件をチェック
172          *
173          * @param string $url   チェック対象のURL
174          * @return bool                 true=アクティブ、false=非アクティブ
175          */
176         function checkMenuItemUrl($url)
177         {
178                 $currentUrl = $this->gEnv->getCurrentRequestUri();
179                 
180                 // 同じURLのとき
181                 if ($url == $currentUrl) return true;
182                 
183                 // URLを解析
184                 $queryArray = array();
185                 $parsedUrl = parse_url($url);
186                 if (!empty($parsedUrl['query'])) parse_str($parsedUrl['query'], $queryArray);           // クエリーの解析
187                 
188                 // ルートかどうかチェック(クエリー文字列なし)
189                 if ($this->isRootUrl($url)){
190                         // ページサブIDで比較
191                         if ($this->gEnv->getCurrentPageSubId() == $this->gEnv->getDefaultPageSubId()) return true;
192                 }
193                 
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;
197                 }
198                 return false;
199         }
200         /**
201          * URLがルートを指しているかどうか取得
202          *
203          * @param string $url   チェック対象のURL
204          * @return bool                 true=ルート、false=ルート以外
205          */
206         function isRootUrl($url)
207         {
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に統一
211
212                 $parsedUrl = parse_url($url);
213                 if (empty($parsedUrl['query'])){                // クエリ文字列がないことが条件。「#」はあっても良い。
214                         // パスを解析
215                         $relativePath = str_replace($systemUrl, '', $url);              // ルートURLからの相対パスを取得
216                         if (empty($relativePath)){                      // Magic3のルートURLの場合
217                                 return true;
218                         } else if (strStartsWith($relativePath, '/') || strStartsWith($relativePath, '/' . M3_FILENAME_INDEX)){         // ルートURL配下のとき
219                                 return true;
220                         } else {                // ルートURL以外のURLのとき(SSL用のURL以下かどうかチェック)
221                                 $relativePath = str_replace($systemSslUrl, '', $url);           // ルートURLからの相対パスを取得
222                                 if (empty($relativePath)){                      // Magic3のルートURLの場合
223                                         return true;
224                                 } else if (strStartsWith($relativePath, '/') || strStartsWith($relativePath, '/' . M3_FILENAME_INDEX)){         // ルートURL配下のとき
225                                         return true;
226                                 }
227                         }
228                 }
229                 return false;
230         }
231 }
232 ?>