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_Shipping
23: * @copyright Copyright (c) 2012 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:
28: class Mage_Shipping_Model_Config extends Varien_Object
29: {
30: /**
31: * Shipping origin settings
32: */
33: const XML_PATH_ORIGIN_COUNTRY_ID = 'shipping/origin/country_id';
34: const XML_PATH_ORIGIN_REGION_ID = 'shipping/origin/region_id';
35: const XML_PATH_ORIGIN_CITY = 'shipping/origin/city';
36: const XML_PATH_ORIGIN_POSTCODE = 'shipping/origin/postcode';
37:
38: protected static $_carriers;
39:
40: /**
41: * Retrieve active system carriers
42: *
43: * @param mixed $store
44: * @return array
45: */
46: public function getActiveCarriers($store = null)
47: {
48: $carriers = array();
49: $config = Mage::getStoreConfig('carriers', $store);
50: foreach ($config as $code => $carrierConfig) {
51: if (Mage::getStoreConfigFlag('carriers/'.$code.'/active', $store)) {
52: $carrierModel = $this->_getCarrier($code, $carrierConfig, $store);
53: if ($carrierModel) {
54: $carriers[$code] = $carrierModel;
55: }
56: }
57: }
58: return $carriers;
59: }
60:
61: /**
62: * Retrieve all system carriers
63: *
64: * @param mixed $store
65: * @return array
66: */
67: public function getAllCarriers($store = null)
68: {
69: $carriers = array();
70: $config = Mage::getStoreConfig('carriers', $store);
71: foreach ($config as $code => $carrierConfig) {
72: $model = $this->_getCarrier($code, $carrierConfig, $store);
73: if ($model) {
74: $carriers[$code] = $model;
75: }
76: }
77: return $carriers;
78: }
79:
80: /**
81: * Retrieve carrier model instance by carrier code
82: *
83: * @param string $carrierCode
84: * @param mixed $store
85: * @return Mage_Usa_Model_Shipping_Carrier_Abstract
86: */
87: public function getCarrierInstance($carrierCode, $store = null)
88: {
89: $carrierConfig = Mage::getStoreConfig('carriers/'.$carrierCode, $store);
90: if (!empty($carrierConfig)) {
91: return $this->_getCarrier($carrierCode, $carrierConfig, $store);
92: }
93: return false;
94: }
95:
96: /**
97: * Get carrier model object
98: *
99: * @param string $code
100: * @param array $config
101: * @param mixed $store
102: * @return Mage_Shipping_Model_Carrier_Abstract
103: */
104: protected function _getCarrier($code, $config, $store = null)
105: {
106: if (!isset($config['model'])) {
107: return false;
108: }
109: $modelName = $config['model'];
110:
111: /**
112: * Added protection from not existing models usage.
113: * Related with module uninstall process
114: */
115: try {
116: $carrier = Mage::getModel($modelName);
117: } catch (Exception $e) {
118: Mage::logException($e);
119: return false;
120: }
121: $carrier->setId($code)->setStore($store);
122: self::$_carriers[$code] = $carrier;
123: return self::$_carriers[$code];
124: }
125: }
126: