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-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
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/dropdown_menuDb.php');
19 class admin_dropdown_menuWidgetContainer extends BaseAdminWidgetContainer
21 private $db; // DB接続オブジェクト
22 private $serialNo; // 選択中の項目のシリアル番号
23 private $serialArray = array(); // 表示中のシリアル番号
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'; // 縦型用メニュークラス名
40 function __construct()
43 parent::__construct();
46 $this->db = new dropdown_menuDb();
51 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
52 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
54 * @param RequestManager $request HTTPリクエスト処理クラス
55 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
56 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
58 function _setTemplate($request, &$param)
60 $task = $request->trimValueOf('task');
61 if ($task == 'list'){ // 一覧画面
62 return 'admin_list.tmpl.html';
64 return 'admin.tmpl.html';
70 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
72 * @param RequestManager $request HTTPリクエスト処理クラス
73 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
76 function _assign($request, &$param)
78 $task = $request->trimValueOf('task');
79 if ($task == 'list'){ // 一覧画面
80 return $this->createList($request);
82 return $this->createDetail($request);
88 * @param RequestManager $request HTTPリクエスト処理クラス
91 function createDetail($request)
93 // ページ定義IDとページ定義のレコードシリアル番号を取得
94 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
96 $userId = $this->gEnv->getCurrentUserId();
97 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
98 $act = $request->trimValueOf('act');
99 $this->serialNo = $request->trimValueOf('serial'); // 選択項目のシリアル番号
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
111 $replaceNew = false; // データを再取得するかどうか
112 if (empty($act)){// 初期起動時
114 $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
115 $replaceNew = true; // データ再取得
116 } else if ($act == 'add'){// 新規追加
118 $this->checkInput($name, '名前');
121 if ($this->getMsgCount() == 0){
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
131 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj, $this->menuId);
133 $this->setGuidanceMsg('データを追加しました');
135 $this->configId = $defConfigId; // 定義定義IDを更新
136 $replaceNew = true; // データ再取得
138 $this->setAppErrorMsg('データ追加に失敗しました');
141 } else if ($act == 'update'){ // 設定更新のとき
144 if ($this->getMsgCount() == 0){ // エラーのないとき
146 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
149 $targetObj->showTitle = $showTitle; // タイトルを表示するかどうか
150 //$targetObj->menuType = $this->menuType; // メニュータイプは更新しない
151 $targetObj->cssId = $this->cssId; // CSS用ID
152 $targetObj->css = $this->css; // メニューCSS
155 $this->menuId = $targetObj->menuId; // メニューID
159 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj, $this->menuId);
161 $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
162 $replaceNew = true; // データ再取得
164 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
167 } else if ($act == 'select' || $act == 'reload'){ // 定義IDを変更、データ再取得
168 $replaceNew = true; // データ再取得
169 } else if ($act == 'select_menutype'){ // メニュータイプ変更
172 $this->createItemMenu();
175 $this->db->getMenuIdList(array($this, 'menuIdListLoop'));
178 $previewHtml = $this->createMenu($this->menuId, 0);
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; // タイトルを表示するかどうか
188 $this->css = $this->getParsedTemplateData('default.tmpl.css', array($this, 'makeCss'));
192 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
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
202 $this->serialNo = $this->configId;
205 $this->tmpl->addVar("_widget", "horiz_disabled", 'disabled');
206 $this->tmpl->addVar("_widget", "vert_disabled", 'disabled');
208 // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
209 if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
212 $this->headCss = str_replace(M3_TAG_START . M3_TAG_MACRO_WIDGET_URL . M3_TAG_END, $this->gEnv->getCurrentWidgetRootUrl(), $this->css);
215 $this->tmpl->addVar("item_name_visible", "name", $name); // 名前
216 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId); // 定義ID
218 if ($showTitle) $checked = 'checked';
219 $this->tmpl->addVar("_widget", "show_title", $checked); // タイトルを表示するかどうか
222 if (empty($this->menuType)){ // メニュータイプ(横型)
223 $this->tmpl->addVar("_widget", "horiz_checked", 'checked');
224 $this->tmpl->addVar("_widget", "css_class", self::DEFAULT_MENU_CLASS);
226 $this->tmpl->addVar("_widget", "vert_checked", 'checked');
227 $this->tmpl->addVar("_widget", "css_class", self::DEFAULT_MENU_CLASS . ' ' . self::VERTICAL_MENU_CLASS);
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);
233 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
236 if (empty($this->serialNo)){ // 新規追加項目を選択しているとき
237 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
239 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
242 $this->convertHelp('update_button');
245 // ページ定義IDとページ定義のレコードシリアル番号を更新
246 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
251 * CSSデータをHTMLのheadタグ内に追加出力する。
252 * _assign()よりも後に実行される。
254 * @param RequestManager $request HTTPリクエスト処理クラス
255 * @param object $param 任意使用パラメータ。
256 * @return string CSS文字列。出力しない場合は空文字列を設定。
258 function _addCssToHead($request, &$param)
260 return $this->headCss;
267 function createItemMenu()
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;// 定義名
274 if ($this->configId == $id) $selected = 'selected';
277 'name' => $name, // 名前
278 'value' => $id, // 定義ID
279 'selected' => $selected // 選択中の項目かどうか
281 $this->tmpl->addVars('title_list', $row);
282 $this->tmpl->parseTemplate('title_list', 'a');
288 * @return string デフォルト名
290 function createDefaultName()
292 $name = self::DEFAULT_NAME_HEAD;
293 for ($j = 1; $j < 100; $j++){
294 $name = self::DEFAULT_NAME_HEAD . $j;
296 for ($i = 0; $i < count($this->paramObj); $i++){
297 $targetObj = $this->paramObj[$i]->object;
298 if ($name == $targetObj->name){ // 定義名
303 if ($i == count($this->paramObj)) break;
312 function createDefaultCssId()
314 return $this->gEnv->getCurrentWidgetId() . '_' . $this->getTempConfigId($this->paramObj);
317 * 取得したデータをテンプレートに設定する
319 * @param int $index 行番号(0~)
320 * @param array $fetchedRow フェッチ取得した行
321 * @param object $param 未使用
322 * @return bool true=処理続行の場合、false=処理終了の場合
324 function menuIdListLoop($index, $fetchedRow, $param)
326 $value = $fetchedRow['mn_id'];
327 $name = $fetchedRow['mn_name'];
330 if ($value == $this->menuId) $selected = 'selected';
333 'value' => $value, // ページID
334 'name' => $name, // ページ名
335 'selected' => $selected // 選択中かどうか
337 $this->tmpl->addVars('menu_id_list', $row);
338 $this->tmpl->parseTemplate('menu_id_list', 'a');
344 * @param RequestManager $request HTTPリクエスト処理クラス
347 function createList($request)
349 // ページ定義IDとページ定義のレコードシリアル番号を取得
350 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
352 $userId = $this->gEnv->getCurrentUserId();
353 $langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
354 $act = $request->trimValueOf('act');
356 if ($act == 'delete'){ // メニュー項目の削除
357 $listedItem = explode(',', $request->trimValueOf('seriallist'));
359 for ($i = 0; $i < count($listedItem); $i++){
361 $itemName = 'item' . $i . '_selected';
362 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
364 if ($itemValue){ // チェック項目
365 $delItems[] = $listedItem[$i];
368 if (count($delItems) > 0){
369 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
370 if ($ret){ // データ削除成功のとき
371 $this->setGuidanceMsg('データを削除しました');
373 $this->setAppErrorMsg('データ削除に失敗しました');
378 $this->createItemList();
380 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
382 // ページ定義IDとページ定義のレコードシリアル番号を更新
383 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
390 function createItemList()
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;// 定義名
399 if ($this->db->getMenu($targetObj->menuId, $row)){
400 $menuName = $row['mn_name'];
405 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
407 $operationDisagled = '';
408 if ($defCount > 0) $operationDisagled = 'disabled';
412 'ope_disabled' => $operationDisagled, // 選択可能かどうか
413 'name' => $this->convertToDispString($name), // 名前
414 'menu_name' => $this->convertToDispString($menuName), // メニュー定義名
415 'def_count' => $defCount // 使用数
417 $this->tmpl->addVars('itemlist', $row);
418 $this->tmpl->parseTemplate('itemlist', 'a');
421 $this->serialArray[] = $id;
427 * @param string $menuId メニューID
428 * @param int $parantId 親メニュー項目ID
429 * @param int $level 階層数
430 * @return string ツリーメニュータグ
432 function createMenu($menuId, $parantId, $level = 0)
435 if ($level >= self::MAX_MENU_TREE_LEVEL) return '';
438 if ($this->db->getChildMenuItems($menuId, $parantId, $rows)){
439 $itemCount = count($rows);
440 for ($i = 0; $i < $itemCount; $i++){
444 if (!$row['md_visible']) continue;
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);
454 switch ($row['md_link_type']){
455 case 0: // 同ウィンドウで開くリンク
457 case 1: // 別ウィンドウで開くリンク
458 $option = 'target="_blank"';
462 $name = $this->getCurrentLangString($row['md_name']);
463 if (empty($name)) continue;
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){ // フォルダのとき
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;
487 * @param object $tmpl テンプレートオブジェクト
490 function makeCss($tmpl)
492 // メニュータイプが縦型のときは縦型用のCSSを追加
493 if (!empty($this->menuType)){
494 $tmpl->setAttribute('add_vertical', 'visibility', 'visible');
495 $tmpl->addVar('add_vertical', 'id', '#' . $this->cssId);
497 $tmpl->addVar('_tmpl', 'id', '#' . $this->cssId);