Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
Currency.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class Currency extends RedBeanModel
41  {
51  private static $currencyIdRowsByCode = array();
52 
53  private static $cachedCurrencyIdByCode = array();
54 
55  private static $cachedCurrencyById = array();
56 
57  private static $allCachedCurrencies = array();
58 
65  protected $isSavableFromRelation = false;
66 
73  public static function getById($id, $modelClassName = null)
74  {
75  assert('$modelClassName == "Currency" || $modelClassName == null');
76  if (isset(self::$cachedCurrencyById[$id]))
77  {
78  return self::$cachedCurrencyById[$id];
79  }
80  $currency = parent::getById($id, $modelClassName);
81  self::$cachedCurrencyById[$id] = $currency;
82  return $currency;
83  }
84 
90  public static function getByCode($code)
91  {
92  assert('is_string($code)');
93  $tableName = Currency::getTableName();
94  $beans = ZurmoRedBean::find($tableName, "code = '$code'");
95  assert('count($beans) <= 1');
96  if (count($beans) == 0)
97  {
98  throw new NotFoundException();
99  }
100  return static::makeModel(end($beans), 'Currency');
101  }
102 
107  public static function getAll($orderBy = null, $sortDescending = false,
108  $modelClassName = null, $buildFirstCurrency = true)
109  {
110  $currencies = parent::getAll($orderBy, $sortDescending, $modelClassName);
111  if (count($currencies) > 0 || $buildFirstCurrency == false)
112  {
113  return $currencies;
114  }
115 
116  return array(self::makeBaseCurrency());
117  }
118 
119  public static function makeBaseCurrency()
120  {
121  $currency = new Currency();
122  $currency->code = Yii::app()->currencyHelper->getBaseCode();
123  $currency->rateToBase = 1;
124  $saved = $currency->save();
125  if (!$saved)
126  {
127  throw new NotSupportedException();
128  }
129  return $currency;
130  }
131 
132  public function __toString()
133  {
134  if (trim($this->code) == '')
135  {
136  return Zurmo::t('Core', '(None)');
137  }
138  return $this->code;
139  }
140 
141  public static function canSaveMetadata()
142  {
143  return true;
144  }
145 
149  public static function getModuleClassName()
150  {
151  return 'ZurmoModule';
152  }
153 
161  protected static function getLabel($language = null)
162  {
163  return Zurmo::t('ZurmoModule', 'Currency', array(), null, $language);
164  }
165 
166  public static function getDefaultMetadata()
167  {
168  $metadata = parent::getDefaultMetadata();
169  $metadata[__CLASS__] = array(
170  'members' => array(
171  'active',
172  'code',
173  'rateToBase',
174  ),
175  'rules' => array(
176  array('active', 'boolean'),
177  array('active', 'default', 'value' => true),
178  array('code', 'required'),
179  array('code', 'unique'),
180  array('code', 'type', 'type' => 'string'),
181  array('code', 'length', 'min' => 3, 'max' => 3),
182  array('code', 'match', 'pattern' => '/^[A-Z][A-Z][A-Z]$/', // Not Coding Standard
183  'message' => 'Code must be a valid currency code.'),
184  array('rateToBase', 'required'),
185  array('rateToBase', 'type', 'type' => 'float'),
186  ),
187  'defaultSortAttribute' => 'code',
188  'lastAttemptedRateUpdateTimeStamp' => null,
189  );
190  return $metadata;
191  }
192 
193  public static function isTypeDeletable()
194  {
195  return true;
196  }
197 
203  public static function getCachedCurrencyByCode($code)
204  {
205  assert('is_string($code)');
206  if (isset(self::$cachedCurrencyIdByCode[$code]) &&
207  self::$cachedCurrencyById[self::$cachedCurrencyIdByCode[$code]])
208  {
209  return self::$cachedCurrencyById[self::$cachedCurrencyIdByCode[$code]];
210  }
211  return null;
212  }
213 
218  public static function setCachedCurrency(Currency $currency)
219  {
220  assert('$currency->id > 0');
221  self::$cachedCurrencyIdByCode[$currency->code] = $currency->id;
222  self::$cachedCurrencyById[$currency->id] = $currency;
223  }
224 
233  public function isUniqueAttributeValue($attributeName, $value)
234  {
235  if ($attributeName != 'code')
236  {
237  return parent::isUniqueAttributeValue($attributeName, $value);
238  }
239  assert('$value !== null');
240  if (isset(static::$currencyIdRowsByCode[$value]))
241  {
242  $rows = static::$currencyIdRowsByCode[$value];
243  }
244  else
245  {
246  $modelClassName = $this->attributeNameToBeanAndClassName[$attributeName][1];
247  $tableName = $modelClassName::getTableName();
248  $rows = ZurmoRedBean::getAll('select id from ' . $tableName . " where $attributeName = ?", array($value));
249  static::$currencyIdRowsByCode[$value] = $rows;
250  }
251  return count($rows) == 0 || count($rows) == 1 && $rows[0]['id'] == $this->id;
252  }
253 
259  public function save($runValidation = true, array $attributeNames = null)
260  {
261  $saved = parent::save($runValidation, $attributeNames);
262  self::resetCaches();
263  return $saved;
264  }
265 
270  public static function getAllCachedCurrencies()
271  {
272  if (empty(self::$allCachedCurrencies))
273  {
274  return null;
275  }
276  return self::$allCachedCurrencies;
277  }
278 
283  public static function setAllCachedCurrencies($currencies)
284  {
285  self::$allCachedCurrencies = $currencies;
286  }
287 
292  public static function resetCaches()
293  {
294  self::$currencyIdRowsByCode = array();
295  self::$cachedCurrencyIdByCode = array();
296  self::$cachedCurrencyById = array();
297  self::$allCachedCurrencies = array();
298  }
299 
300  protected static function translatedAttributeLabels($language)
301  {
302  return array_merge(parent::translatedAttributeLabels($language),
303  array(
304  'active' => Zurmo::t('Core', 'Active', array(), null, $language),
305  'code' => Zurmo::t('ZurmoModule', 'Code', array(), null, $language),
306  'rateToBase' => Zurmo::t('ZurmoModule', 'Rate To Base', array(), null, $language),
307  )
308  );
309  }
310  }
311 ?>
static getLabel($language=null)
Definition: Currency.php:161
static resetCaches()
Definition: Currency.php:292
static getAllCachedCurrencies()
Definition: Currency.php:270
static getModuleClassName()
Definition: Currency.php:149
save($runValidation=true, array $attributeNames=null)
Definition: Currency.php:259
static getByCode($code)
Definition: Currency.php:90
static getCachedCurrencyByCode($code)
Definition: Currency.php:203
static setAllCachedCurrencies($currencies)
Definition: Currency.php:283
static getById($id, $modelClassName=null)
Definition: Currency.php:73
static setCachedCurrency(Currency $currency)
Definition: Currency.php:218
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null, $buildFirstCurrency=true)
Definition: Currency.php:107
isUniqueAttributeValue($attributeName, $value)
Definition: Currency.php:233
static getTableName()
Generated on Sun Nov 29 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.