OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / reserve_main / include / container / admin_reserve_mainResourceWidgetContainer.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-2008 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_reserve_mainResourceWidgetContainer.php 642 2008-05-22 12:25:05Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_reserve_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/reserve_mainDb.php');
18
19 class admin_reserve_mainResourceWidgetContainer extends admin_reserve_mainBaseWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $mainDb;        // DB接続オブジェクト
23         private $sysDb;         // システムDBオブジェクト
24         private $serialNo;                      // シリアル番号
25         private $firstNo;                       // 項目番号
26         private $configType;            // 設定タイプ
27         private $serialArray = array();         // 表示されている項目シリアル番号
28         const DEFAULT_RES_TYPE = 0;     // デフォルトの設定タイプ(常設)
29         const DEFAULT_CONFIG_ID = 0;    // デフォルトの設定ID
30         const DEFAULT_LIST_COUNT = 20;                  // 最大リスト表示数
31                 
32         /**
33          * コンストラクタ
34          */
35         function __construct()
36         {
37                 global $gInstanceManager;
38                 
39                 // 親クラスを呼び出す
40                 parent::__construct();
41                 
42                 // DBオブジェクト作成
43                 $this->db = new reserve_mainDb();
44                 $this->sysDb = $gInstanceManager->getSytemDbObject();
45         }
46         /**
47          * テンプレートファイルを設定
48          *
49          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
50          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
51          *
52          * @param RequestManager $request               HTTPリクエスト処理クラス
53          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
54          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
55          */
56         function _setTemplate($request, &$param)
57         {
58                 $task = $request->trimValueOf('task');
59                 if ($task == 'resource_detail'){                // 詳細画面
60                         return 'admin_resource_detail.tmpl.html';
61                 } else {
62                         return 'admin_resource.tmpl.html';
63                 }
64         }
65         /**
66          * テンプレートにデータ埋め込む
67          *
68          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
69          *
70          * @param RequestManager $request               HTTPリクエスト処理クラス
71          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
72          * @param                                                               なし
73          */
74         function _assign($request, &$param)
75         {
76                 $task = $request->trimValueOf('task');
77                 if ($task == 'resource_detail'){        // 詳細画面
78                         return $this->createDetail($request);
79                 } else {                        // 一覧画面
80                         return $this->createList($request);
81                 }
82         }
83         /**
84          * 一覧画面作成
85          *
86          * @param RequestManager $request               HTTPリクエスト処理クラス
87          * @param                                                               なし
88          */
89         function createList($request)
90         {
91                 global $gEnvManager;
92                 
93                 $pageNo = $request->trimValueOf('page');                                // ページ番号
94                 if (empty($pageNo)) $pageNo = 1;
95                 
96                 // デフォルト値を取得
97                 $maxListCount = self::DEFAULT_LIST_COUNT;       // 表示項目数
98
99                 $act = $request->trimValueOf('act');
100                 if ($act == 'delete'){          // 項目削除の場合
101                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
102                         $delItems = array();
103                         for ($i = 0; $i < count($listedItem); $i++){
104                                 // 項目がチェックされているかを取得
105                                 $itemName = 'item' . $i . '_selected';
106                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
107                                 
108                                 if ($itemValue){                // チェック項目
109                                         $delItems[] = $listedItem[$i];
110                                 }
111                         }
112                         if (count($delItems) > 0){
113                                 $ret = $this->db->deleteResourceById($delItems);
114                                 if ($ret){              // データ削除成功のとき
115                                         $this->setGuidanceMsg('データを削除しました');
116                                 } else {
117                                         $this->setAppErrorMsg('データ削除に失敗しました');
118                                 }
119                         }
120                 }
121                 // 総数を取得
122                 $totalCount = $this->db->getAllResourceListCount(self::DEFAULT_RES_TYPE, self::DEFAULT_CONFIG_ID);
123                 
124                 // 表示するページ番号の修正
125                 $pageCount = (int)(($totalCount -1) / $maxListCount) + 1;               // 総ページ数
126                 if ($pageNo < 1) $pageNo = 1;
127                 if ($pageNo > $pageCount) $pageNo = $pageCount;
128                 $this->firstNo = ($pageNo -1) * $maxListCount + 1;              // 先頭番号
129                 
130                 // リソース一覧を表示
131                 $this->db->getAllResourceList(self::DEFAULT_RES_TYPE, self::DEFAULT_CONFIG_ID, $maxListCount, ($pageNo -1) * $maxListCount, array($this, 'resourceListLoop'));
132                 
133                 // ページング用リンク作成
134                 $pageLink = '';
135                 if ($pageCount > 1){    // ページが2ページ以上のときリンクを作成
136                         for ($i = 1; $i <= $pageCount; $i++){
137                                 //$linkUrl = $this->currentPageUrl . '&category=' . $this->categoryId . '&page=' . $i;
138                                 if ($i == $pageNo){
139                                         $link = '&nbsp;' . $i;
140                                 } else {
141                                         //$link = '&nbsp;<a href="' . $linkUrl . '" >' . $i . '</a>';
142                                         $link = '&nbsp;<a href="#" onclick="selpage(\'' . $i . '\');return false;">' . $i . '</a>';
143                                 }
144                                 $pageLink .= $link;
145                         }
146                 }
147                 $this->tmpl->addVar("_widget", "page_link", $pageLink);
148                 $this->tmpl->addVar("_widget", "page", $pageNo);                // 現在のページ番号
149                 
150                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
151         }
152         /**
153          * 詳細画面作成
154          *
155          * @param RequestManager $request               HTTPリクエスト処理クラス
156          * @param                                                               なし
157          */
158         function createDetail($request)
159         {
160                 global $gEnvManager;
161
162                 // ユーザ情報、表示言語
163                 $now = date("Y/m/d H:i:s");     // 現在日時
164                 $userId         = $gEnvManager->getCurrentUserId();
165                 $langId = $gEnvManager->getCurrentLanguage();           // 表示言語を取得
166                                 
167                 $act = $request->trimValueOf('act');
168                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号(シリアル番号はリソースIDを使用する)
169                 
170                 $name                           = $request->trimValueOf('item_name');           // 名前
171                 $desc                           = $request->trimValueOf('item_desc');           // 説明
172                 $visible                        = ($request->trimValueOf('item_visible') == 'on') ? 1 : 0;      // 表示状態
173                 $index                          = $request->trimValueOf('item_index');          // ソート順
174                                 
175                 $replaceNew = false;            // データを再取得するかどうか
176                 if ($act == 'add'){             // 項目追加の場合
177                         // 入力チェック
178                         $this->checkInput($name, '名前');
179                         $this->checkNumeric($index, '表示順');
180
181                         // エラーなしの場合は、データを登録
182                         if ($this->getMsgCount() == 0){
183                                 $ret = $this->db->updateResource(0/*新規追加*/, self::DEFAULT_RES_TYPE, self::DEFAULT_CONFIG_ID, $name, $desc, $visible, $index, $newResourceId);
184                                 if ($ret){
185                                         $this->setGuidanceMsg('データを追加しました');
186                                         
187                                         $this->serialNo = $newResourceId;
188                                 } else {
189                                         $this->setAppErrorMsg('データ追加に失敗しました');
190                                 }
191                         }
192                 } else if ($act == 'update'){           // 項目更新の場合
193                         if (empty($this->serialNo)){
194                                 $this->setUserErrorMsg('更新するリソースが選択されていません');
195                         }
196                         // 入力チェック
197                         $this->checkInput($name, '名前');
198                         $this->checkNumeric($index, '表示順');
199                         
200                         // エラーなしの場合は、データを登録
201                         if ($this->getMsgCount() == 0){
202                                 $ret = $this->db->updateResource($this->serialNo, self::DEFAULT_RES_TYPE, self::DEFAULT_CONFIG_ID, $name, $desc, $visible, $index, $newResourceId);
203                                 if ($ret){
204                                         $this->setGuidanceMsg('データを更新しました');
205                                         $replaceNew = true;                     // 会員情報を再取得
206                                 } else {
207                                         $this->setAppErrorMsg('データ更新に失敗しました');
208                                 }
209                         }
210                 } else if ($act == 'delete'){           // 項目削除の場合
211                         if (empty($this->serialNo)){
212                                 $this->setUserErrorMsg('削除するリソースが選択されていません');
213                         }
214                         // エラーなしの場合は、データを削除
215                         if ($this->getMsgCount() == 0){
216                                 $ret = $this->db->deleteResourceById(array($this->serialNo));
217                                 if ($ret){              // データ削除成功のとき
218                                         $this->setGuidanceMsg('データを削除しました');
219                                 } else {
220                                         $this->setAppErrorMsg('データ削除に失敗しました');
221                                 }
222                         }
223                 } else if ($act == 'search'){           // 検索再実行
224                 } else {        // 初期表示
225                         if (empty($this->serialNo)){
226                                 // デフォルトの表示順を設定
227                                 $index = $this->db->getMaxResourceIndex(self::DEFAULT_RES_TYPE, self::DEFAULT_CONFIG_ID) + 1;
228                                 $visible = 1;   // 表示状態
229                         } else {
230                                 $replaceNew = true;                     // 会員情報を再取得
231                         }
232                 }
233                 if ($replaceNew){
234                         // リソース情報を取得
235                         $ret = $this->db->getResourceById($this->serialNo, $row);
236                         if ($ret){
237                                 // 取得値を設定
238                                 $name = $this->convertToDispString($row['rr_name']);                    // 名前
239                                 $desc = $this->convertToDispString($row['rr_description']);                     // 説明
240                                 $index = $this->convertToDispString($row['rr_sort_order']);                     // 表示順
241                                 $visible = $row['rr_visible'];  // 表示状態
242                         }
243                 }
244                 
245                 // #### 更新、新規登録部をを作成 ####
246                 $this->tmpl->addVar("_widget", "name", $name);          // 名前
247                 $this->tmpl->addVar("_widget", "desc", $desc);          // 説明
248                 $this->tmpl->addVar("_widget", "index", $index);                // 表示順
249                 $visibleStr = '';
250                 if ($visible){  // 項目の表示
251                         $visibleStr = 'checked';
252                 }
253                 $this->tmpl->addVar("_widget", "visible", $visibleStr);         // 表示状態
254                 
255                 // ボタンの設定
256                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
257                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
258                 } else {
259                         // データ更新、削除ボタン表示
260                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 更新、削除ボタン
261                 }
262                 // 値を埋め込む
263                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
264         }
265         /**
266          * 取得したデータをテンプレートに設定する
267          *
268          * @param int $index                    行番号(0~)
269          * @param array $fetchedRow             フェッチ取得した行
270          * @param object $param                 未使用
271          * @return bool                                 true=処理続行の場合、false=処理終了の場合
272          */
273         function resourceListLoop($index, $fetchedRow, $param)
274         {
275                 global $gEnvManager;
276                 
277                 // 行カラーの設定
278                 $lineColor = '';
279                 if ($index % 2 != 0){
280                         $lineColor = 'class="even"';            // 偶数行
281                 }
282                 $serial = $fetchedRow['rr_id'];// リソースIDをシリアル番号とする
283                 $visible = '';
284                 if ($fetchedRow['rr_visible']){ // 項目の表示
285                         $visible = 'checked';
286                 }
287                 $row = array(
288                         'line_color' => $lineColor,                                             // 行のカラー
289                         'no' => $this->firstNo + $index,                                // 行番号
290                         'index' => $index,                                                              // 項目番号
291                         'serial' => $serial,    // シリアル番号
292                         'id' => $id,                    // ID
293                         'view_index' => $this->convertToDispString($fetchedRow['rr_sort_order']),                       // 表示順
294                         'name' => $this->convertToDispString($fetchedRow['rr_name']),   // 名前
295                         'visible' => $visible                                                                                           // 公開
296                 );
297                 $this->tmpl->addVars('itemlist', $row);
298                 $this->tmpl->parseTemplate('itemlist', 'a');
299                 
300                 // 表示中項目のシリアル番号を保存
301                 $this->serialArray[] = $serial;
302                 return true;
303         }
304 }
305 ?>