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

Revision 21935, 19.3 KB checked in by pineray, 12 years ago (diff)

#1859
foreach でキーしか使わない場合に array_keys で配列を作ってから渡す方法だと、
メモリを倍ほど消費することがわかったので、見苦しいけれど不要なバリューを生成する方向に.
F*in' PHP!

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