OSDN Git Service

660b41e697da4f6ff72b8c1a955d5b5fbe94fd40
[magic3/magic3.git] / widgets / m / contactus_custom / include / container / m_contactus_customWidgetContainer.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-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: m_contactus_customWidgetContainer.php 2363 2009-09-26 14:45:44Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseMobileWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/contactus_customDb.php');
18
19 class m_contactus_customWidgetContainer extends BaseMobileWidgetContainer
20 {
21         private $db;                    // DB接続オブジェクト
22         private $fieldInfoArray = array();                      // お問い合わせ項目情報
23         private $valueArray;            // 項目入力値
24         const DEFAULT_CONFIG_ID = 0;
25         const CONTACTUS_FORM = 'contact_us';            // お問い合わせフォーム
26         const DEFAULT_SEND_MESSAGE = 1;         // メール送信機能を使用するかどうか(デフォルト使用)
27         const DEFAULT_TITLE_NAME = 'お問い合わせ';        // デフォルトのタイトル名
28         const DEFAULT_STR_REQUIRED = '<span style="color:#ff0000"><font color="#ff0000">*必須</font></span>';         // 「必須」表示用テキスト
29         const FIELD_HEAD = 'item';                      // フィールド名の先頭文字列
30         
31         /**
32          * コンストラクタ
33          */
34         function __construct()
35         {
36                 // 親クラスを呼び出す
37                 parent::__construct();
38                 
39                 // DBオブジェクト作成
40                 $this->db = new contactus_customDb();
41         }
42         /**
43          * テンプレートファイルを設定
44          *
45          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
46          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
47          *
48          * @param RequestManager $request               HTTPリクエスト処理クラス
49          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
50          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
51          */
52         function _setTemplate($request, &$param)
53         {
54                 return 'index.tmpl.html';
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                 // 定義ID取得
68                 $configId = $this->gEnv->getCurrentWidgetConfigId();
69                 if (empty($configId)) $configId = self::DEFAULT_CONFIG_ID;
70         
71                 // パラメータオブジェクトを取得
72                 $targetObj = $this->getWidgetParamObjByConfigId($configId);
73                 if (empty($targetObj)) return;          // 定義データが取得できないときは終了
74                 
75                 // デフォルト値設定
76                 $inputEnabled = true;                   // 入力の許可状態
77                 $now = date("Y/m/d H:i:s");     // 現在日時
78                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
79                 $sendMessage = self::DEFAULT_SEND_MESSAGE;                      // メール送信機能を使用するかどうか
80                 $showTitle = 0;                         // タイトルを表示するかどうか
81                 $titleName = self::DEFAULT_TITLE_NAME;                  // タイトル名
82                 $titleBgColor = '';             // タイトルバックグランドカラー
83                 $explanation = '';                      // 説明
84                 
85                 //$sendMessage = $targetObj->sendMessage;                       // メール送信機能を使用するかどうか
86                 $emailReceiver = $targetObj->emailReceiver;             // メール受信者
87                 $emailSubject = $targetObj->emailSubject;               // メール件名
88                 $showTitle = $targetObj->showTitle;                             // タイトルを表示するかどうか
89                 if (!empty($targetObj->titleName)) $titleName = $targetObj->titleName;                  // タイトル名
90                 $titleBgColor = $targetObj->titleBgColor;               // タイトルバックグランドカラー
91                 $explanation = $targetObj->explanation;                 // 説明
92                 $name   = $targetObj->name;// 名前
93                 if (!empty($targetObj->fieldInfo)) $this->fieldInfoArray = $targetObj->fieldInfo;                       // お問い合わせフィールド情報
94                 
95                 // 入力値を取得
96                 $this->valueArray = array();
97                 $fieldCount = count($this->fieldInfoArray);
98                 for ($i = 0; $i < $fieldCount; $i++){
99                         $itemName = self::FIELD_HEAD . ($i + 1);
100                         $itemValue = $request->mobileTrimValueOf($itemName);
101                         $this->valueArray[] = $itemValue;
102                 }
103                 $act = $request->trimValueOf('act');
104                 
105                 if ($act == 'send'){            // お問い合わせメール送信
106                         $postTicket = $request->trimValueOf('ticket');          // POST確認用
107                         if (!empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET)){           // 正常なPOST値のとき
108                                 // 入力状況のチェック
109                                 for ($i = 0; $i < $fieldCount; $i++){
110                                         $infoObj = $this->fieldInfoArray[$i];
111                                         $title = $infoObj->title;// タイトル名
112                                         $type = $infoObj->type;         // 項目タイプ
113                                         $required = $infoObj->required;         // 必須入力
114                                         if (!empty($required) && empty($this->valueArray[$i])) $this->setUserErrorMsg('「' . $title . '」は必須入力項目です');
115                                 }
116
117                                 // エラーなしの場合はメール送信
118                                 if ($this->getMsgCount() == 0){
119                                         $this->setGuidanceMsg('送信完了しました');
120                                 
121                                         // メール送信設定のときはメールを送信
122                                         if ($sendMessage){
123                                                 // メール本文の作成
124                                                 $mailBody = '';
125                                                 for ($i = 0; $i < $fieldCount; $i++){
126                                                         $infoObj = $this->fieldInfoArray[$i];
127                                                         $title = $infoObj->title;// タイトル名
128                                                         $type = $infoObj->type;         // 項目タイプ
129                                                 
130                                                         $mailBody .= $title . "\n";
131                                                         if (!empty($this->valueArray[$i])){
132                                                                 if (is_array($this->valueArray[$i])){           // 配列データのとき
133                                                                         for ($j = 0; $j < count($this->valueArray[$i]); $j++){
134                                                                                 $mailBody .= $this->valueArray[$i][$j] . "\n";
135                                                                         }
136                                                                 } else {
137                                                                         $mailBody .= $this->valueArray[$i] . "\n";
138                                                                 }
139                                                         }
140                                                         $mailBody .= "\n";
141                                                 }
142                                         
143                                                 // 送信元、送信先
144                                                 $fromAddress = $this->gEnv->getSiteEmail();     // 送信元はサイト情報のEメールアドレス
145                                                 $toAddress = $this->gEnv->getSiteEmail();               // デフォルトのサイト向けEメールアドレス
146                                                 if (!empty($emailReceiver)) $toAddress = $emailReceiver;                // 受信メールアドレスが設定されている場合
147
148                                                 // メールを送信
149                                                 if (empty($toAddress)){
150                                                         $this->gOpeLog->writeError(__METHOD__, 'メール送信に失敗しました。基本情報のEメールアドレスが設定されていません。', 1100, 'body=[' . $mailBody . ']');
151                                                 } else {
152                                                         $mailParam = array();
153                                                         $mailParam['BODY'] = $mailBody;
154                                                         $email = '';            // 返信先は空にする(暫定)
155                                                         $ret = $this->gInstance->getMailManager()->sendFormMail(2/*手動送信*/, $this->gEnv->getCurrentWidgetId(), $toAddress, $fromAddress, $email, $emailSubject, self::CONTACTUS_FORM, $mailParam);
156                                                 }
157                                         }
158                                         // 項目を入力不可に設定
159                                         $inputEnabled = false;                  // 入力の許可状態
160
161                                         //$this->tmpl->addVar("_widget", "message", '送信しました');// 送信ボタンラベル
162                                 } else {
163                                         $this->tmpl->addVar("show_send_button", "send_button_label", '送信する');// 送信ボタンラベル
164                                         $this->tmpl->setAttribute('show_send_button', 'visibility', 'visible');
165                                 }
166                         }
167                 } else {
168                         // ハッシュキー作成
169                         $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
170                         $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
171                         $this->tmpl->addVar("_widget", "ticket", $postTicket);                          // 画面に書き出し
172                         
173                         // メール送信不可の場合はボタンを使用不可にする
174                         if ($sendMessage){
175                                 $this->tmpl->addVar("show_send_button", "send_button_label", '送信する');// 送信ボタンラベル
176                         } else {
177                                 $this->tmpl->addVar("show_send_button", "send_button_label", '送信停止中');// 送信ボタンラベル
178                                 //$this->tmpl->addVar("_widget", "send_button_disabled", 'disabled');// 送信ボタン
179                         }
180                         $this->tmpl->setAttribute('show_send_button', 'visibility', 'visible');
181                 }
182                 
183                 // HTMLサブタイトルを設定
184                 //$this->gPage->setHeadSubTitle(self::DEFAULT_TITLE_NAME);
185                 $this->gPage->setHeadSubTitle($this->gEnv->getCurrentWidgetHeadTitle());                        // 共通設定画面の「タイトル」値を使用する
186
187                 // パラメータ埋め込み
188                 $this->tmpl->addVar('_widget', 'url', $this->gEnv->createCurrentPageUrlForMobile());            // Post用URL
189                 $this->tmpl->addVar('_widget', 'act', 'send');
190                 
191                 // タイトルの表示
192                 if ($showTitle){
193                         $titleClassStr = 'align="center" style="text-align:center;';
194                         if (!empty($titleBgColor)) $titleClassStr .= 'background-color:' . $titleBgColor . ';';// タイトルバックグランドカラー
195                         $titleClassStr .= '"';
196                         $this->tmpl->addVar("show_title", "class", $titleClassStr);
197                         $this->tmpl->setAttribute('show_title', 'visibility', 'visible');
198                         $this->tmpl->addVar("show_title", "title_name", $this->convertToDispString($titleName));// タイトル名
199                 }
200                 // 説明の表示
201                 if (!empty($explanation)){
202                         $this->tmpl->setAttribute('show_explanation', 'visibility', 'visible');
203                         $this->tmpl->addVar("show_explanation", "explanation", $explanation);// 説明
204                 }
205                 // お問い合わせフィールド作成
206                 $fieldCount = $this->createFieldList($inputEnabled);
207                 if ($fieldCount == 0) $this->tmpl->setAttribute('field_list', 'visibility', 'hidden');
208                 
209                 $this->tmpl->addVar("_widget", "field_count", $fieldCount);// お問い合わせ項目数
210         }
211         /**
212          * お問い合わせフィールド作成
213          *
214          * @param bool $enabled         項目の入力許可状態
215          * @return int  フィールド項目数
216          */
217         function createFieldList($enabled)
218         {
219                 $fieldCount = count($this->fieldInfoArray);
220                 for ($i = 0; $i < $fieldCount; $i++){
221                         $infoObj = $this->fieldInfoArray[$i];
222                         $title = $infoObj->title;// タイトル名
223                         $desc = $infoObj->desc;         // 説明
224                         $type = $infoObj->type;         // 項目タイプ
225                         $def = $infoObj->def;           // 項目定義
226                         $required = '';
227                         if (!empty($infoObj->required)) $required = '&nbsp;' . self::DEFAULT_STR_REQUIRED;// 必須表示
228                         
229                         // 入力フィールドの作成
230                         $fieldName = self::FIELD_HEAD . ($i + 1);
231                         $inputValue = $this->valueArray[$i];            // 入力値
232                         $inputTag = '';
233                         switch ($type){
234                                 case 'text':            // テキストボックス
235                                         $param = array();
236                                         $paramStr = '';
237                                         $size = 0;
238                                         $defArray = explode(';', $def);
239                                         for ($j = 0; $j < count($defArray); $j++){
240                                                 list($key, $value) = explode('=', $defArray[$j]);
241                                                 $key = trim($key);
242                                                 $value = trim($value);
243                                                 if (strcasecmp($key, 'size') == 0){
244                                                         $size = intval($value);
245                                                         break;
246                                                 }
247                                         }
248                                         if ($size > 0) $param[] = 'size="' . $size . '"';
249                                         if (!empty($inputValue)){
250                                                 $param[] = 'value="' . $inputValue . '"';
251                                         }
252                                         if (!$enabled) $param[] = 'disabled';           // 使用不可
253                                         if (count($param) > 0) $paramStr = ' ' . implode($param, ' ');
254                                         $inputTag = '<input type="text" name="' . $fieldName . '"' . $paramStr . ' /><br />' . M3_NL;
255                                         break;
256                                 case 'textarea':        // テキストエリア
257                                         $param = array();
258                                         $paramStr = '';
259                                         $row = 0;
260                                         $col = 0;
261                                         $defArray = explode(';', $def);
262                                         for ($j = 0; $j < count($defArray); $j++){
263                                                 list($key, $value) = explode('=', $defArray[$j]);
264                                                 $key = trim($key);
265                                                 $value = trim($value);
266                                                 if (strcasecmp($key, 'rows') == 0){
267                                                         $row = intval($value);
268                                                 } else if (strcasecmp($key, 'cols') == 0){
269                                                         $col = intval($value);
270                                                 }
271                                         }
272                                         if ($row > 0) $param[] = 'rows="' . $row . '"';
273                                         if ($col > 0) $param[] = 'cols="' . $col . '"';
274                                         if (!$enabled) $param[] = 'disabled';           // 使用不可
275                                         if (count($param) > 0) $paramStr = ' ' . implode($param, ' ');
276                                         $inputTag = '<textarea name="' . $fieldName . '"' . $paramStr . '>' . $this->convertToDispString($inputValue) . '</textarea><br />' . M3_NL;
277                                         break;
278                                 case 'select':  // セレクトメニュー
279                                         $param = array();
280                                         $paramStr = '';
281                                         if (!$enabled) $param[] = 'disabled';           // 使用不可
282                                         if (count($param) > 0) $paramStr = ' ' . implode($param, ' ');
283                                         $inputTag = '<select name="' . $fieldName . '"'. $paramStr . '>' . M3_NL;
284                                         $inputTag .= '<option value="">&nbsp;</option>' . M3_NL;
285                                         $defArray = explode(';', $def);
286                                         for ($j = 0; $j < count($defArray); $j++){
287                                                 $param = array();
288                                                 $paramStr = '';
289                                                 list($key, $value) = explode('=', $defArray[$j]);
290                                                 $key = trim($key);
291                                                 $value = trim($value);
292                                                 if (empty($value)) $value = $key;
293                                                 if (!empty($key)){
294                                                         if (!empty($inputValue) && strcmp($inputValue, $value) == 0) $param[] = 'selected';
295                                                         if (count($param) > 0) $paramStr = ' ' . implode($param, ' ');
296                                                         $inputTag .= '<option value="' . $this->convertToDispString($value) . '"' . $paramStr . '>' . $this->convertToDispString($key) . '</option>' . M3_NL;
297                                                 }
298                                         }
299                                         $inputTag .= '</select><br />' . M3_NL;
300                                         break;
301                                 case 'checkbox':        // チェックボックス
302                                 case 'radio':   // ラジオボタン
303                                         $fieldName .= '[]';
304                                         $defArray = explode(';', $def);
305                                         for ($j = 0; $j < count($defArray); $j++){
306                                                 $param = array();
307                                                 $paramStr = '';
308                                                 list($key, $value) = explode('=', $defArray[$j]);
309                                                 $key = trim($key);
310                                                 $value = trim($value);
311                                                 if (empty($value)) $value = $key;
312                                                 if (!empty($key) && !empty($value)){
313                                                         for ($k = 0; $k < count($inputValue); $k++){
314                                                                 if (!empty($inputValue[$k]) && strcmp($inputValue[$k], $value) == 0) $param[] = 'checked';
315                                                         }
316                                                         if (!$enabled) $param[] = 'disabled';           // 使用不可
317                                                         if (count($param) > 0) $paramStr = ' ' . implode($param, ' ');
318                                                         $inputTag .= '<input type="' . $type . '" name="' . $fieldName . '" value="' . $this->convertToDispString($value) . '"' . $paramStr . ' />' . $this->convertToDispString($key) . '<br />' . M3_NL;
319                                                 }
320                                         }
321                                         break;
322                         }
323
324                         // 改行の設定
325                         $titleBr = '';
326                         if (!empty($title) || !empty($required)) $titleBr = '<br />';
327                         $descBr = '';
328                         if (!empty($desc)) $descBr = '<br />';
329                         $inputBr = '';
330                         if (!empty($inputTag)) $inputBr = '<br />';
331                         
332                         $row = array(
333                                 'title' => $this->convertToDispString($title),                  // タイトル名
334                                 'desc' => $this->convertToDispString($desc),                            // 説明
335                                 'title_br' => $titleBr,                 // タイトル名改行
336                                 'desc_br' => $descBr,                   // 説明改行
337                                 'input_br' => $inputBr,                 // 入力フィールド改行
338                                 'required' => $required,                        // 必須表示
339                                 'input' => $inputTag                    // 入力フィールド
340                         );
341                         $this->tmpl->addVars('field_list', $row);
342                         $this->tmpl->parseTemplate('field_list', 'a');
343                 }
344                 return $fieldCount;
345         }
346 }
347 ?>