/kernel/shop/classes/ezcurrencydata.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 323 lines · 245 code · 28 blank · 50 comment · 17 complexity · e50bd11ec58906262d860bf1df2fd993 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZCurrencyData class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package kernel
  9. */
  10. class eZCurrencyData extends eZPersistentObject
  11. {
  12. const DEFAULT_AUTO_RATE_VALUE = '0.0000';
  13. const DEFAULT_CUSTOM_RATE_VALUE = '0.0000';
  14. const DEFAULT_RATE_FACTOR_VALUE = '1.0000';
  15. const ERROR_OK = 0;
  16. const ERROR_UNKNOWN = 1;
  17. const ERROR_INVALID_CURRENCY_CODE = 2;
  18. const ERROR_CURRENCY_EXISTS = 3;
  19. const STATUS_ACTIVE = '1';
  20. const STATUS_INACTIVE = '2';
  21. function eZCurrencyData( $row )
  22. {
  23. $this->eZPersistentObject( $row );
  24. $this->RateValue = false;
  25. }
  26. static function definition()
  27. {
  28. return array( 'fields' => array( 'id' => array( 'name' => 'ID',
  29. 'datatype' => 'integer',
  30. 'default' => 0,
  31. 'required' => true ),
  32. 'code' => array( 'name' => 'Code',
  33. 'datatype' => 'string',
  34. 'default' => '',
  35. 'required' => true ),
  36. 'symbol' => array( 'name' => 'Symbol',
  37. 'datatype' => 'string',
  38. 'default' => '',
  39. 'required' => false ),
  40. 'locale' => array( 'name' => 'Locale',
  41. 'datatype' => 'string',
  42. 'default' => '',
  43. 'required' => false ),
  44. 'status' => array( 'name' => 'Status',
  45. 'datatype' => 'integer',
  46. 'default' => 0,
  47. 'required' => true ),
  48. 'auto_rate_value' => array( 'name' => 'AutoRateValue',
  49. 'datatype' => 'string',
  50. 'default' => self::DEFAULT_AUTO_RATE_VALUE,
  51. 'required' => false ),
  52. 'custom_rate_value' => array( 'name' => 'CustomRateValue',
  53. 'datatype' => 'string',
  54. 'default' => self::DEFAULT_CUSTOM_RATE_VALUE,
  55. 'required' => false ),
  56. 'rate_factor' => array( 'name' => 'RateFactor',
  57. 'datatype' => 'string',
  58. 'default' => self::DEFAULT_RATE_FACTOR_VALUE,
  59. 'required' => false ) ),
  60. 'keys' => array( 'id' ),
  61. 'increment_key' => 'id',
  62. 'function_attributes' => array( 'rate_value' => 'rateValue' ),
  63. 'class_name' => "eZCurrencyData",
  64. 'sort' => array( 'code' => 'asc' ),
  65. 'name' => "ezcurrencydata" );
  66. }
  67. /*!
  68. \static
  69. \params codeList can be a single code like 'USD' or an array like array( 'USD', 'NOK' )
  70. or 'false' (means all currencies).
  71. */
  72. static function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false, $asHash = true )
  73. {
  74. $currencyList = array();
  75. $sort = null;
  76. $limitation = null;
  77. if ( $offset !== false or $limit !== false )
  78. $limitation = array( 'offset' => $offset, 'length' => $limit );
  79. $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
  80. null,
  81. $conditions,
  82. $sort,
  83. $limitation,
  84. $asObjects );
  85. if ( count( $rows ) > 0 )
  86. {
  87. if ( $asHash )
  88. {
  89. $keys = array_keys( $rows );
  90. foreach ( $keys as $key )
  91. {
  92. if ( $asObjects )
  93. $currencyList[$rows[$key]->attribute( 'code' )] = $rows[$key];
  94. else
  95. $currencyList[$rows[$key]['code']] = $rows[$key];
  96. }
  97. }
  98. else
  99. {
  100. $currencyList = $rows;
  101. }
  102. }
  103. return $currencyList;
  104. }
  105. /*!
  106. \static
  107. */
  108. static function fetchListCount( $conditions = null )
  109. {
  110. $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
  111. array(),
  112. $conditions,
  113. false,
  114. null,
  115. false,
  116. false,
  117. array( array( 'operation' => 'count( * )',
  118. 'name' => 'count' ) ) );
  119. return $rows[0]['count'];
  120. }
  121. /*!
  122. \static
  123. */
  124. static function fetch( $currencyCode, $asObject = true )
  125. {
  126. if ( $currencyCode )
  127. {
  128. $currency = eZCurrencyData::fetchList( array( 'code' => $currencyCode ), $asObject );
  129. if ( is_array( $currency ) && count( $currency ) > 0 )
  130. return $currency[$currencyCode];
  131. }
  132. return null;
  133. }
  134. /*!
  135. functional attribute
  136. */
  137. function rateValue()
  138. {
  139. if ( $this->RateValue === false )
  140. {
  141. /*
  142. $rateValue = '0.00000';
  143. if ( $this->attribute( 'custom_rate_value' ) > 0 )
  144. {
  145. $rateValue = $this->attribute( 'custom_rate_value' );
  146. }
  147. else
  148. {
  149. $rateValue = $this->attribute( 'auto_rate_value' );
  150. $rateValue = $rateValue * $this->attribute( 'rate_factor' );
  151. $rateValue = sprintf( "%7.5f", $rateValue );
  152. }
  153. */
  154. $rateValue = '0.00000';
  155. if ( $this->attribute( 'custom_rate_value' ) > 0 )
  156. $rateValue = $this->attribute( 'custom_rate_value' );
  157. else
  158. $rateValue = $this->attribute( 'auto_rate_value' );
  159. if ( $rateValue > 0 )
  160. $rateValue = $rateValue * $this->attribute( 'rate_factor' );
  161. $rateValue = sprintf( "%7.5f", $rateValue );
  162. $this->RateValue = $rateValue;
  163. }
  164. return $this->RateValue;
  165. }
  166. function invalidateRateValue()
  167. {
  168. $this->RateValue = false;
  169. }
  170. /*!
  171. \static
  172. */
  173. static function create( $code, $symbol, $locale, $autoRateValue, $customRateValue, $rateFactor, $status = self::STATUS_ACTIVE )
  174. {
  175. $code = strtoupper( $code );
  176. $errCode = eZCurrencyData::canCreate( $code );
  177. if ( $errCode === self::ERROR_OK )
  178. {
  179. $currency = new eZCurrencyData( array( 'code' => $code,
  180. 'symbol' => $symbol,
  181. 'locale' => $locale,
  182. 'status' => $status,
  183. 'auto_rate_value' => $autoRateValue,
  184. 'custom_rate_value' => $customRateValue,
  185. 'rate_factor' => $rateFactor ) );
  186. $currency->setHasDirtyData( true );
  187. return $currency;
  188. }
  189. return $errCode;
  190. }
  191. /*!
  192. \static
  193. */
  194. static function canCreate( $code )
  195. {
  196. $errCode = eZCurrencyData::validateCurrencyCode( $code );
  197. if ( $errCode === self::ERROR_OK && eZCurrencyData::currencyExists( $code ) )
  198. $errCode = self::ERROR_CURRENCY_EXISTS;
  199. return $errCode;
  200. }
  201. /*!
  202. \static
  203. */
  204. static function validateCurrencyCode( $code )
  205. {
  206. if ( !preg_match( "/^[A-Z]{3}$/", $code ) )
  207. return self::ERROR_INVALID_CURRENCY_CODE;
  208. return self::ERROR_OK;
  209. }
  210. /*!
  211. \static
  212. */
  213. static function currencyExists( $code )
  214. {
  215. return ( eZCurrencyData::fetch( $code ) !== null );
  216. }
  217. /*!
  218. \static
  219. */
  220. static function removeCurrencyList( $currencyCodeList )
  221. {
  222. if ( is_array( $currencyCodeList ) && count( $currencyCodeList ) > 0 )
  223. {
  224. $db = eZDB::instance();
  225. $db->begin();
  226. eZPersistentObject::removeObject( eZCurrencyData::definition(),
  227. array( 'code' => array( $currencyCodeList ) ) );
  228. $db->commit();
  229. }
  230. }
  231. function setStatus( $status )
  232. {
  233. $statusNumeric = eZCurrencyData::statusStringToNumeric( $status );
  234. if ( $statusNumeric !== false )
  235. {
  236. $this->setAttribute( 'status', $statusNumeric );
  237. }
  238. else
  239. {
  240. eZDebug::writeError( "Unknow currency's status '$status'", __METHOD__ );
  241. }
  242. }
  243. static function statusStringToNumeric( $statusString )
  244. {
  245. $status = false;
  246. if ( is_numeric( $statusString ) )
  247. {
  248. $status = $statusString;
  249. }
  250. if ( is_string( $statusString ) )
  251. {
  252. $statusString = strtoupper( $statusString );
  253. if ( defined( "self::STATUS_{$statusString}" ) )
  254. $status = constant( "self::STATUS_{$statusString}" );
  255. }
  256. return $status;
  257. }
  258. /*!
  259. \static
  260. */
  261. static function errorMessage( $errorCode )
  262. {
  263. switch ( $errorCode )
  264. {
  265. case self::ERROR_INVALID_CURRENCY_CODE:
  266. return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Invalid characters in currency code.' );
  267. case self::ERROR_CURRENCY_EXISTS:
  268. return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Currency already exists.' );
  269. case self::ERROR_UNKNOWN:
  270. default:
  271. return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Unknown error.' );
  272. }
  273. }
  274. function store( $fieldFilters = null )
  275. {
  276. // data changed => reset RateValue
  277. $this->invalidateRateValue();
  278. eZPersistentObject::store( $fieldFilters );
  279. }
  280. function isActive()
  281. {
  282. return ( $this->attribute( 'status' ) == self::STATUS_ACTIVE );
  283. }
  284. public $RateValue;
  285. }
  286. ?>