You are here

function plus1_nodeapi in Plus 1 6.2

Same name and namespace in other branches
  1. 6 plus1.module \plus1_nodeapi()

Implements hook_nodeapi().

File

./plus1.module, line 282
A simple +1 voting widget module.

Code

function plus1_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':

      // If the build_mode is one of these, bail out now.
      $exclude_modes = array(
        NODE_BUILD_PREVIEW,
        NODE_BUILD_SEARCH_INDEX,
        NODE_BUILD_SEARCH_RESULT,
        NODE_BUILD_RSS,
      );
      if (in_array($node->build_mode, $exclude_modes)) {
        return;
      }

      // Only show the voting widget if voting here is allowed.
      if (plus1_vote_access('view', $node)) {
        drupal_add_css(drupal_get_path('module', 'plus1') . '/plus1.css');

        // Show the widget.
        if ($teaser && variable_get('plus1_in_teaser', 0) || !$teaser && variable_get('plus1_in_full_view', 1)) {
          $node->content['plus1_widget'] = array(
            '#value' => plus1_jquery_widget($node, $teaser, $page),
            '#weight' => (int) variable_get('plus1_weight', '100'),
          );
        }
      }
      return;
    case 'delete':

      // Remove "disable vote".
      $skip = variable_get('plus1_disable_vote', array());
      if (isset($skip[$node->nid])) {
        unset($skip[$node->nid]);
        variable_set('plus1_disable_vote', $skip);
      }

      // Delete votes and results when node is deleted.
      if (variable_get('plus1_delete_nodes', 1) && $node->nid) {
        $criteria = array(
          'content_id' => $node->nid,
        );
        $votes = votingapi_select_votes($criteria);
        if ($votes) {
          votingapi_delete_votes($votes);
          $result = votingapi_select_results($criteria);
          votingapi_delete_results($result);

          // Create notices.
          global $user;
          $c = count($votes);
          drupal_set_message(t('!count votes cleared.', array(
            '!count' => $c,
          )));
          watchdog('plus1', '!count votes deleted for @title (!nid) by user !uid.', array(
            '!count' => $c,
            '@title' => $node->title,
            '!nid' => $node->nid,
            '!uid' => $user->uid,
          ));
        }
      }
      return;

    // On node_load, add the disable flag.
    case 'load':
      $skip = variable_get('plus1_disable_vote', array());
      return array(
        'plus1_disable_vote' => isset($skip[$node->nid]),
      );
    case 'update':
    case 'insert':
      drupal_set_message();
      $skip = variable_get('plus1_disable_vote', array());

      // Is this node disabled?
      $set = isset($skip[$node->nid]);

      // Do they want to disable voting?
      if ($node->plus1_disable_vote) {

        // See if it is aleady disabled.
        if (!$set) {
          $skip[$node->nid] = $node->nid;
          variable_set('plus1_disable_vote', $skip);
          drupal_set_message(t('Voting on this post is disabled.'));
        }
      }
      else {

        // Don't disable, so see if it was set before.
        if ($set) {
          unset($skip[$node->nid]);
          variable_set('plus1_disable_vote', $skip);
          drupal_set_message(t('Voting on this post is enabled.'));
        }
      }
      return;
  }
}