source: branches/version-2_12-dev/data/class/helper/SC_Helper_Mail.php @ 22567

Revision 22567, 19.4 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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