OSDN Git Service

0d9ab1c39c2ba22e777a9c45167b486351ddd95f
[magic3/magic3.git] / widgets / m / menu / include / container / admin_m_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-2014 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() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/menuDb.php');
18
19 class admin_m_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         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
29         const DEFAULT_MENU_ID = 'm_main_menu';                  // デフォルトメニューID
30         
31         /**
32          * コンストラクタ
33          */
34         function __construct()
35         {
36                 // 親クラスを呼び出す
37                 parent::__construct();
38                 
39                 // DBオブジェクト作成
40                 $this->db = new menuDb();
41         }
42         /**
43          * テンプレートファイルを設定
44          *
45          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
46          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
47          *
48          * @param RequestManager $request               HTTPリクエスト処理クラス
49          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
50          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
51          */
52         function _setTemplate($request, &$param)
53         {
54                 $task = $request->trimValueOf('task');
55                 if ($task == 'list'){           // 一覧画面
56                         return 'admin_list.tmpl.html';
57                 } else {                        // 一覧画面
58                         return 'admin.tmpl.html';
59                 }
60         }
61         /**
62          * テンプレートにデータ埋め込む
63          *
64          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
65          *
66          * @param RequestManager $request               HTTPリクエスト処理クラス
67          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
68          * @param                                                               なし
69          */
70         function _assign($request, &$param)
71         {
72                 $task = $request->trimValueOf('task');
73                 if ($task == 'list'){           // 一覧画面
74                         $this->createList($request);
75                 } else {                        // 詳細設定画面
76                         $this->createDetail($request);
77                 }
78         }
79         /**
80          * 詳細画面作成
81          *
82          * @param RequestManager $request               HTTPリクエスト処理クラス
83          * @param                                                               なし
84          */
85         function createDetail($request)
86         {
87                 // ページ定義IDとページ定義のレコードシリアル番号を取得
88                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
89                 
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');              // 選択項目のシリアル番号
95
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;         // ユーザを制限するかどうか
102                 
103                 $replaceNew = false;            // データを再取得するかどうか
104                 if (empty($act)){// 初期起動時
105                         // デフォルト値設定
106                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
107                         $replaceNew = true;                     // データ再取得
108                 } else if ($act == 'add'){// 新規追加
109                         // 入力チェック
110                         $this->checkInput($name, '名前');
111                         
112                         // エラーなしの場合は、データを登録
113                         if ($this->getMsgCount() == 0){
114                                 // 追加オブジェクト作成
115                                 $newObj = new stdClass;
116                                 $newObj->menuId = $this->menuId;                // メニューID
117                                 $newObj->name   = $name;// 表示名
118                                 $newObj->limitUser = $limitUser;                                        // ユーザを制限するかどうか
119                                 
120                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj, $this->menuId);
121                                 if ($ret){
122                                         $this->setGuidanceMsg('データを追加しました');
123                                         
124                                         $this->configId = $defConfigId;         // 定義定義IDを更新
125                                         $replaceNew = true;                     // データ再取得
126                                 } else {
127                                         $this->setAppErrorMsg('データ追加に失敗しました');
128                                 }
129                         }
130                 } else if ($act == 'update'){           // 設定更新のとき
131                         // 入力値のエラーチェック
132                         
133                         if ($this->getMsgCount() == 0){                 // エラーのないとき
134                                 // 現在の設定値を取得
135                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
136                                 if ($ret){
137                                         // ウィジェットオブジェクト更新。更新値のみ再設定。
138                                         if (!empty($defConfigId) && !empty($defSerial)){                // 設定再選択不可の場合
139                                                 // 取得値で更新
140                                                 $this->menuId = $targetObj->menuId;             // メニューID
141                                         } else {                        // 新規で既存設定の更新
142                                                 $targetObj->menuId      = $this->menuId;                // メニューID
143                                         }
144                                         $targetObj->limitUser = $limitUser;                                     // ユーザを制限するかどうか
145                                 }
146
147                                 // 設定値を更新
148                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj, $this->menuId);
149                                 if ($ret){
150                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
151                                         
152                                         $replaceNew = true;                     // データ再取得
153                                 } else {
154                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
155                                 }
156                         }
157                 } else if ($act == 'select'){   // 定義IDを変更
158                         $replaceNew = true;                     // データ再取得
159                 }
160                 
161                 // 表示用データを取得
162                 if (empty($this->configId)){            // 新規登録の場合
163                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
164                         
165                         if ($replaceNew){               // データ再取得時
166                                 $this->menuId = self::DEFAULT_MENU_ID;
167                                 $name = $this->createDefaultName();                     // デフォルト登録項目名
168                                 $limitUser = 0;                                 // ユーザを制限するかどうか
169                         }
170                         $this->serialNo = 0;
171                 } else {
172                         if ($replaceNew){
173                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
174                                 if ($ret){
175                                         $this->menuId   = $targetObj->menuId;           // メニューID
176                                         $name                   = $targetObj->name;// 名前
177                                         $limitUser              = $targetObj->limitUser;                                        // ユーザを制限するかどうか
178                                 }
179                         }
180                         $this->serialNo = $this->configId;
181                                 
182                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
183                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
184                 }
185                 // 設定項目選択メニュー作成
186                 $this->createItemMenu();
187                 
188                 // メニューID選択メニュー作成
189                 $this->db->getMenuIdList(1/*携帯用*/, array($this, 'menuIdListLoop'));
190                 
191                 // 一度設定を保存している場合は、メニュー定義を前面にする(初期起動時のみ)
192                 $activeIndex = 0;
193                 if (empty($act) && !empty($this->configId)) $activeIndex = 1;
194                 // 一覧画面からの戻り画面が指定されてる場合は優先する
195                 if ($anchor == 'widget_config') $activeIndex = 0;
196                 
197                 // ナビゲーションタブ作成
198                 $tabItemIndex = 0;
199                 $tabDef = array();
200                 $tabItem = new stdClass;
201                 $tabItem->name  = 'ウィジェット設定';
202                 $tabItem->task  = '';
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  = 'メニュー定義';
210                 $tabItem->task  = '';
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');
220                 } else {
221                         $this->tmpl->addVar("_widget", "active_tab", 'menu_define');
222                 }
223                 
224                 // 画面にデータを埋め込む
225                 $this->tmpl->addVar("item_name_visible", "name", $name);                // 名前
226                 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId);             // 定義ID
227                 
228                 $this->tmpl->addVar("_widget", "limit_user", $this->convertToCheckedString($limitUser));        // ユーザを制限するかどうか
229                 
230                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
231                 
232                 // ボタンの表示制御
233                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
234                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
235                 } else {
236                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
237                 }
238                 
239                 // ページ定義IDとページ定義のレコードシリアル番号を更新
240                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
241         }
242         /**
243          * 選択用メニューを作成
244          *
245          * @return なし                                               
246          */
247         function createItemMenu()
248         {
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;// 定義名
253                         $selected = '';
254                         if ($this->configId == $id) $selected = 'selected';
255
256                         $row = array(
257                                 'name' => $name,                // 名前
258                                 'value' => $id,         // 定義ID
259                                 'selected' => $selected // 選択中の項目かどうか
260                         );
261                         $this->tmpl->addVars('title_list', $row);
262                         $this->tmpl->parseTemplate('title_list', 'a');
263                 }
264         }
265         /**
266          * デフォルトの名前を取得
267          *
268          * @return string       デフォルト名                                              
269          */
270         function createDefaultName()
271         {
272                 $name = self::DEFAULT_NAME_HEAD;
273                 for ($j = 1; $j < 100; $j++){
274                         $name = self::DEFAULT_NAME_HEAD . $j;
275                         // 設定名の重複チェック
276                         for ($i = 0; $i < count($this->paramObj); $i++){
277                                 $targetObj = $this->paramObj[$i]->object;
278                                 if ($name == $targetObj->name){         // 定義名
279                                         break;
280                                 }
281                         }
282                         // 重複なしのときは終了
283                         if ($i == count($this->paramObj)) break;
284                 }
285                 return $name;
286         }
287         /**
288          * 取得したデータをテンプレートに設定する
289          *
290          * @param int $index                    行番号(0~)
291          * @param array $fetchedRow             フェッチ取得した行
292          * @param object $param                 未使用
293          * @return bool                                 true=処理続行の場合、false=処理終了の場合
294          */
295         function menuIdListLoop($index, $fetchedRow, $param)
296         {
297                 $value = $fetchedRow['mn_id'];
298                 $name = $fetchedRow['mn_name'] . '(' . $value . ')';
299                         
300                 $selected = '';
301                 if ($value == $this->menuId) $selected = 'selected';
302                 
303                 $row = array(
304                         'value'    => $value,                   // ページID
305                         'name'     => $name,                    // ページ名
306                         'selected' => $selected                                                                                                         // 選択中かどうか
307                 );
308                 $this->tmpl->addVars('menu_id_list', $row);
309                 $this->tmpl->parseTemplate('menu_id_list', 'a');
310                 return true;
311         }
312         /**
313          * 一覧画面作成
314          *
315          * @param RequestManager $request               HTTPリクエスト処理クラス
316          * @param                                                               なし
317          */
318         function createList($request)
319         {
320                 // ページ定義IDとページ定義のレコードシリアル番号を取得
321                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
322                 
323                 $userId         = $this->gEnv->getCurrentUserId();
324                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
325                 $act = $request->trimValueOf('act');
326
327                 // 詳細画面からの引継ぎデータ
328                 $menuId = $request->trimValueOf('menuid');
329                 
330                 if ($act == 'delete'){          // メニュー項目の削除
331                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
332                         $delItems = array();
333                         for ($i = 0; $i < count($listedItem); $i++){
334                                 // 項目がチェックされているかを取得
335                                 $itemName = 'item' . $i . '_selected';
336                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
337                                 
338                                 if ($itemValue){                // チェック項目
339                                         $delItems[] = $listedItem[$i];
340                                 }
341                         }
342                         if (count($delItems) > 0){
343                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
344                                 if ($ret){              // データ削除成功のとき
345                                         $this->setGuidanceMsg('データを削除しました');
346                                 } else {
347                                         $this->setAppErrorMsg('データ削除に失敗しました');
348                                 }
349                         }
350                 }
351                 // 定義一覧作成
352                 $this->createItemList();
353                 if (count($this->serialArray) <= 0) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');// 一覧非表示
354                 
355                 // 選択状態はメニュー設定に固定
356                 $activeIndex = 0;
357                 
358                 // ナビゲーションタブ作成
359                 $tabItemIndex = 0;
360                 $tabDef = array();
361                 $tabItem = new stdClass;
362                 $tabItem->name  = 'ウィジェット設定';
363                 $tabItem->task  = '';
364                 $tabItem->url   = '#widget_config';
365                 $tabItem->parent        = 0;
366                 $tabItem->active        = false;
367                 $tabDef[] = $tabItem; $tabItemIndex++;
368                 $tabItem = new stdClass;
369                 $tabItem->name  = 'メニュー定義';
370                 $tabItem->task  = '';
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);
377                 
378                 // メニュー定義画面のURLを作成
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);
383                                         
384                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
385                 
386                 // ページ定義IDとページ定義のレコードシリアル番号を更新
387                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
388         }
389         /**
390          * 定義一覧作成
391          *
392          * @return なし                                               
393          */
394         function createItemList()
395         {
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;// 定義名
400                         
401                         // メニュー定義名を取得
402                         $menuName = '';
403                         if ($this->db->getMenu($targetObj->menuId, $row)){
404                                 $menuName = $row['mn_name'] . '(' . $row['mn_id'] . ')';
405                         }
406                         
407                         $defCount = 0;
408                         if (!empty($id)){
409                                 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
410                         }
411                         $operationDisagled = '';
412                         if ($defCount > 0) $operationDisagled = 'disabled';
413                         $row = array(
414                                 'index' => $i,
415                                 'id' => $id,
416                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
417                                 'name' => $this->convertToDispString($name),            // 名前
418                                 'menu_name' => $this->convertToDispString($menuName),           // メニュー定義名
419                                 'def_count' => $defCount                                                        // 使用数
420                         );
421                         $this->tmpl->addVars('itemlist', $row);
422                         $this->tmpl->parseTemplate('itemlist', 'a');
423                         
424                         // シリアル番号を保存
425                         $this->serialArray[] = $id;
426                 }
427         }
428 }
429 ?>