PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Store.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 1032 lines | 614 code | 115 blank | 303 comment | 99 complexity | bfa306e29352f326d90801e6064c3168 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  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@magentocommerce.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Store model
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. * @category Mage
  31. * @package Mage_Core
  32. */
  33. class Mage_Core_Model_Store extends Mage_Core_Model_Abstract
  34. {
  35. const ENTITY = 'core_store';
  36. const XML_PATH_STORE_IN_URL = 'web/url/use_store';
  37. const XML_PATH_USE_REWRITES = 'web/seo/use_rewrites';
  38. const XML_PATH_UNSECURE_BASE_URL = 'web/unsecure/base_url';
  39. const XML_PATH_SECURE_BASE_URL = 'web/secure/base_url';
  40. const XML_PATH_SECURE_IN_FRONTEND = 'web/secure/use_in_frontend';
  41. const XML_PATH_SECURE_IN_ADMINHTML = 'web/secure/use_in_adminhtml';
  42. const XML_PATH_SECURE_BASE_LINK_URL = 'web/secure/base_link_url';
  43. const XML_PATH_UNSECURE_BASE_LINK_URL = 'web/unsecure/base_link_url';
  44. const XML_PATH_PRICE_SCOPE = 'catalog/price/scope';
  45. const PRICE_SCOPE_GLOBAL = 0;
  46. const PRICE_SCOPE_WEBSITE = 1;
  47. const URL_TYPE_LINK = 'link';
  48. const URL_TYPE_DIRECT_LINK = 'direct_link';
  49. const URL_TYPE_WEB = 'web';
  50. const URL_TYPE_SKIN = 'skin';
  51. const URL_TYPE_JS = 'js';
  52. const URL_TYPE_MEDIA = 'media';
  53. const DEFAULT_CODE = 'default';
  54. const ADMIN_CODE = 'admin';
  55. const CACHE_TAG = 'store';
  56. const COOKIE_NAME = 'store';
  57. /**
  58. * Script name, which returns all the images
  59. */
  60. const MEDIA_REWRITE_SCRIPT = 'get.php/';
  61. protected $_cacheTag = true;
  62. /**
  63. * @var string
  64. */
  65. protected $_eventPrefix = 'store';
  66. /**
  67. * @var string
  68. */
  69. protected $_eventObject = 'store';
  70. protected $_priceFilter;
  71. /**
  72. * Website model
  73. *
  74. * @var Mage_Core_Model_Website
  75. */
  76. protected $_website;
  77. /**
  78. * Group model
  79. *
  80. * @var Mage_Core_Model_Store_Group
  81. */
  82. protected $_group;
  83. protected $_configCache = null;
  84. protected $_configCacheBaseNodes = array();
  85. protected $_dirCache = array();
  86. protected $_urlCache = array();
  87. protected $_baseUrlCache = array();
  88. protected $_session;
  89. protected $_isAdminSecure = null;
  90. protected $_isFrontSecure = null;
  91. protected $_frontendName = null;
  92. /**
  93. * @var bool
  94. */
  95. private $_isReadOnly = false;
  96. protected function _construct()
  97. {
  98. $this->_init('core/store');
  99. $this->_configCacheBaseNodes = array(
  100. self::XML_PATH_PRICE_SCOPE,
  101. self::XML_PATH_SECURE_BASE_URL,
  102. self::XML_PATH_SECURE_IN_ADMINHTML,
  103. self::XML_PATH_SECURE_IN_FRONTEND,
  104. self::XML_PATH_STORE_IN_URL,
  105. self::XML_PATH_UNSECURE_BASE_URL,
  106. self::XML_PATH_USE_REWRITES,
  107. self::XML_PATH_UNSECURE_BASE_LINK_URL,
  108. self::XML_PATH_SECURE_BASE_LINK_URL,
  109. 'general/locale/code'
  110. );
  111. }
  112. /**
  113. * Retrieve store session object
  114. *
  115. * @return Mage_Core_Model_Session_Abstract
  116. */
  117. protected function _getSession()
  118. {
  119. if (!$this->_session) {
  120. $this->_session = Mage::getModel('core/session')
  121. ->init('store_'.$this->getCode());
  122. }
  123. return $this->_session;
  124. }
  125. /**
  126. * Loading store data
  127. *
  128. * @param mixed $id
  129. * @param string $field
  130. * @return Mage_Core_Model_Store
  131. */
  132. public function load($id, $field=null)
  133. {
  134. if (!is_numeric($id) && is_null($field)) {
  135. $this->_getResource()->load($this, $id, 'code');
  136. return $this;
  137. }
  138. return parent::load($id, $field);
  139. }
  140. /**
  141. * Loading store configuration data
  142. *
  143. * @param string $code
  144. * @return Mage_Core_Model_Store
  145. */
  146. public function loadConfig($code)
  147. {
  148. if (is_numeric($code)) {
  149. foreach (Mage::getConfig()->getNode()->stores->children() as $storeCode=>$store) {
  150. if ((int)$store->system->store->id==$code) {
  151. $code = $storeCode;
  152. break;
  153. }
  154. }
  155. } else {
  156. $store = Mage::getConfig()->getNode()->stores->{$code};
  157. }
  158. if (!empty($store)) {
  159. $this->setCode($code);
  160. $id = (int)$store->system->store->id;
  161. $this->setId($id)->setStoreId($id);
  162. $this->setWebsiteId((int)$store->system->website->id);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Retrieve Store code
  168. *
  169. * @return string
  170. */
  171. public function getCode()
  172. {
  173. return $this->_getData('code');
  174. }
  175. /**
  176. * Retrieve store configuration data
  177. *
  178. * @param string $path
  179. * @param string $scope
  180. * @return string|null
  181. */
  182. public function getConfig($path)
  183. {
  184. if (isset($this->_configCache[$path])) {
  185. return $this->_configCache[$path];
  186. }
  187. $config = Mage::getConfig();
  188. $fullPath = 'stores/'.$this->getCode().'/'.$path;
  189. $data = $config->getNode($fullPath);
  190. if (!$data && !Mage::isInstalled()) {
  191. $data = $config->getNode('default/' . $path);
  192. }
  193. if (!$data) {
  194. return null;
  195. }
  196. return $this->_processConfigValue($fullPath, $path, $data);
  197. }
  198. /**
  199. * Initialize base store configuration data
  200. * Method provide cache configuration data without loading store config XML
  201. *
  202. * @return Mage_Core_Model_Config
  203. */
  204. public function initConfigCache()
  205. {
  206. // return $this;
  207. /**
  208. * Funtionality related with config separation
  209. */
  210. if ($this->_configCache === null) {
  211. $code = $this->getCode();
  212. if ($code) {
  213. if (Mage::app()->useCache('config')) {
  214. $cacheId = 'store_' . $code . '_config_cache';
  215. $data = Mage::app()->loadCache($cacheId);
  216. if ($data) {
  217. $data = unserialize($data);
  218. } else {
  219. $data = array();
  220. foreach ($this->_configCacheBaseNodes as $node) {
  221. $data[$node] = $this->getConfig($node);
  222. }
  223. Mage::app()->saveCache(
  224. serialize($data),
  225. $cacheId,
  226. array(self::CACHE_TAG, Mage_Core_Model_Config::CACHE_TAG)
  227. );
  228. }
  229. $this->_configCache = $data;
  230. }
  231. }
  232. }
  233. return $this;
  234. }
  235. /**
  236. * Set config value for CURRENT model
  237. * This value don't save in config
  238. *
  239. * @param string $path
  240. * @param mixed $value
  241. * @return Mage_Core_Model_Store
  242. */
  243. public function setConfig($path, $value)
  244. {
  245. if (isset($this->_configCache[$path])) {
  246. $this->_configCache[$path] = $value;
  247. }
  248. $fullPath = 'stores/'.$this->getCode().'/'.$path;
  249. Mage::getConfig()->setNode($fullPath, $value);
  250. return $this;
  251. }
  252. /**
  253. * Set website model
  254. *
  255. * @param Mage_Core_Model_Website $website
  256. */
  257. public function setWebsite(Mage_Core_Model_Website $website)
  258. {
  259. $this->_website = $website;
  260. }
  261. /**
  262. * Retrieve store website
  263. *
  264. * @return Mage_Core_Model_Website
  265. */
  266. public function getWebsite()
  267. {
  268. if (is_null($this->getWebsiteId())) {
  269. return false;
  270. }
  271. if (is_null($this->_website)) {
  272. $this->_website = Mage::app()->getWebsite($this->getWebsiteId());
  273. }
  274. return $this->_website;
  275. }
  276. /**
  277. * Enter description here...
  278. *
  279. * @param string $fullPath
  280. * @param string $path
  281. * @param Varien_Simplexml_Element $node
  282. * @return string
  283. */
  284. protected function _processConfigValue($fullPath, $path, $node)
  285. {
  286. if (isset($this->_configCache[$path])) {
  287. return $this->_configCache[$path];
  288. }
  289. if ($node->hasChildren()) {
  290. $aValue = array();
  291. foreach ($node->children() as $k=>$v) {
  292. $aValue[$k] = $this->_processConfigValue($fullPath.'/'.$k, $path.'/'.$k, $v);
  293. }
  294. $this->_configCache[$path] = $aValue;
  295. return $aValue;
  296. }
  297. $sValue = (string)$node;
  298. if (!empty($node['backend_model']) && !empty($sValue)) {
  299. $backend = Mage::getModel((string)$node['backend_model']);
  300. $backend->setPath($path)->setValue($sValue)->afterLoad();
  301. $sValue = $backend->getValue();
  302. }
  303. if (is_string($sValue) && strpos($sValue, '{{')!==false) {
  304. if (strpos($sValue, '{{unsecure_base_url}}')!==false) {
  305. $unsecureBaseUrl = $this->getConfig('web/unsecure/base_url');
  306. $sValue = str_replace('{{unsecure_base_url}}', $unsecureBaseUrl, $sValue);
  307. } elseif (strpos($sValue, '{{secure_base_url}}')!==false) {
  308. $secureBaseUrl = $this->getConfig('web/secure/base_url');
  309. $sValue = str_replace('{{secure_base_url}}', $secureBaseUrl, $sValue);
  310. } elseif (strpos($sValue, '{{base_url}}') === false) {
  311. $sValue = Mage::getConfig()->substDistroServerVars($sValue);
  312. }
  313. }
  314. $this->_configCache[$path] = $sValue;
  315. return $sValue;
  316. }
  317. /**
  318. * Convert config values for url pathes
  319. *
  320. * @deprecated after 1.4.2.0
  321. * @param string $value
  322. * @return string
  323. */
  324. public function processSubst($value)
  325. {
  326. if (!is_string($value)) {
  327. return $value;
  328. }
  329. if (strpos($value, '{{unsecure_base_url}}') !== false) {
  330. $unsecureBaseUrl = $this->getConfig('web/unsecure/base_url');
  331. $value = str_replace('{{unsecure_base_url}}', $unsecureBaseUrl, $value);
  332. } elseif (strpos($value, '{{secure_base_url}}') !== false) {
  333. $secureBaseUrl = $this->getConfig('web/secure/base_url');
  334. $value = str_replace('{{secure_base_url}}', $secureBaseUrl, $value);
  335. } elseif (strpos($value, '{{') !== false && strpos($value, '{{base_url}}') === false) {
  336. $value = Mage::getConfig()->substDistroServerVars($value);
  337. }
  338. return $value;
  339. }
  340. public function getDefaultBasePath()
  341. {
  342. if (!isset($_SERVER['SCRIPT_NAME'])) {
  343. return '/';
  344. }
  345. return rtrim(Mage::app()->getRequest()->getBasePath().'/').'/';
  346. }
  347. /**
  348. * Retrieve url using store configuration specific
  349. *
  350. * @param string $route
  351. * @param array $params
  352. * @return string
  353. */
  354. public function getUrl($route = '', $params = array())
  355. {
  356. /* @var $url Mage_Core_Model_Url */
  357. $url = Mage::getModel('core/url')
  358. ->setStore($this);
  359. if (Mage::app()->getStore()->getId() != $this->getId()) {
  360. $params['_store_to_url'] = true;
  361. }
  362. return $url->getUrl($route, $params);
  363. }
  364. public function getBaseUrl($type = self::URL_TYPE_LINK, $secure = null)
  365. {
  366. $cacheKey = $type.'/'.(is_null($secure) ? 'null' : ($secure ? 'true' : 'false'));
  367. if (!isset($this->_baseUrlCache[$cacheKey])) {
  368. switch ($type) {
  369. case self::URL_TYPE_WEB:
  370. $secure = is_null($secure) ? $this->isCurrentlySecure() : (bool)$secure;
  371. $url = $this->getConfig('web/'.($secure ? 'secure' : 'unsecure').'/base_url');
  372. break;
  373. case self::URL_TYPE_LINK:
  374. $secure = (bool)$secure;
  375. $url = $this->getConfig('web/'.($secure ? 'secure' : 'unsecure').'/base_link_url');
  376. $url = $this->_updatePathUseRewrites($url);
  377. $url = $this->_updatePathUseStoreView($url);
  378. break;
  379. case self::URL_TYPE_DIRECT_LINK:
  380. $secure = (bool)$secure;
  381. $url = $this->getConfig('web/'.($secure ? 'secure' : 'unsecure').'/base_link_url');
  382. $url = $this->_updatePathUseRewrites($url);
  383. break;
  384. case self::URL_TYPE_SKIN:
  385. case self::URL_TYPE_JS:
  386. $secure = is_null($secure) ? $this->isCurrentlySecure() : (bool)$secure;
  387. $url = $this->getConfig('web/'.($secure ? 'secure' : 'unsecure').'/base_'.$type.'_url');
  388. break;
  389. case self::URL_TYPE_MEDIA:
  390. $url = $this->_updateMediaPathUseRewrites($secure);
  391. break;
  392. default:
  393. throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid base url type'));
  394. }
  395. if (false !== strpos($url, '{{base_url}}')) {
  396. $baseUrl = Mage::getConfig()->substDistroServerVars('{{base_url}}');
  397. $url = str_replace('{{base_url}}', $baseUrl, $url);
  398. }
  399. $this->_baseUrlCache[$cacheKey] = rtrim($url, '/') . '/';
  400. }
  401. return $this->_baseUrlCache[$cacheKey];
  402. }
  403. /**
  404. * Remove script file name from url in case when server rewrites are enabled
  405. *
  406. * @param string $url
  407. * @return string
  408. */
  409. protected function _updatePathUseRewrites($url)
  410. {
  411. if ($this->isAdmin()
  412. || !$this->getConfig(self::XML_PATH_USE_REWRITES)
  413. || !Mage::isInstalled()) {
  414. $url .= basename($_SERVER['SCRIPT_FILENAME']).'/';
  415. }
  416. return $url;
  417. }
  418. /**
  419. * Gets URL for media catalog.
  420. * If we use Database file storage and server doesn't support rewrites (.htaccess in media folder)
  421. * we have to put name of fetching media script exactly into URL
  422. *
  423. * @param null|boolean $secure
  424. * @param string $type
  425. * @return string
  426. */
  427. protected function _updateMediaPathUseRewrites($secure=null, $type = self::URL_TYPE_MEDIA)
  428. {
  429. $secure = is_null($secure) ? $this->isCurrentlySecure() : (bool)$secure;
  430. $secureStringFlag = $secure ? 'secure' : 'unsecure';
  431. $url = $this->getConfig('web/' . $secureStringFlag . '/base_' . $type . '_url');
  432. if (!$this->getConfig(self::XML_PATH_USE_REWRITES)
  433. && Mage::helper('core/file_storage_database')->checkDbUsage()) {
  434. $urlStart = $this->getConfig('web/' . $secureStringFlag . '/base_url');
  435. $url = str_replace($urlStart, $urlStart . self::MEDIA_REWRITE_SCRIPT, $url);
  436. }
  437. return $url;
  438. }
  439. /**
  440. * Add store code to url in case if it is enabled in configuration
  441. *
  442. * @param string $url
  443. * @return string
  444. */
  445. protected function _updatePathUseStoreView($url)
  446. {
  447. if ($this->getStoreInUrl()) {
  448. $url .= $this->getCode() . '/';
  449. }
  450. return $url;
  451. }
  452. /**
  453. * Returns whether url forming scheme prepends url path with store view code
  454. *
  455. * @return bool
  456. */
  457. public function getStoreInUrl()
  458. {
  459. return Mage::isInstalled() && $this->getConfig(self::XML_PATH_STORE_IN_URL);
  460. }
  461. /**
  462. * Get store identifier
  463. *
  464. * @return int
  465. */
  466. public function getId()
  467. {
  468. return $this->_getData('store_id');
  469. }
  470. /**
  471. * Check if store is admin store
  472. *
  473. * @return unknown
  474. */
  475. public function isAdmin()
  476. {
  477. return $this->getId() == Mage_Core_Model_App::ADMIN_STORE_ID;
  478. }
  479. public function isAdminUrlSecure()
  480. {
  481. if ($this->_isAdminSecure === null) {
  482. $this->_isAdminSecure = Mage::getStoreConfigFlag(
  483. Mage_Core_Model_Url::XML_PATH_SECURE_IN_ADMIN,
  484. $this->getId()
  485. );
  486. }
  487. return $this->_isAdminSecure;
  488. }
  489. public function isFrontUrlSecure()
  490. {
  491. if ($this->_isFrontSecure === null) {
  492. $this->_isFrontSecure = Mage::getStoreConfigFlag(
  493. Mage_Core_Model_Url::XML_PATH_SECURE_IN_FRONT,
  494. $this->getId()
  495. );
  496. }
  497. return $this->_isFrontSecure;
  498. }
  499. public function isCurrentlySecure()
  500. {
  501. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  502. return true;
  503. }
  504. if (Mage::isInstalled()) {
  505. $secureBaseUrl = Mage::getStoreConfig('web/secure/base_route_url');
  506. if (!$secureBaseUrl) {
  507. return false;
  508. }
  509. $uri = Zend_Uri::factory($secureBaseUrl);
  510. $isSecure = ($uri->getScheme() == 'https' )
  511. && isset($_SERVER['SERVER_PORT'])
  512. && ($uri->getPort() == $_SERVER['SERVER_PORT']);
  513. return $isSecure;
  514. } else {
  515. $isSecure = isset($_SERVER['SERVER_PORT']) && (443 == $_SERVER['SERVER_PORT']);
  516. return $isSecure;
  517. }
  518. }
  519. /*************************************************************************************
  520. * Store currency interface
  521. */
  522. /**
  523. * Retrieve store base currency code
  524. *
  525. * @return string
  526. */
  527. public function getBaseCurrencyCode()
  528. {
  529. $configValue = $this->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
  530. if ($configValue == Mage_Core_Model_Store::PRICE_SCOPE_GLOBAL) {
  531. return Mage::app()->getBaseCurrencyCode();
  532. } else {
  533. return $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
  534. }
  535. }
  536. /**
  537. * Retrieve store base currency
  538. *
  539. * @return Mage_Directory_Model_Currency
  540. */
  541. public function getBaseCurrency()
  542. {
  543. $currency = $this->getData('base_currency');
  544. if (is_null($currency)) {
  545. $currency = Mage::getModel('directory/currency')->load($this->getBaseCurrencyCode());
  546. $this->setData('base_currency', $currency);
  547. }
  548. return $currency;
  549. }
  550. /**
  551. * Get default store currency code
  552. *
  553. * @return string
  554. */
  555. public function getDefaultCurrencyCode()
  556. {
  557. $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);
  558. return $result;
  559. }
  560. /**
  561. * Retrieve store default currency
  562. *
  563. * @return Mage_Directory_Model_Currency
  564. */
  565. public function getDefaultCurrency()
  566. {
  567. $currency = $this->getData('default_currency');
  568. if (is_null($currency)) {
  569. $currency = Mage::getModel('directory/currency')->load($this->getDefaultCurrencyCode());
  570. $this->setData('default_currency', $currency);
  571. }
  572. return $currency;
  573. }
  574. /**
  575. * Set current store currency code
  576. *
  577. * @param string $code
  578. * @return string
  579. */
  580. public function setCurrentCurrencyCode($code)
  581. {
  582. $code = strtoupper($code);
  583. if (in_array($code, $this->getAvailableCurrencyCodes())) {
  584. $this->_getSession()->setCurrencyCode($code);
  585. if ($code == $this->getDefaultCurrency()) {
  586. Mage::app()->getCookie()->delete('currency', $code);
  587. } else {
  588. Mage::app()->getCookie()->set('currency', $code);
  589. }
  590. }
  591. return $this;
  592. }
  593. /**
  594. * Get current store currency code
  595. *
  596. * @return string
  597. */
  598. public function getCurrentCurrencyCode()
  599. {
  600. // try to get currently set code among allowed
  601. $code = $this->_getSession()->getCurrencyCode();
  602. if (empty($code)) {
  603. $code = $this->getDefaultCurrencyCode();
  604. }
  605. if (in_array($code, $this->getAvailableCurrencyCodes(true))) {
  606. return $code;
  607. }
  608. // take first one of allowed codes
  609. $codes = array_values($this->getAvailableCurrencyCodes(true));
  610. if (empty($codes)) {
  611. // return default code, if no codes specified at all
  612. return $this->getDefaultCurrencyCode();
  613. }
  614. return array_shift($codes);
  615. }
  616. /**
  617. * Get allowed store currency codes
  618. *
  619. * If base currency is not allowed in current website config scope,
  620. * then it can be disabled with $skipBaseNotAllowed
  621. *
  622. * @param bool $skipBaseNotAllowed
  623. * @return array
  624. */
  625. public function getAvailableCurrencyCodes($skipBaseNotAllowed = false)
  626. {
  627. $codes = $this->getData('available_currency_codes');
  628. if (is_null($codes)) {
  629. $codes = explode(',', $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_ALLOW));
  630. // add base currency, if it is not in allowed currencies
  631. $baseCurrencyCode = $this->getBaseCurrencyCode();
  632. if (!in_array($baseCurrencyCode, $codes)) {
  633. $codes[] = $baseCurrencyCode;
  634. // save base currency code index for further usage
  635. $disallowedBaseCodeIndex = array_keys($codes);
  636. $disallowedBaseCodeIndex = array_pop($disallowedBaseCodeIndex);
  637. $this->setData('disallowed_base_currency_code_index', $disallowedBaseCodeIndex);
  638. }
  639. $this->setData('available_currency_codes', $codes);
  640. }
  641. // remove base currency code, if it is not allowed by config (optional)
  642. if ($skipBaseNotAllowed) {
  643. $disallowedBaseCodeIndex = $this->getData('disallowed_base_currency_code_index');
  644. if (null !== $disallowedBaseCodeIndex) {
  645. unset($codes[$disallowedBaseCodeIndex]);
  646. }
  647. }
  648. return $codes;
  649. }
  650. /**
  651. * Retrieve store current currency
  652. *
  653. * @return Mage_Directory_Model_Currency
  654. */
  655. public function getCurrentCurrency()
  656. {
  657. $currency = $this->getData('current_currency');
  658. if (is_null($currency)) {
  659. $currency = Mage::getModel('directory/currency')->load($this->getCurrentCurrencyCode());
  660. $baseCurrency = $this->getBaseCurrency();
  661. if (! $baseCurrency->getRate($currency)) {
  662. $currency = $baseCurrency;
  663. $this->setCurrentCurrencyCode($baseCurrency->getCode());
  664. }
  665. $this->setData('current_currency', $currency);
  666. }
  667. return $currency;
  668. }
  669. public function getCurrentCurrencyRate()
  670. {
  671. return $this->getBaseCurrency()->getRate($this->getCurrentCurrency());
  672. }
  673. /**
  674. * Convert price from default currency to current currency
  675. *
  676. * @param double $price
  677. * @param boolean $format Format price to currency format
  678. * @param boolean $includeContainer Enclose into <span class="price"><span>
  679. * @return double
  680. */
  681. public function convertPrice($price, $format=false, $includeContainer = true)
  682. {
  683. if ($this->getCurrentCurrency() && $this->getBaseCurrency()) {
  684. $value = $this->getBaseCurrency()->convert($price, $this->getCurrentCurrency());
  685. } else {
  686. $value = $price;
  687. }
  688. if ($this->getCurrentCurrency() && $format) {
  689. $value = $this->formatPrice($value, $includeContainer);
  690. }
  691. return $value;
  692. }
  693. /**
  694. * Round price
  695. *
  696. * @param mixed $price
  697. * @return double
  698. */
  699. public function roundPrice($price)
  700. {
  701. return round($price, 2);
  702. }
  703. /**
  704. * Format price with currency filter (taking rate into consideration)
  705. *
  706. * @param double $price
  707. * @param bool $includeContainer
  708. * @return string
  709. */
  710. public function formatPrice($price, $includeContainer = true)
  711. {
  712. if ($this->getCurrentCurrency()) {
  713. return $this->getCurrentCurrency()->format($price, array(), $includeContainer);
  714. }
  715. return $price;
  716. }
  717. /**
  718. * Get store price filter
  719. *
  720. * @return Varien_Filter_Sprintf
  721. */
  722. public function getPriceFilter()
  723. {
  724. if (!$this->_priceFilter) {
  725. if ($this->getBaseCurrency() && $this->getCurrentCurrency()) {
  726. $this->_priceFilter = $this->getCurrentCurrency()->getFilter();
  727. $this->_priceFilter->setRate($this->getBaseCurrency()->getRate($this->getCurrentCurrency()));
  728. }
  729. elseif($this->getDefaultCurrency()) {
  730. $this->_priceFilter = $this->getDefaultCurrency()->getFilter();
  731. }
  732. else {
  733. $this->_priceFilter = new Varien_Filter_Sprintf('%s', 2);
  734. }
  735. }
  736. return $this->_priceFilter;
  737. }
  738. /**
  739. * Enter description here...
  740. *
  741. * @return int
  742. */
  743. public function getRootCategoryId()
  744. {
  745. if (!$this->getGroup()) {
  746. return 0;
  747. }
  748. return $this->getGroup()->getRootCategoryId();
  749. }
  750. /**
  751. * Set group model for store
  752. *
  753. * @param Mage_Core_Model_Store_Group $group
  754. */
  755. public function setGroup($group)
  756. {
  757. $this->_group = $group;
  758. }
  759. /**
  760. * Retrieve group model
  761. *
  762. * @return Mage_Core_Model_Store_Group
  763. */
  764. public function getGroup()
  765. {
  766. if (is_null($this->getGroupId())) {
  767. return false;
  768. }
  769. if (is_null($this->_group)) {
  770. $this->_group = Mage::getModel('core/store_group')->load($this->getGroupId());
  771. }
  772. return $this->_group;
  773. }
  774. public function getWebsiteId()
  775. {
  776. return $this->_getData('website_id');
  777. }
  778. public function getGroupId()
  779. {
  780. return $this->_getData('group_id');
  781. }
  782. public function getDefaultGroupId()
  783. {
  784. return $this->_getData('default_group_id');
  785. }
  786. public function isCanDelete()
  787. {
  788. if (!$this->getId()) {
  789. return false;
  790. }
  791. return $this->getGroup()->getDefaultStoreId() != $this->getId();
  792. }
  793. /**
  794. * Retrieve current url for store
  795. *
  796. * @param bool|string $fromStore
  797. * @return string
  798. */
  799. public function getCurrentUrl($fromStore = true)
  800. {
  801. $sidQueryParam = $this->_getSession()->getSessionIdQueryParam();
  802. $requestString = Mage::getSingleton('core/url')->escape(
  803. ltrim(Mage::app()->getRequest()->getRequestString(), '/'));
  804. $storeUrl = Mage::app()->getStore()->isCurrentlySecure()
  805. ? $this->getUrl('', array('_secure' => true))
  806. : $this->getUrl('');
  807. $storeParsedUrl = parse_url($storeUrl);
  808. $storeParsedQuery = array();
  809. if (isset($storeParsedUrl['query'])) {
  810. parse_str($storeParsedUrl['query'], $storeParsedQuery);
  811. }
  812. $currQuery = Mage::app()->getRequest()->getQuery();
  813. if (isset($currQuery[$sidQueryParam]) && !empty($currQuery[$sidQueryParam])
  814. && $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]
  815. ) {
  816. unset($currQuery[$sidQueryParam]);
  817. }
  818. foreach ($currQuery as $k => $v) {
  819. $storeParsedQuery[$k] = $v;
  820. }
  821. if (!Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getCode())) {
  822. $storeParsedQuery['___store'] = $this->getCode();
  823. }
  824. if ($fromStore !== false) {
  825. $storeParsedQuery['___from_store'] = $fromStore === true ? Mage::app()->getStore()->getCode() : $fromStore;
  826. }
  827. return $storeParsedUrl['scheme'] . '://' . $storeParsedUrl['host']
  828. . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
  829. . $storeParsedUrl['path'] . $requestString
  830. . ($storeParsedQuery ? '?'.http_build_query($storeParsedQuery, '', '&amp;') : '');
  831. }
  832. public function getIsActive()
  833. {
  834. return $this->_getData('is_active');
  835. }
  836. public function getName()
  837. {
  838. return $this->_getData('name');
  839. }
  840. /**
  841. * Protect delete from non admin area
  842. * Register indexing event before delete store
  843. *
  844. * @return Mage_Core_Model_Store
  845. */
  846. protected function _beforeDelete()
  847. {
  848. $this->_protectFromNonAdmin();
  849. Mage::getSingleton('index/indexer')->logEvent(
  850. $this, self::ENTITY, Mage_Index_Model_Event::TYPE_DELETE
  851. );
  852. return parent::_beforeDelete();
  853. }
  854. /**
  855. * rewrite in order to clear configuration cache
  856. *
  857. * @return Mage_Core_Model_Store
  858. */
  859. protected function _afterDelete()
  860. {
  861. parent::_afterDelete();
  862. Mage::getConfig()->removeCache();
  863. return $this;
  864. }
  865. /**
  866. * Init indexing process after store delete commit
  867. *
  868. * @return Mage_Core_Model_Store
  869. */
  870. protected function _afterDeleteCommit()
  871. {
  872. parent::_afterDeleteCommit();
  873. Mage::getSingleton('index/indexer')->indexEvents(
  874. self::ENTITY, Mage_Index_Model_Event::TYPE_DELETE
  875. );
  876. }
  877. /**
  878. * Reinit and reset Config Data
  879. *
  880. * @return Mage_Core_Model_Store
  881. */
  882. public function resetConfig()
  883. {
  884. Mage::getConfig()->reinit();
  885. $this->_dirCache = array();
  886. $this->_configCache = array();
  887. $this->_baseUrlCache = array();
  888. $this->_urlCache = array();
  889. return $this;
  890. }
  891. /**
  892. * Get/Set isReadOnly flag
  893. *
  894. * @param bool $value
  895. * @return bool
  896. */
  897. public function isReadOnly($value = null)
  898. {
  899. if (null !== $value) {
  900. $this->_isReadOnly = (bool)$value;
  901. }
  902. return $this->_isReadOnly;
  903. }
  904. /**
  905. * Retrieve storegroup name
  906. *
  907. * @return string
  908. */
  909. public function getFrontendName()
  910. {
  911. if (is_null($this->_frontendName)) {
  912. $storeGroupName = (string)Mage::getStoreConfig('general/store_information/name', $this);
  913. $this->_frontendName = (!empty($storeGroupName)) ? $storeGroupName : $this->getGroup()->getName();
  914. }
  915. return $this->_frontendName;
  916. }
  917. }