You are here

function fivestar_comment in Fivestar 6.2

Same name and namespace in other branches
  1. 5 fivestar_comment.module \fivestar_comment()
  2. 6 fivestar_comment.module \fivestar_comment()

Implementation of hook_comment().

7 string references to 'fivestar_comment'
fivestar_comment_install in ./fivestar_comment.install
fivestar_comment_uninstall in ./fivestar_comment.install
fivestar_comment_update_6100 in ./fivestar_comment.install
Add vote_id column to the fivestar_comment table. This update will only be run when upgrading to fivestar schema 6103.
fivestar_comment_update_6200 in ./fivestar_comment.install
Add tag column to the fivestar_comment table.
fivestar_node_type in ./fivestar.module
Implementation of hook_node_types().

... See full list

File

./fivestar_comment.module, line 107

Code

function fivestar_comment(&$comment, $op) {

  // Performance tweak don't do any processing on validate or publish
  if ($op == 'validate' || $op == 'publish') {
    return;
  }
  if (is_array($comment) && is_numeric($comment['nid'])) {
    $nid = $comment['nid'];
  }
  elseif (is_array($comment) && is_array($comment['nid']) && is_numeric($comment['nid']['#value'])) {
    $nid = $comment['nid']['#value'];
  }
  elseif (is_object($comment) && is_numeric($comment->nid)) {
    $nid = $comment->nid;
  }
  if (isset($nid)) {
    $node = node_load($nid);
  }
  switch ($op) {
    case 'view':
      foreach (fivestar_get_tags() as $tag) {
        $suffix = fivestar_get_suffix($node->type, $tag);
        $fivestar_status = variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED);
        if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
          $fivestar_rating_tag = 'fivestar_rating_tag_' . $tag;
          if (!isset($comment->{$fivestar_rating_tag})) {
            $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
            $comment->{$fivestar_rating_tag} = isset($current_rating[$tag]['value']) ? $current_rating[$tag]['value'] : NULL;
          }
          $comment->{$fivestar_rating_tag} = $comment->{$fivestar_rating_tag};
          $comment->fivestar_view .= theme('fivestar_static', $comment->{$fivestar_rating_tag}, variable_get('fivestar_stars_' . $node->type, 5));
        }
      }
      $comment->comment = theme('fivestar_comment_view', $comment->comment, $comment->fivestar_view);
      break;
    case 'insert':
      foreach (fivestar_get_tags() as $tag) {
        $suffix = fivestar_get_suffix($node->type, $tag);
        $fivestar_status = variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED);
        if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
          $fivestar_rating_tag = 'fivestar_rating_tag_' . $tag;
          $comment = (object) $comment;

          // Comment module is inconsistent about comment data structures.
          if ($comment->{$fivestar_rating_tag}) {
            fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->{$fivestar_rating_tag}, $tag);
          }
          $comment = (array) $comment;
        }
      }
    case 'update':
      foreach (fivestar_get_tags() as $tag) {
        $suffix = fivestar_get_suffix($node->type, $tag);
        $fivestar_status = variable_get('fivestar_comment_' . $suffix, FIVESTAR_COMMENT_DISABLED);
        if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
          $fivestar_rating_tag = 'fivestar_rating_tag_' . $tag;
          $comment = (object) $comment;

          // Comment module is inconsistent about comment data structures.
          $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
          if ($comment->{$fivestar_rating_tag}) {
            if (isset($current_rating[$tag]['value'])) {
              fivestar_comment_update($comment->cid, $comment->nid, $comment->uid, $comment->{$fivestar_rating_tag}, $tag);
            }
            else {
              fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->{$fivestar_rating_tag}, $tag);
            }
          }
          elseif ($fivestar_status != FIVESTAR_COMMENT_DISABLED && isset($current_rating[$tag]['vote_id'])) {
            $votes_for_deletion[] = fivestar_comment_create_vote($comment->cid, $comment->nid, $current_rating[$tag]['vote_id']);
          }
          $comment = (array) $comment;
        }
      }
      fivestar_comment_delete($comment->cid, $comment->nid, $votes_for_deletion);
      break;
    case 'delete':
      foreach (fivestar_get_tags() as $tag) {
        $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
        if (isset($current_rating[$tag]['vote_id'])) {
          $votes_for_deletion[] = fivestar_comment_create_vote($comment->cid, $comment->nid, $current_rating[$tag]['vote_id']);
        }
      }
      fivestar_comment_delete($comment->cid, $comment->nid, $votes_for_deletion);
      break;
  }
}