OSDN Git Service

DB更新スクリプト更新。
[magic3/magic3.git] / widgets / m / contactus_custom / include / container / admin_m_contactus_customWidgetContainer.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-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_m_contactus_customWidgetContainer.php 2307 2009-09-13 04:03:00Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/contactus_customDb.php');
18
19 class admin_m_contactus_customWidgetContainer extends BaseAdminWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $sysDb; // DB接続オブジェクト
23         private $serialNo;              // 選択中の項目のシリアル番号
24         private $serialArray = array();                 // 表示中のシリアル番号
25         private $langId;
26         private $configId;              // 定義ID
27         private $paramObj;              // パラメータ保存用オブジェクト
28         private $typeArray;             // 項目タイプ
29         private $fieldInfoArray = array();                      // お問い合わせ項目情報
30         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
31         const DEFAULT_TITLE_NAME = 'お問い合わせ';        // デフォルトのタイトル名
32         
33         /**
34          * コンストラクタ
35          */
36         function __construct()
37         {
38                 // 親クラスを呼び出す
39                 parent::__construct();
40                 
41                 // DBオブジェクト作成
42                 $this->db = new contactus_customDb();
43                 $this->sysDb = $this->gInstance->getSytemDbObject();
44                 
45                 // お問い合わせ項目タイプ
46                 $this->typeArray = array(       array(  'name' => 'テキストボックス',   'value' => 'text'),
47                                                                         array(  'name' => 'テキストエリア',              'value' => 'textarea'),
48                                                                         array(  'name' => 'セレクトメニュー',   'value' => 'select'),
49                                                                         array(  'name' => 'チェックボックス',   'value' => 'checkbox'),
50                                                                         array(  'name' => 'ラジオボタン',         'value' => 'radio'));
51         }
52         /**
53          * テンプレートファイルを設定
54          *
55          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
56          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
57          *
58          * @param RequestManager $request               HTTPリクエスト処理クラス
59          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
60          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
61          */
62         function _setTemplate($request, &$param)
63         {
64                 $task = $request->trimValueOf('task');
65                 if ($task == 'list'){           // 一覧画面
66                         return 'admin_list.tmpl.html';
67                 } else {                        // 一覧画面
68                         return 'admin.tmpl.html';
69                 }
70         }
71         /**
72          * テンプレートにデータ埋め込む
73          *
74          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
75          *
76          * @param RequestManager $request               HTTPリクエスト処理クラス
77          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
78          * @param                                                               なし
79          */
80         function _assign($request, &$param)
81         {
82                 $task = $request->trimValueOf('task');
83                 if ($task == 'list'){           // 一覧画面
84                         return $this->createList($request);
85                 } else {                        // 詳細設定画面
86                         return $this->createDetail($request);
87                 }
88         }
89         /**
90          * 詳細画面作成
91          *
92          * @param RequestManager $request               HTTPリクエスト処理クラス
93          * @param                                                               なし
94          */
95         function createDetail($request)
96         {
97                 // ページ定義IDとページ定義のレコードシリアル番号を取得
98                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
99                 
100                 $userId         = $this->gEnv->getCurrentUserId();
101                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
102                 $act = $request->trimValueOf('act');
103                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
104                 $this->configId = $request->trimValueOf('item_id');             // 定義ID
105                 if (empty($this->configId)) $this->configId = $defConfigId;             // 呼び出しウィンドウから引き継いだ定義ID
106                 
107                 // 入力値を取得
108                 $name   = $request->trimValueOf('item_name');                   // 定義名
109                 $showTitle = ($request->trimValueOf('show_title') == 'on') ? 1 : 0;             // タイトルの表示
110                 $titleName = $request->trimValueOf('title_name');                               // タイトル名
111                 $titleBgColor = $request->trimValueOf('item_title_bgcolor');            // タイトルバックグランドカラー
112                 $explanation = trim($request->valueOf('explanation'));                          // 説明
113                 $fieldCount = $request->trimValueOf('fieldcount');              // お問い合わせ項目数
114                 $titles = $request->trimValueOf('item_title');          // お問い合わせ項目タイトル
115                 $descs = $request->trimValueOf('item_desc');            // お問い合わせ項目説明
116                 $types  = $request->trimValueOf('item_type');           // お問い合わせ項目タイプ
117                 $defs = $request->trimValueOf('item_def');              // お問い合わせ項目定義
118                 $values = $request->trimValueOf('required');            // お問い合わせ項目必須入力
119                 $requireds = array();
120                 if (!empty($values)) $requireds = explode(',', $values);
121                 $emailSubject = $request->trimValueOf('email_subject');         // メールタイトル
122                 $emailReceiver = trim($request->valueOf('email_receiver'));     // メール受信者(aaaa<xxx@xxx.xxx>形式が可能)
123                 
124                 $replaceNew = false;            // データを再取得するかどうか
125                 if ($act == 'add'){// 新規追加
126                         // 入力値のエラーチェック
127                         
128                         // 設定名の重複チェック
129                         for ($i = 0; $i < count($this->paramObj); $i++){
130                                 $targetObj = $this->paramObj[$i]->object;
131                                 if ($name == $targetObj->name){         // 定義名
132                                         $this->setUserErrorMsg('名前が重複しています');
133                                         break;
134                                 }
135                         }
136                         
137                         // エラーなしの場合は、データを登録
138                         if ($this->getMsgCount() == 0){
139                                 // 追加オブジェクト作成
140                                 $newObj = new stdClass;
141                                 $newObj->name           = $name;// 表示名
142                                 $newObj->showTitle              = $showTitle;           // タイトルの表示
143                                 $newObj->titleName              = $titleName;                           // タイトル名
144                                 $newObj->titleBgColor           = $titleBgColor;                // タイトルバックグランドカラー
145                                 $newObj->explanation    = $explanation;                         // 説明
146                                 $newObj->emailSubject = $emailSubject;          // メールタイトル
147                                 $newObj->emailReceiver = $emailReceiver;        // メール受信者(aaaa<xxx@xxx.xxx>形式が可能)
148                                 $newObj->fieldInfo      = array();
149                                 
150                                 for ($i = 0; $i < $fieldCount; $i++){
151                                         $newInfoObj = new stdClass;
152                                         $newInfoObj->title      = $titles[$i];
153                                         $newInfoObj->desc       = $descs[$i];
154                                         $newInfoObj->type       = $types[$i];
155                                         $newInfoObj->def        = $defs[$i];
156                                         $newInfoObj->required   = $requireds[$i];
157                                         $newObj->fieldInfo[] = $newInfoObj;
158                                 }
159                                 
160                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
161                                 if ($ret){
162                                         $this->setGuidanceMsg('データを追加しました');
163                                         
164                                         $this->configId = $defConfigId;         // 定義定義IDを更新
165                                         $replaceNew = true;                     // データ再取得
166                                 } else {
167                                         $this->setAppErrorMsg('データ追加に失敗しました');
168                                 }
169                         }
170                 } else if ($act == 'update'){           // 設定更新のとき
171                         // 入力値のエラーチェック
172                         
173                         if ($this->getMsgCount() == 0){                 // エラーのないとき
174                                 // 現在の設定値を取得
175                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
176                                 if ($ret){
177                                         // ウィジェットオブジェクト更新
178                                         $targetObj->showTitle           = $showTitle;           // タイトルの表示
179                                         $targetObj->titleName           = $titleName;                           // タイトル名
180                                         $targetObj->titleBgColor                = $titleBgColor;                // タイトルバックグランドカラー
181                                         $targetObj->explanation = $explanation;                         // 説明
182                                         $targetObj->emailSubject = $emailSubject;               // メールタイトル
183                                         $targetObj->emailReceiver = $emailReceiver;     // メール受信者(aaaa<xxx@xxx.xxx>形式が可能)
184                                         $targetObj->fieldInfo   = array();
185                                 
186                                         for ($i = 0; $i < $fieldCount; $i++){
187                                                 $newInfoObj = new stdClass;
188                                                 $newInfoObj->title      = $titles[$i];
189                                                 $newInfoObj->desc       = $descs[$i];
190                                                 $newInfoObj->type       = $types[$i];
191                                                 $newInfoObj->def        = $defs[$i];
192                                                 $newInfoObj->required   = $requireds[$i];
193                                                 $targetObj->fieldInfo[] = $newInfoObj;
194                                         }
195                                 }
196                                 
197                                 // 設定値を更新
198                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
199                                 if ($ret){
200                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
201                                         $replaceNew = true;                     // データ再取得
202                                 } else {
203                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
204                                 }
205                         }
206                 } else if ($act == 'select'){   // 定義IDを変更
207                         $replaceNew = true;                     // データ再取得
208                 } else {        // 初期起動時、または上記以外の場合
209                         // デフォルト値設定
210                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
211                         $replaceNew = true;                     // データ再取得
212                 }
213                 // 設定項目選択メニュー作成
214                 $this->createItemMenu();
215                                 
216                 // 表示用データを取得
217                 if (empty($this->configId)){            // 新規登録の場合
218                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
219                         if ($replaceNew){               // データ再取得時
220                                 $name = $this->createDefaultName();                     // デフォルト登録項目名
221                                 $showTitle = 0;         // タイトルの表示
222                                 $titleName = self::DEFAULT_TITLE_NAME;                          // タイトル名
223                                 $titleBgColor = '';             // タイトルバックグランドカラー
224                                 $explanation = '';                              // 説明
225                                 $emailSubject = '';             // メールタイトル
226                                 $emailReceiver = '';    // メール受信者(aaaa<xxx@xxx.xxx>形式が可能)
227                                 $this->fieldInfoArray = array();                        // お問い合わせ項目情報
228                         }
229                         $this->serialNo = 0;
230                 } else {
231                         if ($replaceNew){// データ再取得時
232                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
233                                 if ($ret){
234                                         $name   = $targetObj->name;// 名前
235                                         $showTitle = $targetObj->showTitle;             // タイトルの表示
236                                         $titleName = $targetObj->titleName;                             // タイトル名
237                                         $titleBgColor = $targetObj->titleBgColor;               // タイトルバックグランドカラー
238                                         $explanation = $targetObj->explanation;                         // 説明
239                                         $emailSubject = $targetObj->emailSubject;               // メールタイトル
240                                         $emailReceiver = $targetObj->emailReceiver;     // メール受信者(aaaa<xxx@xxx.xxx>形式が可能)
241                                         if (!empty($targetObj->fieldInfo)) $this->fieldInfoArray = $targetObj->fieldInfo;                       // お問い合わせ項目情報
242                                 }
243                         }
244                         $this->serialNo = $this->configId;
245                                 
246                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
247                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
248                 }
249                 
250                 // 追加用タイプメニュー作成
251                 $this->createTypeMenu1();
252                 
253                 // お問い合わせ項目一覧作成
254                 $this->createFieldList();
255                 if (empty($this->fieldInfoArray)) $this->tmpl->setAttribute('field_list', 'visibility', 'hidden');// お問い合わせ項目情報一覧
256                 
257                 // 画面にデータを埋め込む
258                 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId);             // 定義ID
259                 $this->tmpl->addVar("item_name_visible", "name",        $name);
260                 if (!empty($showTitle)) $this->tmpl->addVar("_widget", "show_title",    'checked');             // タイトルの表示
261                 $this->tmpl->addVar("_widget", "title_name",    $this->convertToDispString($titleName));                                // タイトル名
262                 $this->tmpl->addVar("_widget", "title_bgcolor", $titleBgColor);         // タイトルバックグランドカラー
263                 $this->tmpl->addVar("_widget", "explanation",   $explanation);                          // 説明
264                 $this->tmpl->addVar("_widget", "email_subject", $emailSubject);         // メールタイトル
265                 $this->tmpl->addVar("_widget", "email_receiver",        $emailReceiver);        // メール受信者(aaaa<xxx@xxx.xxx>形式が可能)
266                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
267                 
268                 // ボタンの表示制御
269                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
270                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
271                 } else {
272                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
273                 }
274                 
275                 // ページ定義IDとページ定義のレコードシリアル番号を更新
276                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
277         }
278         /**
279          * 選択用メニューを作成
280          *
281          * @return なし                                               
282          */
283         function createItemMenu()
284         {
285                 for ($i = 0; $i < count($this->paramObj); $i++){
286                         $id = $this->paramObj[$i]->id;// 定義ID
287                         $targetObj = $this->paramObj[$i]->object;
288                         $name = $targetObj->name;// 定義名
289                         $selected = '';
290                         if ($this->configId == $id) $selected = 'selected';
291
292                         $row = array(
293                                 'name' => $name,                // 名前
294                                 'value' => $id,         // 定義ID
295                                 'selected' => $selected // 選択中の項目かどうか
296                         );
297                         $this->tmpl->addVars('title_list', $row);
298                         $this->tmpl->parseTemplate('title_list', 'a');
299                 }
300         }
301         /**
302          * お問い合わせ項目一覧を作成
303          *
304          * @return なし                                               
305          */
306         function createFieldList()
307         {
308                 $fieldCount = count($this->fieldInfoArray);
309                 for ($i = 0; $i < $fieldCount; $i++){
310                         $infoObj = $this->fieldInfoArray[$i];
311                         $title = $infoObj->title;// タイトル名
312                         $desc = $infoObj->desc;         // 説明
313                         $type = $infoObj->type;         // 項目タイプ
314                         $def = $infoObj->def;           // 項目定義
315                         $requiredCheck = '';
316                         if (!empty($infoObj->required)) $requiredCheck = 'checked';
317                         
318                         // 行を作成
319                         $this->tmpl->clearTemplate('type_list2');
320                         
321                         for ($j = 0; $j < count($this->typeArray); $j++){
322                                 $value = $this->typeArray[$j]['value'];
323                                 $name = $this->typeArray[$j]['name'];
324
325                                 $selected = '';
326                                 if ($value == $type) $selected = 'selected';
327
328                                 $tableLine = array(
329                                         'value'    => $value,                   // タイプ値
330                                         'name'     => $this->convertToDispString($name),                        // タイプ名
331                                         'selected' => $selected                 // 選択中かどうか
332                                 );
333                                 $this->tmpl->addVars('type_list2', $tableLine);
334                                 $this->tmpl->parseTemplate('type_list2', 'a');
335                         }
336                         $rootUrl = $this->convertToDispString($this->getUrl($this->gEnv->getRootUrl()));
337                         $row = array(
338                                 'title' => $this->convertToDispString($title),  // タイトル名
339                                 'desc' => $this->convertToDispString($desc),    // 説明
340                                 'def' => $this->convertToDispString($def),              // 定義情報
341                                 'required' => $requiredCheck,                                                   // 必須入力
342                                 'root_url' => $rootUrl
343                         );
344                         $this->tmpl->addVars('field_list', $row);
345                         $this->tmpl->parseTemplate('field_list', 'a');
346                 }
347         }
348         /**
349          * デフォルトの名前を取得
350          *
351          * @return string       デフォルト名                                              
352          */
353         function createDefaultName()
354         {
355                 $name = self::DEFAULT_NAME_HEAD;
356                 for ($j = 1; $j < 100; $j++){
357                         $name = self::DEFAULT_NAME_HEAD . $j;
358                         // 設定名の重複チェック
359                         for ($i = 0; $i < count($this->paramObj); $i++){
360                                 $targetObj = $this->paramObj[$i]->object;
361                                 if ($name == $targetObj->name){         // 定義名
362                                         break;
363                                 }
364                         }
365                         // 重複なしのときは終了
366                         if ($i == count($this->paramObj)) break;
367                 }
368                 return $name;
369         }
370         /**
371          * 一覧画面作成
372          *
373          * @param RequestManager $request               HTTPリクエスト処理クラス
374          * @param                                                               なし
375          */
376         function createList($request)
377         {
378                 // ページ定義IDとページ定義のレコードシリアル番号を取得
379                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
380                 
381                 $userId         = $this->gEnv->getCurrentUserId();
382                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
383                 $act = $request->trimValueOf('act');
384                 
385                 if ($act == 'delete'){          // メニュー項目の削除
386                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
387                         $delItems = array();
388                         for ($i = 0; $i < count($listedItem); $i++){
389                                 // 項目がチェックされているかを取得
390                                 $itemName = 'item' . $i . '_selected';
391                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
392                                 
393                                 if ($itemValue){                // チェック項目
394                                         $delItems[] = $listedItem[$i];
395                                 }
396                         }
397                         if (count($delItems) > 0){
398                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
399                                 if ($ret){              // データ削除成功のとき
400                                         $this->setGuidanceMsg('データを削除しました');
401                                 } else {
402                                         $this->setAppErrorMsg('データ削除に失敗しました');
403                                 }
404                         }
405                 }
406                 // 定義一覧作成
407                 $this->createItemList();
408                 
409                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
410                 
411                 // ページ定義IDとページ定義のレコードシリアル番号を更新
412                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
413         }
414         /**
415          * 定義一覧作成
416          *
417          * @return なし                                               
418          */
419         function createItemList()
420         {
421                 for ($i = 0; $i < count($this->paramObj); $i++){
422                         $id                     = $this->paramObj[$i]->id;// 定義ID
423                         $targetObj      = $this->paramObj[$i]->object;
424                         $name = $targetObj->name;// 定義名
425                         $emailReceiver  = $targetObj->emailReceiver;            // 受信メールアドレス
426                 
427                         // 使用数
428                         $defCount = 0;
429                         if (!empty($id)){
430                                 $defCount = $this->sysDb->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
431                         }
432                         $operationDisagled = '';
433                         if ($defCount > 0) $operationDisagled = 'disabled';
434                         
435                         $row = array(
436                                 'index' => $i,
437                                 'id' => $id,
438                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
439                                 'name' => $this->convertToDispString($name),            // 名前
440                                 'email_receiver' => $this->convertToDispString($emailReceiver), // 受信メールアドレス
441                                 'def_count' => $defCount                                                        // 使用数
442                         );
443                         $this->tmpl->addVars('itemlist', $row);
444                         $this->tmpl->parseTemplate('itemlist', 'a');
445                         
446                         // シリアル番号を保存
447                         $this->serialArray[] = $id;
448                 }
449         }
450         /**
451          * タイプ選択メニュー作成
452          *
453          * @return なし
454          */
455         function createTypeMenu1()
456         {
457                 for ($i = 0; $i < count($this->typeArray); $i++){
458                         $value = $this->typeArray[$i]['value'];
459                         $name = $this->typeArray[$i]['name'];
460                         
461                         $row = array(
462                                 'value'    => $value,                   // タイプ値
463                                 'name'     => $this->convertToDispString($name),                        // タイプ名
464                                 'selected' => $selected                 // 選択中かどうか
465                         );
466                         $this->tmpl->addVars('type_list1', $row);
467                         $this->tmpl->parseTemplate('type_list1', 'a');
468                 }
469         }
470 }
471 ?>