PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/shop/classes/ezcurrencydata.php

https://github.com/lserwatka/ezpublish
PHP | 346 lines | 245 code | 29 blank | 72 comment | 17 complexity | f8533d80698abc4a8d79f9496c0f3648 MD5 | raw file
  1. <?php
  2. //
  3. // Definition of eZCurrencyData class
  4. //
  5. // Created on: <08-Nov-2005 13:06:15 dl>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. class eZCurrencyData extends eZPersistentObject
  33. {
  34. const DEFAULT_AUTO_RATE_VALUE = '0.0000';
  35. const DEFAULT_CUSTOM_RATE_VALUE = '0.0000';
  36. const DEFAULT_RATE_FACTOR_VALUE = '1.0000';
  37. const ERROR_OK = 0;
  38. const ERROR_UNKNOWN = 1;
  39. const ERROR_INVALID_CURRENCY_CODE = 2;
  40. const ERROR_CURRENCY_EXISTS = 3;
  41. const STATUS_ACTIVE = '1';
  42. const STATUS_INACTIVE = '2';
  43. function eZCurrencyData( $row )
  44. {
  45. $this->eZPersistentObject( $row );
  46. $this->RateValue = false;
  47. }
  48. static function definition()
  49. {
  50. return array( 'fields' => array( 'id' => array( 'name' => 'ID',
  51. 'datatype' => 'integer',
  52. 'default' => 0,
  53. 'required' => true ),
  54. 'code' => array( 'name' => 'Code',
  55. 'datatype' => 'string',
  56. 'default' => '',
  57. 'required' => true ),
  58. 'symbol' => array( 'name' => 'Symbol',
  59. 'datatype' => 'string',
  60. 'default' => '',
  61. 'required' => false ),
  62. 'locale' => array( 'name' => 'Locale',
  63. 'datatype' => 'string',
  64. 'default' => '',
  65. 'required' => false ),
  66. 'status' => array( 'name' => 'Status',
  67. 'datatype' => 'integer',
  68. 'default' => 0,
  69. 'required' => true ),
  70. 'auto_rate_value' => array( 'name' => 'AutoRateValue',
  71. 'datatype' => 'string',
  72. 'default' => self::DEFAULT_AUTO_RATE_VALUE,
  73. 'required' => false ),
  74. 'custom_rate_value' => array( 'name' => 'CustomRateValue',
  75. 'datatype' => 'string',
  76. 'default' => self::DEFAULT_CUSTOM_RATE_VALUE,
  77. 'required' => false ),
  78. 'rate_factor' => array( 'name' => 'RateFactor',
  79. 'datatype' => 'string',
  80. 'default' => self::DEFAULT_RATE_FACTOR_VALUE,
  81. 'required' => false ) ),
  82. 'keys' => array( 'id' ),
  83. 'increment_key' => 'id',
  84. 'function_attributes' => array( 'rate_value' => 'rateValue' ),
  85. 'class_name' => "eZCurrencyData",
  86. 'sort' => array( 'code' => 'asc' ),
  87. 'name' => "ezcurrencydata" );
  88. }
  89. /*!
  90. \static
  91. \params codeList can be a single code like 'USD' or an array like array( 'USD', 'NOK' )
  92. or 'false' (means all currencies).
  93. */
  94. static function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false, $asHash = true )
  95. {
  96. $currencyList = array();
  97. $sort = null;
  98. $limitation = null;
  99. if ( $offset !== false or $limit !== false )
  100. $limitation = array( 'offset' => $offset, 'length' => $limit );
  101. $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
  102. null,
  103. $conditions,
  104. $sort,
  105. $limitation,
  106. $asObjects );
  107. if ( count( $rows ) > 0 )
  108. {
  109. if ( $asHash )
  110. {
  111. $keys = array_keys( $rows );
  112. foreach ( $keys as $key )
  113. {
  114. if ( $asObjects )
  115. $currencyList[$rows[$key]->attribute( 'code' )] = $rows[$key];
  116. else
  117. $currencyList[$rows[$key]['code']] = $rows[$key];
  118. }
  119. }
  120. else
  121. {
  122. $currencyList = $rows;
  123. }
  124. }
  125. return $currencyList;
  126. }
  127. /*!
  128. \static
  129. */
  130. static function fetchListCount( $conditions = null )
  131. {
  132. $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
  133. array(),
  134. $conditions,
  135. false,
  136. null,
  137. false,
  138. false,
  139. array( array( 'operation' => 'count( * )',
  140. 'name' => 'count' ) ) );
  141. return $rows[0]['count'];
  142. }
  143. /*!
  144. \static
  145. */
  146. static function fetch( $currencyCode, $asObject = true )
  147. {
  148. if ( $currencyCode )
  149. {
  150. $currency = eZCurrencyData::fetchList( array( 'code' => $currencyCode ), $asObject );
  151. if ( is_array( $currency ) && count( $currency ) > 0 )
  152. return $currency[$currencyCode];
  153. }
  154. return null;
  155. }
  156. /*!
  157. functional attribute
  158. */
  159. function rateValue()
  160. {
  161. if ( $this->RateValue === false )
  162. {
  163. /*
  164. $rateValue = '0.00000';
  165. if ( $this->attribute( 'custom_rate_value' ) > 0 )
  166. {
  167. $rateValue = $this->attribute( 'custom_rate_value' );
  168. }
  169. else
  170. {
  171. $rateValue = $this->attribute( 'auto_rate_value' );
  172. $rateValue = $rateValue * $this->attribute( 'rate_factor' );
  173. $rateValue = sprintf( "%7.5f", $rateValue );
  174. }
  175. */
  176. $rateValue = '0.00000';
  177. if ( $this->attribute( 'custom_rate_value' ) > 0 )
  178. $rateValue = $this->attribute( 'custom_rate_value' );
  179. else
  180. $rateValue = $this->attribute( 'auto_rate_value' );
  181. if ( $rateValue > 0 )
  182. $rateValue = $rateValue * $this->attribute( 'rate_factor' );
  183. $rateValue = sprintf( "%7.5f", $rateValue );
  184. $this->RateValue = $rateValue;
  185. }
  186. return $this->RateValue;
  187. }
  188. function invalidateRateValue()
  189. {
  190. $this->RateValue = false;
  191. }
  192. /*!
  193. \static
  194. */
  195. static function create( $code, $symbol, $locale, $autoRateValue, $customRateValue, $rateFactor, $status = self::STATUS_ACTIVE )
  196. {
  197. $code = strtoupper( $code );
  198. $errCode = eZCurrencyData::canCreate( $code );
  199. if ( $errCode === self::ERROR_OK )
  200. {
  201. $currency = new eZCurrencyData( array( 'code' => $code,
  202. 'symbol' => $symbol,
  203. 'locale' => $locale,
  204. 'status' => $status,
  205. 'auto_rate_value' => $autoRateValue,
  206. 'custom_rate_value' => $customRateValue,
  207. 'rate_factor' => $rateFactor ) );
  208. $currency->setHasDirtyData( true );
  209. return $currency;
  210. }
  211. return $errCode;
  212. }
  213. /*!
  214. \static
  215. */
  216. static function canCreate( $code )
  217. {
  218. $errCode = eZCurrencyData::validateCurrencyCode( $code );
  219. if ( $errCode === self::ERROR_OK && eZCurrencyData::currencyExists( $code ) )
  220. $errCode = self::ERROR_CURRENCY_EXISTS;
  221. return $errCode;
  222. }
  223. /*!
  224. \static
  225. */
  226. static function validateCurrencyCode( $code )
  227. {
  228. if ( !preg_match( "/^[A-Z]{3}$/", $code ) )
  229. return self::ERROR_INVALID_CURRENCY_CODE;
  230. return self::ERROR_OK;
  231. }
  232. /*!
  233. \static
  234. */
  235. static function currencyExists( $code )
  236. {
  237. return ( eZCurrencyData::fetch( $code ) !== null );
  238. }
  239. /*!
  240. \static
  241. */
  242. static function removeCurrencyList( $currencyCodeList )
  243. {
  244. if ( is_array( $currencyCodeList ) && count( $currencyCodeList ) > 0 )
  245. {
  246. $db = eZDB::instance();
  247. $db->begin();
  248. eZPersistentObject::removeObject( eZCurrencyData::definition(),
  249. array( 'code' => array( $currencyCodeList ) ) );
  250. $db->commit();
  251. }
  252. }
  253. function setStatus( $status )
  254. {
  255. $statusNumeric = eZCurrencyData::statusStringToNumeric( $status );
  256. if ( $statusNumeric !== false )
  257. {
  258. $this->setAttribute( 'status', $statusNumeric );
  259. }
  260. else
  261. {
  262. eZDebug::writeError( "Unknow currency's status '$status'", 'eZCurrencyData::setStatus' );
  263. }
  264. }
  265. static function statusStringToNumeric( $statusString )
  266. {
  267. $status = false;
  268. if ( is_numeric( $statusString ) )
  269. {
  270. $status = $statusString;
  271. }
  272. if ( is_string( $statusString ) )
  273. {
  274. $statusString = strtoupper( $statusString );
  275. if ( defined( "self::STATUS_{$statusString}" ) )
  276. $status = constant( "self::STATUS_{$statusString}" );
  277. }
  278. return $status;
  279. }
  280. /*!
  281. \static
  282. */
  283. static function errorMessage( $errorCode )
  284. {
  285. switch ( $errorCode )
  286. {
  287. case self::ERROR_INVALID_CURRENCY_CODE:
  288. return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Invalid characters in currency code.' );
  289. case self::ERROR_CURRENCY_EXISTS:
  290. return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Currency already exists.' );
  291. case self::ERROR_UNKNOWN:
  292. default:
  293. return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Unknown error.' );
  294. }
  295. }
  296. function store( $fieldFilters = null )
  297. {
  298. // data changed => reset RateValue
  299. $this->invalidateRateValue();
  300. eZPersistentObject::store( $fieldFilters );
  301. }
  302. function isActive()
  303. {
  304. return ( $this->attribute( 'status' ) == self::STATUS_ACTIVE );
  305. }
  306. public $RateValue;
  307. }
  308. ?>