| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * 2007-2011 PrestaShop |
|---|
| 4 | * |
|---|
| 5 | * NOTICE OF LICENSE |
|---|
| 6 | * |
|---|
| 7 | * This source file is subject to the Open Software License (OSL 3.0) |
|---|
| 8 | * that is bundled with this package in the file LICENSE.txt. |
|---|
| 9 | * It is also available through the world-wide-web at this URL: |
|---|
| 10 | * http://opensource.org/licenses/osl-3.0.php |
|---|
| 11 | * If you did not receive a copy of the license and are unable to |
|---|
| 12 | * obtain it through the world-wide-web, please send an email |
|---|
| 13 | * to license@prestashop.com so we can send you a copy immediately. |
|---|
| 14 | * |
|---|
| 15 | * DISCLAIMER |
|---|
| 16 | * |
|---|
| 17 | * Do not edit or add to this file if you wish to upgrade PrestaShop to newer |
|---|
| 18 | * versions in the future. If you wish to customize PrestaShop for your |
|---|
| 19 | * needs please refer to http://www.prestashop.com for more information. |
|---|
| 20 | * |
|---|
| 21 | * @author PrestaShop SA <contact@prestashop.com> |
|---|
| 22 | * @copyright 2007-2011 PrestaShop SA |
|---|
| 23 | * @version Release: $Revision: 1.4 $ |
|---|
| 24 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|---|
| 25 | * International Registered Trademark & Property of PrestaShop SA |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | class CurrencyCore extends ObjectModel |
|---|
| 29 | { |
|---|
| 30 | public $id; |
|---|
| 31 | |
|---|
| 32 | /** @var string Name */ |
|---|
| 33 | public $name; |
|---|
| 34 | |
|---|
| 35 | /** @var string Iso code */ |
|---|
| 36 | public $iso_code; |
|---|
| 37 | |
|---|
| 38 | /** @var string Iso code numeric */ |
|---|
| 39 | public $iso_code_num; |
|---|
| 40 | |
|---|
| 41 | /** @var string Symbol for short display */ |
|---|
| 42 | public $sign; |
|---|
| 43 | |
|---|
| 44 | /** @var int bool used for displaying blank between sign and price */ |
|---|
| 45 | public $blank; |
|---|
| 46 | |
|---|
| 47 | /** @var string Conversion rate from euros */ |
|---|
| 48 | public $conversion_rate; |
|---|
| 49 | |
|---|
| 50 | /** @var boolean True if currency has been deleted (staying in database as deleted) */ |
|---|
| 51 | public $deleted = 0; |
|---|
| 52 | |
|---|
| 53 | /** @var int ID used for displaying prices */ |
|---|
| 54 | public $format; |
|---|
| 55 | |
|---|
| 56 | /** @var int bool Display decimals on prices */ |
|---|
| 57 | public $decimals; |
|---|
| 58 | |
|---|
| 59 | /** @var int bool active */ |
|---|
| 60 | public $active; |
|---|
| 61 | |
|---|
| 62 | protected $fieldsRequired = array('name', 'iso_code', 'sign', 'conversion_rate', 'format', 'decimals'); |
|---|
| 63 | protected $fieldsSize = array('name' => 32, 'iso_code' => 3, 'iso_code_num' => 3, 'sign' => 8); |
|---|
| 64 | protected $fieldsValidate = array('name' => 'isGenericName', 'iso_code' => 'isLanguageIsoCode', 'iso_code_num' => 'isNumericIsoCode', 'blank' => 'isInt', 'sign' => 'isGenericName', |
|---|
| 65 | 'format' => 'isUnsignedId', 'decimals' => 'isBool', 'conversion_rate' => 'isFloat', 'deleted' => 'isBool', 'active' => 'isBool'); |
|---|
| 66 | |
|---|
| 67 | protected $table = 'currency'; |
|---|
| 68 | protected $identifier = 'id_currency'; |
|---|
| 69 | |
|---|
| 70 | /** @var Currency Current currency */ |
|---|
| 71 | static protected $current = NULL; |
|---|
| 72 | /** @var array Currency cache */ |
|---|
| 73 | static protected $currencies = array(); |
|---|
| 74 | |
|---|
| 75 | protected $webserviceParameters = array( |
|---|
| 76 | 'fields' => array( |
|---|
| 77 | ), |
|---|
| 78 | ); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Overriding check if currency with the same iso code already exists. |
|---|
| 83 | * If it's true, currency is doesn't added. |
|---|
| 84 | * |
|---|
| 85 | * @see ObjectModelCore::add() |
|---|
| 86 | */ |
|---|
| 87 | public function add($autodate = true, $nullValues = false) |
|---|
| 88 | { |
|---|
| 89 | return Currency::exists($this->iso_code) ? false : parent::add(); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Check if a curency already exists. |
|---|
| 94 | * |
|---|
| 95 | * @param int|string $iso_code int for iso code number string for iso code |
|---|
| 96 | * @return boolean |
|---|
| 97 | */ |
|---|
| 98 | public static function exists ($iso_code) |
|---|
| 99 | { |
|---|
| 100 | if(is_int($iso_code)) |
|---|
| 101 | $id_currency_exists = Currency::getIdByIsoCodeNum($iso_code); |
|---|
| 102 | else |
|---|
| 103 | $id_currency_exists = Currency::getIdByIsoCode($iso_code); |
|---|
| 104 | |
|---|
| 105 | if ($id_currency_exists){ |
|---|
| 106 | return true; |
|---|
| 107 | } else { |
|---|
| 108 | return false; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | public function getFields() |
|---|
| 112 | { |
|---|
| 113 | parent::validateFields(); |
|---|
| 114 | |
|---|
| 115 | $fields['name'] = pSQL($this->name); |
|---|
| 116 | $fields['iso_code'] = pSQL($this->iso_code); |
|---|
| 117 | $fields['iso_code_num'] = pSQL($this->iso_code_num); |
|---|
| 118 | $fields['sign'] = pSQL($this->sign); |
|---|
| 119 | $fields['format'] = (int)($this->format); |
|---|
| 120 | $fields['decimals'] = (int)($this->decimals); |
|---|
| 121 | $fields['blank'] = (int)($this->blank); |
|---|
| 122 | $fields['conversion_rate'] = (float)($this->conversion_rate); |
|---|
| 123 | $fields['deleted'] = (int)($this->deleted); |
|---|
| 124 | $fields['active'] = (int)($this->active); |
|---|
| 125 | |
|---|
| 126 | return $fields; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public function deleteSelection($selection) |
|---|
| 130 | { |
|---|
| 131 | if (!is_array($selection) OR !Validate::isTableOrIdentifier($this->identifier) OR !Validate::isTableOrIdentifier($this->table)) |
|---|
| 132 | die(Tools::displayError()); |
|---|
| 133 | $result = true; |
|---|
| 134 | foreach ($selection AS $id) |
|---|
| 135 | { |
|---|
| 136 | $obj = new Currency((int)($id)); |
|---|
| 137 | $res[$id] = $obj->delete(); |
|---|
| 138 | } |
|---|
| 139 | foreach ($res AS $value) |
|---|
| 140 | if (!$value) |
|---|
| 141 | return false; |
|---|
| 142 | return true; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | public function delete() |
|---|
| 146 | { |
|---|
| 147 | if ($this->id == Configuration::get('PS_CURRENCY_DEFAULT')) |
|---|
| 148 | { |
|---|
| 149 | $result = Db::getInstance()->getRow('SELECT `id_currency` FROM '._DB_PREFIX_.'currency WHERE `id_currency` != '.(int)($this->id).' AND `deleted` = 0'); |
|---|
| 150 | if (!$result['id_currency']) |
|---|
| 151 | return false; |
|---|
| 152 | Configuration::updateValue('PS_CURRENCY_DEFAULT', $result['id_currency']); |
|---|
| 153 | } |
|---|
| 154 | $this->deleted = 1; |
|---|
| 155 | return $this->update(); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | * Return formated sign |
|---|
| 160 | * |
|---|
| 161 | * @param string $side left or right |
|---|
| 162 | * @return string formated sign |
|---|
| 163 | */ |
|---|
| 164 | public function getSign($side=NULL) |
|---|
| 165 | { |
|---|
| 166 | if (!$side) |
|---|
| 167 | return $this->sign; |
|---|
| 168 | $formated_strings = array( |
|---|
| 169 | 'left' => $this->sign.' ', |
|---|
| 170 | 'right' => ' '.$this->sign |
|---|
| 171 | ); |
|---|
| 172 | $formats = array( |
|---|
| 173 | 1 => array('left' => &$formated_strings['left'], 'right' => ''), |
|---|
| 174 | 2 => array('left' => '', 'right' => &$formated_strings['right']), |
|---|
| 175 | 3 => array('left' => &$formated_strings['left'], 'right' => ''), |
|---|
| 176 | 4 => array('left' => '', 'right' => &$formated_strings['right']), |
|---|
| 177 | ); |
|---|
| 178 | return ($formats[$this->format][$side]); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * Return available currencies |
|---|
| 183 | * |
|---|
| 184 | * @return array Currencies |
|---|
| 185 | */ |
|---|
| 186 | static public function getCurrencies($object = false, $active = 1) |
|---|
| 187 | { |
|---|
| 188 | $tab = Db::getInstance()->ExecuteS(' |
|---|
| 189 | SELECT * |
|---|
| 190 | FROM `'._DB_PREFIX_.'currency` |
|---|
| 191 | WHERE `deleted` = 0 |
|---|
| 192 | '.($active == 1 ? 'AND `active` = 1' : '').' |
|---|
| 193 | ORDER BY `name` ASC'); |
|---|
| 194 | if ($object) |
|---|
| 195 | foreach ($tab as $key => $currency) |
|---|
| 196 | $tab[$key] = Currency::getCurrencyInstance($currency['id_currency']); |
|---|
| 197 | return $tab; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | static public function getPaymentCurrenciesSpecial($id_module) |
|---|
| 201 | { |
|---|
| 202 | return Db::getInstance()->getRow(' |
|---|
| 203 | SELECT mc.* |
|---|
| 204 | FROM `'._DB_PREFIX_.'module_currency` mc |
|---|
| 205 | WHERE mc.`id_module` = '.(int)($id_module)); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | static public function getPaymentCurrencies($id_module) |
|---|
| 209 | { |
|---|
| 210 | return Db::getInstance()->ExecuteS(' |
|---|
| 211 | SELECT c.* |
|---|
| 212 | FROM `'._DB_PREFIX_.'module_currency` mc |
|---|
| 213 | LEFT JOIN `'._DB_PREFIX_.'currency` c ON c.`id_currency` = mc.`id_currency` |
|---|
| 214 | WHERE c.`deleted` = 0 |
|---|
| 215 | AND mc.`id_module` = '.(int)($id_module).' |
|---|
| 216 | ORDER BY c.`name` ASC'); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | static public function checkPaymentCurrencies($id_module) |
|---|
| 220 | { |
|---|
| 221 | return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' |
|---|
| 222 | SELECT mc.* |
|---|
| 223 | FROM `'._DB_PREFIX_.'module_currency` mc |
|---|
| 224 | WHERE mc.`id_module` = '.(int)($id_module)); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | static public function getCurrency($id_currency) |
|---|
| 228 | { |
|---|
| 229 | return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' |
|---|
| 230 | SELECT * |
|---|
| 231 | FROM `'._DB_PREFIX_.'currency` |
|---|
| 232 | WHERE `deleted` = 0 |
|---|
| 233 | AND `id_currency` = '.(int)($id_currency)); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | static public function getIdByIsoCode($iso_code) |
|---|
| 237 | { |
|---|
| 238 | $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' |
|---|
| 239 | SELECT `id_currency` |
|---|
| 240 | FROM `'._DB_PREFIX_.'currency` |
|---|
| 241 | WHERE `deleted` = 0 |
|---|
| 242 | AND `iso_code` = \''.pSQL($iso_code).'\''); |
|---|
| 243 | return $result['id_currency']; |
|---|
| 244 | } |
|---|
| 245 | static public function getIdByIsoCodeNum($iso_code) |
|---|
| 246 | { |
|---|
| 247 | $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' |
|---|
| 248 | SELECT `id_currency` |
|---|
| 249 | FROM `'._DB_PREFIX_.'currency` |
|---|
| 250 | WHERE `deleted` = 0 |
|---|
| 251 | AND `iso_code_num` = \''.pSQL($iso_code).'\''); |
|---|
| 252 | return (int)$result['id_currency']; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | /** |
|---|
| 256 | * Refresh the currency conversion rate |
|---|
| 257 | * The XML file define conversion rate for each from a default currency ($isoCodeSource). |
|---|
| 258 | * |
|---|
| 259 | * @param $data XML content which contains all the conversion rates |
|---|
| 260 | * @param $isoCodeSource The default currency used in the XML file |
|---|
| 261 | * @param $defaultCurrency The default currency object |
|---|
| 262 | */ |
|---|
| 263 | public function refreshCurrency($data, $isoCodeSource, $defaultCurrency) |
|---|
| 264 | { |
|---|
| 265 | // fetch the conversion rate of the default currency |
|---|
| 266 | $conversion_rate = 1; |
|---|
| 267 | if ($defaultCurrency->iso_code != $isoCodeSource) |
|---|
| 268 | { |
|---|
| 269 | foreach ($data->currency AS $currency) |
|---|
| 270 | if ($currency['iso_code'] == $defaultCurrency->iso_code) |
|---|
| 271 | { |
|---|
| 272 | $conversion_rate = round((float)$currency['rate'], 6); |
|---|
| 273 | break; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if ($defaultCurrency->iso_code == $this->iso_code) |
|---|
| 278 | $this->conversion_rate = 1; |
|---|
| 279 | else |
|---|
| 280 | { |
|---|
| 281 | if ($this->iso_code == $isoCodeSource) |
|---|
| 282 | $rate = 1; |
|---|
| 283 | else |
|---|
| 284 | { |
|---|
| 285 | foreach ($data->currency AS $obj) |
|---|
| 286 | if ($this->iso_code == strval($obj['iso_code'])) |
|---|
| 287 | { |
|---|
| 288 | $rate = (float) $obj['rate']; |
|---|
| 289 | break; |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | $this->conversion_rate = round($rate / $conversion_rate, 6); |
|---|
| 294 | } |
|---|
| 295 | $this->update(); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /** |
|---|
| 299 | * @deprecated |
|---|
| 300 | **/ |
|---|
| 301 | static public function refreshCurrenciesGetDefault($data, $isoCodeSource, $idCurrency) |
|---|
| 302 | { |
|---|
| 303 | Tools::displayAsDeprecated(); |
|---|
| 304 | |
|---|
| 305 | $defaultCurrency = new Currency($idCurrency); |
|---|
| 306 | |
|---|
| 307 | /* Change defaultCurrency rate if not as currency of feed source */ |
|---|
| 308 | if ($defaultCurrency->iso_code != $isoCodeSource) |
|---|
| 309 | foreach ($data->currency AS $obj) |
|---|
| 310 | if ($defaultCurrency->iso_code == strval($obj['iso_code'])) |
|---|
| 311 | $defaultCurrency->conversion_rate = round((float)($obj['rate']), 6); |
|---|
| 312 | |
|---|
| 313 | return $defaultCurrency; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | public static function getDefaultCurrency() |
|---|
| 317 | { |
|---|
| 318 | $id_currency = (int)Configuration::get('PS_CURRENCY_DEFAULT'); |
|---|
| 319 | if ($id_currency == 0) |
|---|
| 320 | return false; |
|---|
| 321 | |
|---|
| 322 | return new Currency($id_currency); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | static public function refreshCurrencies() |
|---|
| 326 | { |
|---|
| 327 | // Parse |
|---|
| 328 | if (!$feed = @simplexml_load_file('http://www.prestashop.com/xml/currencies.xml')) |
|---|
| 329 | return Tools::displayError('Cannot parse feed.'); |
|---|
| 330 | |
|---|
| 331 | // Default feed currency (EUR) |
|---|
| 332 | $isoCodeSource = strval($feed->source['iso_code']); |
|---|
| 333 | |
|---|
| 334 | if (!$default_currency = self::getDefaultCurrency()) |
|---|
| 335 | return Tools::displayError('No default currency'); |
|---|
| 336 | |
|---|
| 337 | $currencies = self::getCurrencies(true); |
|---|
| 338 | foreach ($currencies as $currency) |
|---|
| 339 | $currency->refreshCurrency($feed->list, $isoCodeSource, $default_currency); |
|---|
| 340 | |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | static public function getCurrent() |
|---|
| 344 | { |
|---|
| 345 | global $cookie; |
|---|
| 346 | |
|---|
| 347 | if (!self::$current) |
|---|
| 348 | { |
|---|
| 349 | if (isset($cookie->id_currency) AND $cookie->id_currency) |
|---|
| 350 | self::$current = new Currency((int)($cookie->id_currency)); |
|---|
| 351 | else |
|---|
| 352 | self::$current = new Currency((int)(Configuration::get('PS_CURRENCY_DEFAULT'))); |
|---|
| 353 | } |
|---|
| 354 | return self::$current; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | static public function getCurrencyInstance($id) |
|---|
| 358 | { |
|---|
| 359 | if (!array_key_exists($id, self::$currencies)) |
|---|
| 360 | self::$currencies[(int)($id)] = new Currency((int)($id)); |
|---|
| 361 | return self::$currencies[(int)($id)]; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | |
|---|