OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / ec_main / include / container / admin_ec_mainProductcategoryWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
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_ec_mainProductcategoryWidgetContainer.php 5440 2012-12-08 09:37:39Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_ec_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/ec_mainProductCategoryDb.php');
18
19 class admin_ec_mainProductcategoryWidgetContainer extends admin_ec_mainBaseWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $langId;                // 表示言語
23         private $serialNo;                      // シリアル番号
24         private $serialArray = array();         // 表示されている項目シリアル番号
25         private $parentCategoryId;              // 親カテゴリーID
26         private $id;            // カテゴリーID
27         
28         /**
29          * コンストラクタ
30          */
31         function __construct()
32         {
33                 // 親クラスを呼び出す
34                 parent::__construct();
35                 
36                 // DBオブジェクト作成
37                 $this->db = new ec_mainProductCategoryDb();
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 == 'productcategory_detail'){         // 詳細画面
53                         return 'admin_productcategory_detail.tmpl.html';
54                 } else {                        // 一覧画面
55                         return 'admin_productcategory.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 == 'productcategory_detail'){ // 詳細画面
71                         return $this->createDetail($request);
72                 } else {                        // 一覧画面
73                         return $this->createList($request);
74                 }
75         }
76         /**
77          * 一覧画面作成
78          *
79          * @param RequestManager $request               HTTPリクエスト処理クラス
80          * @param                                                               なし
81          */
82         function createList($request)
83         {
84                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
85                 $act = $request->trimValueOf('act');
86                 
87                 if ($act == 'delete'){          // 項目削除の場合
88                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
89                         $delItems = array();
90                         for ($i = 0; $i < count($listedItem); $i++){
91                                 // 項目がチェックされているかを取得
92                                 $itemName = 'item' . $i . '_selected';
93                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
94                                 
95                                 if ($itemValue){                // チェック項目
96                                         $delItems[] = $listedItem[$i];
97                                 }
98                         }
99                         if (count($delItems) > 0){
100                                 $ret = $this->db->delCategoryBySerial($delItems);
101                                 if ($ret){              // データ削除成功のとき
102                                         $this->setGuidanceMsg('データを削除しました');
103                                 } else {
104                                         $this->setAppErrorMsg('データ削除に失敗しました');
105                                 }
106                         }
107                 }
108                 // #### カテゴリーリストを作成 ####
109                 $this->db->getAllCategory($this->langId, array($this, 'categoryListLoop'));// デフォルト言語で取得
110                 
111                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
112         }
113         /**
114          * 詳細画面作成
115          *
116          * @param RequestManager $request               HTTPリクエスト処理クラス
117          * @param                                                               なし
118          */
119         function createDetail($request)
120         {
121                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
122                 $userId = $this->gEnv->getCurrentUserId();
123                 
124                 $act = $request->trimValueOf('act');
125                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
126
127                 $name   = $request->trimValueOf('item_name');           // カテゴリー名称
128                 $index  = $request->trimValueOf('item_index');          // 表示順
129                 $visible = ($request->trimValueOf('item_visible') == 'on') ? 1 : 0;                     // 表示するかどうか
130                 $this->parentCategoryId = $request->trimValueOf('item_pcategory');              // 親カテゴリーID
131                 
132                 $replaceNew = false;            // データを再取得するかどうか
133                 if ($act == 'add'){             // 項目追加の場合
134                         // 入力チェック
135                         $this->checkInput($name, '名前');
136                         $this->checkNumeric($index, '表示順');
137                         
138                         // エラーなしの場合は、データを登録
139                         if ($this->getMsgCount() == 0){
140                                 $ret = $this->db->addCategory(0, $this->langId, $name, $this->parentCategoryId, $index, $visible, $userId, $newSerial);
141                                 if ($ret){
142                                         $this->setGuidanceMsg('データを追加しました');
143                                         
144                                         // シリアル番号更新
145                                         $this->serialNo = $newSerial;
146                                         $replaceNew = true;                     // データを再取得
147                                 } else {
148                                         $this->setAppErrorMsg('データ追加に失敗しました');
149                                 }
150                         }
151                 } else if ($act == 'update'){           // 項目更新の場合
152                         // 入力チェック
153                         $this->checkInput($name, '名前');
154                         $this->checkNumeric($index, '表示順');               
155                         
156                         // エラーなしの場合は、データを登録
157                         if ($this->getMsgCount() == 0){
158                                 $ret = $this->db->updateCategory($this->serialNo, $name, $this->parentCategoryId, $index, $visible, $userId, $newSerial);
159                                 if ($ret){
160                                         $this->setGuidanceMsg('データを更新しました');
161                                         
162                                         // 登録済みのカテゴリーを取得
163                                         $this->serialNo = $newSerial;
164                                         $replaceNew = true;                     // データを再取得
165                                 } else {
166                                         $this->setAppErrorMsg('データ更新に失敗しました');
167                                 }
168                         }
169                 } else if ($act == 'delete'){           // 項目削除の場合
170                         $ret = $this->db->delCategoryBySerial(array($this->serialNo));
171                         if ($ret){              // データ削除成功のとき
172                                 $this->setGuidanceMsg('データを削除しました');
173                         } else {
174                                 $this->setAppErrorMsg('データ削除に失敗しました');
175                         }
176                 } else {        // 初期表示
177                         // 入力値初期化
178                         if (empty($this->serialNo)){            // シリアル番号
179                                 $name = '';             // 名前
180                                 $index = $this->db->getMaxIndex($this->langId) + 1;     // 表示順
181                                 $visible = 1;   // 表示状態
182                                 $this->parentCategoryId = 0;            // 親カテゴリーID
183                         } else {
184                                 $replaceNew = true;                     // データを再取得
185                         }
186                 }
187                 // データを再取得のとき
188                 if ($replaceNew){
189                         $ret = $this->db->getCategoryBySerial($this->serialNo, $row);
190                         if ($ret){
191                                 // 取得値を設定
192                                 $this->id = $row['pc_id'];              // ID
193                                 $this->langId = $row['pc_language_id'];         // 言語ID
194                                 $name = $row['pc_name'];                // 名前
195                                 $index = $row['pc_sort_order']; // 表示順
196                                 $visible = $row['pc_visible'];  // 表示状態
197                                 $this->parentCategoryId = $row['pc_parent_id'];         // 親カテゴリーID
198                                 $updateUser = $this->convertToDispString($row['lu_name']);      // 更新者
199                                 $updateDt = $this->convertToDispDateTime($row['pc_create_dt']); // 更新日時
200                         }
201                 }
202                 // 親カテゴリーメニュー作成
203                 $this->db->getAllCategory($this->langId, array($this, 'pcategoryListLoop'));
204                 
205                 // #### 更新、新規登録部をを作成 ####
206                 if (empty($this->serialNo)){            // シリアル番号のときは新規とする
207                         $this->tmpl->addVar("_widget", "id", '新規');
208                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
209                 } else {
210                         $this->tmpl->addVar("_widget", "id", $this->id);
211                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');
212                 }
213                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
214                 $this->tmpl->addVar("_widget", "name", $name);          // 名前
215                 $this->tmpl->addVar("_widget", "index", $index);                // 表示順
216                 
217                 $visibleStr = '';
218                 if ($visible){  // 項目の表示
219                         $visibleStr = 'checked';
220                 }
221                 $this->tmpl->addVar("_widget", "visible", $visibleStr);         // 表示状態
222                 if (!empty($updateUser)) $this->tmpl->addVar("_widget", "update_user", $updateUser);    // 更新者
223                 if (!empty($updateDt)) $this->tmpl->addVar("_widget", "update_dt", $updateDt);  // 更新日時
224         }
225         /**
226          * 取得したデータをテンプレートに設定する
227          *
228          * @param int $index                    行番号(0~)
229          * @param array $fetchedRow             フェッチ取得した行
230          * @param object $param                 未使用
231          * @return bool                                 true=処理続行の場合、false=処理終了の場合
232          */
233         function categoryListLoop($index, $fetchedRow, $param)
234         {
235                 $serial = $fetchedRow['pc_serial'];
236                 $id = $this->convertToDispString($fetchedRow['pc_id']);
237                 
238                 // 対応言語を取得
239                 $lang = '';
240                 $ret = $this->db->getLangByCategoryId($fetchedRow['pc_id'], $rows);
241                 if ($ret){
242                         $count = count($rows);
243                         for ($i = 0; $i < $count; $i++){
244                                 if ($this->gEnv->getCurrentLanguage() == 'ja'){ // 日本語の場合
245                                         $lang .= $rows[$i]['ln_name'];
246                                         if ($i != $count -1) $lang .= ',';
247                                 } else {
248                                         $lang .= $rows[$i]['ln_name_en'];
249                                         if ($i != $count -1) $lang .= ',';
250                                 }
251                         }
252                 }
253                 // 親カテゴリー名を取得
254                 $pcatId = $fetchedRow['pc_parent_id'];
255                 $pcategoryName = '';
256                 if ($pcatId != 0){
257                         $ret = $this->db->getCategoryByCategoryId($pcatId, $this->gEnv->getDefaultLanguage(), $row);
258                         if ($ret) $pcategoryName = $this->convertToDispString($row['pc_name']);
259                 }
260                 $visible = '';
261                 if ($fetchedRow['pc_visible']){ // 項目の表示
262                         $visible = 'checked';
263                 }               
264                 $row = array(
265                         'index' => $index,                                                                                                      // 行番号
266                         'serial' => $serial,    // シリアル番号
267                         'id' => $id,                    // ID
268                         'name' => $this->convertToDispString($fetchedRow['pc_name']),           // 名前
269                         'pcategory_name' => $pcategoryName,             // 親カテゴリー名前
270                         'view_index' => $this->convertToDispString($fetchedRow['pc_sort_order']),               // 表示順
271                         'lang' => $lang,                                                                                                        // 対応言語
272                         'update_user' => $this->convertToDispString($fetchedRow['lu_name']),    // 更新者
273                         'update_dt' => $this->convertToDispDateTime($fetchedRow['pc_create_dt']),       // 更新日時
274                         'visible' => $visible,                                                                                  // メニュー項目表示制御
275                         'default' => $default                                                                                   // デフォルト項目
276                 );
277                 $this->tmpl->addVars('itemlist', $row);
278                 $this->tmpl->parseTemplate('itemlist', 'a');
279
280                 // 表示中項目のシリアル番号を保存
281                 $this->serialArray[] = $serial;
282                 return true;
283         }
284         /**
285          * 取得した言語をテンプレートに設定する
286          *
287          * @param int $index                    行番号(0~)
288          * @param array $fetchedRow             フェッチ取得した行
289          * @param object $param                 未使用
290          * @return bool                                 true=処理続行の場合、false=処理終了の場合
291          */
292         function langLoop($index, $fetchedRow, $param)
293         {
294                 $selected = '';
295                 if ($fetchedRow['ln_id'] == $this->langId){
296                         $selected = 'selected';
297                 }
298                 if ($this->gEnv->getCurrentLanguage() == 'ja'){         // 日本語表示の場合
299                         $name = $this->convertToDispString($fetchedRow['ln_name']);
300                 } else {
301                         $name = $this->convertToDispString($fetchedRow['ln_name_en']);
302                 }
303
304                 $row = array(
305                         'value'    => $this->convertToDispString($fetchedRow['ln_id']),                 // 言語ID
306                         'name'     => $name,                    // 言語名
307                         'selected' => $selected                                                                                                         // 選択中かどうか
308                 );
309                 $this->tmpl->addVars('lang_list', $row);
310                 $this->tmpl->parseTemplate('lang_list', 'a');
311                 return true;
312         }
313         /**
314          * 取得したデータをテンプレートに設定する
315          *
316          * @param int $index                    行番号(0~)
317          * @param array $fetchedRow             フェッチ取得した行
318          * @param object $param                 未使用
319          * @return bool                                 true=処理続行の場合、false=処理終了の場合
320          */
321         function pcategoryListLoop($index, $fetchedRow, $param)
322         {
323                 $id = $fetchedRow['pc_id'];// カテゴリ項目ID
324                 $name = $fetchedRow['pc_name'];         // カテゴリ項目名
325                 $selected = '';
326                 if ($id == $this->parentCategoryId) $selected = 'selected';
327                 
328                 // 自分自身は親カテゴリーメニューに加えない
329                 if ($this->id == $id) return true;
330                 
331                 $row = array(
332                         'value' => $this->convertToDispString($id),             // カテゴリ項目ID
333                         'name' => $this->convertToDispString($name),    // カテゴリ項目名
334                         'selected'      => $selected                                            // 選択中かどうか
335                 );
336                 $this->tmpl->addVars('category_list', $row);
337                 $this->tmpl->parseTemplate('category_list', 'a');
338                 return true;
339         }
340 }
341 ?>