<?php

/**
 * @file
 * This module provides currency exchange rates.
 */

// Copyright 2005 Khalid Baheyeldin http://2bits.com

/**
 * Implements hook_help().
 */
function currency_help($path, $arg) {
  switch ($path) {
    case 'admin/help#currency':
      return t('This module provides currency exchange rates.');
  }
}

/**
 * Implements hook_menu().
 */
function currency_menu() {
  $items['admin/config/regional/currency'] = array(
    'title' => 'Currency',
    'description' => 'Settings for currency exchange rates.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('currency_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['currency'] = array(
    'title' => 'Currency exchange',
    'access arguments' => array('use currency'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('currency_form'),
  );

  return $items;
}
  
/**
 * Menu callback; module settings form.
 */
function currency_admin_settings() {  
  $form['currency_default_from'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Currency From'),
    '#default_value' => variable_get('currency_default_from', 'CAD'),
    '#size' => 3,
    '#maxlength' => 3,
    '#description' => t('Three letter symbol for default currency to convert from.'),
  );

  $form['currency_default_to'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Currency To'),
    '#default_value' => variable_get('currency_default_to', 'USD'),
    '#size' => 3,
    '#maxlength' => 3,
    '#description' => t('Three letter symbol for default currency to convert to.'),
  );

  $form['currency_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Currency form text'),
    '#default_value' => variable_get('currency_description', t('You can use this form to do currency exchange.')),
    '#description' => t('Text to display on the top of the currency form.'),
  );

  return system_settings_form($form);
}  

/**
 * Implements hook_permission().
 */
function currency_permission() {
  return array(
    'use currency' => array(
      'title' => t('Use currency'),
    ),
  );
}

/**
 * Currency exchange form.
 */
function currency_form($form, &$form_state) {
  if (isset($form_state['storage']['currency_amount']) && isset($form_state['storage']['currency_from']) && $form_state['storage']['currency_to']) {
    // Get the saved data from the previous form submission.
    $amount = $form_state['storage']['currency_amount'];
    $from = $form_state['storage']['currency_from'];
    $to = $form_state['storage']['currency_to'];

    $form['currency_result'] = array(
      '#markup' => theme('currency_result', array('currency_from' => $from, 'currency_to' => $to, 'amount' => $amount)),
      '#weight' => 5,
    );
  }
  else {
    // Get the saved data from the session, if any.
    $amount = isset($_SESSION['currency_amount']) ? $_SESSION['currency_amount'] : 1;
    $from = isset($_SESSION['currency_from']) ? $_SESSION['currency_from'] : variable_get('currency_default_from', 'CAD');
    $to = isset($_SESSION['currency_to']) ? $_SESSION['currency_to'] : variable_get('currency_default_to', 'USD');
  }

  $form['currency_description'] = array(
    '#markup' => variable_get('currency_description', t('You can use this form to do currency exchange.')),
  );

  $form['currency_amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Amount'),
    '#default_value' => $amount,
    '#size' => 9,
    '#maxlength' => 9,
    '#description' => t('Amount to convert'),
  );
  
  $form['currency_from'] = array(
    '#type' => 'select',
    '#title' => t('From'),
    '#default_value' => $from,
    '#options' => currency_api_get_list(),
  );
  
  $form['currency_to'] = array(
    '#type' => 'select',
    '#title' => t('To'),
    '#default_value' => $to,
    '#options' => currency_api_get_list(),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Convert'),
    '#weight' => 10,
  );

  return $form;
}

/**
 * Validate handler for the currency exchange form.
 */
function currency_form_validate($form, &$form_state) {
  if (!$form_state['values']['currency_amount']) {
    form_set_error('currency_amount', t('Amount is required.'));
  }

  if (!is_numeric($form_state['values']['currency_amount'])) {
    form_set_error('currency_amount', t('Invalid Amount. Please enter a valid numeric amount.'));
  }
}

/**
 * Submit handler for the currency exchange form.
 */
function currency_form_submit($form, &$form_state) {
  // Save the last used values in the session.
  $_SESSION['currency_amount'] = $form_state['values']['currency_amount'];
  $_SESSION['currency_from'] = $form_state['values']['currency_from'];
  $_SESSION['currency_to'] = $form_state['values']['currency_to'];

  // Rebuild the form.
  $form_state['rebuild'] = TRUE;

  // Store submitted values.
  $form_state['storage']['currency_amount'] = $form_state['values']['currency_amount'];
  $form_state['storage']['currency_from'] = $form_state['values']['currency_from'];
  $form_state['storage']['currency_to'] = $form_state['values']['currency_to'];
}

/**
 * Implements hook_theme().
 */
function currency_theme($existing, $type, $theme, $path) {
  return array(
    'currency_result' => array(
      'variables' => array('currency_from' => NULL, 'currency_to' => NULL, 'amount' => NULL),
    ),
  );
}

/**
 * Theme implementation for currency exchange result.
 */
function theme_currency_result($variables) {
  $output = '';

  $currency_from = $variables['currency_from'];
  $currency_to = $variables['currency_to'];
  $amount = $variables['amount'];

  $url = 'http://finance.yahoo.com/q?s=' . $currency_from . $currency_to . '=X';

  $ret = currency_api_convert($currency_from, $currency_to, $amount);
  if ($ret['status'] == FALSE) { 
    $output .= t('Currency exchange error: @message', array('@message' => $ret['message']));
  }
  else {
    $output .= '<p class="result">';
    $output .=  t('@amount @from = @value @to', array(
      '@amount' => $amount,
      '@from' => currency_api_get_desc($currency_from),
      '@value' => $ret['value'],
      '@to' => currency_api_get_desc($currency_to)));
    $output .= '</p>';
    $output .= '<p class="detailed-history">' . l(t('Detailed history and chart'), $url) . '</p>';
  }

  $output = '<div class="currency-result">' . $output . '</div>';

  return $output;
}


/**
 * Implements hook_views().
 */
function currency_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'currency') . '/includes/views',
  );
}
