source: branches/version-2_5-dev/data/class/helper/SC_Helper_Mail.php @ 20435

Revision 20435, 18.4 KB checked in by nanasess, 13 years ago (diff)

#937(dtb_order_detail に主キーが無い)

  • dtb_order_detail.order_detail_id を作成
  • 受注詳細の並び順を order_detail_id に変更

#624(軽微な表示乱れを修正)

  • 受注完了のメッセージを修正
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/**
25 * メール関連 のヘルパークラス.
26 *
27 * @package Helper
28 * @author LOCKON CO.,LTD.
29 * @version $Id$
30 */
31class SC_Helper_Mail {
32
33    /** メールテンプレートのパス */
34    var $arrMAILTPLPATH;
35
36    /**
37     * コンストラクタ.
38     */
39    function SC_Helper_Mail() {
40        $masterData = new SC_DB_MasterData_Ex();
41        $this->arrMAILTPLPATH =  $masterData->getMasterData("mtb_mail_tpl_path");
42        $this->arrPref = $masterData->getMasterData('mtb_pref');
43    }
44
45    /* DBに登録されたテンプレートメールの送信 */
46    function sfSendTemplateMail($to, $to_name, $template_id, &$objPage, $from_address = "", $from_name = "", $reply_to = "") {
47
48        $objQuery = new SC_Query();
49        // メールテンプレート情報の取得
50        $where = "template_id = ?";
51        $arrRet = $objQuery->select("subject, header, footer", "dtb_mailtemplate", $where, array($template_id));
52        $objPage->tpl_header = $arrRet[0]['header'];
53        $objPage->tpl_footer = $arrRet[0]['footer'];
54        $tmp_subject = $arrRet[0]['subject'];
55
56        $objSiteInfo = new SC_SiteInfo();
57        $arrInfo = $objSiteInfo->data;
58
59        $objMailView = new SC_SiteView_Ex();
60        // メール本文の取得
61        $objMailView->assignobj($objPage);
62        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
63
64        // メール送信処理
65        $objSendMail = new SC_SendMail_Ex();
66        if ($from_address == "") $from_address = $arrInfo['email03'];
67        if ($from_name == "") $from_name = $arrInfo['shop_name'];
68        if ($reply_to == "") $reply_to = $arrInfo['email03'];
69        $error = $arrInfo['email04'];
70        $tosubject = $this->sfMakeSubject($tmp_subject);
71       
72        $objSendMail->setItem('', $tosubject, $body, $from_address, $from_name, $reply_to, $error, $error);
73        $objSendMail->setTo($to, $to_name);
74        $objSendMail->sendMail();    // メール送信
75    }
76
77    /* 受注完了メール送信 */
78    function sfSendOrderMail($order_id, $template_id, $subject = "", $header = "", $footer = "", $send = true) {
79
80        $arrTplVar = new stdClass();
81        $objSiteInfo = new SC_SiteInfo();
82        $arrInfo = $objSiteInfo->data;
83        $arrTplVar->arrInfo = $arrInfo;
84
85        $objQuery = new SC_Query();
86
87        if($subject == "" && $header == "" && $footer == "") {
88            // メールテンプレート情報の取得
89            $where = "template_id = ?";
90            $arrRet = $objQuery->select("subject, header, footer", "dtb_mailtemplate", $where, array($template_id));
91            $arrTplVar->tpl_header = $arrRet[0]['header'];
92            $arrTplVar->tpl_footer = $arrRet[0]['footer'];
93            $tmp_subject = $arrRet[0]['subject'];
94        } else {
95            $arrTplVar->tpl_header = $header;
96            $arrTplVar->tpl_footer = $footer;
97            $tmp_subject = $subject;
98        }
99
100        // 受注情報の取得
101        $where = "order_id = ?";
102        $arrRet = $objQuery->select("*", "dtb_order", $where, array($order_id));
103        $arrOrder = $arrRet[0];
104        $objQuery->setOrder('order_detail_id');
105        $arrTplVar->arrOrderDetail = $objQuery->select("*", "dtb_order_detail", $where, array($order_id));
106
107        $objProduct = new SC_Product();
108        $objQuery->setOrder('shipping_id');
109        $arrRet = $objQuery->select("*", "dtb_shipping", "order_id = ?", array($order_id));
110        foreach (array_keys($arrRet) as $key) {
111            $objQuery->setOrder('shipping_id');
112            $arrItems = $objQuery->select("*", "dtb_shipment_item", "order_id = ? AND shipping_id = ?",
113                                          array($order_id, $arrRet[$key]['shipping_id']));
114            foreach ($arrItems as $itemKey => $arrDetail) {
115                foreach ($arrDetail as $detailKey => $detailVal) {
116                    $arrRet[$key]['shipment_item'][$arrDetail['product_class_id']][$detailKey] = $detailVal;
117                }
118
119                $arrRet[$key]['shipment_item'][$arrDetail['product_class_id']]['productsClass'] =& $objProduct->getDetailAndProductsClass($arrDetail['product_class_id']);
120            }
121        }
122        $arrTplVar->arrShipping = $arrRet;
123
124        $arrTplVar->Message_tmp = $arrOrder['message'];
125
126        // 顧客情報の取得
127        $customer_id = $arrOrder['customer_id'];
128        $objQuery->setOrder('customer_id');
129        $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
130        $arrCustomer = isset($arrRet[0]) ? $arrRet[0] : "";
131
132        $arrTplVar->arrCustomer = $arrCustomer;
133        $arrTplVar->arrOrder = $arrOrder;
134
135        //その他決済情報
136        if($arrOrder['memo02'] != "") {
137            $arrOther = unserialize($arrOrder['memo02']);
138
139            foreach($arrOther as $other_key => $other_val){
140                if(SC_Utils_Ex::sfTrim($other_val["value"]) == ""){
141                    $arrOther[$other_key]["value"] = "";
142                }
143            }
144
145            $arrTplVar->arrOther = $arrOther;
146        }
147
148        // 都道府県変換
149        $arrTplVar->arrPref = $this->arrPref;
150
151        $objCustomer = new SC_Customer();
152        $arrTplVar->tpl_user_point = $objCustomer->getValue('point');
153
154       if(Net_UserAgent_Mobile::isMobile() === true) {
155            $objMailView = new SC_MobileView_Ex();
156       } else {
157            $objMailView = new SC_SiteView_Ex();
158       }
159        // メール本文の取得
160        $objMailView->assignobj($arrTplVar);
161        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
162
163        // メール送信処理
164        $objSendMail = new SC_SendMail_Ex();
165        $bcc = $arrInfo['email01'];
166        $from = $arrInfo['email03'];
167        $error = $arrInfo['email04'];
168        $tosubject = $this->sfMakeSubject($tmp_subject);
169
170        $objSendMail->setItem('', $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
171        $objSendMail->setTo($arrOrder["order_email"], $arrOrder["order_name01"] . " ". $arrOrder["order_name02"] ." 様");
172
173
174        // 送信フラグ:trueの場合は、送信する。
175        if($send) {
176            if ($objSendMail->sendMail()) {
177                $this->sfSaveMailHistory($order_id, $template_id, $tosubject, $body);
178            }
179        }
180
181        return $objSendMail;
182    }
183
184    // テンプレートを使用したメールの送信
185    function sfSendTplMail($to, $tmp_subject, $tplpath, &$objPage) {
186        $objMailView = new SC_SiteView_Ex();
187        $objSiteInfo = new SC_SiteInfo();
188        $arrInfo = $objSiteInfo->data;
189        // メール本文の取得
190        $objPage->tpl_shopname=$arrInfo['shop_name'];
191        $objPage->tpl_infoemail = $arrInfo['email02'];
192        $objMailView->assignobj($objPage);
193        $body = $objMailView->fetch($tplpath);
194        // メール送信処理
195        $objSendMail = new SC_SendMail_Ex();
196        $bcc = $arrInfo['email01'];
197        $from = $arrInfo['email03'];
198        $error = $arrInfo['email04'];
199        $tosubject = $this->sfMakeSubject($tmp_subject);
200       
201        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
202        $objSendMail->sendMail();
203    }
204
205    // 通常のメール送信
206    function sfSendMail($to, $tmp_subject, $body) {
207        $objSiteInfo = new SC_SiteInfo();
208        $arrInfo = $objSiteInfo->data;
209        // メール送信処理
210        $objSendMail = new SC_SendMail_Ex();
211        $bcc = $arrInfo['email01'];
212        $from = $arrInfo['email03'];
213        $error = $arrInfo['email04'];
214        $tosubject = $this->sfMakeSubject($tmp_subject);
215       
216        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
217        $objSendMail->sendMail();
218    }
219
220    //件名にテンプレートを用いる
221    function sfMakeSubject($subject) {
222        $objQuery = new SC_Query();
223        $objMailView = new SC_SiteView_Ex();
224        $objTplAssign = new stdClass;
225       
226        $arrInfo = $objQuery->select("*","dtb_baseinfo");
227        $arrInfo = $arrInfo[0];
228        $objTplAssign->tpl_shopname=$arrInfo['shop_name'];
229        $objTplAssign->tpl_infoemail=$subject; // 従来互換
230        $objTplAssign->tpl_mailtitle=$subject;
231        $objMailView->assignobj($objTplAssign);
232        $subject = $objMailView->fetch('mail_templates/mail_title.tpl');
233        return $subject;
234    }
235
236    // メール配信履歴への登録
237    function sfSaveMailHistory($order_id, $template_id, $subject, $body) {
238        $sqlval['subject'] = $subject;
239        $sqlval['order_id'] = $order_id;
240        $sqlval['template_id'] = $template_id;
241        $sqlval['send_date'] = "Now()";
242        if (!isset($_SESSION['member_id'])) $_SESSION['member_id'] = "";
243        if($_SESSION['member_id'] != "") {
244            $sqlval['creator_id'] = $_SESSION['member_id'];
245        } else {
246            $sqlval['creator_id'] = '0';
247        }
248        $sqlval['mail_body'] = $body;
249
250        $objQuery = new SC_Query();
251        $sqlval['send_id'] = $objQuery->nextVal("dtb_mail_history_send_id");
252        $objQuery->insert("dtb_mail_history", $sqlval);
253    }
254
255    /* 会員登録があるかどうかのチェック(仮会員を含まない) */
256    function sfCheckCustomerMailMaga($email) {
257        $col = "email, mailmaga_flg, customer_id";
258        $from = "dtb_customer";
259        $where = "(email = ? OR email_mobile = ?) AND status = 2 AND del_flg = 0";
260        $objQuery = new SC_Query();
261        $arrRet = $objQuery->select($col, $from, $where, array($email));
262        // 会員のメールアドレスが登録されている
263        if(!empty($arrRet[0]['customer_id'])) {
264            return true;
265        }
266        return false;
267    }
268
269    /**
270     * 登録メールを送信する。
271     *
272     * @param string $secret_key 顧客固有キー
273     * @param integer $customer_id 顧客ID
274     * @param boolean $is_mobile false(default):PCアドレスにメールを送る true:携帯アドレスにメールを送る
275     * @return boolean true:成功 false:失敗
276     */
277    function sfSendRegistMail($secret_key, $customer_id = '', $is_mobile = false) {
278        // 顧客データの取得
279        if(SC_Utils_Ex::sfIsInt($customer_id)) {
280            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId($customer_id);
281        }else{
282            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId('', "secret_key = ?", array($secret_key));
283        }
284        if(SC_Utils_Ex::isBlank($arrCustomerData)) {
285            return false;
286        }
287
288        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
289       
290        $objMailText = new SC_SiteView_Ex();
291        $objMailText->assign("CONF", $CONF);
292        $objMailText->assign("name01", $arrCustomerData['name01']);
293        $objMailText->assign("name02", $arrCustomerData['name02']);
294        $objMailText->assign("uniqid", $arrCustomerData['secret_key']);
295        $objMailText->assignobj($arrCustomerData);
296        $objMailText->assignobj($this);
297
298        $objHelperMail  = new SC_Helper_Mail_Ex();
299
300        // 仮会員が有効の場合
301        if(CUSTOMER_CONFIRM_MAIL == true and $arrCustomerData['status'] == 1) {
302            $subject        = $objHelperMail->sfMakeSubject('会員登録のご確認');
303            $toCustomerMail = $objMailText->fetch("mail_templates/customer_mail.tpl");
304        } else {
305            $subject        = $objHelperMail->sfMakeSubject('会員登録のご完了');
306            $toCustomerMail = $objMailText->fetch("mail_templates/customer_regist_mail.tpl");
307        }
308
309        $objMail = new SC_SendMail();
310        $objMail->setItem(
311            ''                    // 宛先
312            , $subject              // サブジェクト
313            , $toCustomerMail       // 本文
314            , $CONF["email03"]      // 配送元アドレス
315            , $CONF["shop_name"]    // 配送元 名前
316            , $CONF["email03"]      // reply_to
317            , $CONF["email04"]      // return_path
318            , $CONF["email04"]      // Errors_to
319            , $CONF["email01"]      // Bcc
320        );
321        // 宛先の設定
322        if($is_mobile) {
323            $to_addr = $arrCustomerData["email_mobile"];
324        }else{
325            $to_addr = $arrCustomerData["email"];
326        }
327        $objMail->setTo($to_addr, $arrCustomerData["name01"] . $arrCustomerData["name02"] ." 様");
328
329        $objMail->sendMail();
330        return true;
331    }
332   
333    /**
334     * 保存されているメルマガテンプレートの取得
335     * @param integer 特定IDのテンプレートを取り出したい時はtemplate_idを指定。未指定時は全件取得
336     * @return array メールテンプレート情報を格納した配列
337     * @todo   表示順も引数で変更できるように
338     */
339    function sfGetMailmagaTemplate($template_id = null){
340        // 初期化
341        $where = '';
342        $objQuery =& SC_Query::getSingletonInstance();
343       
344        // 条件文
345        $where = 'del_flg = ?';
346        $arrValues[] = 0;
347        //template_id指定時
348        if (SC_Utils_Ex::sfIsInt($template_id) === true) {
349            $where .= 'AND template_id = ?';
350            $arrValues[] = $template_id;
351        }
352       
353        // 表示順
354        $objQuery->setOrder("create_date DESC");
355       
356        $arrResults = $objQuery->select('*', 'dtb_mailmaga_template', $where, $arrValues);
357        return $arrResults;
358    }
359   
360    /**
361     * 保存されているメルマガ送信履歴の取得
362     * @param integer 特定の送信履歴を取り出したい時はsend_idを指定。未指定時は全件取得
363     * @return array 送信履歴情報を格納した配列
364     */
365    function sfGetSendHistory($send_id = null){
366        // 初期化
367        $where = '';
368        $objQuery =& SC_Query::getSingletonInstance();
369       
370        // 条件文
371        $where = 'del_flg = ?';
372        $arrValues[] = 0;
373       
374        //send_id指定時
375        if (SC_Utils_Ex::sfIsInt($send_id) === true) {
376            $where .= 'AND send_id = ?';
377            $arrValues[] = $send_id;
378        }
379       
380        // 表示順
381        $objQuery->setOrder("create_date DESC");
382       
383        $arrResults = $objQuery->select('*', 'dtb_send_history', $where, $arrValues);
384        return $arrResults;
385    }
386
387    /**
388     * 指定したIDのメルマガ配送を行う
389     *
390     * @param integer $send_id dtb_send_history の情報
391     * @return void
392     */
393    function sfSendMailmagazine($send_id) {
394        $objQuery =& SC_Query::getSingletonInstance();
395        $objDb = new SC_Helper_DB_Ex();
396        $objSite = $objDb->sfGetBasisData();
397        $objMail = new SC_SendMail_Ex();
398       
399        $where = 'del_flg = 0 AND send_id = ?';
400        $arrMail = $objQuery->getRow('*', 'dtb_send_history', $where, array($send_id));
401
402        // 対象となる$send_idが見つからない
403        if (SC_Utils_Ex::isBlank($arrMail)) return;
404
405        // 送信先リストの取得
406        $arrDestinationList = $objQuery->select(
407            '*',
408            'dtb_send_customer',
409            'send_id = ? AND (send_flag = 2 OR send_flag IS NULL)',
410            array($send_id)
411        );
412       
413        // 現在の配信数
414        $complete_count = $arrMail['complete_count'];
415        if(SC_Utils_Ex::isBlank($arrMail)) $complete_count = 0;
416       
417        foreach ($arrDestinationList as $arrDestination) {
418
419            // 顧客名の変換
420            $customerName = trim($arrDestination["name"]);
421            $subjectBody = preg_replace("/{name}/", $customerName, $arrMail["subject"]);
422            $mailBody = preg_replace("/{name}/", $customerName, $arrMail["body"]);
423
424            $objMail->setItem(
425                $arrDestination["email"],
426                $subjectBody,
427                $mailBody,
428                $objSite["email03"],      // 送信元メールアドレス
429                $objSite["shop_name"],    // 送信元名
430                $objSite["email03"],      // reply_to
431                $objSite["email04"],      // return_path
432                $objSite["email04"]       // errors_to
433            );
434           
435            // テキストメール配信の場合
436            if ($arrMail["mail_method"] == 2) {
437                $sendResut = $objMail->sendMail();
438            // HTMLメール配信の場合
439            } else {
440                $sendResut = $objMail->sendHtmlMail();
441            }
442
443            // 送信完了なら1、失敗なら2をメール送信結果フラグとしてDBに挿入
444            if (!$sendResut) {
445                $sendFlag = '2';
446            } else {
447                // 完了を 1 増やす
448                $sendFlag = '1';
449                $complete_count++;
450            }
451           
452            // 送信結果情報を更新
453            $objQuery->update('dtb_send_customer',
454                              array('send_flag'=>$sendFlag),
455                              'send_id = ? AND customer_id = ?',
456                              array($send_id,$arrDestination["customer_id"]));
457        }
458
459        // メール全件送信完了後の処理
460        $objQuery->update('dtb_send_history',
461                          array('end_date'=>"now()", 'complete_count'=>$complete_count),
462                          'send_id = ?',
463                          array($send_id));
464
465        // 送信完了 報告メール
466        $compSubject = date("Y年m月d日H時i分") . "  下記メールの配信が完了しました。";
467        // 管理者宛に変更
468        $objMail->setTo($objSite["email03"]);
469        $objMail->setSubject($compSubject);
470
471        // テキストメール配信の場合
472        if ($arrMail["mail_method"] == 2 ) {
473            $sendResut = $objMail->sendMail();
474        // HTMLメール配信の場合
475        } else {
476            $sendResut = $objMail->sendHtmlMail();
477        }
478        return;
479    }
480}
481?>
Note: See TracBrowser for help on using the repository browser.