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-2014 Magic3 Project.
12 * @license http://www.gnu.org/copyleft/gpl.html GPL License
14 * @link http://www.magic3.org
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/menuDb.php');
19 class admin_m_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 const DEFAULT_NAME_HEAD = '名称未設定'; // デフォルトの設定名
29 const DEFAULT_MENU_ID = 'm_main_menu'; // デフォルトメニューID
34 function __construct()
37 parent::__construct();
40 $this->db = new menuDb();
45 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
46 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
48 * @param RequestManager $request HTTPリクエスト処理クラス
49 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
50 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
52 function _setTemplate($request, &$param)
54 $task = $request->trimValueOf('task');
55 if ($task == 'list'){ // 一覧画面
56 return 'admin_list.tmpl.html';
58 return 'admin.tmpl.html';
64 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
66 * @param RequestManager $request HTTPリクエスト処理クラス
67 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
70 function _assign($request, &$param)
72 $task = $request->trimValueOf('task');
73 if ($task == 'list'){ // 一覧画面
74 $this->createList($request);
76 $this->createDetail($request);
82 * @param RequestManager $request HTTPリクエスト処理クラス
85 function createDetail($request)
87 // ページ定義IDとページ定義のレコードシリアル番号を取得
88 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
90 $userId = $this->gEnv->getCurrentUserId();
91 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
92 $anchor = $request->trimValueOf('anchor');
93 $act = $request->trimValueOf('act');
94 $this->serialNo = $request->trimValueOf('serial'); // 選択項目のシリアル番号
96 $this->configId = $request->trimValueOf('item_id'); // 定義ID
97 if (empty($this->configId)) $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
98 $this->menuId = $request->trimValueOf('menuid');
99 if (empty($this->menuId)) $this->menuId = self::DEFAULT_MENU_ID;
100 $name = $request->trimValueOf('item_name'); // ヘッダタイトル
101 $limitUser = ($request->trimValueOf('item_limituser') == 'on') ? 1 : 0; // ユーザを制限するかどうか
103 $replaceNew = false; // データを再取得するかどうか
104 if (empty($act)){// 初期起動時
106 $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
107 $replaceNew = true; // データ再取得
108 } else if ($act == 'add'){// 新規追加
110 $this->checkInput($name, '名前');
113 if ($this->getMsgCount() == 0){
115 $newObj = new stdClass;
116 $newObj->menuId = $this->menuId; // メニューID
117 $newObj->name = $name;// 表示名
118 $newObj->limitUser = $limitUser; // ユーザを制限するかどうか
120 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj, $this->menuId);
122 $this->setGuidanceMsg('データを追加しました');
124 $this->configId = $defConfigId; // 定義定義IDを更新
125 $replaceNew = true; // データ再取得
127 $this->setAppErrorMsg('データ追加に失敗しました');
130 } else if ($act == 'update'){ // 設定更新のとき
133 if ($this->getMsgCount() == 0){ // エラーのないとき
135 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
137 // ウィジェットオブジェクト更新。更新値のみ再設定。
138 if (!empty($defConfigId) && !empty($defSerial)){ // 設定再選択不可の場合
140 $this->menuId = $targetObj->menuId; // メニューID
141 } else { // 新規で既存設定の更新
142 $targetObj->menuId = $this->menuId; // メニューID
144 $targetObj->limitUser = $limitUser; // ユーザを制限するかどうか
148 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj, $this->menuId);
150 $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
152 $replaceNew = true; // データ再取得
154 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
157 } else if ($act == 'select'){ // 定義IDを変更
158 $replaceNew = true; // データ再取得
162 if (empty($this->configId)){ // 新規登録の場合
163 $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
165 if ($replaceNew){ // データ再取得時
166 $this->menuId = self::DEFAULT_MENU_ID;
167 $name = $this->createDefaultName(); // デフォルト登録項目名
168 $limitUser = 0; // ユーザを制限するかどうか
173 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
175 $this->menuId = $targetObj->menuId; // メニューID
176 $name = $targetObj->name;// 名前
177 $limitUser = $targetObj->limitUser; // ユーザを制限するかどうか
180 $this->serialNo = $this->configId;
182 // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
183 if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
186 $this->createItemMenu();
189 $this->db->getMenuIdList(1/*携帯用*/, array($this, 'menuIdListLoop'));
191 // 一度設定を保存している場合は、メニュー定義を前面にする(初期起動時のみ)
193 if (empty($act) && !empty($this->configId)) $activeIndex = 1;
194 // 一覧画面からの戻り画面が指定されてる場合は優先する
195 if ($anchor == 'widget_config') $activeIndex = 0;
200 $tabItem = new stdClass;
201 $tabItem->name = 'ウィジェット設定';
203 $tabItem->url = '#widget_config';
204 $tabItem->parent = 0;
205 // $tabItem->active = ($tabItemIndex == $activeIndex) ? true : false;
206 $tabItem->active = false;
207 $tabDef[] = $tabItem; $tabItemIndex++;
208 $tabItem = new stdClass;
209 $tabItem->name = 'メニュー定義';
211 $tabItem->url = '#menu_define';
212 $tabItem->parent = 0;
213 // $tabItem->active = ($tabItemIndex == $activeIndex) ? true : false;
214 $tabItem->active = false;
215 $tabDef[] = $tabItem; $tabItemIndex++;
216 $tabHtml = $this->gDesign->createConfigNavTab($tabDef);
217 $this->tmpl->addVar("_widget", "nav_tab", $tabHtml);
218 if (empty($activeIndex)){ // タブの選択
219 $this->tmpl->addVar("_widget", "active_tab", 'widget_config');
221 $this->tmpl->addVar("_widget", "active_tab", 'menu_define');
225 $this->tmpl->addVar("item_name_visible", "name", $name); // 名前
226 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId); // 定義ID
228 $this->tmpl->addVar("_widget", "limit_user", $this->convertToCheckedString($limitUser)); // ユーザを制限するかどうか
230 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
233 if (empty($this->serialNo)){ // 新規追加項目を選択しているとき
234 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
236 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
239 // ページ定義IDとページ定義のレコードシリアル番号を更新
240 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
247 function createItemMenu()
249 for ($i = 0; $i < count($this->paramObj); $i++){
250 $id = $this->paramObj[$i]->id;// 定義ID
251 $targetObj = $this->paramObj[$i]->object;
252 $name = $targetObj->name;// 定義名
254 if ($this->configId == $id) $selected = 'selected';
257 'name' => $name, // 名前
258 'value' => $id, // 定義ID
259 'selected' => $selected // 選択中の項目かどうか
261 $this->tmpl->addVars('title_list', $row);
262 $this->tmpl->parseTemplate('title_list', 'a');
268 * @return string デフォルト名
270 function createDefaultName()
272 $name = self::DEFAULT_NAME_HEAD;
273 for ($j = 1; $j < 100; $j++){
274 $name = self::DEFAULT_NAME_HEAD . $j;
276 for ($i = 0; $i < count($this->paramObj); $i++){
277 $targetObj = $this->paramObj[$i]->object;
278 if ($name == $targetObj->name){ // 定義名
283 if ($i == count($this->paramObj)) break;
288 * 取得したデータをテンプレートに設定する
290 * @param int $index 行番号(0~)
291 * @param array $fetchedRow フェッチ取得した行
292 * @param object $param 未使用
293 * @return bool true=処理続行の場合、false=処理終了の場合
295 function menuIdListLoop($index, $fetchedRow, $param)
297 $value = $fetchedRow['mn_id'];
298 $name = $fetchedRow['mn_name'] . '(' . $value . ')';
301 if ($value == $this->menuId) $selected = 'selected';
304 'value' => $value, // ページID
305 'name' => $name, // ページ名
306 'selected' => $selected // 選択中かどうか
308 $this->tmpl->addVars('menu_id_list', $row);
309 $this->tmpl->parseTemplate('menu_id_list', 'a');
315 * @param RequestManager $request HTTPリクエスト処理クラス
318 function createList($request)
320 // ページ定義IDとページ定義のレコードシリアル番号を取得
321 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
323 $userId = $this->gEnv->getCurrentUserId();
324 $langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
325 $act = $request->trimValueOf('act');
328 $menuId = $request->trimValueOf('menuid');
330 if ($act == 'delete'){ // メニュー項目の削除
331 $listedItem = explode(',', $request->trimValueOf('seriallist'));
333 for ($i = 0; $i < count($listedItem); $i++){
335 $itemName = 'item' . $i . '_selected';
336 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
338 if ($itemValue){ // チェック項目
339 $delItems[] = $listedItem[$i];
342 if (count($delItems) > 0){
343 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
344 if ($ret){ // データ削除成功のとき
345 $this->setGuidanceMsg('データを削除しました');
347 $this->setAppErrorMsg('データ削除に失敗しました');
352 $this->createItemList();
353 if (count($this->serialArray) <= 0) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');// 一覧非表示
361 $tabItem = new stdClass;
362 $tabItem->name = 'ウィジェット設定';
364 $tabItem->url = '#widget_config';
365 $tabItem->parent = 0;
366 $tabItem->active = false;
367 $tabDef[] = $tabItem; $tabItemIndex++;
368 $tabItem = new stdClass;
369 $tabItem->name = 'メニュー定義';
371 $tabItem->url = '#menu_define';
372 $tabItem->parent = 0;
373 $tabItem->active = false;
374 $tabDef[] = $tabItem; $tabItemIndex++;
375 $tabHtml = $this->gDesign->createConfigNavTab($tabDef);
376 $this->tmpl->addVar("_widget", "nav_tab", $tabHtml);
379 $taskValue = 'smenudef';
380 $menuDefUrl = $this->gEnv->getDefaultAdminUrl() . '?' . 'task=' . $taskValue . '&openby=tabs&menuid=' . $menuId;
381 $this->tmpl->addVar("_widget", "url", $this->getUrl($menuDefUrl));
382 $this->tmpl->addVar("_widget", "menu_id", $menuId);
384 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
386 // ページ定義IDとページ定義のレコードシリアル番号を更新
387 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
394 function createItemList()
396 for ($i = 0; $i < count($this->paramObj); $i++){
397 $id = $this->paramObj[$i]->id;// 定義ID
398 $targetObj = $this->paramObj[$i]->object;
399 $name = $targetObj->name;// 定義名
403 if ($this->db->getMenu($targetObj->menuId, $row)){
404 $menuName = $row['mn_name'] . '(' . $row['mn_id'] . ')';
409 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
411 $operationDisagled = '';
412 if ($defCount > 0) $operationDisagled = 'disabled';
416 'ope_disabled' => $operationDisagled, // 選択可能かどうか
417 'name' => $this->convertToDispString($name), // 名前
418 'menu_name' => $this->convertToDispString($menuName), // メニュー定義名
419 'def_count' => $defCount // 使用数
421 $this->tmpl->addVars('itemlist', $row);
422 $this->tmpl->parseTemplate('itemlist', 'a');
425 $this->serialArray[] = $id;