<?php

/**
 * @file
 * Code for the Groups Ambassador feature.
 */

include_once 'groups_ambassador.features.inc';

/**
 * Implements hook_menu()
 */
function groups_ambassador_menu() {
  $items['ambassador-program/ask'] = array(
    'title' => 'Ask the Ambassadors',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('groups_ambassador_ask_form'),
    'access callback' => 'user_is_logged_in',
    'file' => 'includes/ask.form.inc',
  );
  $items['ambassador-program/apply'] = array(
    'title' => 'OpenStack Ambassador Application',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('groups_ambassador_apply_form'),
    'access callback' => 'user_is_logged_in',
    'file' => 'includes/apply.form.inc',
  );
  return $items;
}

/**
 * Implements hook_block_info()
 */
function groups_ambassador_block_info() {
  $blocks['groups_ambassador_page_banner'] = array(
    'info' => t('Groups Ambassador Page Banner block'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  $blocks['groups_ambassador_roleof'] = array(
    'info' => t('Groups Ambassador Role of Ambassadors block'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  $blocks['groups_ambassador_selection'] = array(
    'info' => t('Groups Ambassador Selection Process block'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  $blocks['groups_ambassador_faq'] = array(
    'info' => t('Groups Ambassador FAQ block'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  $blocks['groups_ambassador_ask'] = array(
    'info' => t('Groups Ambassador Ask Questions'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function groups_ambassador_block_view($delta='') {
  $block = array();
  switch ($delta) {
    case 'groups_ambassador_page_banner':
      $block['content'] = theme('groups_ambassador_page_banner', array());
      $block['subject'] = 'Ambassador Program';
      break;
    case 'groups_ambassador_roleof':
      $block['content'] = theme('groups_ambassador_roleof', array());
      $block['subject'] = 'Ambassadors are here to help';
      break;
    case 'groups_ambassador_selection':
      $block['content'] = theme('groups_ambassador_selection', array());
      $block['subject'] = 'Are you Ambassador material?';
      break;
    case 'groups_ambassador_faq':
      $block['content'] = theme('groups_ambassador_faq', array());
      $block['subject'] = 'Frequently Asked Questions';
      break;
    case 'groups_ambassador_ask':
      $block['content'] = theme('groups_ambassador_ask', array());
      $block['subject'] = 'Ask Questions';
      break;
  }
  return $block;
}

/**
 * Implements hook_theme()
 */
function groups_ambassador_theme() {
  $module_path = drupal_get_path('module', 'groups_ambassador');
  $base = array(
      'path' => "$module_path/templates",
  );
  return array(
    'groups_ambassador_page_banner' => $base + array(
      'template' => 'groups_ambassador_page_banner',
      'variables' => array(),
    ),
    'groups_ambassador_roleof' => $base + array(
      'template' => 'groups_ambassador_roleof',
      'variables' => array(),
    ),
    'groups_ambassador_selection' => $base + array(
      'template' => 'groups_ambassador_selection',
      'variables' => array(),
    ),
    'groups_ambassador_faq' => $base + array(
      'template' => 'groups_ambassador_faq',
      'variables' => array(),
    ),
    'groups_ambassador_ask' => $base + array(
      'template' => 'groups_ambassador_ask',
      'variables' => array(),
    ),
    'groups_ambassador_submission' => $base + array(
      'template' => 'groups_ambassador_submission',
      'variables' => array(),
    ),
    'groups_ambassador_apply_confirm' => $base + array(
      'template' => 'groups_ambassador_apply_confirm',
      'variables' => array(),
    ),
  );
}

/**
 * Return the list of ambassador profiles.
 */
function _groups_ambassador_get_ambassador_list() {
  $uids = array();
  $ambassador_role = user_role_load_by_name('ambassador');
  $query = 'SELECT DISTINCT(ur.uid)
    FROM {users_roles} AS ur
    WHERE ur.rid = :rid';
  $result = db_query($query, array(':rid' => $ambassador_role->rid));
  foreach ($result as $row) {
    $uids[] = $row->uid;
  }
  return user_load_multiple($uids);
}

/**
 * Convert standard user array into _privatemsg recipient format.
 */
function groups_ambassador_get_privatemsg_recipients($recipient_users) {
  $recipients = array();
  foreach ($recipient_users as $user) {
    $user->type = 'user';
    $user->recipient = $user->uid;
    $recipients['user_'.$user->uid] = $user;
  }
  return $recipients;
}
