Saturday, September 15, 2012

Update K2 to send mail notifications on new comments

Problem:

Really it's not a problem, is a new requirement to allow Joomla K2 component to send notifications when a new comment has been published on frontpage; i still don't understand how that functionality has not been implementd on standar release of K2.

Solution:

we need to edit the code that allow to save new comments, you can find that on components/com_k2/models/item.php once.

1.- edit item.php and search for function comment()

2.- search string "...K2_COMMENT_ADDED_AND_WAITING_FOR_APPROVAL..."

3.- before that line add the next code:


$mainframe = &JFactory::getApplication(); $mail = &JFactory::getMailer(); $senderEmail = $mainframe->getCfg('mailfrom'); $senderName = $mainframe->getCfg('fromname'); $mail->setSender(array($senderEmail, $senderName)); $mail->setSubject(JText::_('K2_COMMENT_REPORT')); $mail->IsHTML(true); $body = " <strong>".JText::_('K2_NAME')."</strong>: ".$name." <br/> <strong>".JText::_('K2_REPORT_REASON')."</strong>: ".$reportReason." <br/> <strong>".JText::_('K2_COMMENT')."</strong>: ".nl2br($row->commentText)." <br/> "; $mail->setBody($body); $mail->ClearAddresses(); $mail->AddAddress($params->get('commentsReportRecipient', $mainframe->getCfg('mailfrom'))); $mail->Send();

3 comments:

  1. Hi, this is really awesome thanks a million.
    I experienced one problem when implementing it, once I click submit the comment there is an arrow that goes around and around it does not display the text your comment has been submitted and pending approval.
    Could you please assist me as to what to do to fix this?

    ReplyDelete
  2. add '$response->message = ' above
    JText::_('K2_COMMENT_ADDED_AND_WAITING_FOR_APPROVAL');
    even though he says to place the new code between them - have $response->message = in both places like this:


    $response->message =
    $mainframe = &JFactory::getApplication();
    $mail = &JFactory::getMailer();
    $senderEmail = $mainframe->getCfg('mailfrom');
    $senderName = $mainframe->getCfg('fromname');
    $mail->setSender(array($senderEmail, $senderName));
    $mail->setSubject(JText::_('K2_COMMENT_REPORT'));
    $mail->IsHTML(true);
    $body = "
    ".JText::_('K2_NAME').": ".$name."

    ".JText::_('K2_REPORT_REASON').": ".$reportReason."

    ".JText::_('K2_COMMENT').": ".nl2br($row->commentText)."

    ";
    $mail->setBody($body);
    $mail->ClearAddresses();
    $mail->AddAddress($params->get('commentsReportRecipient', $mainframe->getCfg('mailfrom')));
    $mail->Send();
    $response->message =
    JText::_('K2_COMMENT_ADDED_AND_WAITING_FOR_APPROVAL');
    $response->cssClass = 'k2FormLogSuccess';
    echo json_encode($response);

    ReplyDelete