OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / event_main / include / container / admin_event_mainCategoryWidgetContainer.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_event_mainCategoryWidgetContainer.php 3976 2011-02-03 13:58:42Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_event_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/event_categoryDb.php');
18
19 class admin_event_mainCategoryWidgetContainer extends admin_event_mainBaseWidgetContainer
20 {
21         private $langId;                // 言語ID
22         private $serialNo;              // 選択中の項目のシリアル番号
23         private $serialArray = array();         // 表示されている項目シリアル番号
24         private $categoryItemArray = array();                   // カテゴリ項目情報
25         
26         /**
27          * コンストラクタ
28          */
29         function __construct()
30         {
31                 // 親クラスを呼び出す
32                 parent::__construct();
33                 
34                 // DBオブジェクト作成
35                 $this->db = new event_categoryDb();
36         }
37         /**
38          * テンプレートファイルを設定
39          *
40          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
41          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
42          *
43          * @param RequestManager $request               HTTPリクエスト処理クラス
44          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
45          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
46          */
47         function _setTemplate($request, &$param)
48         {
49                 $task = $request->trimValueOf('task');
50                 if ($task == 'category_detail'){                // 詳細画面
51                         return 'admin_category_detail.tmpl.html';
52                 } else {                        // 一覧画面
53                         return 'admin_category.tmpl.html';
54                 }
55         }
56         /**
57          * テンプレートにデータ埋め込む
58          *
59          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
60          *
61          * @param RequestManager $request               HTTPリクエスト処理クラス
62          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
63          * @param                                                               なし
64          */
65         function _assign($request, &$param)
66         {
67                 $task = $request->trimValueOf('task');
68                 if ($task == 'category_detail'){        // 詳細画面
69                         return $this->createDetail($request);
70                 } else {                        // 一覧画面
71                         return $this->createList($request);
72                 }
73         }
74         /**
75          * 一覧画面作成
76          *
77          * @param RequestManager $request               HTTPリクエスト処理クラス
78          * @param                                                               なし
79          */
80         function createList($request)
81         {
82                 $this->langId = $this->gEnv->getDefaultLanguage();              // デフォルト言語
83                 $act = $request->trimValueOf('act');
84                 
85                 if ($act == 'delete'){          // 項目削除の場合
86                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
87                         $delItems = array();
88                         for ($i = 0; $i < count($listedItem); $i++){
89                                 // 項目がチェックされているかを取得
90                                 $itemName = 'item' . $i . '_selected';
91                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
92                                 
93                                 if ($itemValue){                // チェック項目
94                                         $delItems[] = $listedItem[$i];
95                                 }
96                         }
97                         if (count($delItems) > 0){
98                                 $ret = $this->db->delCategory($delItems);
99                                 if ($ret){              // データ削除成功のとき
100                                         $this->setGuidanceMsg('データを削除しました');
101                                 } else {
102                                         $this->setAppErrorMsg('データ削除に失敗しました');
103                                 }
104                         }
105                 }
106                 
107                 // 一覧作成
108                 $this->db->getAllCategory($this->langId, array($this, 'itemLoop'));
109                 
110                 if (count($this->serialArray) > 0){
111                         $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
112                 } else {
113                         // 項目がないときは、一覧を表示しない
114                         $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
115                 }
116         }
117         /**
118          * 詳細画面作成
119          *
120          * @param RequestManager $request               HTTPリクエスト処理クラス
121          * @param                                                               なし
122          */
123         function createDetail($request)
124         {
125                 $this->langId = $this->gEnv->getDefaultLanguage();              // デフォルト言語
126                 
127                 $act = $request->trimValueOf('act');
128                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
129                 $name   = $request->trimValueOf('item_name');   // 名前
130                 $id             = $request->trimValueOf('item_id');     // 識別ID
131                 $index  = $request->trimValueOf('item_index');  // 表示順
132                 $itemCount = intval($request->trimValueOf('itemcount'));                // カテゴリ項目数
133                 $lineIds = $request->trimValueOf('item_lineid');                // 行カテゴリID
134                 $lineNames = $request->trimValueOf('item_linename');            // 行カテゴリ名前
135                 
136                 // 行データを取得
137                 for ($i = 0; $i < $itemCount; $i++){
138                         $line = array();
139                         $line['ec_item_id'] = $lineIds[$i];
140                         $line['ec_name'] = $lineNames[$i];
141                         $line['ec_index'] = $i + 1;
142                         $this->categoryItemArray[] = $line;
143                 }
144
145                 $replaceNew = false;            // データを再取得するかどうか
146                 if ($act == 'add'){             // 新規追加のとき
147                         // 入力チェック
148                         $this->checkInput($name, '表示名');
149                         $this->checkSingleByte($id, 'カテゴリ識別ID');
150                         $this->checkNumeric($index, '表示順');               // 表示順
151                         for ($i = 0; $i < $itemCount; $i++){
152                                 $no = $i + 1;
153                                 $this->checkInput($this->categoryItemArray[$i]['ec_name'], 'カテゴリ項目名(No.' . $no . ')');
154                                 $this->checkSingleByte($this->categoryItemArray[$i]['ec_item_id'], 'カテゴリ項目ID(No.' . $no . ')');
155                         }
156                         
157                         // 同じIDがある場合はエラー
158                         if ($this->db->getCategoryById($id, $this->langId, $row)) $this->setMsg(self::MSG_USER_ERR, 'カテゴリ識別IDが重複しています');
159                         
160                         // エラーなしの場合は、データを更新
161                         if ($this->getMsgCount() == 0){
162                                 $ret = $this->db->updateCategory(0/*新規*/, $id, $this->langId, $name, $index, $this->categoryItemArray, $newSerial);
163                                 if ($ret){              // データ追加成功のとき
164                                         $this->setMsg(self::MSG_GUIDANCE, 'データを追加しました');
165                                         
166                                         $this->serialNo = $newSerial;           // シリアル番号を更新
167                                         $replaceNew = true;                     // データを再取得
168                                 } else {
169                                         $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
170                                 }
171                         }
172                 } else if ($act == 'update'){           // 行更新のとき
173                         // 入力チェック
174                         $this->checkInput($name, '表示名');
175                         $this->checkSingleByte($id, 'カテゴリ識別ID');
176                         $this->checkNumeric($index, '表示順');               // 表示順
177                         for ($i = 0; $i < $itemCount; $i++){
178                                 $no = $i + 1;
179                                 $this->checkInput($this->categoryItemArray[$i]['ec_name'], 'カテゴリ項目名(No.' . $no . ')');
180                                 $this->checkSingleByte($this->categoryItemArray[$i]['ec_item_id'], 'カテゴリ項目ID(No.' . $no . ')');
181                         }
182                         
183                         // エラーなしの場合は、データを更新
184                         if ($this->getMsgCount() == 0){
185                                 $ret = $this->db->updateCategory($this->serialNo, $id, $this->langId, $name, $index, $this->categoryItemArray, $newSerial);
186                                 if ($ret){              // データ追加成功のとき
187                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
188                                 
189                                         $this->serialNo = $newSerial;           // シリアル番号を更新
190                                         $replaceNew = true;                     // データを再取得
191                                 } else {
192                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
193                                 }
194                         }
195                 } else {                // 初期状態
196                         // シリアル番号からデータを取得
197                         $ret = $this->db->getCategoryBySerial($this->serialNo, $row);
198                         if ($ret) $id = $row['ec_id'];                  // カテゴリ識別ID
199                         
200                         if (empty($id)){                // カテゴリIDが空のときは新規とする
201                                 $this->serialNo = 0;
202                                 $id             = '';           // 識別ID
203                                 $name   = '';   // 名前
204                                 $index = 0;     // 表示順
205                         } else {
206                                 $replaceNew = true;                     // データを再取得
207                         }
208                 }
209                 // 表示データ再取得
210                 if ($replaceNew){
211                         // タブ識別IDからデータを取得
212                         $ret = $this->db->getCategoryById($id, $this->langId, $row);
213                         if ($ret){
214                                 $this->serialNo = $row['ec_serial'];
215                                 $name           = $row['ec_name'];
216                                 $index  = $row['ec_index'];             // 表示順
217                                 
218                                 // カテゴリ項目取得
219                                 $ret = $this->db->getAllCategoryItemsById($id, $this->langId, $this->categoryItemArray);
220                         }
221                 }
222                 
223                 // カテゴリ項目一覧作成
224                 $this->createCategoryItemList();
225                 if (empty($this->categoryItemArray)) $this->tmpl->setAttribute('item_list', 'visibility', 'hidden');// カテゴリ項目情報一覧
226                 
227                 if (empty($this->serialNo)){            // シリアル番号が空のときは新規とする
228                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
229                         $this->tmpl->setAttribute('new_id_field', 'visibility', 'visible');// 新規ID入力フィールド表示
230                         
231                         $this->tmpl->addVar("new_id_field", "id", $id);         // 識別キー
232                 } else {
233                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 更新ボタン表示
234                         $this->tmpl->setAttribute('id_field', 'visibility', 'visible');// 固定IDフィールド表示
235                         
236                         $this->tmpl->addVar("id_field", "id", $id);             // 識別キー
237                 }
238                 
239                 // 画面にデータを埋め込む
240                 $this->tmpl->addVar("_widget", "name", $name);          // 名前
241                 $this->tmpl->addVar("_widget", "index", $index);                // 表示順
242                 
243                 // 選択中のシリアル番号を設定
244                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
245         }
246         /**
247          * 取得したタブ定義をテンプレートに設定する
248          *
249          * @param int $index                    行番号(0~)
250          * @param array $fetchedRow             フェッチ取得した行
251          * @param object $param                 未使用
252          * @return bool                                 true=処理続行の場合、false=処理終了の場合
253          */
254         function itemLoop($index, $fetchedRow, $param)
255         {
256                 $row = array(
257                         'index' => $index,
258                         'serial' => $fetchedRow['ec_serial'],
259                         'id' => $this->convertToDispString($fetchedRow['ec_id']),               // 識別ID
260                         'name'     => $this->convertToDispString($fetchedRow['ec_name']),                       // 表示名
261                         'item_index' => $fetchedRow['ec_index']                         // 表示順
262                 );
263                 $this->tmpl->addVars('itemlist', $row);
264                 $this->tmpl->parseTemplate('itemlist', 'a');
265                 
266                 // 表示中項目のシリアル番号を保存
267                 $this->serialArray[] = $fetchedRow['ec_serial'];
268                 return true;
269         }
270         /**
271          * カテゴリ項目一覧を作成
272          *
273          * @return なし                                               
274          */
275         function createCategoryItemList()
276         {
277                 $itemCount = count($this->categoryItemArray);
278                 for ($i = 0; $i < $itemCount; $i++){
279                         $id = $this->categoryItemArray[$i]['ec_item_id'];// カテゴリ項目ID
280                         $name = $this->categoryItemArray[$i]['ec_name'];                // カテゴリ項目名
281                         
282                         $row = array(
283                                 'id' => $this->convertToDispString($id),        // カテゴリ項目ID
284                                 'name' => $this->convertToDispString($name),    // カテゴリ項目名
285                                 'root_url' => $this->convertToDispString($this->getUrl($this->gEnv->getRootUrl()))
286                         );
287                         $this->tmpl->addVars('item_list', $row);
288                         $this->tmpl->parseTemplate('item_list', 'a');
289                 }
290         }
291 }
292 ?>