userモジュールのメール配信機能で使われてるmodules/user/class/mailjob.phpですが、コンストラクタで、
- Code: Select all
$this->mGetReplaceTitle =& new XCube_Delegate();
$this->mGetReplaceTitle->register('UserMailjobObject.GetReplaceTitle');
とデリゲート宣言してあるのに、使われてません。
# 本文の方は使われてます。
下記のようにすれば、{X_UNAME}などをタイトルでも使えるようになり、タイトルのデリゲートも効くようになります
- Code: Select all
function _replace(&$string, &$to_user, &$from_user)
{
//
// TODO {X_UACTLINK}
//
$string = str_replace('{X_UID}', $to_user->get('uid'), $string);
$string = str_replace('{X_UNAME}', $to_user->get('uname'), $string);
$string = str_replace('{X_UEMAIL}', $to_user->get('email'), $string);
}
function getReplaceTitle(&$to_user, &$from_user)
{
$t_title = $this->get('title');
$this->_replace($t_title, $to_user, $from_user);
$this->mGetReplaceTitle->call(new XCube_Ref($t_title), $to_user, $from_user);
return $t_title;
}
function getReplaceBody(&$to_user, &$from_user)
{
$t_body = $this->get('body');
$this->_replace($t_body, $to_user, $from_user);
$this->mGetReplaceBody->call(new XCube_Ref($t_body), $to_user, $from_user);
return $t_body;
}
デリゲートの検証のために、下記のようなpreloadをつくって試してみました。
- Code: Select all
<?php
class RyusExmailString extends XCube_ActionFilter
{
function preBlockFilter()
{
$this->mRoot->mDelegateManager->add( 'UserMailjobObject.GetReplaceTitle' , array( $this , 'replaceTitle' ) ) ;
$this->mRoot->mDelegateManager->add( 'UserMailjobObject.GetReplaceBody' , array( $this , 'replaceBody' ) ) ;
}
function replaceTitle(& $t_title, &$to_user, &$from_user)
{
$this->_replace($t_title, $to_user, $from_user);
}
/**
*
* @param $t_body string メール本文
* @param $to_user XoopsUser 送り先
* @param $from_user XoopsUser 送信者
* @return void
*/
function replaceBody(& $t_body, &$to_user, &$from_user)
{
$this->_replace($t_body, $to_user, $from_user);
}
function _replace(& $string, &$to_user, &$from_user)
{
/*
* ここに置き換えを追加すれば、本文もタイトルでも置き換えタグを拡張できます。
*/
$to_user_name = $to_user->get('name');
$to_user_name = empty($to_user_name) ? $to_user->get('uname') : $to_user_name;
$string = str_replace('{X_NAME}', $to_user_name, $string);
$string = str_replace('{X_TODAY}', formatTimestamp(time(), 's', $to_user->get('timezone_offset')), $string);
}
}
?>
テスト時にメール配信のタイトルを
{X_NAME}さん、今日は{X_TODAY}です。
本文を
{X_NAME}さん
今日は{X_TODAY}です。
アカウントは{X_UNAME}ですね
と入力してPM送信してテストしまして、置き換えられるのをタイトル、本文ともにおきかえられるのを確認しました。
