1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32:
33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: class Eabi_Postoffice_Model_Carriermodule extends Mage_Core_Model_Abstract {
47:
48: 49: 50: 51:
52: private $_groups = array();
53:
54:
55: public function _construct() {
56: parent::_construct();
57: $this->_init('eabi_postoffice/carriermodule');
58:
59: }
60:
61: 62: 63: 64: 65: 66:
67: public function updateCarrierData($byPassTimeCheck) {
68: $className = $this->getData('class_name');
69: $date = new Zend_Date(time(), Zend_Date::TIMESTAMP);
70: if (!$className || $className == '' || $this->getId() <= 0) {
71: throw new Exception('Cannot update Carrier data for empty CarrierModule');
72: }
73:
74: $shippingMethodModel = Mage::getModel($this->getData('class_name'));
75:
76: if (!($shippingMethodModel instanceof Eabi_Postoffice_Model_Carrier_Abstract)) {
77: throw new Exception('This method can only update instances of Eabi_Postoffice_Model_Office carriers');
78: }
79:
80:
81: $lastUpdated = $shippingMethodModel->getConfigData('last_updated');
82: $updateInterval = $shippingMethodModel->getConfigData('update_interval');
83:
84: if ($lastUpdated + ($updateInterval * 60) < $date->get(Zend_Date::TIMESTAMP) || $byPassTimeCheck) {
85: $oldData = array();
86:
87:
88: $oldDataCollection = Mage::getModel('eabi_postoffice/office')->getCollection()
89: ->addFieldToFilter('remote_module_id', $this->getId())
90: ;
91:
92: foreach ($oldDataCollection as $oldDataElement) {
93: $oldData[(string)$oldDataElement->getRemotePlaceId()] = $oldDataElement;
94:
95: if ($oldDataElement->getGroupName() != '' && $oldDataElement->getGroupId() > 0) {
96: $this->_groups[(string)$oldDataElement->getGroupId()] = $oldDataElement->getGroupName();
97: }
98:
99: }
100:
101: $newData = $shippingMethodModel->getOfficeList();
102: if (!is_array($newData)) {
103:
104:
105: $shippingMethodModel->setConfigData('last_updated', $date->get(Zend_Date::TIMESTAMP));
106: return;
107: }
108: $processedPlaceIds = array();
109: foreach ($newData as $newDataElement) {
110:
111: if (!isset($newDataElement['group_id']) || !isset($newDataElement['group_name'])
112: || $newDataElement['group_id'] == '' || $newDataElement['group_name'] == '') {
113: $this->assignGroup($newDataElement);
114:
115: }
116: if (!isset($newDataElement['group_sort'])) {
117: $newDataElement['group_sort'] = $shippingMethodModel->getGroupSort($newDataElement['group_name']);
118: }
119:
120: if (!isset($oldData[(string)$newDataElement['place_id']])) {
121:
122: $oldData[(string)$newDataElement['place_id']] = Mage::getModel('eabi_postoffice/office')
123: ->fromOfficeElement($newDataElement, $this->getId());
124: } else {
125:
126: $oldData[(string)$newDataElement['place_id']] = $oldData[(string)$newDataElement['place_id']]->fromOfficeElement($newDataElement);
127: }
128: $processedPlaceIds[(string)$newDataElement['place_id']] = (string)$newDataElement['place_id'];
129:
130: }
131:
132:
133: foreach ($oldData as $placeId => $oldDataElement) {
134: if (!isset($processedPlaceIds[(string)$placeId])) {
135:
136: $oldDataElement->delete();
137:
138: } else {
139:
140: $oldDataElement->save();
141:
142: }
143: }
144:
145:
146:
147: $shippingMethodModel->setConfigData('last_updated', $date->get(Zend_Date::TIMESTAMP));
148:
149:
150:
151: }
152:
153: }
154:
155: 156: 157: 158:
159: protected function assignGroup(array &$dataElement) {
160: $groupNames = array();
161: if (isset($dataElement['county']) && !empty($dataElement['county'])) {
162: $groupNames[] = $dataElement['county'];
163: }
164: if (isset($dataElement['city']) && !empty($dataElement['city'])) {
165: $groupNames[] = $dataElement['city'];
166: }
167: if (count($groupNames) > 0) {
168: $groupName = implode('/', $groupNames);
169: if (in_array($groupName, $this->_groups)) {
170: $dataElement['group_name'] = $groupName;
171: $dataElement['group_id'] = array_search($groupName, $this->_groups);
172: } else {
173: $new_id = 1;
174: if (count($this->_groups) > 0) {
175: $new_id = max(array_keys($this->_groups)) + 1;
176:
177: }
178: $this->_groups[(string)$new_id] = $groupName;
179: $dataElement['group_name'] = $groupName;
180: $dataElement['group_id'] = array_search($groupName, $this->_groups);
181: }
182: }
183: }
184:
185: 186: 187: 188:
189: protected function _beforeSave() {
190: if ($this->isObjectNew()) {
191: $this->setCreatedTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
192: $this->setUpdateTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
193:
194: } else {
195: $this->setUpdateTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
196: }
197: return parent::_beforeSave();
198: }
199:
200: 201: 202:
203: public static function eabi_init() {
204: self::$_date = new Zend_Date(time(), Zend_Date::TIMESTAMP);
205: }
206:
207: protected static $_date;
208:
209: }
210: Eabi_Postoffice_Model_Carriermodule::eabi_init();
211: