twitter sns クチコミ comments neta

2007/10/16 22:07:19

招待機能を実装してみて挫折する

PDTが起動しなくなったので、環境をもどしたりいろいろしてたら時間ロスしてやる気ダウンしまくりんぐでした。
今回は招待の機能を実装してみます。
勢いで書いてるので、ミスあるかもしれませんが。。。

ただ、CakePHPのに沿ってやると何かかっこ悪い気がするのでちょっとだけ招待用のURLを変えてみたりしました。
相変わらず自信ありません。

全部を一つの記事でやると死ねるので、とりあえず招待メールを送信するまでを。
微妙にイケて無い気がするんですが、とりあえずrhacosnsクラスにuserInviteメソッドを追加しました

    function userInvite(){
        if(!RequestLogin::isLogin()){
            Header::redirect(Rhaco::page('login'));
        }
        $user = RequestLogin::getLoginSession();
        if($this->isPost()){
            $email = $this->getVariable('email');
            $message = $this->getVariable('message');
            $friend_user = $this->dbUtil->get(new User(), new C(Q::eq(User::columnEmail(), $email)));
            if(is_null($friend_user)){
                $invite = $this->dbUtil->insert($this->toObject(new UserInvite()));
                if(Variable::istype("TableObjectBase", $invite)){
                    $mail_body = "hello, " . htmlspecialchars($user->getName(), ENT_QUOTES) . " wants you!\n";
                    if($message){
                        $mail_body .= "\nmessage:\n" . $message . "\n\n";
                    }
                    $mail_body .= "click a link below to join in!\n\n";
                    $mail_body .= Rhaco::page('user') . '/join/' . $invite->getHash();
                    if(mail($email, sprintf('[%s] Invitation', Rhaco::constant('SITE_TITLE')), $mail_body))
                        Header::redirect(Rhaco::url());
                }
            } else {
                if($friend_user->getEmail() == $user->getEmail()) Header::redirect(Rhaco::url());
                $friend = new Friend();
                $friend->setFriendUserId($friend_user->getId());
                $this->dbUtil->insert($friend);
                $mail_body = sprintf("hello %s, %s wants to join your network. click a link below to accept him!\n", $friend_user->getName(), $user->getName());
                if($message){
                    $mail_body .= "\nmessage:\n" . $message . "\n\n";
                }
                $mail_body .= Rhaco::page('friend') . '/accept/' . $user->getId();
                mail($friend_user->getEmail(), sprintf("[%s] Invitation", Rhaco::constant('SITE_TITLE')), $mail_body);
                Header::redirect(Rhaco::url());
            }
        }
        return $this->read(new UserInvite(), new C(Q::eq(UserInvite::columnUserId(), $user->getId())));
    }

はい。
まだ実装していないfriend/acceptなんてものも登場しますが、とりあえず、ユーザーが存在しない場合と存在する場合について条件を分けてみました。
rhacoにはメール周りのライブラリが無いので直接mailを叩いていますが、日本語とか入れられるとたぶんこけますこれ。
今回は面倒なのでこのままにさせてください><
あとで修正しますから><!!

そして、テンプレートをこんな感じで用意しました。
user_invite.html
<rt:extends href="./index.html" />

<rt:block name="pan">
	<li><a href="{$rhaco.url()}">{$object.name}</a></li>
	<li>invite</li>
</rt:block>

<rt:block name="menu">
	<li id="MENU01"><a href="{$rhaco.url()}">index</a></li>
	<li id="MENU02"><a href="{$rhaco.page('diary')}">Diary</a></li>
	<li id="MENU03" class="menu-on"><a href="{$rhaco.url()}">Friends</a></li>
	<li id="MENU04"><a
		href="{$rhaco.page('setting')}">Settings</a></li>
	<li id="MENU05"><a href="{$rhaco.url()}">Logout</a></li>
</rt:block>

<rt:block name="content">
	<h2>invite</h2>
	<div class="text">
	<h3>new user</h3>
	<rt:invalid name="exceptions" errors="errors">
		<ul class="exception">
			<rt:loop param="errors" var="msg">
				<li>{$msg}</li>
			</rt:loop>
		</ul>
	</rt:invalid>
	<form action="{$rhaco.page('user')}/invite" method="post">
	<p><label for="email">email</label><input name="email" type="text"
		size="30" reference="true" /></p>
    <p><label for="message">message</label><textarea name="message"></textarea></p>
	<p><input type="submit" value="Send" /></p>
	</form>
	<ul class="modori">
		<li><a href="#PAGETOP">TOP</a></li>
	</ul>

    <h3>history</h3>
	</div>
</rt:block>

これもまた実装途中でごめんなさい。
今までに送った招待メールの履歴も確認できるようにしようとした痕跡がありますねw

このあたりからもうほとんど自信が無くなってきてます。

上の他に、変更のあったファイルは何があったか良く覚えてません。。。;
とりあえず、model.FriendにbeforeInsertを定義します。
    function beforeInsert(){
        if(is_null($this->getUserId())){
            $user = RequestLogin::getLoginSession();
            $this->setUserId($user->getId());
        }
    }

何かかっこ悪い気もします。。。

次回はたぶんメールを受け取ったあとの処理を書いていきます…!

  • Add Comment

    name:

    comment:

    question:
    1 + 1 = ?
    answer: