You are here

function commentactivity_comment in Activity 6

Same name and namespace in other branches
  1. 5.4 contrib/commentactivity/commentactivity.module \commentactivity_comment()
  2. 5.3 contrib/commentactivity/commentactivity.module \commentactivity_comment()

Implementation of hook_comment().

File

contrib/commentactivity/commentactivity.module, line 117

Code

function commentactivity_comment($comment, $op) {
  switch ($op) {

    // $comment is array for insert/update, is object for delete
    case 'insert':
    case 'update':
      if ($comment['status'] == COMMENT_NOT_PUBLISHED) {
        break;
      }
    case 'delete':
      if ($comment->status == COMMENT_NOT_PUBLISHED) {
        break;
      }

    // publish operation results in dupe records in D6

    //case 'publish':
    case 'unpublish':

      // Check if both type and operation are
      // enabled for activity. If not then stop here
      if (!in_array('comment', variable_get('commentactivity_token_types', array(
        'comment',
      )), TRUE) || !in_array($op, variable_get('commentactivity_op_types', array(
        $op,
      )), TRUE)) {
        return FALSE;
      }

      // cast $comment to array
      $comment = (array) $comment;

      // Privacy setting check
      $user = user_load(array(
        'uid' => $comment['uid'],
      ));
      if (activity_user_privacy_optout($user)) {
        return FALSE;
      }

      // User hide activity permission check
      if (user_access('hide activity', $user)) {
        return FALSE;
      }
      $node = node_load($comment['nid']);

      // If using comment threading
      if ($comment['pid']) {
        if ($parent_comment_author_uid = db_result(db_query("SELECT uid FROM {comments} WHERE cid = %d", $comment['pid']))) {
          $parent_comment_author = activity_user_load($parent_comment_author_uid);
          $parent_comment = db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $comment['pid']));
        }
      }
      $data = array(
        'comment-cid' => $comment['cid'],
        'comment-subject' => $comment['subject'],
        'parent-node-author-uid' => $node->uid,
        'parent-node-id' => $node->nid,
        'parent-node-title' => $node->title,
        'parent-node-type' => $node->type,
      );
      $target_users_roles = array();

      // The order in which elements are added to $target_users_roles is
      // important. Since several of the roles can be the same person
      // eg. a person comments on their own node or replies to their own
      // comment, they may overwrite each other in the array.
      // This is fine as long as the most important role wins...
      if (is_object($parent_comment_author)) {
        $data['parent-comment-cid'] = $parent_comment->cid;
        $data['parent-comment-subject'] = $parent_comment->subject;
        $data['parent-comment-author-uid'] = $parent_comment_author_uid;
        $target_users_roles[$parent_comment_author_uid] = 'parent_comment_author';
      }
      $target_users_roles = array(
        ACTIVITY_ALL => 'all',
        $node->uid => 'parent_node_author',
        // and the most important role is 'author' so we add it last.
        $comment['uid'] => 'author',
      );
      activity_insert($comment['uid'], 'commentactivity', 'comment', $op, $data, $target_users_roles);
      break;
  }
}