You are here

function fivestar_comment in Fivestar 5

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

Implementation of hook_comment().

3 string references to 'fivestar_comment'
fivestar_node_type in ./fivestar.module
Implementation of hook_node_types().
fivestar_update_5702 in ./fivestar.install
Move comment support to a separate module.
theme_fivestar_node_type_form in ./fivestar.module
Theme function to add the Fivestar preview to the node type form.

File

./fivestar_comment.module, line 81

Code

function fivestar_comment(&$comment, $op) {
  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);
    $fivestar_status = variable_get('fivestar_comment_' . $node->type, FIVESTAR_COMMENT_DISABLED);
  }
  switch ($op) {
    case 'view':
      if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
        if (!isset($comment->fivestar_rating)) {
          $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
          $comment->fivestar_rating = isset($current_rating['value']) ? $current_rating['value'] : NULL;
        }
        $comment->fivestar_rating = $comment->fivestar_rating;
        $comment->fivestar_view = theme('fivestar_static', $comment->fivestar_rating, variable_get('fivestar_stars_' . $node->type, 5));
        if ($comment->fivestar_rating) {

          // Implement the theme in template.php to change the order:
          $comment->comment = theme('fivestar_comment_view', $comment->comment, $comment->fivestar_view);
        }
      }
      break;
    case 'insert':
      if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
        $comment = (object) $comment;

        // Comment module is inconsistent about comment data structures.
        if ($comment->fivestar_rating) {
          fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->fivestar_rating);
        }
        $comment = (array) $comment;
      }
    case 'update':
      if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
        $comment = (object) $comment;

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