<?php
// $Id$

/**
 * @file
 * Provides notifications when comments are posted to flagged content
 *
 * The flag module does not provide a mechanism for notifying users when nodes
 * that they have flagged have new comments. Drupal does provide a hook for
 * comments, so this module provides the missing functionality.
 */


/*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
 * Includes that provide supporting functions
 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */

// ...


/*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
 * Hooks into core modules
 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */
 
/**
 * Implementation of hook_comment(); 
 */
function flag_comment_notify_comment(&$a1, $op) {
  switch ($op) {
  case 'insert':
    module_load_include('inc', 'drupal_queue', 'drupal_queue');
    $queue = DrupalQueue::get('flag_comment_notify');
    $subscribed_users = array_keys((array) flag_get_content_flags('node', $a1['nid'], 'subscriptions'));
    foreach ($subscribed_users as $uid) {
      $queue->createItem(array('uid' => $uid, 'cid' => (int) $a1['cid']));
    }
    break;
  default:
  }
}

/**
 * Implementation of hook_cron_queue_info()
 */
function flag_comment_notify_cron_queue_info() {
  $queues = array();
  $queues['flag_comment_notify'] = array(
    'worker callback' => 'flag_comment_notify_send_notification',
    'time' => 60,
  );
  return $queues;
}


/*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
 * Supporting functions
 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */

function flag_comment_notify_send_notification($data) {
  $account = user_load($data['uid']);
  $comment = _comment_load($data['cid']);
  $node = node_load($comment->nid);
  $author = user_load($comment->uid);
  if ($account->mail AND $node->nid) {
    // Don't send notifications to the author of the comment
    if ($account->uid != $author->uid) {
      $params['account'] = $account;
      $params['comment'] = $comment;
      $params['node'] = $node;
      $params['author'] = $author;
      drupal_mail('flag_comment_notify', 'comment_posted', $account->mail,
        user_preferred_language($account), $params);
    }
  }
}


function flag_comment_notify_mail($key, &$message, $params) {
  $language = $message['language'];
  $variables = user_mail_tokens($params['account'], $language);
  $variables['!comment_url'] = url(
    "goto/comment/{$params['comment']->cid}",
    array(
      'absolute' => TRUE,
    )
  );
  $variables['!topic_name'] = $params['node']->title;
  $variables['!author'] = $params['author']->name;
  switch($key) {
  case 'comment_posted':
    $message['subject'] = bts('!site: comment posted to "!topic_name"', $variables,
      $language->language, 'boinc:forum-topic-subscription-email-notification');
    $message['body'][] = bts('!author has posted a reply to "!topic_name".',
      $variables, $language->language, 'boinc:forum-topic-subscription-email-notification');
    $message['body'][] = bts("To view this topic at !site, click here: \n!comment_url",
      $variables, $language->language, 'boinc:forum-topic-subscription-email-notification');
    break;
  default:
  }
}
