OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / dropdown_menu / include / container / admin_dropdown_menuWidgetContainer.php
1 <?php
2 /**
3  * コンテナクラス
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: admin_dropdown_menuWidgetContainer.php 4945 2012-06-08 01:35:42Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/dropdown_menuDb.php');
18
19 class admin_dropdown_menuWidgetContainer extends BaseAdminWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $serialNo;              // 選択中の項目のシリアル番号
23         private $serialArray = array();                 // 表示中のシリアル番号
24         private $langId;
25         private $configId;              // 定義ID
26         private $paramObj;              // パラメータ保存用オブジェクト
27         private $menuId;                // メニューID
28         private $menuType;              // メニュータイプ
29         private $css;                   // メニュー用CSS
30         private $cssId;                 // CSS用ID
31         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
32         const DEFAULT_MENU_ID = 'main_menu';                    // デフォルトメニューID
33         const MAX_MENU_TREE_LEVEL = 5;                  // メニュー階層最大数
34         const DEFAULT_MENU_CLASS = 'sf-menu';   // メニューのクラス名
35         const VERTICAL_MENU_CLASS = 'sf-vertical';              // 縦型用メニュークラス名
36         
37         /**
38          * コンストラクタ
39          */
40         function __construct()
41         {
42                 // 親クラスを呼び出す
43                 parent::__construct();
44                 
45                 // DBオブジェクト作成
46                 $this->db = new dropdown_menuDb();
47         }
48         /**
49          * テンプレートファイルを設定
50          *
51          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
52          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
53          *
54          * @param RequestManager $request               HTTPリクエスト処理クラス
55          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
56          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
57          */
58         function _setTemplate($request, &$param)
59         {
60                 $task = $request->trimValueOf('task');
61                 if ($task == 'list'){           // 一覧画面
62                         return 'admin_list.tmpl.html';
63                 } else {                        // 一覧画面
64                         return 'admin.tmpl.html';
65                 }
66         }
67         /**
68          * テンプレートにデータ埋め込む
69          *
70          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
71          *
72          * @param RequestManager $request               HTTPリクエスト処理クラス
73          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
74          * @param                                                               なし
75          */
76         function _assign($request, &$param)
77         {
78                 $task = $request->trimValueOf('task');
79                 if ($task == 'list'){           // 一覧画面
80                         return $this->createList($request);
81                 } else {                        // 詳細設定画面
82                         return $this->createDetail($request);
83                 }
84         }
85         /**
86          * 詳細画面作成
87          *
88          * @param RequestManager $request               HTTPリクエスト処理クラス
89          * @param                                                               なし
90          */
91         function createDetail($request)
92         {
93                 // ページ定義IDとページ定義のレコードシリアル番号を取得
94                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
95                 
96                 $userId         = $this->gEnv->getCurrentUserId();
97                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
98                 $act = $request->trimValueOf('act');
99                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
100
101                 $this->configId = $request->trimValueOf('item_id');             // 定義ID
102                 if (empty($this->configId)) $this->configId = $defConfigId;             // 呼び出しウィンドウから引き継いだ定義ID
103                 $name   = $request->trimValueOf('item_name');                   // ヘッダタイトル
104                 $showTitle = ($request->trimValueOf('item_showtitle') == 'on') ? 1 : 0;         // タイトルを表示するかどうか
105                 $this->menuType = $request->trimValueOf('item_menu_type');              // メニュータイプ
106                 $this->menuId = $request->trimValueOf('item_menuid');
107                 if ($this->menuId == '') $this->menuId = self::DEFAULT_MENU_ID;
108                 $this->css      = $request->trimValueOf('item_css');            // メニュー用CSS
109                 $this->cssId    = $request->trimValueOf('item_css_id');         // CSS用ID
110                 
111                 $replaceNew = false;            // データを再取得するかどうか
112                 if (empty($act)){// 初期起動時
113                         // デフォルト値設定
114                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
115                         $replaceNew = true;                     // データ再取得
116                 } else if ($act == 'add'){// 新規追加
117                         // 入力チェック
118                         $this->checkInput($name, '名前');
119                         
120                         // エラーなしの場合は、データを登録
121                         if ($this->getMsgCount() == 0){
122                                 // 追加オブジェクト作成
123                                 $newObj = new stdClass;
124                                 $newObj->menuId = $this->menuId;                // メニューID
125                                 $newObj->name   = $name;// 表示名
126                                 $newObj->showTitle = $showTitle;                // タイトルを表示するかどうか
127                                 $newObj->menuType = $this->menuType;                    // メニュータイプ
128                                 $newObj->cssId  = $this->cssId;                                 // CSS用ID
129                                 $newObj->css    = $this->css;                                   // メニューCSS
130                                 
131                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj, $this->menuId);
132                                 if ($ret){
133                                         $this->setGuidanceMsg('データを追加しました');
134                                         
135                                         $this->configId = $defConfigId;         // 定義定義IDを更新
136                                         $replaceNew = true;                     // データ再取得
137                                 } else {
138                                         $this->setAppErrorMsg('データ追加に失敗しました');
139                                 }
140                         }
141                 } else if ($act == 'update'){           // 設定更新のとき
142                         // 入力値のエラーチェック
143                         
144                         if ($this->getMsgCount() == 0){                 // エラーのないとき
145                                 // 現在の設定値を取得
146                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
147                                 if ($ret){
148                                         // ウィジェットオブジェクト更新
149                                         $targetObj->showTitle = $showTitle;                                     // タイトルを表示するかどうか
150                                         //$targetObj->menuType = $this->menuType;                       // メニュータイプは更新しない
151                                         $targetObj->cssId       = $this->cssId;                                 // CSS用ID
152                                         $targetObj->css         = $this->css;                                   // メニューCSS
153                                         
154                                         // 取得値で更新
155                                         $this->menuId = $targetObj->menuId;             // メニューID
156                                 }
157                                 
158                                 // 設定値を更新
159                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj, $this->menuId);
160                                 if ($ret){
161                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
162                                         $replaceNew = true;                     // データ再取得
163                                 } else {
164                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
165                                 }
166                         }
167                 } else if ($act == 'select' || $act == 'reload'){       // 定義IDを変更、データ再取得
168                         $replaceNew = true;                     // データ再取得
169                 } else if ($act == 'select_menutype'){          // メニュータイプ変更
170                 }
171                 // 設定項目選択メニュー作成
172                 $this->createItemMenu();
173                 
174                 // メニューID選択メニュー作成
175                 $this->db->getMenuIdList(array($this, 'menuIdListLoop'));
176                 
177                 // プレビューメニュー作成
178                 $previewHtml = $this->createMenu($this->menuId, 0);
179                                 
180                 // 表示用データを取得
181                 if (empty($this->configId)){            // 新規登録の場合
182                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
183                         $name = $this->createDefaultName();                     // デフォルト登録項目名
184                         $this->cssId = $this->createDefaultCssId();     // CSS用ID
185                         $showTitle = 1;                                 // タイトルを表示するかどうか
186                         
187                         // デフォルト用のCSSを取得
188                         $this->css = $this->getParsedTemplateData('default.tmpl.css', array($this, 'makeCss'));
189                         $this->serialNo = 0;
190                 } else {
191                         if ($replaceNew){
192                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
193                                 if ($ret){
194                                         $this->menuId   = $targetObj->menuId;           // メニューID
195                                         $name                   = $targetObj->name;// 名前
196                                         $showTitle              = $targetObj->showTitle;                                        // タイトルを表示するかどうか
197                                         $this->menuType = $targetObj->menuType;                 // メニュータイプ
198                                         $this->cssId    = $targetObj->cssId;                                    // CSS用ID
199                                         $this->css              = $targetObj->css;                                      // メニューCSS
200                                 }
201                         }
202                         $this->serialNo = $this->configId;
203                         
204                         // メニュータイプの変更を不可にする
205                         $this->tmpl->addVar("_widget", "horiz_disabled", 'disabled');
206                         $this->tmpl->addVar("_widget", "vert_disabled", 'disabled');
207                                 
208                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
209                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
210                 }
211                 // プレビュー用のCSSを作成
212                 $this->headCss = str_replace(M3_TAG_START . M3_TAG_MACRO_WIDGET_URL . M3_TAG_END, $this->gEnv->getCurrentWidgetRootUrl(), $this->css);
213                 
214                 // 画面にデータを埋め込む
215                 $this->tmpl->addVar("item_name_visible", "name", $name);                // 名前
216                 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId);             // 定義ID
217                 $checked = '';
218                 if ($showTitle) $checked = 'checked';
219                 $this->tmpl->addVar("_widget", "show_title", $checked); // タイトルを表示するかどうか
220                 
221                 // メニュークラスの設定
222                 if (empty($this->menuType)){                    // メニュータイプ(横型)
223                         $this->tmpl->addVar("_widget", "horiz_checked", 'checked');
224                         $this->tmpl->addVar("_widget", "css_class",     self::DEFAULT_MENU_CLASS);
225                 } else {
226                         $this->tmpl->addVar("_widget", "vert_checked", 'checked');
227                         $this->tmpl->addVar("_widget", "css_class",     self::DEFAULT_MENU_CLASS . ' ' . self::VERTICAL_MENU_CLASS);
228                 }
229                 $this->tmpl->addVar("_widget", "css_id",        $this->cssId);  // CSS用ID
230                 $this->tmpl->addVar("_widget", "css",   $this->css);
231                 $this->tmpl->addVar("_widget", "preview",       $previewHtml);
232                 
233                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
234                 
235                 // ボタンの表示制御
236                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
237                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
238                 } else {
239                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
240                         
241                         // ヘルプの追加
242                         $this->convertHelp('update_button');
243                 }
244                 
245                 // ページ定義IDとページ定義のレコードシリアル番号を更新
246                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
247         }
248         /**
249          * CSSデータをHTMLヘッダ部に設定
250          *
251          * CSSデータをHTMLのheadタグ内に追加出力する。
252          * _assign()よりも後に実行される。
253          *
254          * @param RequestManager $request               HTTPリクエスト処理クラス
255          * @param object         $param                 任意使用パラメータ。
256          * @return string                                               CSS文字列。出力しない場合は空文字列を設定。
257          */
258         function _addCssToHead($request, &$param)
259         {
260                 return $this->headCss;
261         }
262         /**
263          * 選択用メニューを作成
264          *
265          * @return なし                                               
266          */
267         function createItemMenu()
268         {
269                 for ($i = 0; $i < count($this->paramObj); $i++){
270                         $id = $this->paramObj[$i]->id;// 定義ID
271                         $targetObj = $this->paramObj[$i]->object;
272                         $name = $targetObj->name;// 定義名
273                         $selected = '';
274                         if ($this->configId == $id) $selected = 'selected';
275
276                         $row = array(
277                                 'name' => $name,                // 名前
278                                 'value' => $id,         // 定義ID
279                                 'selected' => $selected // 選択中の項目かどうか
280                         );
281                         $this->tmpl->addVars('title_list', $row);
282                         $this->tmpl->parseTemplate('title_list', 'a');
283                 }
284         }
285         /**
286          * デフォルトの名前を取得
287          *
288          * @return string       デフォルト名                                              
289          */
290         function createDefaultName()
291         {
292                 $name = self::DEFAULT_NAME_HEAD;
293                 for ($j = 1; $j < 100; $j++){
294                         $name = self::DEFAULT_NAME_HEAD . $j;
295                         // 設定名の重複チェック
296                         for ($i = 0; $i < count($this->paramObj); $i++){
297                                 $targetObj = $this->paramObj[$i]->object;
298                                 if ($name == $targetObj->name){         // 定義名
299                                         break;
300                                 }
301                         }
302                         // 重複なしのときは終了
303                         if ($i == count($this->paramObj)) break;
304                 }
305                 return $name;
306         }
307         /**
308          * CSS用のデフォルトのIDを取得
309          *
310          * @return string       ID                                              
311          */
312         function createDefaultCssId()
313         {
314                 return $this->gEnv->getCurrentWidgetId() . '_' . $this->getTempConfigId($this->paramObj);
315         }
316         /**
317          * 取得したデータをテンプレートに設定する
318          *
319          * @param int $index                    行番号(0~)
320          * @param array $fetchedRow             フェッチ取得した行
321          * @param object $param                 未使用
322          * @return bool                                 true=処理続行の場合、false=処理終了の場合
323          */
324         function menuIdListLoop($index, $fetchedRow, $param)
325         {
326                 $value = $fetchedRow['mn_id'];
327                 $name = $fetchedRow['mn_name'];
328                         
329                 $selected = '';
330                 if ($value == $this->menuId) $selected = 'selected';
331                 
332                 $row = array(
333                         'value'    => $value,                   // ページID
334                         'name'     => $name,                    // ページ名
335                         'selected' => $selected                                                                                                         // 選択中かどうか
336                 );
337                 $this->tmpl->addVars('menu_id_list', $row);
338                 $this->tmpl->parseTemplate('menu_id_list', 'a');
339                 return true;
340         }
341         /**
342          * 一覧画面作成
343          *
344          * @param RequestManager $request               HTTPリクエスト処理クラス
345          * @param                                                               なし
346          */
347         function createList($request)
348         {
349                 // ページ定義IDとページ定義のレコードシリアル番号を取得
350                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
351                 
352                 $userId         = $this->gEnv->getCurrentUserId();
353                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
354                 $act = $request->trimValueOf('act');
355                 
356                 if ($act == 'delete'){          // メニュー項目の削除
357                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
358                         $delItems = array();
359                         for ($i = 0; $i < count($listedItem); $i++){
360                                 // 項目がチェックされているかを取得
361                                 $itemName = 'item' . $i . '_selected';
362                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
363                                 
364                                 if ($itemValue){                // チェック項目
365                                         $delItems[] = $listedItem[$i];
366                                 }
367                         }
368                         if (count($delItems) > 0){
369                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
370                                 if ($ret){              // データ削除成功のとき
371                                         $this->setGuidanceMsg('データを削除しました');
372                                 } else {
373                                         $this->setAppErrorMsg('データ削除に失敗しました');
374                                 }
375                         }
376                 }
377                 // 定義一覧作成
378                 $this->createItemList();
379                 
380                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
381                 
382                 // ページ定義IDとページ定義のレコードシリアル番号を更新
383                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
384         }
385         /**
386          * 定義一覧作成
387          *
388          * @return なし                                               
389          */
390         function createItemList()
391         {
392                 for ($i = 0; $i < count($this->paramObj); $i++){
393                         $id                     = $this->paramObj[$i]->id;// 定義ID
394                         $targetObj      = $this->paramObj[$i]->object;
395                         $name = $targetObj->name;// 定義名
396                         
397                         // メニュー定義名を取得
398                         $menuName = '';
399                         if ($this->db->getMenu($targetObj->menuId, $row)){
400                                 $menuName = $row['mn_name'];
401                         }
402                         
403                         $defCount = 0;
404                         if (!empty($id)){
405                                 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
406                         }
407                         $operationDisagled = '';
408                         if ($defCount > 0) $operationDisagled = 'disabled';
409                         $row = array(
410                                 'index' => $i,
411                                 'id' => $id,
412                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
413                                 'name' => $this->convertToDispString($name),            // 名前
414                                 'menu_name' => $this->convertToDispString($menuName),           // メニュー定義名
415                                 'def_count' => $defCount                                                        // 使用数
416                         );
417                         $this->tmpl->addVars('itemlist', $row);
418                         $this->tmpl->parseTemplate('itemlist', 'a');
419                         
420                         // シリアル番号を保存
421                         $this->serialArray[] = $id;
422                 }
423         }
424         /**
425          * メニューツリー作成
426          *
427          * @param string        $menuId         メニューID
428          * @param int           $parantId       親メニュー項目ID
429          * @param int           $level          階層数
430          * @return string               ツリーメニュータグ
431          */
432         function createMenu($menuId, $parantId, $level = 0)
433         {
434                 // メニューの階層を制限
435                 if ($level >= self::MAX_MENU_TREE_LEVEL) return '';
436                 
437                 $treeHtml = '';
438                 if ($this->db->getChildMenuItems($menuId, $parantId, $rows)){
439                         $itemCount = count($rows);
440                         for ($i = 0; $i < $itemCount; $i++){
441                                 $row = $rows[$i];
442                                 
443                                 // 非表示のときは処理を飛ばす
444                                 if (!$row['md_visible']) continue;
445
446                                 // リンク先の作成
447                                 $linkUrl = $row['md_link_url'];
448                                 $linkUrl = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getRootUrl(), $linkUrl);
449                                 if (empty($linkUrl)) $linkUrl = '#';
450                                 $linkUrl = $this->convertUrlToHtmlEntity($linkUrl);
451                                 
452                                 // リンクタイプに合わせてタグを生成
453                                 $option = '';
454                                 switch ($row['md_link_type']){
455                                         case 0:                 // 同ウィンドウで開くリンク
456                                                 break;
457                                         case 1:                 // 別ウィンドウで開くリンク
458                                                 $option = 'target="_blank"';
459                                                 break;
460                                 }
461                                 // メニュー項目を作成
462                                 $name = $this->getCurrentLangString($row['md_name']);
463                                 if (empty($name)) continue;
464                                 
465                                 // ##### ツリーメニュー作成 #####
466                                 if ($row['md_type'] == 0){      // リンク項目のとき
467                                         $treeHtml .= '<li><a href="' . $linkUrl . '" ' . $option . '>' . $this->convertToDispString($name) . '</a></li>' . M3_NL;
468                                 } else if ($row['md_type'] == 1){                       // フォルダのとき
469                                         // サブメニュー作成
470                                         $treeHtml .= '<li><a class="head" href="#">' . $this->convertToDispString($name) . '</a>' . M3_NL;
471                                         $treeHtml .= '<ul>' . M3_NL;
472                                         $treeHtml .= $this->createMenu($menuId, $row['md_id'], $level + 1);
473                                         $treeHtml .= '</ul>' . M3_NL;
474                                         $treeHtml .= '</li>' . M3_NL;
475                                 } else if ($row['md_type'] == 2){                       // テキストのとき
476                                         //$treeHtml .= '<li><span>' . $this->convertToDispString($name) . '</span></li>' . M3_NL;
477                                 } else if ($row['md_type'] == 3){                       // セパレータのとき
478                                         //$treeHtml .= '<li><span>' . '-----' . '</span></li>' . M3_NL;
479                                 }
480                         }
481                 }
482                 return $treeHtml;
483         }
484         /**
485          * CSSデータ作成処理コールバック
486          *
487          * @param object         $tmpl                  テンプレートオブジェクト
488          * @param                                                               なし
489          */
490         function makeCss($tmpl)
491         {
492                 // メニュータイプが縦型のときは縦型用のCSSを追加
493                 if (!empty($this->menuType)){
494                         $tmpl->setAttribute('add_vertical', 'visibility', 'visible');
495                         $tmpl->addVar('add_vertical', 'id', '#' . $this->cssId);
496                 }
497                 $tmpl->addVar('_tmpl', 'id', '#' . $this->cssId);
498         }
499 }
500 ?>