PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 1464 lines | 782 code | 148 blank | 534 comment | 123 complexity | edfac27b4f03d6efb868a9d006dd740c 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. * Application model
  28. *
  29. * Application should have: areas, store, locale, translator, design package
  30. *
  31. * @category Mage
  32. * @package Mage_Core
  33. * @author Magento Core Team <core@magentocommerce.com>
  34. */
  35. class Mage_Core_Model_App
  36. {
  37. const XML_PATH_INSTALL_DATE = 'global/install/date';
  38. const DEFAULT_ERROR_HANDLER = 'mageCoreErrorHandler';
  39. const DISTRO_LOCALE_CODE = 'en_US';
  40. /**
  41. * Cache tag for all cache data exclude config cache
  42. *
  43. */
  44. const CACHE_TAG = 'MAGE';
  45. /**
  46. * Default store Id (for install)
  47. */
  48. const DISTRO_STORE_ID = 1;
  49. /**
  50. * Default store code (for install)
  51. *
  52. */
  53. const DISTRO_STORE_CODE = 'default';
  54. /**
  55. * Admin store Id
  56. *
  57. */
  58. const ADMIN_STORE_ID = 0;
  59. /**
  60. * Application loaded areas array
  61. *
  62. * @var array
  63. */
  64. protected $_areas = array();
  65. /**
  66. * Application store object
  67. *
  68. * @var Mage_Core_Model_Store
  69. */
  70. protected $_store;
  71. /**
  72. * Application website object
  73. *
  74. * @var Mage_Core_Model_Website
  75. */
  76. protected $_website;
  77. /**
  78. * Application location object
  79. *
  80. * @var Mage_Core_Model_Locale
  81. */
  82. protected $_locale;
  83. /**
  84. * Application translate object
  85. *
  86. * @var Mage_Core_Model_Translate
  87. */
  88. protected $_translator;
  89. /**
  90. * Application design package object
  91. *
  92. * @var Mage_Core_Model_Design_Package
  93. */
  94. protected $_design;
  95. /**
  96. * Application layout object
  97. *
  98. * @var Mage_Core_Model_Layout
  99. */
  100. protected $_layout;
  101. /**
  102. * Application configuration object
  103. *
  104. * @var Mage_Core_Model_Config
  105. */
  106. protected $_config;
  107. /**
  108. * Application front controller
  109. *
  110. * @var Mage_Core_Controller_Varien_Front
  111. */
  112. protected $_frontController;
  113. /**
  114. * Cache object
  115. *
  116. * @var Zend_Cache_Core
  117. */
  118. protected $_cache;
  119. /**
  120. * Use Cache
  121. *
  122. * @var array
  123. */
  124. protected $_useCache;
  125. /**
  126. * Websites cache
  127. *
  128. * @var array
  129. */
  130. protected $_websites = array();
  131. /**
  132. * Groups cache
  133. *
  134. * @var array
  135. */
  136. protected $_groups = array();
  137. /**
  138. * Stores cache
  139. *
  140. * @var array
  141. */
  142. protected $_stores = array();
  143. /**
  144. * is a single store mode
  145. *
  146. * @var bool
  147. */
  148. protected $_isSingleStore;
  149. /**
  150. * @var bool
  151. */
  152. protected $_isSingleStoreAllowed = true;
  153. /**
  154. * Default store code
  155. *
  156. * @var string
  157. */
  158. protected $_currentStore;
  159. /**
  160. * Request object
  161. *
  162. * @var Zend_Controller_Request_Http
  163. */
  164. protected $_request;
  165. /**
  166. * Response object
  167. *
  168. * @var Zend_Controller_Response_Http
  169. */
  170. protected $_response;
  171. /**
  172. * Events cache
  173. *
  174. * @var array
  175. */
  176. protected $_events = array();
  177. /**
  178. * Update process run flag
  179. *
  180. * @var bool
  181. */
  182. protected $_updateMode = false;
  183. /**
  184. * Use session in URL flag
  185. *
  186. * @see Mage_Core_Model_Url
  187. * @var bool
  188. */
  189. protected $_useSessionInUrl = true;
  190. /**
  191. * Use session var instead of SID for session in URL
  192. *
  193. * @var bool
  194. */
  195. protected $_useSessionVar = false;
  196. protected $_isCacheLocked = null;
  197. /**
  198. * Constructor
  199. */
  200. public function __construct()
  201. {
  202. }
  203. /**
  204. * Initialize application without request processing
  205. *
  206. * @param string|array $code
  207. * @param string $type
  208. * @param string|array $options
  209. * @return Mage_Core_Model_App
  210. */
  211. public function init($code, $type = null, $options = array())
  212. {
  213. $this->_initEnvironment();
  214. if (is_string($options)) {
  215. $options = array('etc_dir'=>$options);
  216. }
  217. Varien_Profiler::start('mage::app::init::config');
  218. $this->_config = Mage::getConfig();
  219. $this->_config->setOptions($options);
  220. $this->_initBaseConfig();
  221. $this->_initCache();
  222. $this->_config->init($options);
  223. Varien_Profiler::stop('mage::app::init::config');
  224. if (Mage::isInstalled($options)) {
  225. $this->_initCurrentStore($code, $type);
  226. $this->_initRequest();
  227. }
  228. return $this;
  229. }
  230. /**
  231. * Common logic for all run types
  232. *
  233. * @param string|array $options
  234. * @return Mage_Core_Model_App
  235. */
  236. public function baseInit($options)
  237. {
  238. $this->_initEnvironment();
  239. $this->_config = Mage::getConfig();
  240. $this->_config->setOptions($options);
  241. $this->_initBaseConfig();
  242. $this->_initCache();
  243. return $this;
  244. }
  245. /**
  246. * Run light version of application with specified modules support
  247. *
  248. * @see Mage_Core_Model_App->run()
  249. *
  250. * @param string|array $scopeCode
  251. * @param string $scopeType
  252. * @param string|array $options
  253. * @param string|array $modules
  254. * @return Mage_Core_Model_App
  255. */
  256. public function initSpecified($scopeCode, $scopeType = null, $options = array(), $modules = array())
  257. {
  258. $this->baseInit($options);
  259. if (!empty($modules)) {
  260. $this->_config->addAllowedModules($modules);
  261. }
  262. $this->_initModules();
  263. $this->_initCurrentStore($scopeCode, $scopeType);
  264. return $this;
  265. }
  266. /**
  267. * Run application. Run process responsible for request processing and sending response.
  268. * List of supported parameters:
  269. * scope_code - code of default scope (website/store_group/store code)
  270. * scope_type - type of default scope (website/group/store)
  271. * options - configuration options
  272. *
  273. * @param array $params application run parameters
  274. * @return Mage_Core_Model_App
  275. */
  276. public function run($params)
  277. {
  278. $options = isset($params['options']) ? $params['options'] : array();
  279. $this->baseInit($options);
  280. if ($this->_cache->processRequest()) {
  281. $this->getResponse()->sendResponse();
  282. } else {
  283. $this->_initModules();
  284. $this->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
  285. if ($this->_config->isLocalConfigLoaded()) {
  286. $scopeCode = isset($params['scope_code']) ? $params['scope_code'] : '';
  287. $scopeType = isset($params['scope_type']) ? $params['scope_type'] : 'store';
  288. $this->_initCurrentStore($scopeCode, $scopeType);
  289. $this->_initRequest();
  290. Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
  291. }
  292. $this->getFrontController()->dispatch();
  293. }
  294. return $this;
  295. }
  296. /**
  297. * Initialize PHP environment
  298. *
  299. * @return Mage_Core_Model_App
  300. */
  301. protected function _initEnvironment()
  302. {
  303. $this->setErrorHandler(self::DEFAULT_ERROR_HANDLER);
  304. date_default_timezone_set(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
  305. return $this;
  306. }
  307. /**
  308. * Initialize base system configuration (local.xml and config.xml files).
  309. * Base configuration provide ability initialize DB connection and cache backend
  310. *
  311. * @return Mage_Core_Model_App
  312. */
  313. protected function _initBaseConfig()
  314. {
  315. Varien_Profiler::start('mage::app::init::system_config');
  316. $this->_config->loadBase();
  317. Varien_Profiler::stop('mage::app::init::system_config');
  318. return $this;
  319. }
  320. /**
  321. * Initialize application cache instance
  322. *
  323. * @return Mage_Core_Model_App
  324. */
  325. protected function _initCache()
  326. {
  327. $options = $this->_config->getNode('global/cache');
  328. if ($options) {
  329. $options = $options->asArray();
  330. } else {
  331. $options = array();
  332. }
  333. $this->_cache = Mage::getModel('core/cache', $options);
  334. return $this;
  335. }
  336. /**
  337. * Initialize active modules configuration and data
  338. *
  339. * @return Mage_Core_Model_App
  340. */
  341. protected function _initModules()
  342. {
  343. if (!$this->_config->loadModulesCache()) {
  344. $this->_config->loadModules();
  345. if ($this->_config->isLocalConfigLoaded()) {
  346. Varien_Profiler::start('mage::app::init::apply_db_schema_updates');
  347. Mage_Core_Model_Resource_Setup::applyAllUpdates();
  348. Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
  349. }
  350. $this->_config->loadDb();
  351. $this->_config->saveCache();
  352. }
  353. return $this;
  354. }
  355. /**
  356. * Init request object
  357. *
  358. * @return Mage_Core_Model_App
  359. */
  360. protected function _initRequest()
  361. {
  362. $this->getRequest()->setPathInfo();
  363. return $this;
  364. }
  365. /**
  366. * Initialize currently ran store
  367. *
  368. * @param string $scopeCode code of default scope (website/store_group/store code)
  369. * @param string $scopeType type of default scope (website/group/store)
  370. * @return unknown_type
  371. */
  372. protected function _initCurrentStore($scopeCode, $scopeType)
  373. {
  374. Varien_Profiler::start('mage::app::init::stores');
  375. $this->_initStores();
  376. Varien_Profiler::stop('mage::app::init::stores');
  377. if (empty($scopeCode) && !is_null($this->_website)) {
  378. $scopeCode = $this->_website->getCode();
  379. $scopeType = 'website';
  380. }
  381. switch ($scopeType) {
  382. case 'store':
  383. $this->_currentStore = $scopeCode;
  384. break;
  385. case 'group':
  386. $this->_currentStore = $this->_getStoreByGroup($scopeCode);
  387. break;
  388. case 'website':
  389. $this->_currentStore = $this->_getStoreByWebsite($scopeCode);
  390. break;
  391. default:
  392. $this->throwStoreException();
  393. }
  394. if (!empty($this->_currentStore)) {
  395. $this->_checkCookieStore($scopeType);
  396. $this->_checkGetStore($scopeType);
  397. }
  398. $this->_useSessionInUrl = $this->getStore()->getConfig(
  399. Mage_Core_Model_Session_Abstract::XML_PATH_USE_FRONTEND_SID);
  400. return $this;
  401. }
  402. /**
  403. * Retrieve cookie object
  404. *
  405. * @return Mage_Core_Model_Cookie
  406. */
  407. public function getCookie()
  408. {
  409. return Mage::getSingleton('core/cookie');
  410. }
  411. /**
  412. * Check get store
  413. *
  414. * @return Mage_Core_Model_App
  415. */
  416. protected function _checkGetStore($type)
  417. {
  418. if (empty($_GET)) {
  419. return $this;
  420. }
  421. /**
  422. * @todo check XML_PATH_STORE_IN_URL
  423. */
  424. if (!isset($_GET['___store'])) {
  425. return $this;
  426. }
  427. $store = $_GET['___store'];
  428. if (!isset($this->_stores[$store])) {
  429. return $this;
  430. }
  431. $storeObj = $this->_stores[$store];
  432. if (!$storeObj->getId() || !$storeObj->getIsActive()) {
  433. return $this;
  434. }
  435. /**
  436. * prevent running a store from another website or store group,
  437. * if website or store group was specified explicitly in Mage::run()
  438. */
  439. $curStoreObj = $this->_stores[$this->_currentStore];
  440. if ($type == 'website' && $storeObj->getWebsiteId() == $curStoreObj->getWebsiteId()) {
  441. $this->_currentStore = $store;
  442. }
  443. elseif ($type == 'group' && $storeObj->getGroupId() == $curStoreObj->getGroupId()) {
  444. $this->_currentStore = $store;
  445. }
  446. elseif ($type == 'store') {
  447. $this->_currentStore = $store;
  448. }
  449. if ($this->_currentStore == $store) {
  450. $store = $this->getStore($store);
  451. if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
  452. $this->getCookie()->delete(Mage_Core_Model_Store::COOKIE_NAME);
  453. } else {
  454. $this->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $this->_currentStore, true);
  455. }
  456. }
  457. return $this;
  458. }
  459. /**
  460. * Check cookie store
  461. *
  462. * @param string $type
  463. * @return Mage_Core_Model_App
  464. */
  465. protected function _checkCookieStore($type)
  466. {
  467. if (!$this->getCookie()->get()) {
  468. return $this;
  469. }
  470. $store = $this->getCookie()->get(Mage_Core_Model_Store::COOKIE_NAME);
  471. if ($store && isset($this->_stores[$store])
  472. && $this->_stores[$store]->getId()
  473. && $this->_stores[$store]->getIsActive()) {
  474. if ($type == 'website'
  475. && $this->_stores[$store]->getWebsiteId() == $this->_stores[$this->_currentStore]->getWebsiteId()) {
  476. $this->_currentStore = $store;
  477. }
  478. if ($type == 'group'
  479. && $this->_stores[$store]->getGroupId() == $this->_stores[$this->_currentStore]->getGroupId()) {
  480. $this->_currentStore = $store;
  481. }
  482. if ($type == 'store') {
  483. $this->_currentStore = $store;
  484. }
  485. }
  486. return $this;
  487. }
  488. public function reinitStores()
  489. {
  490. return $this->_initStores();
  491. }
  492. /**
  493. * Init store, group and website collections
  494. *
  495. */
  496. protected function _initStores()
  497. {
  498. $this->_stores = array();
  499. $this->_groups = array();
  500. $this->_website = null;
  501. $this->_websites = array();
  502. /** @var $websiteCollection Mage_Core_Model_Website */
  503. $websiteCollection = Mage::getModel('core/website')->getCollection()
  504. ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Website::CACHE_TAG))
  505. ->setLoadDefault(true);
  506. /** @var $websiteCollection Mage_Core_Model_Store_Group */
  507. $groupCollection = Mage::getModel('core/store_group')->getCollection()
  508. ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store_Group::CACHE_TAG))
  509. ->setLoadDefault(true);
  510. /** @var $websiteCollection Mage_Core_Model_Store */
  511. $storeCollection = Mage::getModel('core/store')->getCollection()
  512. ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store::CACHE_TAG))
  513. ->setLoadDefault(true);
  514. $this->_isSingleStore = false;
  515. if ($this->_isSingleStoreAllowed) {
  516. $this->_isSingleStore = $storeCollection->count() < 3;
  517. }
  518. $websiteStores = array();
  519. $websiteGroups = array();
  520. $groupStores = array();
  521. foreach ($storeCollection as $store) {
  522. /** @var $store Mage_Core_Model_Store */
  523. $store->initConfigCache();
  524. $store->setWebsite($websiteCollection->getItemById($store->getWebsiteId()));
  525. $store->setGroup($groupCollection->getItemById($store->getGroupId()));
  526. $this->_stores[$store->getId()] = $store;
  527. $this->_stores[$store->getCode()] = $store;
  528. $websiteStores[$store->getWebsiteId()][$store->getId()] = $store;
  529. $groupStores[$store->getGroupId()][$store->getId()] = $store;
  530. if (is_null($this->_store) && $store->getId()) {
  531. $this->_store = $store;
  532. }
  533. }
  534. foreach ($groupCollection as $group) {
  535. /* @var $group Mage_Core_Model_Store_Group */
  536. if (!isset($groupStores[$group->getId()])) {
  537. $groupStores[$group->getId()] = array();
  538. }
  539. $group->setStores($groupStores[$group->getId()]);
  540. $group->setWebsite($websiteCollection->getItemById($group->getWebsiteId()));
  541. $websiteGroups[$group->getWebsiteId()][$group->getId()] = $group;
  542. $this->_groups[$group->getId()] = $group;
  543. }
  544. foreach ($websiteCollection as $website) {
  545. /* @var $website Mage_Core_Model_Website */
  546. if (!isset($websiteGroups[$website->getId()])) {
  547. $websiteGroups[$website->getId()] = array();
  548. }
  549. if (!isset($websiteStores[$website->getId()])) {
  550. $websiteStores[$website->getId()] = array();
  551. }
  552. if ($website->getIsDefault()) {
  553. $this->_website = $website;
  554. }
  555. $website->setGroups($websiteGroups[$website->getId()]);
  556. $website->setStores($websiteStores[$website->getId()]);
  557. $this->_websites[$website->getId()] = $website;
  558. $this->_websites[$website->getCode()] = $website;
  559. }
  560. }
  561. /**
  562. * Is single Store mode (only one store without default)
  563. *
  564. * @return bool
  565. */
  566. public function isSingleStoreMode()
  567. {
  568. if (!Mage::isInstalled()) {
  569. return false;
  570. }
  571. return $this->_isSingleStore;
  572. }
  573. /**
  574. * Retrieve store code or null by store group
  575. *
  576. * @param int $group
  577. * @return string|null
  578. */
  579. protected function _getStoreByGroup($group)
  580. {
  581. if (!isset($this->_groups[$group])) {
  582. return null;
  583. }
  584. if (!$this->_groups[$group]->getDefaultStoreId()) {
  585. return null;
  586. }
  587. return $this->_stores[$this->_groups[$group]->getDefaultStoreId()]->getCode();
  588. }
  589. /**
  590. * Retrieve store code or null by website
  591. *
  592. * @param int|string $website
  593. * @return string|null
  594. */
  595. protected function _getStoreByWebsite($website)
  596. {
  597. if (!isset($this->_websites[$website])) {
  598. return null;
  599. }
  600. if (!$this->_websites[$website]->getDefaultGroupId()) {
  601. return null;
  602. }
  603. return $this->_getStoreByGroup($this->_websites[$website]->getDefaultGroupId());
  604. }
  605. /**
  606. * Set current default store
  607. *
  608. * @param string $store
  609. * @return Mage_Core_Model_App
  610. */
  611. public function setCurrentStore($store)
  612. {
  613. $this->_currentStore = $store;
  614. return $this;
  615. }
  616. /**
  617. * Initialize application front controller
  618. *
  619. * @return Mage_Core_Model_App
  620. */
  621. protected function _initFrontController()
  622. {
  623. $this->_frontController = new Mage_Core_Controller_Varien_Front();
  624. Mage::register('controller', $this->_frontController);
  625. Varien_Profiler::start('mage::app::init_front_controller');
  626. $this->_frontController->init();
  627. Varien_Profiler::stop('mage::app::init_front_controller');
  628. return $this;
  629. }
  630. /**
  631. * Redeclare custom error handler
  632. *
  633. * @param string $handler
  634. * @return Mage_Core_Model_App
  635. */
  636. public function setErrorHandler($handler)
  637. {
  638. set_error_handler($handler);
  639. return $this;
  640. }
  641. /**
  642. * Loading application area
  643. *
  644. * @param string $code
  645. * @return Mage_Core_Model_App
  646. */
  647. public function loadArea($code)
  648. {
  649. $this->getArea($code)->load();
  650. return $this;
  651. }
  652. /**
  653. * Loding part of area data
  654. *
  655. * @param string $area
  656. * @param string $part
  657. * @return Mage_Core_Model_App
  658. */
  659. public function loadAreaPart($area, $part)
  660. {
  661. $this->getArea($area)->load($part);
  662. return $this;
  663. }
  664. /**
  665. * Retrieve application area
  666. *
  667. * @param string $code
  668. * @return Mage_Core_Model_App_Area
  669. */
  670. public function getArea($code)
  671. {
  672. if (!isset($this->_areas[$code])) {
  673. $this->_areas[$code] = new Mage_Core_Model_App_Area($code, $this);
  674. }
  675. return $this->_areas[$code];
  676. }
  677. /**
  678. * Retrieve application store object
  679. *
  680. * @return Mage_Core_Model_Store
  681. */
  682. public function getStore($id=null)
  683. {
  684. if (!Mage::isInstalled() || $this->getUpdateMode()) {
  685. return $this->_getDefaultStore();
  686. }
  687. if ($id === true && $this->isSingleStoreMode()) {
  688. return $this->_store;
  689. }
  690. if (is_null($id) || ''===$id || $id === true) {
  691. $id = $this->_currentStore;
  692. }
  693. if ($id instanceof Mage_Core_Model_Store) {
  694. return $id;
  695. }
  696. if (is_null($id)) {
  697. $this->throwStoreException();
  698. }
  699. if (empty($this->_stores[$id])) {
  700. $store = Mage::getModel('core/store');
  701. /* @var $store Mage_Core_Model_Store */
  702. if (is_numeric($id)) {
  703. $store->load($id);
  704. } elseif (is_string($id)) {
  705. $store->load($id, 'code');
  706. }
  707. if (!$store->getCode()) {
  708. $this->throwStoreException();
  709. }
  710. $this->_stores[$store->getStoreId()] = $store;
  711. $this->_stores[$store->getCode()] = $store;
  712. }
  713. return $this->_stores[$id];
  714. }
  715. /**
  716. * Retrieve application store object without Store_Exception
  717. *
  718. * @param string|int|Mage_Core_Model_Store $id
  719. * @return Mage_Core_Model_Store
  720. */
  721. public function getSafeStore($id = null)
  722. {
  723. try {
  724. return $this->getStore($id);
  725. }
  726. catch (Exception $e) {
  727. if ($this->_currentStore) {
  728. $this->getRequest()->setActionName('noRoute');
  729. return new Varien_Object();
  730. }
  731. else {
  732. Mage::throwException(Mage::helper('core')->__('Requested invalid store "%s"', $id));
  733. }
  734. }
  735. }
  736. /**
  737. * Retrieve stores array
  738. *
  739. * @param bool $withDefault
  740. * @param bool $codeKey
  741. * @return array
  742. */
  743. public function getStores($withDefault = false, $codeKey = false)
  744. {
  745. $stores = array();
  746. foreach ($this->_stores as $store) {
  747. if (!$withDefault && $store->getId() == 0) {
  748. continue;
  749. }
  750. if ($codeKey) {
  751. $stores[$store->getCode()] = $store;
  752. }
  753. else {
  754. $stores[$store->getId()] = $store;
  755. }
  756. }
  757. return $stores;
  758. }
  759. protected function _getDefaultStore()
  760. {
  761. if (empty($this->_store)) {
  762. $this->_store = Mage::getModel('core/store')
  763. ->setId(self::DISTRO_STORE_ID)
  764. ->setCode(self::DISTRO_STORE_CODE);
  765. }
  766. return $this->_store;
  767. }
  768. /**
  769. * Retrieve default store for default group and website
  770. *
  771. * @return Mage_Core_Model_Store
  772. */
  773. public function getDefaultStoreView()
  774. {
  775. foreach ($this->getWebsites() as $_website) {
  776. if ($_website->getIsDefault()) {
  777. $_defaultStore = $this->getGroup($_website->getDefaultGroupId())->getDefaultStore();
  778. if ($_defaultStore) {
  779. return $_defaultStore;
  780. }
  781. }
  782. }
  783. return null;
  784. }
  785. public function getDistroLocaleCode()
  786. {
  787. return self::DISTRO_LOCALE_CODE;
  788. }
  789. /**
  790. * Retrieve application website object
  791. *
  792. * @return Mage_Core_Model_Website
  793. */
  794. public function getWebsite($id=null)
  795. {
  796. if (is_null($id)) {
  797. $id = $this->getStore()->getWebsiteId();
  798. } elseif ($id instanceof Mage_Core_Model_Website) {
  799. return $id;
  800. } elseif ($id === true) {
  801. return $this->_website;
  802. }
  803. if (empty($this->_websites[$id])) {
  804. $website = Mage::getModel('core/website');
  805. if (is_numeric($id)) {
  806. $website->load($id);
  807. if (!$website->hasWebsiteId()) {
  808. throw Mage::exception('Mage_Core', 'Invalid website id requested.');
  809. }
  810. } elseif (is_string($id)) {
  811. $websiteConfig = $this->_config->getNode('websites/'.$id);
  812. if (!$websiteConfig) {
  813. throw Mage::exception('Mage_Core', 'Invalid website code requested: '.$id);
  814. }
  815. $website->loadConfig($id);
  816. }
  817. $this->_websites[$website->getWebsiteId()] = $website;
  818. $this->_websites[$website->getCode()] = $website;
  819. }
  820. return $this->_websites[$id];
  821. }
  822. public function getWebsites($withDefault = false, $codeKey = false)
  823. {
  824. $websites = array();
  825. if (is_array($this->_websites)) {
  826. foreach ($this->_websites as $website) {
  827. if (!$withDefault && $website->getId() == 0) {
  828. continue;
  829. }
  830. if ($codeKey) {
  831. $websites[$website->getCode()] = $website;
  832. }
  833. else {
  834. $websites[$website->getId()] = $website;
  835. }
  836. }
  837. }
  838. return $websites;
  839. }
  840. /**
  841. * Retrieve application store group object
  842. *
  843. * @return Mage_Core_Model_Store_Group
  844. */
  845. public function getGroup($id=null)
  846. {
  847. if (is_null($id)) {
  848. $id = $this->getStore()->getGroup()->getId();
  849. } elseif ($id instanceof Mage_Core_Model_Store_Group) {
  850. return $id;
  851. }
  852. if (empty($this->_groups[$id])) {
  853. $group = Mage::getModel('core/store_group');
  854. if (is_numeric($id)) {
  855. $group->load($id);
  856. if (!$group->hasGroupId()) {
  857. throw Mage::exception('Mage_Core', 'Invalid store group id requested.');
  858. }
  859. }
  860. $this->_groups[$group->getGroupId()] = $group;
  861. }
  862. return $this->_groups[$id];
  863. }
  864. /**
  865. * Retrieve application locale object
  866. *
  867. * @return Mage_Core_Model_Locale
  868. */
  869. public function getLocale()
  870. {
  871. if (!$this->_locale) {
  872. $this->_locale = Mage::getSingleton('core/locale');
  873. }
  874. return $this->_locale;
  875. }
  876. /**
  877. * Retrive layout object
  878. *
  879. * @return Mage_Core_Model_Layout
  880. */
  881. public function getLayout()
  882. {
  883. if (!$this->_layout) {
  884. if ($this->getFrontController()->getAction()) {
  885. $this->_layout = $this->getFrontController()->getAction()->getLayout();
  886. } else {
  887. $this->_layout = Mage::getSingleton('core/layout');
  888. }
  889. }
  890. return $this->_layout;
  891. }
  892. /**
  893. * Retrieve translate object
  894. *
  895. * @return Mage_Core_Model_Translate
  896. */
  897. public function getTranslator()
  898. {
  899. if (!$this->_translator) {
  900. $this->_translator = Mage::getSingleton('core/translate');
  901. }
  902. return $this->_translator;
  903. }
  904. /**
  905. * Retrieve helper object
  906. *
  907. * @param string $name
  908. * @return Mage_Core_Helper_Abstract
  909. */
  910. public function getHelper($name)
  911. {
  912. return Mage::helper($name);
  913. }
  914. /**
  915. * Retrieve application base currency code
  916. *
  917. * @return string
  918. */
  919. public function getBaseCurrencyCode()
  920. {
  921. //return Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE, 0);
  922. return (string) Mage::app()->getConfig()
  923. ->getNode('default/' . Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
  924. }
  925. /**
  926. * Retrieve configuration object
  927. *
  928. * @return Mage_Core_Model_Config
  929. */
  930. public function getConfig()
  931. {
  932. return $this->_config;
  933. }
  934. /**
  935. * Retrieve front controller object
  936. *
  937. * @return Mage_Core_Controller_Varien_Front
  938. */
  939. public function getFrontController()
  940. {
  941. if (!$this->_frontController) {
  942. $this->_initFrontController();
  943. }
  944. return $this->_frontController;
  945. }
  946. /**
  947. * Get core cache model
  948. *
  949. * @return Mage_Core_Model_Cache
  950. */
  951. public function getCacheInstance()
  952. {
  953. if (!$this->_cache) {
  954. $this->_initCache();
  955. }
  956. return $this->_cache;
  957. }
  958. /**
  959. * Retrieve cache object
  960. *
  961. * @return Zend_Cache_Core
  962. */
  963. public function getCache()
  964. {
  965. if (!$this->_cache) {
  966. $this->_initCache();
  967. }
  968. return $this->_cache->getFrontend();
  969. }
  970. /**
  971. * Loading cache data
  972. *
  973. * @param string $id
  974. * @return mixed
  975. */
  976. public function loadCache($id)
  977. {
  978. return $this->_cache->load($id);
  979. }
  980. /**
  981. * Saving cache data
  982. *
  983. * @param mixed $data
  984. * @param string $id
  985. * @param array $tags
  986. * @return Mage_Core_Model_App
  987. */
  988. public function saveCache($data, $id, $tags=array(), $lifeTime=false)
  989. {
  990. $this->_cache->save($data, $id, $tags, $lifeTime);
  991. return $this;
  992. }
  993. /**
  994. * Remove cache
  995. *
  996. * @param string $id
  997. * @return Mage_Core_Model_App
  998. */
  999. public function removeCache($id)
  1000. {
  1001. $this->_cache->remove($id);
  1002. return $this;
  1003. }
  1004. /**
  1005. * Cleaning cache
  1006. *
  1007. * @param array $tags
  1008. * @return Mage_Core_Model_App
  1009. */
  1010. public function cleanCache($tags=array())
  1011. {
  1012. $this->_cache->clean($tags);
  1013. Mage::dispatchEvent('application_clean_cache', array('tags' => $tags));
  1014. return $this;
  1015. }
  1016. /**
  1017. * Check whether to use cache for specific component
  1018. *
  1019. * @return boolean
  1020. */
  1021. public function useCache($type=null)
  1022. {
  1023. return $this->_cache->canUse($type);
  1024. }
  1025. /**
  1026. * Save cache usage settings
  1027. *
  1028. * @param array $data
  1029. * @return Mage_Core_Model_App
  1030. */
  1031. public function saveUseCache($data)
  1032. {
  1033. $this->_cache->saveOptions($data);
  1034. return $this;
  1035. }
  1036. /**
  1037. * Deletes all session files
  1038. *
  1039. */
  1040. public function cleanAllSessions()
  1041. {
  1042. if (session_module_name()=='files') {
  1043. $dir = session_save_path();
  1044. mageDelTree($dir);
  1045. }
  1046. return $this;
  1047. }
  1048. /**
  1049. * Retrieve request object
  1050. *
  1051. * @return Mage_Core_Controller_Request_Http
  1052. */
  1053. public function getRequest()
  1054. {
  1055. if (empty($this->_request)) {
  1056. $this->_request = new Mage_Core_Controller_Request_Http();
  1057. }
  1058. return $this->_request;
  1059. }
  1060. /**
  1061. * Retrieve response object
  1062. *
  1063. * @return Zend_Controller_Response_Http
  1064. */
  1065. public function getResponse()
  1066. {
  1067. if (empty($this->_response)) {
  1068. $this->_response = new Mage_Core_Controller_Response_Http();
  1069. $this->_response->headersSentThrowsException = Mage::$headersSentThrowsException;
  1070. $this->_response->setHeader("Content-Type", "text/html; charset=UTF-8");
  1071. }
  1072. return $this->_response;
  1073. }
  1074. public function addEventArea($area)
  1075. {
  1076. if (!isset($this->_events[$area])) {
  1077. $this->_events[$area] = array();
  1078. }
  1079. return $this;
  1080. }
  1081. public function dispatchEvent($eventName, $args)
  1082. {
  1083. foreach ($this->_events as $area=>$events) {
  1084. if (!isset($events[$eventName])) {
  1085. $eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
  1086. if (!$eventConfig) {
  1087. $this->_events[$area][$eventName] = false;
  1088. continue;
  1089. }
  1090. $observers = array();
  1091. foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
  1092. $observers[$obsName] = array(
  1093. 'type' => (string)$obsConfig->type,
  1094. 'model' => $obsConfig->class ? (string)$obsConfig->class : $obsConfig->getClassName(),
  1095. 'method'=> (string)$obsConfig->method,
  1096. 'args' => (array)$obsConfig->args,
  1097. );
  1098. }
  1099. $events[$eventName]['observers'] = $observers;
  1100. $this->_events[$area][$eventName]['observers'] = $observers;
  1101. }
  1102. if (false===$events[$eventName]) {
  1103. continue;
  1104. } else {
  1105. $event = new Varien_Event($args);
  1106. $event->setName($eventName);
  1107. $observer = new Varien_Event_Observer();
  1108. }
  1109. foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
  1110. $observer->setData(array('event'=>$event));
  1111. Varien_Profiler::start('OBSERVER: '.$obsName);
  1112. switch ($obs['type']) {
  1113. case 'disabled':
  1114. break;
  1115. case 'object': case 'model':
  1116. $method = $obs['method'];
  1117. $observer->addData($args);
  1118. $object = Mage::getModel($obs['model']);
  1119. $this->_callObserverMethod($object, $method, $observer);
  1120. break;
  1121. default:
  1122. $method = $obs['method'];
  1123. $observer->addData($args);
  1124. $object = Mage::getSingleton($obs['model']);
  1125. $this->_callObserverMethod($object, $method, $observer);
  1126. break;
  1127. }
  1128. Varien_Profiler::stop('OBSERVER: '.$obsName);
  1129. }
  1130. }
  1131. return $this;
  1132. }
  1133. /**
  1134. * Added not existin observers methods calls protection
  1135. *
  1136. * @param object $object
  1137. * @param string $method
  1138. * @param Varien_Event_Observer $observer
  1139. */
  1140. protected function _callObserverMethod($object, $method, $observer)
  1141. {
  1142. if (method_exists($object, $method)) {
  1143. $object->$method($observer);
  1144. } elseif (Mage::getIsDeveloperMode()) {
  1145. Mage::throwException('Method "'.$method.'" is not defined in "'.get_class($object).'"');
  1146. }
  1147. return $this;
  1148. }
  1149. public function setUpdateMode($value)
  1150. {
  1151. $this->_updateMode = $value;
  1152. }
  1153. public function getUpdateMode()
  1154. {
  1155. return $this->_updateMode;
  1156. }
  1157. public function throwStoreException()
  1158. {
  1159. throw new Mage_Core_Model_Store_Exception('');
  1160. }
  1161. /**
  1162. * Set use session var instead of SID for URL
  1163. *
  1164. * @param bool $var
  1165. * @return Mage_Core_Model_App
  1166. */
  1167. public function setUseSessionVar($var)
  1168. {
  1169. $this->_useSessionVar = (bool)$var;
  1170. return $this;
  1171. }
  1172. /**
  1173. * Retrieve use flag session var instead of SID for URL
  1174. *
  1175. * @return bool
  1176. */
  1177. public function getUseSessionVar()
  1178. {
  1179. return $this->_useSessionVar;
  1180. }
  1181. /**
  1182. * Get either default or any store view
  1183. *
  1184. * @return Mage_Core_Model_Store
  1185. */
  1186. public function getAnyStoreView()
  1187. {
  1188. $store = $this->getDefaultStoreView();
  1189. if ($store) {
  1190. return $store;
  1191. }
  1192. foreach ($this->getStores() as $store) {
  1193. return $store;
  1194. }
  1195. }
  1196. /**
  1197. * Set Use session in URL flag
  1198. *
  1199. * @param bool $flag
  1200. * @return Mage_Core_Model_App
  1201. */
  1202. public function setUseSessionInUrl($flag = true)
  1203. {
  1204. $this->_useSessionInUrl = (bool)$flag;
  1205. return $this;
  1206. }
  1207. /**
  1208. * Retrieve use session in URL flag
  1209. *
  1210. * @return bool
  1211. */
  1212. public function getUseSessionInUrl()
  1213. {
  1214. return $this->_useSessionInUrl;
  1215. }
  1216. /**
  1217. * Allow or disallow single store mode
  1218. *
  1219. * @param bool $value
  1220. * @return Mage_Core_Model_App
  1221. */
  1222. public function setIsSingleStoreModeAllowed($value)
  1223. {
  1224. $this->_isSingleStoreAllowed = (bool)$value;
  1225. return $this;
  1226. }
  1227. /**
  1228. * Prepare array of store groups
  1229. * can be filtered to contain default store group or not by $withDefault flag
  1230. * depending on flag $codeKey array keys can be group id or group code
  1231. *
  1232. * @param bool $withDefault
  1233. * @param bool $codeKey
  1234. * @return array
  1235. */
  1236. public function getGroups($withDefault = false, $codeKey = false)
  1237. {
  1238. $groups = array();
  1239. if (is_array($this->_groups)) {
  1240. foreach ($this->_groups as $group) {
  1241. if (!$withDefault && $group->getId() == 0) {
  1242. continue;
  1243. }
  1244. if ($codeKey) {
  1245. $groups[$group->getCode()] = $group;
  1246. }
  1247. else {
  1248. $groups[$group->getId()] = $group;
  1249. }
  1250. }
  1251. }
  1252. return $groups;
  1253. }
  1254. /**
  1255. * Retrieve application installation flag
  1256. *
  1257. * @deprecated since 1.2
  1258. * @return bool
  1259. */
  1260. public function isInstalled()
  1261. {
  1262. return Mage::isInstalled();
  1263. }
  1264. /**
  1265. * Generate cache tags from cache id
  1266. *
  1267. * @deprecated after 1.4.0.0-alpha3, functionality implemented in Mage_Core_Model_Cache
  1268. * @param string $id
  1269. * @param array $tags
  1270. * @return array
  1271. */
  1272. protected function _getCacheTags($tags=array())
  1273. {
  1274. foreach ($tags as $index=>$value) {
  1275. $tags[$index] = $this->_getCacheId($value);
  1276. }
  1277. return $tags;
  1278. }
  1279. /**
  1280. * Get file name with cache configuration settings
  1281. *
  1282. * @deprecated after 1.4.0.0-alpha3, functionality implemented in Mage_Core_Model_Cache
  1283. * @return string
  1284. */
  1285. public function getUseCacheFilename()
  1286. {
  1287. return $this->_config->getOptions()->getEtcDir().DS.'use_cache.ser';
  1288. }
  1289. /**
  1290. * Generate cache id with application specific data
  1291. *
  1292. * @deprecated after 1.4.0.0-alpha3, functionality implemented in Mage_Core_Model_Cache
  1293. * @param string $id
  1294. * @return string
  1295. */
  1296. protected function _getCacheId($id=null)
  1297. {
  1298. if ($id) {
  1299. $id = $this->prepareCacheId($id);
  1300. }
  1301. return $id;
  1302. }
  1303. /**
  1304. * Prepare identifier which can be used as cache id or cache tag
  1305. *
  1306. * @deprecated after 1.4.0.0-alpha3, functionality implemented in Mage_Core_Model_Cache
  1307. * @param string $id
  1308. * @return string
  1309. */
  1310. public function prepareCacheId($id)
  1311. {
  1312. $id = strtoupper($id);
  1313. $id = preg_replace('/([^a-zA-Z0-9_]{1,1})/', '_', $id);
  1314. return $id;
  1315. }
  1316. }