Sunday, May 29, 2011

Tip 4: Modifying Email Notification on Incoming Comment of an Article



Comment is one of the important elements in an CMS. Comments show the visitors who read someone’s article on a web page. In addition, the comments also enable the interaction between visitors and writers or other visitors.

A good CMS will notify the author if there is a writing which is commented on by visitors. Notification can be executed via email or user's personal menu. Tiki, through the Tiki User Watch feature, will notify the author via email, when there is a visitor who gives a comment on posts made by a Tiki user.

However, there is one weakness in the feature of users watch. If there are 5 users using this feature, then all the fifth users will also be notified via email. This is due to the collaborative nature of Tiki, meaning together. Written works are created by multiple people together (collaboratively) rather than individually, each contributor has an equal ability to add, edit, and remove text. Hence, each contributor gets same email notifications, as well.

The following tip will tell you how to send email notification to only one user, i.e. author, if there is a comment written by visitors in an article page.
  1. Open /lib/comments/commentslib.php.
    //function to post a comment via email notification, begin
    function post_comment($threadId, $userTo, $from, $to, $objectDetail, $objectTitle, $subject, $body, $event) {
        global $smarty, $userlib, $prefs, $tikilib, $user;
        $subject = strip_tags($subject);
        $body = strip_tags($body, '<a><b><img><i>');
        $foo = parse_url($_SERVER["REQUEST_URI"]);
        $parts = explode('/', $foo['path']);
        if (count($parts) > 1) unset ($parts[count($parts) - 1]);
        $machine = $this->httpPrefix( true ). $foo["path"];
        $machine = str_replace('messu-compose', 'messu-mailbox', $machine);
            if (!isset($_SERVER["SERVER_NAME"])) {
                $_SERVER["SERVER_NAME"] = $_SERVER["HTTP_HOST"];
            }
            $email = $userlib->get_user_email($userTo);
            if ($email) {
                include_once('lib/webmail/tikimaillib.php');
                if (!empty($user)){
                    $user_realNameFrom= $tikilib -> get_userRealName($from);
                } else {
                    $user_realNameFrom= $from;
                }   
                $user_realNameTo= $tikilib -> get_userRealName($userTo);
                $smarty->assign('mail_objectTitle', $objectTitle);
                $smarty->assign('mail_site', ($tikilib->curPageURL() . "threadId=" . $threadId . "&comzone=show#comments"));
                $smarty->assign('mail_machine_raw', $tikilib->httpPrefix( true ). implode('/', $parts));
                $smarty->assign('mail_machine', $machine);
                $smarty->assign('mail_date', $tikilib->get_short_datetime($this->now,$userTo) . "-" . $tikilib->get_display_timezone($userTo));
                $smarty->assign('mail_user', $user_realNameTo);
                $smarty->assign('mail_from', $user_realNameFrom);
                $smarty->assign('mail_subject', stripslashes($subject));
                $smarty->assign('mail_body', stripslashes($body));
                $smarty->assign('itemEmailNotification', $event);
                $mail = new TikiMail($userTo);
                $lg = $this->get_user_preference($userTo, 'language', $prefs['site_language']);
                $s = $smarty->fetchLang($lg, 'mail/comments_notification_subject.tpl');
                $mail->setSubject(sprintf($s, $_SERVER["SERVER_NAME"]));
                $mail_data = $smarty->fetchLang($lg, 'mail/comments_notification.tpl');
                $mail->setHtml($mail_data);

                if ($userlib->get_user_preference($from,'email is public','n') == 'y') {
                    $prefs['sender_email'] = $userlib->get_user_email($from);
                }
                    $mail->setHeader("Reply-To", 'Example Comments<comment-noreply@example.com>');
                    $mail->setHeader("From", 'Example Comments<comment-noreply@example.com>');
                if (!$mail->send(array($email), 'mail'))
                    return false; //TODO echo $mail->errors;
            }
        return true;
    }
    //function to post a comment via email notification, end

    Save and close /lib/comments/commentslib.php.
  2. Open /comments.php. Insert the below scripts, right after the script $threadId =  $commentslib->post_in_object($comments_objectId, $_REQUEST, $feedbacks, $errors); :
    // if there is a comment written, notify author via email, begin
    if ((!empty($threadId)    && empty($errors))) {
        $object = explode( ":", $comments_objectId, 2);
        $objectDetail = $object[0] . $object[1];
        if ($object[0]='article') {
            global $artlib; include_once("lib/article/artlib.php");
            $author= $artlib->get_articleAuthor($object[1]);
            $objectTitle = $artlib->get_title((int)$object[1]);
            if (empty($user)) {
             $commentFrom=$_REQUEST['anonymous_name'];
            } else {
             $commentFrom=$user;
            }
                $commentslib->post_comment($threadId, $author, $commentFrom, $author, $objectDetail, $objectTitle, $_REQUEST["comments_title"], $_REQUEST["comments_data"], 'comment_received');
                header ("location: " . $tikilib->curPageURL());
        }
    }
    //
    if there is a comment written, notify author via email, end

    Save and close /comments.php.
  3. Open /templates/mail/comments_notification.tpl. Change or erase all the contents in this file and copy the scripts below instead:
    <p>Hi {$mail_user},

    <p>A new comment was posted to you on <a href="{$mail_site}">{$mail_objectTitle}</a> on {$mail_date}</p>
    <strong>Comment From&nbsp;&nbsp;:&nbsp;</strong>{$mail_from}<br />
    {if !empty($mail_subject)}
        <strong>Title&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</strong>    {$mail_subject}<br />
    {/if}
    <strong>Comment Detail&nbsp;: </strong><br />
        {$mail_body}


    <p>&nbsp;</p>
    <p><a href="http://www.example.com">The Example Team</a></p>

    Save and close /templates/mail/comments_notification.tpl.
  4. Open comments_notification_subject.tpl. Change or erase all the contents in this file and copy the scripts below instead:
    {tr}[Example] New comment from {/tr} {$mail_from} {tr} has arrived on {$mail_objectTitle}{/tr}

    Save and close /templates/mail/comments_notification_subject.tpl.
  5. Finish. You may try to add a comment on an article that you wrote for testing purpose only and soon, an email notification will send to your email address. You may delete that comment later.

No comments:

Post a Comment