- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / shop / classes / ezcurrencydata.php
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
31 /*! \file
32 */
33
34 class eZCurrencyData extends eZPersistentObject
35 {
36     const DEFAULT_AUTO_RATE_VALUE = '0.0000';
37     const DEFAULT_CUSTOM_RATE_VALUE = '0.0000';
38     const DEFAULT_RATE_FACTOR_VALUE = '1.0000';
39
40     const ERROR_OK = 0;
41     const ERROR_UNKNOWN = 1;
42     const ERROR_INVALID_CURRENCY_CODE = 2;
43     const ERROR_CURRENCY_EXISTS = 3;
44
45     const STATUS_ACTIVE = '1';
46     const STATUS_INACTIVE = '2';
47
48     function eZCurrencyData( $row )
49     {
50         $this->eZPersistentObject( $row );
51         $this->RateValue = false;
52     }
53
54     static function definition()
55     {
56         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
57                                                         'datatype' => 'integer',
58                                                         'default' => 0,
59                                                         'required' => true ),
60                                          'code' => array( 'name' => 'Code',
61                                                           'datatype' => 'string',
62                                                           'default' => '',
63                                                           'required' => true ),
64                                          'symbol' => array( 'name' => 'Symbol',
65                                                             'datatype' => 'string',
66                                                             'default' => '',
67                                                             'required' => false ),
68                                          'locale' => array( 'name' => 'Locale',
69                                                             'datatype' => 'string',
70                                                             'default' => '',
71                                                             'required' => false ),
72                                          'status' => array( 'name' => 'Status',
73                                                             'datatype' => 'integer',
74                                                             'default' => 0,
75                                                             'required' => true ),
76                                          'auto_rate_value' => array( 'name' => 'AutoRateValue',
77                                                                 'datatype' => 'string',
78                                                                 'default' => self::DEFAULT_AUTO_RATE_VALUE,
79                                                                 'required' => false ),
80                                          'custom_rate_value' => array( 'name' => 'CustomRateValue',
81                                                                   'datatype' => 'string',
82                                                                   'default' => self::DEFAULT_CUSTOM_RATE_VALUE,
83                                                                   'required' => false ),
84                                          'rate_factor' => array( 'name' => 'RateFactor',
85                                                                  'datatype' => 'string',
86                                                                  'default' => self::DEFAULT_RATE_FACTOR_VALUE,
87                                                                  'required' => false ) ),
88                       'keys' => array( 'id' ),
89                       'increment_key' => 'id',
90                       'function_attributes' => array( 'rate_value' => 'rateValue' ),
91                       'class_name' => "eZCurrencyData",
92                       'sort' => array( 'code' => 'asc' ),
93                       'name' => "ezcurrencydata" );
94     }
95
96     /*!
97      \static
98      \params codeList can be a single code like 'USD' or an array like array( 'USD', 'NOK' )
99      or 'false' (means all currencies).
100     */
101     static function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false, $asHash = true )
102     {
103         $currencyList = array();
104         $sort = null;
105         $limitation = null;
106         if ( $offset !== false or $limit !== false )
107             $limitation = array( 'offset' => $offset, 'length' => $limit );
108
109         $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
110                                                      null,
111                                                      $conditions,
112                                                      $sort,
113                                                      $limitation,
114                                                      $asObjects );
115
116         if ( count( $rows ) > 0 )
117         {
118             if ( $asHash )
119             {
120                 $keys = array_keys( $rows );
121                 foreach ( $keys as $key )
122                 {
123                     if ( $asObjects )
124                         $currencyList[$rows[$key]->attribute( 'code' )] = $rows[$key];
125                     else
126                         $currencyList[$rows[$key]['code']] = $rows[$key];
127                 }
128             }
129             else
130             {
131                 $currencyList = $rows;
132             }
133         }
134
135         return $currencyList;
136     }
137
138     /*!
139      \static
140     */
141     static function fetchListCount( $conditions = null )
142     {
143         $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
144                                                      array(),
145                                                      $conditions,
146                                                      false,
147                                                      null,
148                                                      false,
149                                                      false,
150                                                      array( array( 'operation' => 'count( * )',
151                                                                    'name' => 'count' ) ) );
152         return $rows[0]['count'];
153     }
154
155     /*!
156      \static
157     */
158     static function fetch( $currencyCode, $asObject = true )
159     {
160         if ( $currencyCode )
161         {
162             $currency = eZCurrencyData::fetchList( array( 'code' => $currencyCode ), $asObject );
163             if ( is_array( $currency ) && count( $currency ) > 0 )
164                 return $currency[$currencyCode];
165         }
166
167         return null;
168     }
169
170     /*!
171      functional attribute
172     */
173     function rateValue()
174     {
175         if ( $this->RateValue === false )
176         {
177             /*
178             $rateValue = '0.00000';
179             if ( $this->attribute( 'custom_rate_value' ) > 0 )
180             {
181                 $rateValue = $this->attribute( 'custom_rate_value' );
182             }
183             else
184             {
185                 $rateValue = $this->attribute( 'auto_rate_value' );
186                 $rateValue = $rateValue * $this->attribute( 'rate_factor' );
187                 $rateValue = sprintf( "%7.5f", $rateValue );
188             }
189             */
190
191             $rateValue = '0.00000';
192             if ( $this->attribute( 'custom_rate_value' ) > 0 )
193                 $rateValue = $this->attribute( 'custom_rate_value' );
194             else
195                 $rateValue = $this->attribute( 'auto_rate_value' );
196
197             if ( $rateValue > 0 )
198                 $rateValue = $rateValue * $this->attribute( 'rate_factor' );
199
200             $rateValue = sprintf( "%7.5f", $rateValue );
201
202             $this->RateValue = $rateValue;
203         }
204
205         return $this->RateValue;
206     }
207
208     function invalidateRateValue()
209     {
210         $this->RateValue = false;
211     }
212
213     /*!
214      \static
215     */
216     static function create( $code, $symbol, $locale, $autoRateValue, $customRateValue, $rateFactor, $status = self::STATUS_ACTIVE )
217     {
218         $code = strtoupper( $code );
219         $errCode = eZCurrencyData::canCreate( $code );
220         if ( $errCode === self::ERROR_OK )
221         {
222             $currency = new eZCurrencyData( array( 'code' => $code,
223                                                    'symbol' => $symbol,
224                                                    'locale' => $locale,
225                                                    'status' => $status,
226                                                    'auto_rate_value' => $autoRateValue,
227                                                    'custom_rate_value' => $customRateValue,
228                                                    'rate_factor' => $rateFactor ) );
229             $currency->setHasDirtyData( true );
230             return $currency;
231         }
232
233         return $errCode;
234     }
235
236     /*!
237      \static
238    */
239     static function canCreate( $code )
240     {
241         $errCode = eZCurrencyData::validateCurrencyCode( $code );
242         if ( $errCode === self::ERROR_OK && eZCurrencyData::currencyExists( $code ) )
243             $errCode = self::ERROR_CURRENCY_EXISTS;
244
245         return $errCode;
246     }
247
248     /*!
249      \static
250     */
251     static function validateCurrencyCode( $code )
252     {
253         if ( !preg_match( "/^[A-Z]{3}$/", $code ) )
254             return self::ERROR_INVALID_CURRENCY_CODE;
255
256         return self::ERROR_OK;
257     }
258
259     /*!
260      \static
261     */
262     static function currencyExists( $code )
263     {
264         return ( eZCurrencyData::fetch( $code ) !== null );
265     }
266
267     /*!
268      \static
269     */
270     static function removeCurrencyList( $currencyCodeList )
271     {
272         if ( is_array( $currencyCodeList ) && count( $currencyCodeList ) > 0 )
273         {
274             $db = eZDB::instance();
275             $db->begin();
276                 eZPersistentObject::removeObject( eZCurrencyData::definition(),
277                                                   array( 'code' => array( $currencyCodeList ) ) );
278             $db->commit();
279         }
280     }
281
282     function setStatus( $status )
283     {
284         $statusNumeric = eZCurrencyData::statusStringToNumeric( $status );
285         if ( $statusNumeric !== false )
286         {
287             $this->setAttribute( 'status', $statusNumeric );
288         }
289         else
290         {
291             eZDebug::writeError( "Unknow currency's status '$status'", 'eZCurrencyData::setStatus' );
292         }
293     }
294
295     static function statusStringToNumeric( $statusString )
296     {
297         $status = false;
298         if ( is_numeric( $statusString ) )
299         {
300             $status = $statusString;
301         }
302         if ( is_string( $statusString ) )
303         {
304             $statusString = strtoupper( $statusString );
305             if ( defined( "self::STATUS_{$statusString}" ) )
306                 $status = constant( "self::STATUS_{$statusString}" );
307         }
308
309         return $status;
310     }
311
312     /*!
313      \static
314     */
315     static function errorMessage( $errorCode )
316     {
317         switch ( $errorCode )
318         {
319             case self::ERROR_INVALID_CURRENCY_CODE:
320                 return ezpI18n::translate( 'kernel/shop/classes/ezcurrencydata', 'Invalid characters in currency code.' );
321
322             case self::ERROR_CURRENCY_EXISTS:
323                 return ezpI18n::translate( 'kernel/shop/classes/ezcurrencydata', 'Currency already exists.' );
324
325             case self::ERROR_UNKNOWN:
326             default:
327                 return ezpI18n::translate( 'kernel/shop/classes/ezcurrencydata', 'Unknown error.' );
328         }
329     }
330
331     function store( $fieldFilters = null )
332     {
333         // data changed => reset RateValue
334         $this->invalidateRateValue();
335         eZPersistentObject::store( $fieldFilters );
336     }
337
338     function isActive()
339     {
340         return ( $this->attribute( 'status' ) == self::STATUS_ACTIVE );
341     }
342
343     public $RateValue;
344 }
345
346 ?>