You are here

function fivestar_comment in Fivestar 6

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

Implementation of hook_comment().

6 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_node_type in ./fivestar.module
Implementation of hook_node_types().
fivestar_update_6103 in ./fivestar.install
Move comment support to a separate module.

... See full list

File

./fivestar_comment.module, line 92

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;
      }
      break;
    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;
  }
}