PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/GoogleBase/Model/Item.php

https://github.com/rgranadino/magento-mirror
PHP | 386 lines | 213 code | 28 blank | 145 comment | 28 complexity | 72ef99ff25697aec15c25148bed4a113 MD5 | raw file
  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_GoogleBase
  23. * @copyright Copyright (c) 2011 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. * Google Base Item Types Model
  28. *
  29. * @method Mage_GoogleBase_Model_Resource_Item _getResource()
  30. * @method Mage_GoogleBase_Model_Resource_Item getResource()
  31. * @method int getTypeId()
  32. * @method Mage_GoogleBase_Model_Item setTypeId(int $value)
  33. * @method int getProductId()
  34. * @method Mage_GoogleBase_Model_Item setProductId(int $value)
  35. * @method string getGbaseItemId()
  36. * @method Mage_GoogleBase_Model_Item setGbaseItemId(string $value)
  37. * @method int getStoreId()
  38. * @method Mage_GoogleBase_Model_Item setStoreId(int $value)
  39. * @method string getPublished()
  40. * @method Mage_GoogleBase_Model_Item setPublished(string $value)
  41. * @method string getExpires()
  42. * @method Mage_GoogleBase_Model_Item setExpires(string $value)
  43. * @method int getImpr()
  44. * @method Mage_GoogleBase_Model_Item setImpr(int $value)
  45. * @method int getClicks()
  46. * @method Mage_GoogleBase_Model_Item setClicks(int $value)
  47. * @method int getViews()
  48. * @method Mage_GoogleBase_Model_Item setViews(int $value)
  49. * @method int getIsHidden()
  50. * @method Mage_GoogleBase_Model_Item setIsHidden(int $value)
  51. *
  52. * @deprecated after 1.5.1.0
  53. * @category Mage
  54. * @package Mage_GoogleBase
  55. * @author Magento Core Team <core@magentocommerce.com>
  56. */
  57. class Mage_GoogleBase_Model_Item extends Mage_Core_Model_Abstract
  58. {
  59. const ATTRIBUTES_REGISTRY_KEY = 'gbase_attributes_registry';
  60. const TYPES_REGISTRY_KEY = 'gbase_types_registry';
  61. protected function _construct()
  62. {
  63. parent::_construct();
  64. $this->_init('googlebase/item');
  65. }
  66. /**
  67. * Return Service Item Instance
  68. *
  69. * @return Mage_GoogleBase_Model_Service_Item
  70. */
  71. public function getServiceItem()
  72. {
  73. return Mage::getModel('googlebase/service_item')->setStoreId($this->getStoreId());
  74. }
  75. /**
  76. * Target Country
  77. *
  78. * @return string Two-letters country ISO code
  79. */
  80. public function getTargetCountry()
  81. {
  82. return Mage::getSingleton('googlebase/config')->getTargetCountry($this->getStoreId());
  83. }
  84. /**
  85. * Save item to Google Base
  86. *
  87. * @return Mage_GoogleBase_Model_Item
  88. */
  89. public function insertItem()
  90. {
  91. $this->_checkProduct()
  92. ->_prepareProductObject();
  93. $typeModel = $this->_getTypeModel();
  94. $this->getServiceItem()
  95. ->setItem($this)
  96. ->setObject($this->getProduct())
  97. ->setAttributeValues($this->_getAttributeValues())
  98. ->setItemType($typeModel->getGbaseItemtype())
  99. ->insert();
  100. $this->setTypeId($typeModel->getTypeId());
  101. return $this;
  102. }
  103. /**
  104. * Update Item data
  105. *
  106. * @return Mage_GoogleBase_Model_Item
  107. */
  108. public function updateItem()
  109. {
  110. $this->_checkProduct()
  111. ->_prepareProductObject();
  112. $this->loadByProduct($this->getProduct());
  113. if ($this->getId()) {
  114. $typeModel = $this->_getTypeModel();
  115. $this->getServiceItem()
  116. ->setItem($this)
  117. ->setObject($this->getProduct())
  118. ->setAttributeValues($this->_getAttributeValues())
  119. ->setItemType($typeModel->getGbaseItemtype())
  120. ->update();
  121. }
  122. return $this;
  123. }
  124. /**
  125. * Delete Item from Google Base
  126. *
  127. * @return Mage_GoogleBase_Model_Item
  128. */
  129. public function deleteItem()
  130. {
  131. $this->getServiceItem()->setItem($this)->delete();
  132. return $this;
  133. }
  134. /**
  135. * Delete Item from Google Base
  136. *
  137. * @return Mage_GoogleBase_Model_Item
  138. */
  139. public function hideItem()
  140. {
  141. $this->getServiceItem()->setItem($this)->hide();
  142. $this->setIsHidden(1);
  143. $this->save();
  144. return $this;
  145. }
  146. /**
  147. * Delete Item from Google Base
  148. *
  149. * @return Mage_GoogleBase_Model_Item
  150. */
  151. public function activateItem()
  152. {
  153. $this->getServiceItem()->setItem($this)->activate();
  154. $this->setIsHidden(0);
  155. $this->save();
  156. return $this;
  157. }
  158. /**
  159. * Load Item Model by Product
  160. *
  161. * @param Mage_Catalog_Model_Product $product
  162. * @return Mage_GoogleBase_Model_Item
  163. */
  164. public function loadByProduct($product)
  165. {
  166. if (!$this->getProduct()) {
  167. $this->setProduct($product);
  168. }
  169. $this->getResource()->loadByProduct($this);
  170. return $this;
  171. }
  172. /**
  173. * Product Setter
  174. *
  175. * @param Mage_Catalog_Model_Product
  176. * @return Mage_GoogleBase_Model_Item
  177. */
  178. public function setProduct($product)
  179. {
  180. if (!($product instanceof Mage_Catalog_Model_Product)) {
  181. Mage::throwException(Mage::helper('googlebase')->__('Invalid Product Model for Google Base Item'));
  182. }
  183. $this->setData('product', $product);
  184. $this->setProductId($product->getId());
  185. $this->setStoreId($product->getStoreId());
  186. return $this;
  187. }
  188. /**
  189. * Check product instance
  190. *
  191. * @return Mage_GoogleBase_Model_Item
  192. */
  193. protected function _checkProduct()
  194. {
  195. if (!($this->getProduct() instanceof Mage_Catalog_Model_Product)) {
  196. Mage::throwException(Mage::helper('googlebase')->__('Invalid Product Model for Google Base Item'));
  197. }
  198. return $this;
  199. }
  200. /**
  201. * Copy Product object and assign additional data to the copy
  202. *
  203. * @return Mage_GoogleBase_Model_Item
  204. */
  205. protected function _prepareProductObject()
  206. {
  207. $product = clone $this->getProduct();
  208. /* @var $product Mage_Catalog_Model_Product */
  209. $url = $product->getProductUrl(false);
  210. if (!Mage::getStoreConfigFlag('web/url/use_store')) {
  211. $urlInfo = parse_url($url);
  212. $store = $product->getStore()->getCode();
  213. if (isset($urlInfo['query']) && $urlInfo['query'] != '') {
  214. $url .= '&___store=' . $store;
  215. } else {
  216. $url .= '?___store=' . $store;
  217. }
  218. }
  219. $product->setUrl($url)
  220. ->setQuantity( $this->getProduct()->getStockItem()->getQty() )
  221. ->setImageUrl( Mage::helper('catalog/product')->getImageUrl($product) );
  222. $this->setProduct($product);
  223. return $this;
  224. }
  225. /**
  226. * Return Product attribute values array
  227. *
  228. * @return array Product attribute values
  229. */
  230. protected function _getAttributeValues()
  231. {
  232. $result = array();
  233. $productAttributes = $this->_getProductAttributes();
  234. foreach ($this->_getAttributesCollection() as $attribute) {
  235. $attributeId = $attribute->getAttributeId();
  236. if (isset($productAttributes[$attributeId])) {
  237. $productAttribute = $productAttributes[$attributeId];
  238. if ($attribute->getGbaseAttribute()) {
  239. $name = $attribute->getGbaseAttribute();
  240. } else {
  241. $name = $this->_getAttributeLabel($productAttribute, $this->getProduct()->getStoreId());
  242. }
  243. $value = $productAttribute->getGbaseValue();
  244. $type = Mage::getSingleton('googlebase/attribute')->getGbaseAttributeType($productAttribute);
  245. if ($name && $value && $type) {
  246. $result[$name] = array(
  247. 'value' => $value,
  248. 'type' => $type
  249. );
  250. }
  251. }
  252. }
  253. return $result;
  254. }
  255. /**
  256. * Return Product Attribute Store Label
  257. *
  258. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  259. * @param int $storeId Store View Id
  260. * @return string Attribute Store View Label or Attribute code
  261. */
  262. protected function _getAttributeLabel($attribute, $storeId)
  263. {
  264. $frontendLabel = $attribute->getFrontend()->getLabel();
  265. if (is_array($frontendLabel)) {
  266. $frontendLabel = array_shift($frontendLabel);
  267. }
  268. if (!$this->_translations) {
  269. $moduleName = Mage_Catalog_Model_Entity_Attribute::MODULE_NAME;
  270. $separator = Mage_Core_Model_Translate::SCOPE_SEPARATOR;
  271. $this->_translations = Mage::getModel('core/translate_string')
  272. ->load($moduleName . $separator . $frontendLabel)
  273. ->getStoreTranslations();
  274. }
  275. if (isset($this->_translations[$storeId])) {
  276. return $this->_translations[$storeId];
  277. } else {
  278. return $attribute->getAttributeCode();
  279. }
  280. }
  281. /**
  282. * Return Google Base Item Type Model for current Product Attribute Set
  283. *
  284. * @return Mage_GoogleBase_Model_Type
  285. */
  286. protected function _getTypeModel()
  287. {
  288. $registry = Mage::registry(self::TYPES_REGISTRY_KEY);
  289. $attributeSetId = $this->getProduct()->getAttributeSetId();
  290. if (is_array($registry) && isset($registry[$attributeSetId])) {
  291. return $registry[$attributeSetId];
  292. }
  293. $model = Mage::getModel('googlebase/type')->loadByAttributeSetId($attributeSetId, $this->getTargetCountry());
  294. $registry[$attributeSetId] = $model;
  295. Mage::unregister(self::TYPES_REGISTRY_KEY);
  296. Mage::register(self::TYPES_REGISTRY_KEY, $registry);
  297. return $model;
  298. }
  299. /**
  300. * Return Product attributes array
  301. *
  302. * @return array Product attributes
  303. */
  304. protected function _getProductAttributes()
  305. {
  306. $product = $this->getProduct();
  307. $attributes = $product->getAttributes();
  308. $result = array();
  309. foreach ($attributes as $attribute) {
  310. $value = $attribute->getFrontend()->getValue($product);
  311. if (is_string($value) && strlen($value) && $product->hasData($attribute->getAttributeCode())) {
  312. $attribute->setGbaseValue($value);
  313. $result[$attribute->getAttributeId()] = $attribute;
  314. }
  315. }
  316. return $result;
  317. }
  318. /**
  319. * Get Product Media files info
  320. *
  321. * @return array Media files info
  322. */
  323. protected function _getProductImages()
  324. {
  325. $product = $this->getProduct();
  326. $galleryData = $product->getData('media_gallery');
  327. if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
  328. return array();
  329. }
  330. $result = array();
  331. foreach ($galleryData['images'] as $image) {
  332. $image['url'] = Mage::getSingleton('catalog/product_media_config')
  333. ->getMediaUrl($image['file']);
  334. $result[] = $image;
  335. }
  336. return $result;
  337. }
  338. /**
  339. * Return attribute collection for current Product Attribute Set
  340. *
  341. * @return Mage_GoogleBase_Model_Mysql4_Attribute_Collection
  342. */
  343. protected function _getAttributesCollection()
  344. {
  345. $registry = Mage::registry(self::ATTRIBUTES_REGISTRY_KEY);
  346. $attributeSetId = $this->getProduct()->getAttributeSetId();
  347. if (is_array($registry) && isset($registry[$attributeSetId])) {
  348. return $registry[$attributeSetId];
  349. }
  350. $collection = Mage::getResourceModel('googlebase/attribute_collection')
  351. ->addAttributeSetFilter($attributeSetId, $this->getTargetCountry())
  352. ->load();
  353. $registry[$attributeSetId] = $collection;
  354. Mage::unregister(self::ATTRIBUTES_REGISTRY_KEY);
  355. Mage::register(self::ATTRIBUTES_REGISTRY_KEY, $registry);
  356. return $collection;
  357. }
  358. }