CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Drupal - Contribute Widget - lose the cents
Pages: [1]

Author Topic: Drupal - Contribute Widget - lose the cents  (Read 1978 times)

HeneryH

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 1
  • CMS version: 1
  • MySQL version: 1
  • PHP version: 1
Drupal - Contribute Widget - lose the cents
May 24, 2013, 05:14:49 pm
Argh, it keeps telling me I can't post external links but I'm not???  Lets try just this.

HeneryH

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 1
  • CMS version: 1
  • MySQL version: 1
  • PHP version: 1
Re: Drupal - Contribute Widget - lose the cents
May 24, 2013, 05:15:53 pm
Add a little more back slowly...

I'd like to tweak the Contribution Widget a touch to display the target and current values without the cents.  The strings are just too long for me.

Problem is, I am terrible with PHP and it isn't working like I think it should.  Could I have some help with this from people who are good with code?

The widget is in the modules/civicrm/CRM/Contribute/BAO/Widget[dot]php file and uses a function call to a money util for formatting:
Code: [Select]
$data['money_target_display'] = CRM_Utils_Money::format($data['money_target']);

The formatter code is in modules/civicrm/CRM/Utils/Money[dot]php as  (lots cut out but I think this is the gist of it)
Code: [Select]
  /**
   * format a monetary string
   *
   * Format a monetary string basing on the amount provided,
   * ISO currency code provided and a format string consisting of:
   *
   * %a - the formatted amount
   * %C - the currency ISO code (e.g., 'USD') if provided
   * %c - the currency symbol (e.g., '$') if available
   *
   * @param float  $amount    the monetary amount to display (1234.56)
   * @param string $currency  the three-letter ISO currency code ('USD')
   * @param string $format    the desired currency format
   *
   * @return string  formatted monetary string
   *
   * @static
   */
  static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE) {
.
.
.
    if (is_numeric($amount) && function_exists('money_format')) {
      $amount = money_format($config->moneyvalueformat, $amount);
    }
.
.
.
    $replacements = array(
      '%a' => $amount,
      '%C' => $currency,
      '%c' => CRM_Utils_Array::value($currency, self::$_currencySymbols, $currency),
    );
    return strtr($format, $replacements);
}

I thought that I could modify the Widget[dot]php call to include a format string of '%.0n' but that didn't work.  I cleared all drupal caches and the widget stayed the same look.

I then went into the money formatter function and jammed the '%.0n' sting in there but still no luck.

Am I missing something obvious?
« Last Edit: May 24, 2013, 05:20:43 pm by HeneryH »

Guy Iaccarino

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 5
    • Greenleaf Advancement
  • CiviCRM version: 4.4.10, 4.5.4
  • CMS version: WordPress 4, Drupal 7, Drupal 6, Joomla 3
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Drupal - Contribute Widget - lose the cents
May 25, 2013, 05:06:51 am
I'm no good with the code either, but for what it's worth, I agree with you. The widget is presenting a big picture view, and hence the cents just provide "too much information."


Just my two cents.  :)
Guy Iaccarino
www.greenleafadvancement.com

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Drupal - Contribute Widget - lose the cents
May 25, 2013, 08:00:19 am

you have to modify the $config->moneyvalueFormat and get the amount in the right format there, rather than in the format string

from a brief read of the code seems like the format string just takes a few arguments and is not a complete amount formatting string

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

HeneryH

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 1
  • CMS version: 1
  • MySQL version: 1
  • PHP version: 1
Re: Drupal - Contribute Widget - lose the cents
May 25, 2013, 02:34:40 pm
Ah, figured it out.  I was mixing up "value format" and "amount format".

Turns out the format expected in
Code: [Select]
  static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE) {

is the amount format which is telling if you want the symbol, three-letter-code, value or some combination.  It is NOT the format of the number itself.

So, unfortunately the money format function provided by
modules/civicrm/CRM/Utils/Money.php

while it allows the overriding of the amount format it does not allow the overriding of the value format.

Easy enough to fix, I added another parameter to the function with the value format and a default value so that it doesn't break any existing calls.

Now I can have the widget override the number of digits without messing up anything else.  I'll submit a feature request to ask this change be implemented in the base.

modules/civicrm/CRM/Utils/Money.php:
Code: [Select]
class CRM_Utils_Money {
  static $_currencySymbols = NULL;

  /**
   * format a monetary string
   *
   * Format a monetary string basing on the amount provided,
   * ISO currency code provided and a format string consisting of:
   *
   * %a - the formatted amount
   * %C - the currency ISO code (e.g., 'USD') if provided
   * %c - the currency symbol (e.g., '$') if available
   *
   * @param float  $amount    the monetary amount to display (1234.56)
   * @param string $currency  the three-letter ISO currency code ('USD')
   * @param string $a_format  the desired currency amount format
   * @param bool   $onlyNumbr t= only number, f=formatted
   * @param string $v_format  the desired currency value format
   *
   * @return string  formatted monetary string
   *
   * @static
   */
  static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE, $v_format = NULL) {

    if (CRM_Utils_System::isNull($amount)) {
      return '';
    }

    $config = CRM_Core_Config::singleton();

    if (!$format) {
      $format = $config->moneyformat;
    }

    if ($onlyNumber) {
      // money_format() exists only in certain PHP install (CRM-650)
      if (is_numeric($amount) and function_exists('money_format')) {
        $amount = money_format($config->moneyvalueformat, $amount);
      }
      return $amount;
    }

    if (!self::$_currencySymbols) {
      $currencySymbolName = CRM_Core_PseudoConstant::currencySymbols('name');
      $currencySymbol = CRM_Core_PseudoConstant::currencySymbols();

      self::$_currencySymbols = array_combine($currencySymbolName, $currencySymbol);
    }

    if (!$currency) {
      $currency = $config->defaultCurrency;
    }

    if (!$format) {
      $format = $config->moneyformat;
    }

    if (!$v_format) {
      $v_format = $config->moneyvalueformat;
    }

    // money_format() exists only in certain PHP install (CRM-650)
    // setlocale() affects native gettext (CRM-11054, CRM-9976)
    if (is_numeric($amount) && function_exists('money_format')) {
      $lc = setlocale(LC_MONETARY, 0);
      setlocale(LC_MONETARY, 'en_US.utf8', 'en_US', 'en_US.utf8', 'en_US', 'C');
      $amount = money_format($v_format, $amount);
      setlocale(LC_MONETARY, $lc);
    }

    $rep = array(
      ',' => $config->monetaryThousandSeparator,
      '.' => $config->monetaryDecimalPoint,
    );

    // If it contains tags, means that HTML was passed and the
    // amount is already converted properly,
    // so don't mess with it again.
    if (strpos($amount, '<') === FALSE) {
      $amount = strtr($amount, $rep);
    }

    $replacements = array(
      '%a' => $amount,
      '%C' => $currency,
      '%c' => CRM_Utils_Array::value($currency, self::$_currencySymbols, $currency),
    );
    return strtr($format, $replacements);
  }
}

« Last Edit: May 25, 2013, 02:36:53 pm by HeneryH »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Drupal - Contribute Widget - lose the cents

This forum was archived on 2017-11-26.