OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / accordion_menu / include / container / accordion_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-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: accordion_menuWidgetContainer.php 4943 2012-06-07 23:21:43Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/accordion_menuDb.php');
18
19 class accordion_menuWidgetContainer extends BaseWidgetContainer
20 {
21         private $db;                    // DB接続オブジェクト
22         private $langId;                // 現在の言語
23         private $paramObj;              // 定義取得用
24         private $headCss;                       // ヘッダ出力用CSS
25         const DEFAULT_CONFIG_ID = 0;
26         const MAX_MENU_TREE_LEVEL = 5;                  // メニュー階層最大数
27         
28         /**
29          * コンストラクタ
30          */
31         function __construct()
32         {
33                 // 親クラスを呼び出す
34                 parent::__construct();
35                 
36                 // DBオブジェクト作成
37                 $this->db = new accordion_menuDb();
38         }
39         /**
40          * テンプレートファイルを設定
41          *
42          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
43          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
44          *
45          * @param RequestManager $request               HTTPリクエスト処理クラス
46          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
47          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
48          */
49         function _setTemplate($request, &$param)
50         {
51                 return 'index.tmpl.html';
52         }
53         /**
54          * テンプレートにデータ埋め込む
55          *
56          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
57          *
58          * @param RequestManager $request               HTTPリクエスト処理クラス
59          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
60          * @param                                                               なし
61          */
62         function _assign($request, &$param)
63         {
64                 $this->langId = $this->gEnv->getCurrentLanguage();
65                 
66                 // 定義ID取得
67                 $configId = $this->gEnv->getCurrentWidgetConfigId();
68                 if (empty($configId)) $configId = self::DEFAULT_CONFIG_ID;
69                 
70                 // パラメータオブジェクトを取得
71                 $targetObj = $this->getWidgetParamObjByConfigId($configId);
72                 if (!empty($targetObj)){                // 定義データが取得できたとき
73                         $menuId         = $targetObj->menuId;   // メニューID
74                         $name           = $targetObj->name;// 定義名
75                         $css            = $targetObj->css;              // メニューCSS
76                         $cssId          = $targetObj->cssId;    // メニューCSSのID
77
78                         $this->tmpl->setAttribute('_widget', 'visibility', 'visible');
79                 
80                         // メニュー作成
81                         $menuHtml = $this->createMenu($menuId, 0);
82                         $this->tmpl->addVar("_widget", "menu_html", $menuHtml);
83                         $this->tmpl->addVar("_widget", "css_id",        $cssId);        // CSS用ID
84                         
85                         // CSS作成
86                         $this->headCss = str_replace(M3_TAG_START . M3_TAG_MACRO_WIDGET_URL . M3_TAG_END, $this->gEnv->getCurrentWidgetRootUrl(), $css);
87                 }
88         }
89         /**
90          * CSSデータをHTMLヘッダ部に設定
91          *
92          * CSSデータをHTMLのheadタグ内に追加出力する。
93          * _assign()よりも後に実行される。
94          *
95          * @param RequestManager $request               HTTPリクエスト処理クラス
96          * @param object         $param                 任意使用パラメータ。
97          * @return string                                               CSS文字列。出力しない場合は空文字列を設定。
98          */
99         function _addCssToHead($request, &$param)
100         {
101                 return $this->headCss;
102         }
103         /**
104          * メニューツリー作成
105          *
106          * @param string        $menuId         メニューID
107          * @param int           $parantId       親メニュー項目ID
108          * @param int           $level          階層数
109          * @return string               ツリーメニュータグ
110          */
111         function createMenu($menuId, $parantId, $level = 0)
112         {
113                 // メニューの階層を制限
114                 if ($level >= self::MAX_MENU_TREE_LEVEL) return '';
115                 
116                 $treeHtml = '';
117                 if ($this->db->getChildMenuItems($menuId, $parantId, $rows)){
118                         $itemCount = count($rows);
119                         for ($i = 0; $i < $itemCount; $i++){
120                                 $row = $rows[$i];
121                                 
122                                 // 非表示のときは処理を飛ばす
123                                 if (!$row['md_visible']) continue;
124
125                                 // リンク先の作成
126                                 $linkUrl = $row['md_link_url'];
127                                 $linkUrl = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getRootUrl(), $linkUrl);
128                                 if (empty($linkUrl)) $linkUrl = '#';
129                                 $linkUrl = $this->convertUrlToHtmlEntity($linkUrl);
130                                 
131                                 // リンクタイプに合わせてタグを生成
132                                 $option = '';
133                                 switch ($row['md_link_type']){
134                                         case 0:                 // 同ウィンドウで開くリンク
135                                                 break;
136                                         case 1:                 // 別ウィンドウで開くリンク
137                                                 $option = 'target="_blank"';
138                                                 break;
139                                 }
140                                 
141                                 // メニュー項目タイトル
142                                 $name = $this->getCurrentLangString($row['md_name']);
143                                 if (empty($name)) continue;
144                                 
145                                 // ##### ツリーメニュー作成 #####
146                                 if ($row['md_type'] == 0){      // リンク項目のとき
147                                         $treeHtml .= '<li><a href="' . $linkUrl . '" ' . $option . '>' . $this->convertToDispString($name) . '</a></li>' . M3_NL;
148                                 } else if ($row['md_type'] == 1){                       // フォルダのとき
149                                         // サブメニュー作成
150                                         $treeHtml .= '<li><a class="head" href="#">' . $this->convertToDispString($name) . '</a>' . M3_NL;
151                                         $treeHtml .= '<ul>' . M3_NL;
152                                         $treeHtml .= $this->createMenu($menuId, $row['md_id'], $level + 1);
153                                         $treeHtml .= '</ul>' . M3_NL;
154                                         $treeHtml .= '</li>' . M3_NL;
155                                 } else if ($row['md_type'] == 2){                       // テキストのとき
156                                         //$treeHtml .= '<li><span>' . $this->convertToDispString($row['md_name']) . '</span></li>' . M3_NL;
157                                 } else if ($row['md_type'] == 3){                       // セパレータのとき
158                                         //$treeHtml .= '<li><span>' . '-----' . '</span></li>' . M3_NL;
159                                 }
160                         }
161                 }
162                 return $treeHtml;
163         }
164 }
165 ?>