OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / s / jquery_init / include / container / admin_s_jquery_initWidgetContainer.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_s_jquery_initWidgetContainer.php 4647 2012-02-01 14:09:31Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17
18 class admin_s_jquery_initWidgetContainer extends BaseAdminWidgetContainer
19 {
20         private $serialNo;              // 選択中の項目のシリアル番号
21         private $serialArray = array();                 // 表示中のシリアル番号
22         private $configId;              // 定義ID
23         private $paramObj;              // パラメータ保存用オブジェクト
24         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
25         const DEFAULT_SCRIPT = '// init script here.';          // デフォルトのスクリプト
26         
27         /**
28          * コンストラクタ
29          */
30         function __construct()
31         {
32                 // 親クラスを呼び出す
33                 parent::__construct();
34         }
35         /**
36          * テンプレートファイルを設定
37          *
38          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
39          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
40          *
41          * @param RequestManager $request               HTTPリクエスト処理クラス
42          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
43          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
44          */
45         function _setTemplate($request, &$param)
46         {
47                 $task = $request->trimValueOf('task');
48                 if ($task == 'list'){           // 一覧画面
49                         return 'admin_list.tmpl.html';
50                 } else {                        // 一覧画面
51                         return 'admin.tmpl.html';
52                 }
53         }
54         /**
55          * テンプレートにデータ埋め込む
56          *
57          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
58          *
59          * @param RequestManager $request               HTTPリクエスト処理クラス
60          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
61          * @param                                                               なし
62          */
63         function _assign($request, &$param)
64         {
65                 $task = $request->trimValueOf('task');
66                 if ($task == 'list'){           // 一覧画面
67                         return $this->createList($request);
68                 } else {                        // 詳細設定画面
69                         return $this->createDetail($request);
70                 }
71         }
72         /**
73          * 詳細画面作成
74          *
75          * @param RequestManager $request               HTTPリクエスト処理クラス
76          * @param                                                               なし
77          */
78         function createDetail($request)
79         {
80                 // ページ定義IDとページ定義のレコードシリアル番号を取得
81                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
82                 
83                 $userId         = $this->gEnv->getCurrentUserId();
84                 $act = $request->trimValueOf('act');
85                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
86                 $this->configId = $request->trimValueOf('item_id');             // 定義ID
87                 if (empty($this->configId)) $this->configId = $defConfigId;             // 呼び出しウィンドウから引き継いだ定義ID
88                 
89                 // 入力値を取得
90                 $name   = $request->trimValueOf('item_name');                   // ヘッダタイトル
91                 $script = $request->valueOf('item_script');             // スクリプト
92                 
93                 $replaceNew = false;            // データを再取得するかどうか
94                 if ($act == 'add'){// 新規追加
95                         // 入力チェック
96                         $this->checkInput($name, '名前');
97                         
98                         // 設定名の重複チェック
99                         for ($i = 0; $i < count($this->paramObj); $i++){
100                                 $targetObj = $this->paramObj[$i]->object;
101                                 if ($defName == $targetObj->name){              // 定義名
102                                         $this->setUserErrorMsg('名前が重複しています');
103                                         break;
104                                 }
105                         }
106                         
107                         // エラーなしの場合は、データを登録
108                         if ($this->getMsgCount() == 0){
109                                 // 追加オブジェクト作成
110                                 $newObj = new stdClass;
111                                 $newObj->id             = $newConfigId;// 定義ID
112                                 $newObj->name   = $name;// 表示名
113                                 $newObj->script = $script;              // スクリプト
114                         
115                                 // ウィジェットパラメータオブジェクト更新
116                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
117                                 if ($ret){
118                                         $this->setGuidanceMsg('データを追加しました');
119                                         
120                                         $this->configId = $defConfigId;         // 定義定義IDを更新
121                                         $replaceNew = true;                     // データ再取得
122                                 } else {
123                                         $this->setAppErrorMsg('データ追加に失敗しました');
124                                 }
125                         }
126                 } else if ($act == 'update'){           // 設定更新のとき
127                         // 入力値のエラーチェック
128                         if ($this->getMsgCount() == 0){                 // エラーのないとき
129                                 // 現在の設定値を取得
130                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
131                                 if ($ret){
132                                         // ウィジェットオブジェクト更新
133                                         $targetObj->script              = $script;                                      // スクリプト
134                                 }
135                                 
136                                 // 設定値を更新
137                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
138                                 if ($ret){
139                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
140                                         $replaceNew = true;                     // データ再取得
141                                 } else {
142                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
143                                 }
144                         }
145                 } else if ($act == 'select'){   // 定義IDを変更
146                         $replaceNew = true;                     // データ再取得
147                 } else {
148                         // デフォルト値設定
149                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
150                         $replaceNew = true;                     // データ再取得
151                 }
152                 
153                 // 設定項目選択メニュー作成
154                 $this->createItemMenu();
155                 
156                 // 表示用データを取得
157                 if (empty($this->configId)){            // 新規登録の場合
158                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
159                         if ($replaceNew){               // データ再取得時
160                                 $name = $this->createDefaultName();                     // デフォルト登録項目名
161                                 $script = self::DEFAULT_SCRIPT;         // スクリプト
162                         }
163                         $this->serialNo = 0;
164                 } else {
165                         if ($replaceNew){// データ再取得時
166                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
167                                 if ($ret){
168                                         $name           = $targetObj->name;     // 名前
169                                         $script = $targetObj->script;           // スクリプト
170                                 }
171                         }
172                         $this->serialNo = $this->configId;
173                                 
174                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
175                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
176                 }
177                 
178                 // 画面にデータを埋め込む
179                 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId);             // 定義ID
180                 $this->tmpl->addVar("item_name_visible", "name",        $name);
181                 $this->tmpl->addVar("_widget", "script",        $script);                                       // スクリプト
182                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
183                 
184                 // ボタンの表示制御
185                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
186                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
187                 } else {
188                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
189                 }
190                 
191                 // ページ定義IDとページ定義のレコードシリアル番号を更新
192                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
193         }
194         /**
195          * 選択用メニューを作成
196          *
197          * @return なし                                               
198          */
199         function createItemMenu()
200         {
201                 for ($i = 0; $i < count($this->paramObj); $i++){
202                         $id = $this->paramObj[$i]->id;// 定義ID
203                         $targetObj = $this->paramObj[$i]->object;
204                         $defName = $targetObj->name;// 定義名
205                         $selected = '';
206                         if ($this->configId == $id) $selected = 'selected';
207
208                         $row = array(
209                                 'name' => $defName,             // 名前
210                                 'value' => $id,         // 定義ID
211                                 'selected' => $selected // 選択中の項目かどうか
212                         );
213                         $this->tmpl->addVars('title_list', $row);
214                         $this->tmpl->parseTemplate('title_list', 'a');
215                 }
216         }
217         /**
218          * デフォルトの名前を取得
219          *
220          * @return string       デフォルト名                                              
221          */
222         function createDefaultName()
223         {
224                 $name = self::DEFAULT_NAME_HEAD;
225                 for ($j = 1; $j < 100; $j++){
226                         $name = self::DEFAULT_NAME_HEAD . $j;
227                         // 設定名の重複チェック
228                         for ($i = 0; $i < count($this->paramObj); $i++){
229                                 $targetObj = $this->paramObj[$i]->object;
230                                 if ($name == $targetObj->name){         // 定義名
231                                         break;
232                                 }
233                         }
234                         // 重複なしのときは終了
235                         if ($i == count($this->paramObj)) break;
236                 }
237                 return $name;
238         }
239         /**
240          * 一覧画面作成
241          *
242          * @param RequestManager $request               HTTPリクエスト処理クラス
243          * @param                                                               なし
244          */
245         function createList($request)
246         {
247                 // ページ定義IDとページ定義のレコードシリアル番号を取得
248                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
249
250                 $userId         = $this->gEnv->getCurrentUserId();
251                 $act = $request->trimValueOf('act');
252                 
253                 if ($act == 'delete'){          // メニュー項目の削除
254                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
255                         $delItems = array();
256                         for ($i = 0; $i < count($listedItem); $i++){
257                                 // 項目がチェックされているかを取得
258                                 $itemName = 'item' . $i . '_selected';
259                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
260                                 
261                                 if ($itemValue){                // チェック項目
262                                         $delItems[] = $listedItem[$i];
263                                 }
264                         }
265                         if (count($delItems) > 0){
266                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
267                                 if ($ret){              // データ削除成功のとき
268                                         $this->setGuidanceMsg('データを削除しました');
269                                 } else {
270                                         $this->setAppErrorMsg('データ削除に失敗しました');
271                                 }
272                         }
273                 }
274                 // 定義一覧作成
275                 $this->createItemList();
276                 if (count($this->serialArray) == 0) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');// 一覧項目がないときは、一覧を表示しない
277                 
278                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
279                 
280                 // ページ定義IDとページ定義のレコードシリアル番号を更新
281                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
282         }
283         /**
284          * 定義一覧作成
285          *
286          * @return なし                                               
287          */
288         function createItemList()
289         {
290                 for ($i = 0; $i < count($this->paramObj); $i++){
291                         $id                     = $this->paramObj[$i]->id;// 定義ID
292                         $targetObj      = $this->paramObj[$i]->object;
293                         $defName = $targetObj->name;// 定義名
294                 
295                         // 使用数
296                         $defCount = 0;
297                         if (!empty($id)){
298                                 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
299                         }
300                         $operationDisagled = '';
301                         if ($defCount > 0) $operationDisagled = 'disabled';
302                         
303                         $row = array(
304                                 'index' => $i,
305                                 'id' => $id,
306                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
307                                 'name' => $this->convertToDispString($defName),         // 名前
308                                 'def_count' => $defCount                                                        // 使用数
309                         );
310                         $this->tmpl->addVars('itemlist', $row);
311                         $this->tmpl->parseTemplate('itemlist', 'a');
312                         
313                         // シリアル番号を保存
314                         $this->serialArray[] = $id;
315                 }
316         }
317 }
318 ?>