Download & Extend

How do I display the currency symbol?

Project:Currency Exchange
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Right now I'm fetching the symbols from the api and then using a function to find the correct one in the hash. This seems like a ghetto way of doing it. Do you have a better way or is it something that needs to be coded?

Comments

#1

Version:master» 5.x-1.x-dev

Right now, it is two function calls.

Seems that we need to combine the currency_api_get_symbols() and currency_api_get_list() in one function that returns one array with the symbols and names, then have a lookup function like currency_api_get_info() using the currency code, and returning both the description and symbol.

At a later stage, we can have that in a database table.

Can you work on a patch for that?

#2

Status:active» needs work

OK, here's my first attempt of patching anything in my life so here we go. All I did was get a hash of the currency names and a hash of the currency symbols and then do a lookup in both and return the values in yet another hash. Also you might think about moving the symbols to a text file bc eclipse complains when you try to edit this file, I had to edit it in notepad. -Jay

### Eclipse Workspace Patch 1.0
#P currency
Index: currency_api/currency_api.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/currency/currency_api/currency_api.module,v
retrieving revision 1.6
diff -u -r1.6 currency_api.module
--- currency_api/currency_api.module 29 Oct 2007 22:32:49 -0000 1.6
+++ currency_api/currency_api.module 15 Apr 2008 23:36:50 -0000
@@ -1,4 +1,4 @@
-<?php
+<?php

//$Id: currency_api.module,v 1.6 2007/10/29 22:32:49 wimleers Exp $

@@ -538,6 +538,14 @@
return $currency;
}

+function currency_api_get_info($acronym) {
+ $currency_list = currency_api_get_list();
+ $currency_symbols = currency_api_get_symbols();
+
+ if ($currency_list[$acronym])
+ return array('name' => $currency_list[$acronym], 'symbol' => $currency_symbols[$acronym]);
+}
+
function currency_api_get_fields($array) {
while (list($field, $header) = each($array)) {
$field_string = $field_string . $field;

#3

Jay

This is not the optimal way to handle this.

Why don't we combine both functions in one, i.e. symbols and description?

function currency_api_get_info() {
  return array(
    'AFN' => array(t('afghani'), t('Afghanistani Afghani')),
    'ALL' => array(t('lek'), t('Albanian Lek')),
    ...
  );
}

Now, both are indexed by the currency code, and we can get either from one function.

We could take that a step further to a currency table with 3 columns (code, symbol, description), and it gets populated in the _install() hook. We then have the function above do a lookup on the table by the code using SQL.

Regarding Eclipse, check its settings for how to handle Unicode UTF-8 correctly.

#4

Here's a little wrapper function I created to return a currency symbol. It also caches the symbols array returned by currency_api_get_symbols(). Note: change function name prefix to suit.

/**
* Custom function to return currency symbol.
*
* @param string 3-letter currency type
* @return string currency symbol
*/
function _transaction_get_symbol($currency) {
  static $symbols = array();

  if (isset($symbols[$currency]) == FALSE) {
    // cache symbols in static array variable for subsequent lookups
    $symbols = currency_api_get_symbols();
  }
  // return symbol for currency exchange rate
  return $symbols[$currency];
}

#5

BTW, I would not recommend putting the codes, descriptions and symbols in the database. Since these do not change very often it just creates unnecessary overhead.

#6

Status:needs work» closed (fixed)

This is fixed in the 6.x branch and 5.x is not maintained anymore.