OSDN Git Service

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