00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_GoogleBase_Model_Item extends Mage_Core_Model_Abstract
00035 {
00036 const ATTRIBUTES_REGISTRY_KEY = 'gbase_attributes_registry';
00037 const TYPES_REGISTRY_KEY = 'gbase_types_registry';
00038
00039 protected function _construct()
00040 {
00041 parent::_construct();
00042 $this->_init('googlebase/item');
00043 }
00044
00045
00046
00047
00048
00049
00050 public function getServiceItem()
00051 {
00052 return Mage::getModel('googlebase/service_item')->setStoreId($this->getStoreId());
00053 }
00054
00055
00056
00057
00058
00059
00060 public function getTargetCountry()
00061 {
00062 return Mage::getSingleton('googlebase/config')->getTargetCountry($this->getStoreId());
00063 }
00064
00065
00066
00067
00068
00069
00070 public function insertItem()
00071 {
00072 $this->_checkProduct()
00073 ->_prepareProductObject();
00074
00075 $typeModel = $this->_getTypeModel();
00076 $this->getServiceItem()
00077 ->setItem($this)
00078 ->setObject($this->getProduct())
00079 ->setAttributeValues($this->_getAttributeValues())
00080 ->setItemType($typeModel->getGbaseItemtype())
00081 ->insert();
00082 $this->setTypeId($typeModel->getTypeId());
00083 return $this;
00084 }
00085
00086
00087
00088
00089
00090
00091 public function updateItem()
00092 {
00093 $this->_checkProduct()
00094 ->_prepareProductObject();
00095 $this->loadByProduct($this->getProduct());
00096 if ($this->getId()) {
00097 $typeModel = $this->_getTypeModel();
00098 $this->getServiceItem()
00099 ->setItem($this)
00100 ->setObject($this->getProduct())
00101 ->setAttributeValues($this->_getAttributeValues())
00102 ->setItemType($typeModel->getGbaseItemtype())
00103 ->update();
00104 }
00105 return $this;
00106 }
00107
00108
00109
00110
00111
00112
00113 public function deleteItem()
00114 {
00115 $this->getServiceItem()->setItem($this)->delete();
00116 return $this;
00117 }
00118
00119
00120
00121
00122
00123
00124 public function hideItem()
00125 {
00126 $this->getServiceItem()->setItem($this)->hide();
00127 $this->setIsHidden(1);
00128 $this->save();
00129 return $this;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function activateItem()
00138 {
00139 $this->getServiceItem()->setItem($this)->activate();
00140 $this->setIsHidden(0);
00141 $this->save();
00142 return $this;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function loadByProduct($product)
00152 {
00153 if (!$this->getProduct()) {
00154 $this->setProduct($product);
00155 }
00156 $this->getResource()->loadByProduct($this);
00157 return $this;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166 public function setProduct($product)
00167 {
00168 if (!($product instanceof Mage_Catalog_Model_Product)) {
00169 Mage::throwException(Mage::helper('googlebase')->__('Invalid Product Model for Google Base Item'));
00170 }
00171 $this->setData('product', $product);
00172 $this->setProductId($product->getId());
00173 $this->setStoreId($product->getStoreId());
00174 return $this;
00175 }
00176
00177
00178
00179
00180
00181
00182 protected function _checkProduct()
00183 {
00184 if (!($this->getProduct() instanceof Mage_Catalog_Model_Product)) {
00185 Mage::throwException(Mage::helper('googlebase')->__('Invalid Product Model for Google Base Item'));
00186 }
00187 return $this;
00188 }
00189
00190
00191
00192
00193
00194
00195 protected function _prepareProductObject()
00196 {
00197 $product = clone $this->getProduct();
00198
00199 $url = $product->getProductUrl(false);
00200 if (!Mage::getStoreConfigFlag('web/url/use_store')) {
00201 $urlInfo = parse_url($url);
00202 $store = $product->getStore()->getCode();
00203 if (isset($urlInfo['query']) && $urlInfo['query'] != '') {
00204 $url .= '&___store=' . $store;
00205 } else {
00206 $url .= '?___store=' . $store;
00207 }
00208 }
00209 $product->setUrl($url)
00210 ->setQuantity( $this->getProduct()->getStockItem()->getQty() )
00211 ->setImageUrl( Mage::helper('catalog/product')->getImageUrl($product) );
00212 $this->setProduct($product);
00213 return $this;
00214 }
00215
00216
00217
00218
00219
00220
00221 protected function _getAttributeValues()
00222 {
00223 $result = array();
00224 $productAttributes = $this->_getProductAttributes();
00225
00226 foreach ($this->_getAttributesCollection() as $attribute) {
00227
00228 $attributeId = $attribute->getAttributeId();
00229
00230 if (isset($productAttributes[$attributeId])) {
00231 $productAttribute = $productAttributes[$attributeId];
00232
00233 if ($attribute->getGbaseAttribute()) {
00234 $name = $attribute->getGbaseAttribute();
00235 } else {
00236 $name = $this->_getAttributeLabel($productAttribute, $this->getProduct()->getStoreId());
00237 }
00238
00239 $value = $productAttribute->getGbaseValue();
00240 $type = Mage::getSingleton('googlebase/attribute')->getGbaseAttributeType($productAttribute);
00241
00242 if ($name && $value && $type) {
00243 $result[$name] = array(
00244 'value' => $value,
00245 'type' => $type
00246 );
00247 }
00248 }
00249 }
00250 return $result;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260 protected function _getAttributeLabel($attribute, $storeId)
00261 {
00262 $frontendLabel = $attribute->getFrontend()->getLabel();
00263 if (is_array($frontendLabel)) {
00264 $frontendLabel = array_shift($frontendLabel);
00265 }
00266 if (!$this->_translations) {
00267 $this->_translations = Mage::getModel('core/translate_string')
00268 ->load(Mage_Catalog_Model_Entity_Attribute::MODULE_NAME.Mage_Core_Model_Translate::SCOPE_SEPARATOR.$frontendLabel)
00269 ->getStoreTranslations();
00270 }
00271 if (isset($this->_translations[$storeId])) {
00272 return $this->_translations[$storeId];
00273 } else {
00274 return $attribute->getAttributeCode();
00275 }
00276 }
00277
00278
00279
00280
00281
00282
00283 protected function _getTypeModel()
00284 {
00285 $registry = Mage::registry(self::TYPES_REGISTRY_KEY);
00286 $attributeSetId = $this->getProduct()->getAttributeSetId();
00287 if (is_array($registry) && isset($registry[$attributeSetId])) {
00288 return $registry[$attributeSetId];
00289 }
00290 $model = Mage::getModel('googlebase/type')->loadByAttributeSetId($attributeSetId, $this->getTargetCountry());
00291 $registry[$attributeSetId] = $model;
00292 Mage::unregister(self::TYPES_REGISTRY_KEY);
00293 Mage::register(self::TYPES_REGISTRY_KEY, $registry);
00294 return $model;
00295 }
00296
00297
00298
00299
00300
00301
00302 protected function _getProductAttributes()
00303 {
00304 $product = $this->getProduct();
00305 $attributes = $product->getAttributes();
00306 $result = array();
00307 foreach ($attributes as $attribute) {
00308 $value = $attribute->getFrontend()->getValue($product);
00309 if (is_string($value) && strlen($value) && $product->hasData($attribute->getAttributeCode())) {
00310 $attribute->setGbaseValue($value);
00311 $result[$attribute->getAttributeId()] = $attribute;
00312 }
00313 }
00314 return $result;
00315 }
00316
00317
00318
00319
00320
00321
00322 protected function _getProductImages()
00323 {
00324 $product = $this->getProduct();
00325 $galleryData = $product->getData('media_gallery');
00326
00327 if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
00328 return array();
00329 }
00330
00331 $result = array();
00332 foreach ($galleryData['images'] as $image) {
00333 $image['url'] = Mage::getSingleton('catalog/product_media_config')
00334 ->getMediaUrl($image['file']);
00335 $result[] = $image;
00336 }
00337 return $result;
00338 }
00339
00340
00341
00342
00343
00344
00345 protected function _getAttributesCollection()
00346 {
00347 $registry = Mage::registry(self::ATTRIBUTES_REGISTRY_KEY);
00348 $attributeSetId = $this->getProduct()->getAttributeSetId();
00349 if (is_array($registry) && isset($registry[$attributeSetId])) {
00350 return $registry[$attributeSetId];
00351 }
00352 $collection = Mage::getResourceModel('googlebase/attribute_collection')
00353 ->addAttributeSetFilter($attributeSetId, $this->getTargetCountry())
00354 ->load();
00355 $registry[$attributeSetId] = $collection;
00356 Mage::unregister(self::ATTRIBUTES_REGISTRY_KEY);
00357 Mage::register(self::ATTRIBUTES_REGISTRY_KEY, $registry);
00358 return $collection;
00359 }
00360 }