source: branches/version-2_13-dev/data/class/helper/SC_Helper_Mail.php @ 23574

Revision 23574, 19.9 KB checked in by shutta, 10 years ago (diff)

#2598 (受注メール中の「ご注文商品明細」欄と「配送情報」欄で商品の順番が異なる)
配送情報の取得側の方にOrder句の指定が無かったのを修正。

  • 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-2014 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    public $arrMAILTPLPATH;
35
36    /**
37     * LC_Pageオブジェクト.
38     *
39     * @var LC_Page
40     */
41    protected $objPage;
42
43    /**
44     * コンストラクタ.
45     */
46    public function __construct()
47    {
48        $masterData = new SC_DB_MasterData_Ex();
49        $this->arrMAILTPLPATH =  $masterData->getMasterData('mtb_mail_tpl_path');
50        $this->arrPref = $masterData->getMasterData('mtb_pref');
51        $this->arrCountry = $masterData->getMasterData('mtb_country');
52    }
53
54    /**
55     * LC_Pageオブジェクトをセットします.
56     *
57     * @param LC_Page $objPage
58     */
59    public function setPage(LC_Page $objPage)
60    {
61        $this->objPage = $objPage;
62    }
63
64    /**
65     * LC_Pageオブジェクトを返します.
66     *
67     * @return LC_Page
68     */
69    public function getPage()
70    {
71        return $this->objPage;
72    }
73
74    /* DBに登録されたテンプレートメールの送信 */
75    public function sfSendTemplateMail($to, $to_name, $template_id, &$objPage, $from_address = '', $from_name = '', $reply_to = '', $bcc = '')
76    {
77        // メールテンプレート情報の取得
78        $objMailtemplate = new SC_Helper_Mailtemplate_Ex();
79        $mailtemplate = $objMailtemplate->get($template_id);
80        $objPage->tpl_header = $mailtemplate['header'];
81        $objPage->tpl_footer = $mailtemplate['footer'];
82        $tmp_subject = $mailtemplate['subject'];
83
84        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
85
86        $objMailView = new SC_SiteView_Ex();
87        $objMailView->setPage($this->getPage());
88        // メール本文の取得
89        $objMailView->assignobj($objPage);
90        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
91
92        // メール送信処理
93        $objSendMail = new SC_SendMail_Ex();
94        if ($from_address == '') $from_address = $arrInfo['email03'];
95        if ($from_name == '') $from_name = $arrInfo['shop_name'];
96        if ($reply_to == '') $reply_to = $arrInfo['email03'];
97        $error = $arrInfo['email04'];
98        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
99
100        $objSendMail->setItem('', $tosubject, $body, $from_address, $from_name, $reply_to, $error, $error, $bcc);
101        $objSendMail->setTo($to, $to_name);
102        $objSendMail->sendMail();    // メール送信
103    }
104
105    /* 注文受付メール送信 */
106    public function sfSendOrderMail($order_id, $template_id, $subject = '', $header = '', $footer = '', $send = true)
107    {
108        $arrTplVar = new stdClass();
109        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
110        $arrTplVar->arrInfo = $arrInfo;
111
112        $objQuery =& SC_Query_Ex::getSingletonInstance();
113
114        if ($subject == '' && $header == '' && $footer == '') {
115            // メールテンプレート情報の取得
116            $objMailtemplate = new SC_Helper_Mailtemplate_Ex();
117            $mailtemplate = $objMailtemplate->get($template_id);
118            $arrTplVar->tpl_header = $mailtemplate['header'];
119            $arrTplVar->tpl_footer = $mailtemplate['footer'];
120            $tmp_subject = $mailtemplate['subject'];
121        } else {
122            $arrTplVar->tpl_header = $header;
123            $arrTplVar->tpl_footer = $footer;
124            $tmp_subject = $subject;
125        }
126
127        // 受注情報の取得
128        $where = 'order_id = ? AND del_flg = 0';
129        $arrOrder = $objQuery->getRow('*', 'dtb_order', $where, array($order_id));
130
131        if (empty($arrOrder)) {
132            trigger_error("該当する受注が存在しない。(注文番号: $order_id)", E_USER_ERROR);
133        }
134
135        $where = 'order_id = ?';
136        $objQuery->setOrder('order_detail_id');
137        $arrTplVar->arrOrderDetail = $objQuery->select('*', 'dtb_order_detail', $where, array($order_id));
138
139        // 配送情報の取得
140        $arrTplVar->arrShipping = $this->sfGetShippingData($order_id);
141
142        $arrTplVar->Message_tmp = $arrOrder['message'];
143
144        // 会員情報の取得
145        $customer_id = $arrOrder['customer_id'];
146        $objQuery->setOrder('customer_id');
147        $arrRet = $objQuery->select('point', 'dtb_customer', 'customer_id = ?', array($customer_id));
148        $arrCustomer = isset($arrRet[0]) ? $arrRet[0] : '';
149
150        $arrTplVar->arrCustomer = $arrCustomer;
151        $arrTplVar->arrOrder = $arrOrder;
152
153        //その他決済情報
154        if ($arrOrder['memo02'] != '') {
155            $arrOther = unserialize($arrOrder['memo02']);
156
157            foreach ($arrOther as $other_key => $other_val) {
158                if (SC_Utils_Ex::sfTrim($other_val['value']) == '') {
159                    $arrOther[$other_key]['value'] = '';
160                }
161            }
162
163            $arrTplVar->arrOther = $arrOther;
164        }
165
166        // 都道府県変換
167        $arrTplVar->arrPref = $this->arrPref;
168        // 国変換
169        $arrTplVar->arrCountry = $this->arrCountry;
170
171        $objCustomer = new SC_Customer_Ex();
172        $arrTplVar->tpl_user_point = $objCustomer->getValue('point');
173
174        $objMailView = null;
175        // 注文受付メール(携帯)
176        if ($template_id == 2) {
177            $objMailView = new SC_SiteView_Ex(true, DEVICE_TYPE_MOBILE);
178        } else {
179            $objMailView = new SC_SiteView_Ex();
180        }
181        // メール本文の取得
182        $objMailView->setPage($this->getPage());
183        $objMailView->assignobj($arrTplVar);
184        $body = $objMailView->fetch($this->arrMAILTPLPATH[$template_id]);
185
186        // メール送信処理
187        $objSendMail = new SC_SendMail_Ex();
188        $bcc = $arrInfo['email01'];
189        $from = $arrInfo['email03'];
190        $error = $arrInfo['email04'];
191        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
192
193        $objSendMail->setItem('', $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
194        $objSendMail->setTo($arrOrder['order_email'], $arrOrder['order_name01'] . ' '. $arrOrder['order_name02'] .' 様');
195
196        // 送信フラグ:trueの場合は、送信する。
197        if ($send) {
198            if ($objSendMail->sendMail()) {
199                $this->sfSaveMailHistory($order_id, $template_id, $tosubject, $body);
200            }
201        }
202
203        return $objSendMail;
204    }
205
206    /**
207     * 配送情報の取得
208     *
209     * @param integer $order_id 受注ID
210     * @return array 配送情報を格納した配列
211     */
212    function sfGetShippingData($order_id)
213    {
214        $objQuery =& SC_Query_Ex::getSingletonInstance();
215
216        $objQuery->setOrder('shipping_id');
217        $arrRet = $objQuery->select('*', 'dtb_shipping', 'order_id = ?', array($order_id));
218        foreach ($arrRet as $key => $value) {
219            $col = 's_i.*, tax_rate, tax_rule';
220            $from = 'dtb_shipment_item AS s_i JOIN dtb_order_detail AS o_d
221                USING(order_id, product_class_id)';
222            $where = 'order_id = ? AND shipping_id = ?';
223            $arrWhereVal = array($order_id, $arrRet[$key]['shipping_id']);
224            $objQuery->setOrder('order_detail_id');
225            $arrItems = $objQuery->select($col, $from, $where, $arrWhereVal);
226            $arrRet[$key]['shipment_item'] = $arrItems;
227        }
228
229        return $arrRet;
230    }
231
232    // テンプレートを使用したメールの送信
233    public function sfSendTplMail($to, $tmp_subject, $tplpath, &$objPage)
234    {
235        $objMailView = new SC_SiteView_Ex();
236        $objMailView->setPage($this->getPage());
237        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
238        // メール本文の取得
239        $objPage->tpl_shopname=$arrInfo['shop_name'];
240        $objPage->tpl_infoemail = $arrInfo['email02'];
241        $objMailView->assignobj($objPage);
242        $body = $objMailView->fetch($tplpath);
243        // メール送信処理
244        $objSendMail = new SC_SendMail_Ex();
245        $bcc = $arrInfo['email01'];
246        $from = $arrInfo['email03'];
247        $error = $arrInfo['email04'];
248        $tosubject = $this->sfMakeSubject($tmp_subject, $objMailView);
249
250        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
251        $objSendMail->sendMail();
252    }
253
254    // 通常のメール送信
255    public function sfSendMail($to, $tmp_subject, $body)
256    {
257        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
258        // メール送信処理
259        $objSendMail = new SC_SendMail_Ex();
260        $bcc = $arrInfo['email01'];
261        $from = $arrInfo['email03'];
262        $error = $arrInfo['email04'];
263        $tosubject = $this->sfMakeSubject($tmp_subject);
264
265        $objSendMail->setItem($to, $tosubject, $body, $from, $arrInfo['shop_name'], $from, $error, $error, $bcc);
266        $objSendMail->sendMail();
267    }
268
269    //件名にテンプレートを用いる
270    public function sfMakeSubject($subject, &$objMailView = NULL)
271    {
272        if (empty($objMailView)) {
273            $objMailView = new SC_SiteView_Ex();
274            $objMailView->setPage($this->getPage());
275        }
276        $objTplAssign = new stdClass;
277
278        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
279        $objTplAssign->tpl_shopname=$arrInfo['shop_name'];
280        $objTplAssign->tpl_infoemail=$subject; // 従来互換
281        $objTplAssign->tpl_mailtitle=$subject;
282        $objMailView->assignobj($objTplAssign);
283        $subject = $objMailView->fetch('mail_templates/mail_title.tpl');
284        // #1940 (SC_Helper_Mail#sfMakeSubject 先頭に改行を含む値を返す) 対応
285        $subject = trim($subject);
286
287        return $subject;
288    }
289
290    // メール配信履歴への登録
291    public function sfSaveMailHistory($order_id, $template_id, $subject, $body)
292    {
293        $sqlval = array();
294        $sqlval['subject'] = $subject;
295        $sqlval['order_id'] = $order_id;
296        $sqlval['template_id'] = $template_id;
297        $sqlval['send_date'] = 'CURRENT_TIMESTAMP';
298        if (!isset($_SESSION['member_id'])) $_SESSION['member_id'] = '';
299        if ($_SESSION['member_id'] != '') {
300            $sqlval['creator_id'] = $_SESSION['member_id'];
301        } else {
302            $sqlval['creator_id'] = '0';
303        }
304        $sqlval['mail_body'] = $body;
305
306        $objQuery =& SC_Query_Ex::getSingletonInstance();
307        $sqlval['send_id'] = $objQuery->nextVal('dtb_mail_history_send_id');
308        $objQuery->insert('dtb_mail_history', $sqlval);
309    }
310
311    /* 会員登録があるかどうかのチェック(仮会員を含まない) */
312    public function sfCheckCustomerMailMaga($email)
313    {
314        $col = 'email, mailmaga_flg, customer_id';
315        $from = 'dtb_customer';
316        $where = '(email = ? OR email_mobile = ?) AND status = 2 AND del_flg = 0';
317        $objQuery =& SC_Query_Ex::getSingletonInstance();
318        $arrRet = $objQuery->select($col, $from, $where, array($email));
319        // 会員のメールアドレスが登録されている
320        if (!empty($arrRet[0]['customer_id'])) {
321            return true;
322        }
323
324        return false;
325    }
326
327    /**
328     * 登録メールを送信する。
329     *
330     * @param  string  $secret_key  会員固有キー
331     * @param  integer $customer_id 会員ID
332     * @param  boolean $is_mobile   false(default):PCアドレスにメールを送る true:携帯アドレスにメールを送る
333     * @param $resend_flg true  仮登録メール再送
334     * @return boolean true:成功 false:失敗
335     * 
336     */
337    public function sfSendRegistMail($secret_key, $customer_id = '', $is_mobile = false, $resend_flg = false)
338    {
339        // 会員データの取得
340        if (SC_Utils_Ex::sfIsInt($customer_id)) {
341            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId($customer_id);
342        } else {
343            $arrCustomerData = SC_Helper_Customer_Ex::sfGetCustomerDataFromId('', 'secret_key = ?', array($secret_key));
344        }
345        if (SC_Utils_Ex::isBlank($arrCustomerData)) {
346            return false;
347        }
348
349        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
350
351        $objMailText = new SC_SiteView_Ex();
352        $objMailText->setPage($this->getPage());
353        $objMailText->assign('CONF', $CONF);
354        $objMailText->assign('name01', $arrCustomerData['name01']);
355        $objMailText->assign('name02', $arrCustomerData['name02']);
356        $objMailText->assign('uniqid', $arrCustomerData['secret_key']);
357        $objMailText->assignobj($arrCustomerData);
358        $objMailText->assignobj($this);
359
360        $objHelperMail  = new SC_Helper_Mail_Ex();
361        // 仮会員が有効の場合   
362        if (CUSTOMER_CONFIRM_MAIL == true and $arrCustomerData['status'] == 1 or $arrCustomerData['status'] == 1 and $resend_flg == true) {
363            $subject        = $objHelperMail->sfMakeSubject('会員登録のご確認', $objMailText);
364            $toCustomerMail = $objMailText->fetch('mail_templates/customer_mail.tpl');
365        } else {
366            $subject        = $objHelperMail->sfMakeSubject('会員登録のご完了', $objMailText);
367            $toCustomerMail = $objMailText->fetch('mail_templates/customer_regist_mail.tpl');
368           
369        }
370
371        $objMail = new SC_SendMail_Ex();
372        $objMail->setItem(
373            '',                     // 宛先
374            $subject,               // サブジェクト
375            $toCustomerMail,        // 本文
376            $CONF['email03'],       // 配送元アドレス
377            $CONF['shop_name'],     // 配送元 名前
378            $CONF['email03'],       // reply_to
379            $CONF['email04'],       // return_path
380            $CONF['email04'],       // Errors_to
381            $CONF['email01']        // Bcc
382        );
383        // 宛先の設定
384        if ($is_mobile) {
385            $to_addr = $arrCustomerData['email_mobile'];
386        } else {
387            $to_addr = $arrCustomerData['email'];
388        }
389        $objMail->setTo($to_addr, $arrCustomerData['name01'] . $arrCustomerData['name02'] .' 様');
390
391        $objMail->sendMail();
392
393        return true;
394    }
395
396    /**
397     * 保存されているメルマガテンプレートの取得
398     * @param integer 特定IDのテンプレートを取り出したい時はtemplate_idを指定。未指定時は全件取得
399     * @return array メールテンプレート情報を格納した配列
400     * @todo   表示順も引数で変更できるように
401     */
402    public function sfGetMailmagaTemplate($template_id = null)
403    {
404        // 初期化
405        $where = '';
406        $objQuery =& SC_Query_Ex::getSingletonInstance();
407
408        // 条件文
409        $where = 'del_flg = ?';
410        $arrValues[] = 0;
411        //template_id指定時
412        if (SC_Utils_Ex::sfIsInt($template_id) === true) {
413            $where .= ' AND template_id = ?';
414            $arrValues[] = $template_id;
415        }
416
417        // 表示順
418        $objQuery->setOrder('create_date DESC');
419
420        $arrResults = $objQuery->select('*', 'dtb_mailmaga_template', $where, $arrValues);
421
422        return $arrResults;
423    }
424
425    /**
426     * 保存されているメルマガ送信履歴の取得
427     * @param integer 特定の送信履歴を取り出したい時はsend_idを指定。未指定時は全件取得
428     * @return array 送信履歴情報を格納した配列
429     */
430    public function sfGetSendHistory($send_id = null)
431    {
432        // 初期化
433        $where = '';
434        $objQuery =& SC_Query_Ex::getSingletonInstance();
435
436        // 条件文
437        $where = 'del_flg = ?';
438        $arrValues[] = 0;
439
440        //send_id指定時
441        if (SC_Utils_Ex::sfIsInt($send_id) === true) {
442            $where .= ' AND send_id = ?';
443            $arrValues[] = $send_id;
444        }
445
446        // 表示順
447        $objQuery->setOrder('create_date DESC');
448
449        $arrResults = $objQuery->select('*', 'dtb_send_history', $where, $arrValues);
450
451        return $arrResults;
452    }
453
454    /**
455     * 指定したIDのメルマガ配送を行う
456     *
457     * @param integer $send_id dtb_send_history の情報
458     * @return void
459     */
460    public function sfSendMailmagazine($send_id)
461    {
462        $objQuery =& SC_Query_Ex::getSingletonInstance();
463        $objDb = new SC_Helper_DB_Ex();
464        $objSite = $objDb->sfGetBasisData();
465        $objMail = new SC_SendMail_Ex();
466
467        $where = 'del_flg = 0 AND send_id = ?';
468        $arrMail = $objQuery->getRow('*', 'dtb_send_history', $where, array($send_id));
469
470        // 対象となる$send_idが見つからない
471        if (SC_Utils_Ex::isBlank($arrMail)) return;
472
473        // 送信先リストの取得
474        $arrDestinationList = $objQuery->select(
475            '*',
476            'dtb_send_customer',
477            'send_id = ? AND (send_flag = 2 OR send_flag IS NULL)',
478            array($send_id)
479        );
480
481        // 現在の配信数
482        $complete_count = $arrMail['complete_count'];
483        if (SC_Utils_Ex::isBlank($arrMail)) {
484            $complete_count = 0;
485        }
486
487        foreach ($arrDestinationList as $arrDestination) {
488            // お名前の変換
489            $customerName = trim($arrDestination['name']);
490            $subjectBody = preg_replace('/{name}/', $customerName, $arrMail['subject']);
491            $mailBody = preg_replace('/{name}/', $customerName, $arrMail['body']);
492
493            $objMail->setItem(
494                $arrDestination['email'],
495                $subjectBody,
496                $mailBody,
497                $objSite['email03'],      // 送信元メールアドレス
498                $objSite['shop_name'],    // 送信元名
499                $objSite['email03'],      // reply_to
500                $objSite['email04'],      // return_path
501                $objSite['email04']       // errors_to
502            );
503
504            // テキストメール配信の場合
505            if ($arrMail['mail_method'] == 2) {
506                $sendResut = $objMail->sendMail();
507            // HTMLメール配信の場合
508            } else {
509                $sendResut = $objMail->sendHtmlMail();
510            }
511
512            // 送信完了なら1、失敗なら2をメール送信結果フラグとしてDBに挿入
513            if (!$sendResut) {
514                $sendFlag = '2';
515            } else {
516                // 完了を 1 増やす
517                $sendFlag = '1';
518                $complete_count++;
519            }
520
521            // 送信結果情報を更新
522            $objQuery->update('dtb_send_customer',
523                              array('send_flag'=>$sendFlag),
524                              'send_id = ? AND customer_id = ?',
525                              array($send_id,$arrDestination['customer_id']));
526        }
527
528        // メール全件送信完了後の処理
529        $objQuery->update('dtb_send_history',
530                          array('end_date'=>'CURRENT_TIMESTAMP', 'complete_count'=>$complete_count),
531                          'send_id = ?',
532                          array($send_id));
533
534        // 送信完了 報告メール
535        $compSubject = date('Y年m月d日H時i分') . '  下記メールの配信が完了しました。';
536        // 管理者宛に変更
537        $objMail->setTo($objSite['email03']);
538        $objMail->setSubject($compSubject);
539
540        // テキストメール配信の場合
541        if ($arrMail['mail_method'] == 2) {
542            $sendResut = $objMail->sendMail();
543        // HTMLメール配信の場合
544        } else {
545            $sendResut = $objMail->sendHtmlMail();
546        }
547
548        return;
549    }
550}
Note: See TracBrowser for help on using the repository browser.