OSDN Git Service

Eコマース機能更新。
[magic3/magic3.git] / widgets / ec_main / include / container / admin_ec_mainDelivmethodWidgetContainer.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-2015 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id$
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_ec_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/ec_mainDelivMethodDb.php');
18
19 class admin_ec_mainDelivmethodWidgetContainer extends admin_ec_mainBaseWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $langId;                // 表示言語
23         private $iWidgetId;     // インナーウィジェットID
24         private $serialNo;              // 選択中の項目のシリアル番号
25         private $serialArray = array();         // 表示されている項目シリアル番号
26         const IWIDGET_TYPE = 'DELIVERY';                // 配送方法用インナーウィジェット
27         const DELIV_TASK = 'delivmethod';               // 配送方法選択画面表示用
28         
29         /**
30          * コンストラクタ
31          */
32         function __construct()
33         {
34                 // 親クラスを呼び出す
35                 parent::__construct();
36                 
37                 // DBオブジェクト作成
38                 $this->db = new ec_mainDelivMethodDb();
39         }
40         /**
41          * テンプレートファイルを設定
42          *
43          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
44          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
45          *
46          * @param RequestManager $request               HTTPリクエスト処理クラス
47          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
48          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
49          */
50         function _setTemplate($request, &$param)
51         {
52                 $task = $request->trimValueOf('task');
53                 if ($task == 'delivmethod_detail'){             // 詳細画面
54                         return 'admin_delivery_detail.tmpl.html';
55                 } else {                        // 一覧画面
56                         return 'admin_delivery.tmpl.html';
57                 }
58         }
59         /**
60          * テンプレートにデータ埋め込む
61          *
62          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
63          *
64          * @param RequestManager $request               HTTPリクエスト処理クラス
65          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
66          * @param                                                               なし
67          */
68         function _assign($request, &$param)
69         {
70                 $task = $request->trimValueOf('task');
71                 if ($task == 'delivmethod_detail'){     // 詳細画面
72                         return $this->createDetail($request);
73                 } else {                        // 一覧画面
74                         return $this->createList($request);
75                 }
76         }
77         /**
78          * 一覧画面作成
79          *
80          * @param RequestManager $request               HTTPリクエスト処理クラス
81          * @param                                                               なし
82          */
83         function createList($request)
84         {
85                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
86                 $act = $request->trimValueOf('act');
87                 
88                 if ($act == 'delete'){          // 項目削除の場合
89                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
90                         $delItems = array();
91                         for ($i = 0; $i < count($listedItem); $i++){
92                                 // 項目がチェックされているかを取得
93                                 $itemName = 'item' . $i . '_selected';
94                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
95                                 
96                                 if ($itemValue){                // チェック項目
97                                         $delItems[] = $listedItem[$i];
98                                 }
99                         }
100                         if (count($delItems) > 0){
101                                 $ret = $this->db->deleteDelivMethodDefBySerial($delItems);
102                                 if ($ret){              // データ削除成功のとき
103                                         $this->setGuidanceMsg('データを削除しました');
104                                 } else {
105                                         $this->setAppErrorMsg('データ削除に失敗しました');
106                                 }
107                         }
108                 }
109                 // 一覧を表示
110                 $this->db->getAllDelivMethodDef($this->langId, 0/*デフォルトのセットID*/, array($this, 'delivDefLoop'));
111                 
112                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
113                 
114                 // プレビュー表示ボタンのリンクを作成
115                 $url = $this->gPage->createWidgetCmdUrl($this->gEnv->getCurrentWidgetId(), ''/*送信元ウィジェット指定なし*/,
116                                                                         'task=' . self::DELIV_TASK . '&' . M3_REQUEST_PARAM_OPERATION_COMMAND . '=' . M3_REQUEST_CMD_PREVIEW, $this->gEnv->getDefaultPageId());
117                 $this->tmpl->addVar("_widget", "url", $this->getUrl($url, true));
118         }
119         /**
120          * 詳細画面作成
121          *
122          * @param RequestManager $request               HTTPリクエスト処理クラス
123          * @param                                                               なし
124          */
125         function createDetail($request)
126         {
127                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
128                 $act = $request->trimValueOf('act');
129                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
130                 if (empty($this->serialNo)) $this->serialNo = 0;
131                                 
132                 $this->id       = $request->trimValueOf('id');  // ID
133                 $addid  = $request->trimValueOf('item_id');     // ID
134                 $name   = $request->trimValueOf('item_name');   // 名前
135                 $desc   = $request->valueOf('item_desc');       // 説明
136                 $index  = $request->trimValueOf('item_index');  // 表示順
137                 $visible        = ($request->trimValueOf('item_visible') == 'on') ? 1 : 0;      // 表示状態
138                 $this->iWidgetId        = $request->trimValueOf('item_iwidget');        // インナーウィジェットID
139                 $param = '';                    // インナーウィジェット用パラメータ
140                 
141                 $replaceNew = false;            // データを再取得するかどうか
142                 if ($act == 'add'){             // 新規追加のとき
143                         // 入力チェック
144                         $this->checkSingleByte($addid, 'ID');
145                         $this->checkInput($name, '表示名');
146                         $this->checkNumeric($index, '表示順');
147                         
148                         // 同じIDがある場合はエラー
149                         if ($this->db->isExistsDelivMethodId($this->langId, 0/*デフォルトのセットID*/, $addid)) $this->setMsg(self::MSG_USER_ERR, 'IDが重複しています');
150
151                         // インナーウィジェット更新
152                         if ($this->getMsgCount() == 0){
153                                 if (!empty($this->iWidgetId)){
154                                         // インナーウィジェットでのパラメータの更新に成功した場合
155                                         $this->updateIWidgetParam($this->iWidgetId, $this->id, $param, $optionParam, true);
156                                         
157                                         // インナーウィジェット内で発生したエラーを取得
158                                         $this->getGlobalMsg();
159                                 }
160                         }
161                         
162                         // エラーなしの場合は、データを更新
163                         if ($this->getMsgCount() == 0){
164                                 $ret = $this->db->updateDelivMethodDef($addid, $this->langId, 0/*デフォルトのセットID*/, $name, $desc, $index, $visible, $this->iWidgetId, $param);
165                                 if ($ret){              // データ追加成功のとき
166                                         $this->setMsg(self::MSG_GUIDANCE, 'データを追加しました');
167                                         
168                                         $this->id = $addid;             // 新規IDに更新
169                                         $replaceNew = true;                     // データを再取得
170                                 } else {
171                                         $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
172                                 }
173                         }
174                 } else if ($act == 'update'){           // 行更新のとき
175                         // 入力チェック
176                         $this->checkInput($name, '表示名');
177                         $this->checkNumeric($index, '表示順');
178                         
179                         // インナーウィジェット更新
180                         if ($this->getMsgCount() == 0){
181                                 if (!empty($this->iWidgetId)){
182                                         // インナーウィジェットでのパラメータの更新に成功した場合
183                                         $this->updateIWidgetParam($this->iWidgetId, $this->id, $param, $optionParam, true);
184                                         
185                                         // インナーウィジェット内で発生したエラーを取得
186                                         $this->getGlobalMsg();
187                                 }
188                         }
189                         
190                         // エラーなしの場合は、データを更新
191                         if ($this->getMsgCount() == 0){
192                                 $ret = $this->db->updateDelivMethodDef($this->id, $this->langId, 0/*デフォルトのセットID*/, $name, $desc, $index, $visible, $this->iWidgetId, $param);
193                                 if ($ret){              // 更新成功のとき
194                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
195                                         $replaceNew = true;                     // データを再取得
196                                 } else {
197                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
198                                 }
199                         }
200                 } else if ($act == 'delete'){           // 削除のとき
201                         $ret = $this->db->deleteDelivMethodDefBySerial(array($this->serialNo));
202                         if ($ret){              // データ更新成功のとき
203                                 $this->setMsg(self::MSG_GUIDANCE, '項目を削除しました');
204                         } else {
205                                 $this->setMsg(self::MSG_APP_ERR, '項目削除に失敗しました');
206                         }
207                 } else if ($act == 'selectcalc'){               // 計算方法選択のとき
208                         if (!empty($this->iWidgetId)){
209                                 // パラメータをインナーウィジェットに設定
210                                 $optionParam = new stdClass;
211                                 $optionParam->init = true;              // 初期データ取得
212                                 $this->setIWidgetParam($this->iWidgetId, $this->id, $row['do_param'], $optionParam, true);
213                         }
214                 } else {                // 初期状態
215                         // シリアル番号からIDを取得
216                         $this->id = $this->db->getDelivMethodDefIdBySerial($this->serialNo);                    // 選択中の配送項目ID
217                         if (empty($this->id)){  // 空のときは新規とする
218                                 $this->serialNo = 0;
219                                 
220                                 $addid          = '';   // ID
221                                 $name   = '';   // 名前
222                                 $desc   = '';   // 説明
223                                 $index  = $this->db->getMaxDelivMethodIndex($this->langId, 0/*デフォルトのセットID*/) + 1;     // 表示順
224                                 $visible        = 1;    // 表示状態
225                                 $this->iWidgetId        = '';   // インナーウィジェットID
226                         } else {
227                                 $replaceNew = true;                     // データを再取得
228                         }
229                 }
230                 // データを再取得のとき
231                 if ($replaceNew){
232                         $ret = $this->db->getDelivMethodDef($this->id, $this->langId, 0/*デフォルトのセットID*/, $row);
233                         if ($ret){
234                                 $this->serialNo = $row['do_serial'];    // シリアル番号
235                                 $addid          = $row['do_id'];        // ID
236                                 $name           = $row['do_name'];      // 名前
237                                 $desc           = $row['do_description'];       // 説明
238                                 $index          = $row['do_index'];     // 表示順
239                                 $visible        = $row['do_visible'];   // 表示状態
240                                 $this->iWidgetId        = $row['do_iwidget_id'];        // インナーウィジェットID
241                                 if (!empty($this->iWidgetId)){
242                                         // パラメータをインナーウィジェットに設定
243                                         $optionParam = new stdClass;
244                                         $optionParam->init = true;              // 初期データ取得
245                                         $this->setIWidgetParam($this->iWidgetId, $this->id, $row['do_param'], $optionParam, true);
246                                 }
247                         }
248                 }
249                 
250                 if (empty($this->serialNo)){            // シリアル番号が空のときは新規とする
251                         $this->tmpl->addVar("_widget", "id_label", '');                 // 選択項目のIDラベル
252                         $this->tmpl->addVar("_widget", "new_selected", 'checked');// ユーザIDが0のときは新規追加をチェック状態にする
253                         
254                         $this->tmpl->setAttribute('add_id_field', 'visibility', 'visible');// 新規ID入力フィールド表示
255                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
256                 } else {
257                         $this->tmpl->addVar("_widget", "id_label", $this->id);                  // 選択項目のIDラベル
258                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 更新ボタン表示
259                 }
260                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
261                 $this->tmpl->addVar("_widget", "id", $this->id);                        // ID
262                 $this->tmpl->addVar("add_id_field", "add_id", $addid);                  // 追加ID
263                 $this->tmpl->addVar("_widget", "name", $name);          // 名前
264                 $this->tmpl->addVar("_widget", "index", $index);        // 表示順
265                 $this->tmpl->addVar("_widget", "desc", $desc);  // 説明
266                 $visibleStr = '';
267                 if ($visible){  // 項目の表示
268                         $visibleStr = 'checked';
269                 }
270                 $this->tmpl->addVar("_widget", "visible", $visibleStr);         // 表示状態
271                 
272                 // インナーウィジェット選択メニューを作成
273                 $this->_db->getAllIWidgetListByType($this->gEnv->getCurrentWidgetId(), self::IWIDGET_TYPE, array($this, 'iWidgetLoop'));
274                 
275                 // 選択中のインナーウィジェットの管理画面を取得
276                 if (!empty($this->iWidgetId)){
277                         $innerContent = $this->getIWidgetContent($this->iWidgetId, $this->id, true);    // 管理者画面を取得
278                         $this->tmpl->addVar("_widget", "iwidget", $innerContent);
279                 }
280
281                 // パスの設定
282                 //$this->tmpl->addVar("_widget", "root_url", $this->gEnv->getRootUrl());
283                 //$this->tmpl->addVar("_widget", "widget_sc_url", $this->gEnv->getCurrentWidgetScriptsUrl());
284         }
285         /**
286          * 取得した配送方法定義をテンプレートに設定する
287          *
288          * @param int $index                    行番号(0~)
289          * @param array $fetchedRow             フェッチ取得した行
290          * @param object $param                 未使用
291          * @return bool                                 true=処理続行の場合、false=処理終了の場合
292          */
293         function delivDefLoop($index, $fetchedRow, $param)
294         {
295                 $checked = '';
296                 if ($fetchedRow['do_id'] == $this->id){
297                         $checked = 'checked';
298                 }
299                 $visible = '';
300                 if ($fetchedRow['do_visible']){ // 項目の表示
301                         $visible = 'checked';
302                 }
303                 $row = array(
304                         'serial' => $fetchedRow['do_serial'],                                                           // シリアル番号
305                         'index' => $index,                                                                                                      // 項目番号
306                         'id'     => $this->convertToDispString($fetchedRow['do_id']),                   // ID
307                         'name'     => $this->convertToDispString($fetchedRow['do_name']),                       // 表示名
308                         'view_index'     => $this->convertToDispString($fetchedRow['do_index']),                        // 表示順
309                         'visible' => $visible,                                                                                  // メニュー項目表示制御
310                         'checked' => $checked                                                                                                           // 選択中かどうか
311                 );
312                 $this->tmpl->addVars('delivmethod_list', $row);
313                 $this->tmpl->parseTemplate('delivmethod_list', 'a');
314                 
315                 // 表示中項目のシリアル番号を保存
316                 $this->serialArray[] = $fetchedRow['do_serial'];
317                 return true;
318         }
319         /**
320          * 取得した配送方法インナーウィジェットをテンプレートに設定する
321          *
322          * @param int $index                    行番号(0~)
323          * @param array $fetchedRow             フェッチ取得した行
324          * @param object $param                 未使用
325          * @return bool                                 true=処理続行の場合、false=処理終了の場合
326          */
327         function iWidgetLoop($index, $fetchedRow, $param)
328         {
329                 $id = $fetchedRow['iw_widget_id'] . ',' . $fetchedRow['iw_id'];
330                 $selected = '';
331                 if ($id == $this->iWidgetId){           // 選択中のインナーウィジェット
332                         $selected = 'selected';
333                 }
334
335                 $row = array(
336                         'value'    => $id,                      // ID
337                         'name'     => $this->convertToDispString($fetchedRow['iw_name']),                       // 表示名
338                         'selected' => $selected                                                                                                         // 選択中かどうか
339                 );
340                 $this->tmpl->addVars('iwidget_list', $row);
341                 $this->tmpl->parseTemplate('iwidget_list', 'a');
342                 return true;
343         }
344 }
345 ?>