<?php

/**
 * @file
 * Module hooks.
 */

/**
 * Implements hook_menu_alter().
 */
function opigno_poll_app_menu_alter(&$items) {
  $items['node/%node/votes']['access callback'] = 'opigno_poll_app_poll_menu_access';
}

/**
 * Implements hook_og_permission().
 */
function opigno_poll_app_og_permission() {
  return poll_permission();
}

/**
 * Implements hook_node_load().
 */
function opigno_poll_app_node_load($nodes, $types) {
  global $user;
  if (in_array('poll', $types)) {
    foreach ($nodes as $node) {
      if (!user_access('vote on polls')) {
        /**
         * This part is a verbatim copy from poll_load().
         */
        $poll = db_query("SELECT runtime, active FROM {poll} WHERE nid = :nid", array(':nid' => $node->nid))->fetchObject();
        if (empty($poll)) {
          $poll = new stdClass();
        }
        if ($poll->active && !empty($node->og_group_ref)) {
          $access = FALSE;
          foreach ($node->og_group_ref as $lang => $items) {
            foreach ($items as $item) {
              if ($access = og_user_access('node', $item['target_id'], 'vote on polls')) {
                break 2;
              }
            }
          }

          if ($access) {
            if ($user->uid) {
              // If authenticated, find existing vote based on uid.
              $poll->vote = db_query('SELECT chid FROM {poll_vote} WHERE nid = :nid AND uid = :uid', array(':nid' => $node->nid, ':uid' => $user->uid))->fetchField();
              if (empty($poll->vote)) {
                $poll->vote = -1;
                $poll->allowvotes = TRUE;
              }
            }
            elseif (!empty($_SESSION['poll_vote'][$node->nid])) {
              // Otherwise the user is anonymous. Look for an existing vote in the
              // user's session.
              $poll->vote = $_SESSION['poll_vote'][$node->nid];
            }
            else {
              // Finally, query the database for an existing vote based on anonymous
              // user's hostname.
              $poll->allowvotes = !db_query("SELECT 1 FROM {poll_vote} WHERE nid = :nid AND hostname = :hostname AND uid = 0", array(':nid' => $node->nid, ':hostname' => ip_address()))->fetchField();
            }
          }
        }
        foreach ($poll as $key => $value) {
          $nodes[$node->nid]->$key = $value;
        }
      }
    }
  }
}

/**
 * Implements hook_preprocess_poll_results().
 */
function opigno_poll_app_preprocess_poll_results(&$variables) {
  $node = node_load($variables['nid']);
  if (isset($variables['vote']) && $variables['vote'] > -1 && !user_access('cancel own vote') && !empty($node->og_group_ref)) {
    $access = FALSE;
    foreach ($node->og_group_ref as $lang => $items) {
      foreach ($items as $item) {
        if ($access = og_user_access('node', $item['target_id'], 'cancel own vote')) {
          break 2;
        }
      }
    }
    if ($access) {
      $elements = drupal_get_form('poll_cancel_form', $variables['nid']);
      $variables['cancel_form'] = drupal_render($elements);
    }
  }
}

/**
 * Callback function to see if a node is acceptable for poll menu items.
 */
function opigno_poll_app_poll_menu_access($node, $perm, $inspect_allowvotes) {
  $access = _poll_menu_access($node, $perm, $inspect_allowvotes);
  if (!$access && ($node->type == 'poll') && ($node->allowvotes || !$inspect_allowvotes)) {
    foreach ($node->og_group_ref as $lang => $items) {
      foreach ($items as $item) {
        if ($access = og_user_access('node', $item['target_id'], $perm)) {
          break 2;
        }
      }
    }
  }
  return $access;
}
