PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 447 lines | 231 code | 46 blank | 170 comment | 27 complexity | 715786cc45097bfeddc8f1da78a623e5 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_GoogleBase
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Google Base Item Model
  28. *
  29. * @deprecated after 1.5.1.0
  30. * @category Mage
  31. * @package Mage_GoogleBase
  32. * @author Magento Core Team <core@magentocommerce.com>
  33. */
  34. class Mage_GoogleBase_Model_Service_Item extends Mage_GoogleBase_Model_Service
  35. {
  36. const DEFAULT_ITEM_TYPE = 'products';
  37. const DEFAULT_ATTRIBUTE_TYPE = 'text';
  38. /**
  39. * Object instance to populate entry data
  40. *
  41. * @var Varien_Object
  42. */
  43. protected $_object = null;
  44. /**
  45. * Item instance to update entry data
  46. *
  47. * @var Mage_GoogleBase_Model_Item
  48. */
  49. protected $_item = null;
  50. /**
  51. * $_object Setter
  52. *
  53. * @param Varien_Object $object
  54. * @return Mage_GoogleBase_Model_Service_Item
  55. */
  56. public function setObject($object)
  57. {
  58. $this->_object = $object;
  59. return $this;
  60. }
  61. /**
  62. * $_object Getter
  63. *
  64. * @return Varien_Object
  65. */
  66. public function getObject()
  67. {
  68. return $this->_object;
  69. }
  70. /**
  71. * $_item Setter
  72. *
  73. * @param Mage_GoogleBase_Model_Item $item
  74. * @return Mage_GoogleBase_Model_Service_Item
  75. */
  76. public function setItem($item)
  77. {
  78. $this->_item = $item;
  79. return $this;
  80. }
  81. /**
  82. * $_item Getter
  83. *
  84. * @return Mage_GoogleBase_Model_Item
  85. */
  86. public function getItem()
  87. {
  88. return $this->_item;
  89. }
  90. /**
  91. * Return Store level Service Instance
  92. *
  93. * @return Zend_Gdata_Gbase
  94. */
  95. public function getService($storeId = null)
  96. {
  97. if ($storeId === null) {
  98. $storeId = $this->getStoreId();
  99. }
  100. return parent::getService($storeId);
  101. }
  102. /**
  103. * Insert Item into Google Base
  104. *
  105. * @return Zend_Gdata_Gbase_ItemEntry
  106. */
  107. public function insert()
  108. {
  109. $this->_checkItem();
  110. $service = $this->getService();
  111. $entry = $service->newItemEntry();
  112. $this->setEntry($entry);
  113. $this->_prepareEnrtyForSave();
  114. $this->getEntry()->setItemType($this->_getItemType());
  115. $entry = $service->insertGbaseItem($this->getEntry());
  116. $this->setEntry($entry);
  117. $entryId = $this->getEntry()->getId();
  118. $published = $this->gBaseDate2DateTime($this->getEntry()->getPublished()->getText());
  119. $this->getItem()
  120. ->setGbaseItemId($entryId)
  121. ->setPublished($published);
  122. if ($expires = $this->_getAttributeValue('expiration_date')) {
  123. $expires = $this->gBaseDate2DateTime($expires);
  124. $this->getItem()->setExpires($expires);
  125. }
  126. }
  127. /**
  128. * Update Item data in Google Base
  129. *
  130. * @return Zend_Gdata_Gbase_ItemEntry
  131. */
  132. public function update()
  133. {
  134. $this->_checkItem();
  135. $service = $this->getService();
  136. $entry = $service->getGbaseItemEntry( $this->getItem()->getGbaseItemId() );
  137. $this->setEntry($entry);
  138. $this->_prepareEnrtyForSave();
  139. $entry = $service->updateGbaseItem($this->getEntry());
  140. }
  141. /**
  142. * Delete Item from Google Base
  143. *
  144. * @return Zend_Gdata_Gbase_ItemFeed
  145. */
  146. public function delete()
  147. {
  148. $this->_checkItem();
  149. $service = $this->getService();
  150. $entry = $service->getGbaseItemEntry( $this->getItem()->getGbaseItemId() );
  151. return $service->deleteGbaseItem($entry, $this->getDryRun());
  152. }
  153. /**
  154. * Hide item in Google Base
  155. *
  156. * @return Mage_GoogleBase_Model_Service_Item
  157. */
  158. public function hide()
  159. {
  160. $this->_saveDraft(true);
  161. return $this;
  162. }
  163. /**
  164. * Publish item in Google Base
  165. *
  166. * @return Mage_GoogleBase_Model_Service_Item
  167. */
  168. public function activate()
  169. {
  170. $this->_saveDraft(false);
  171. return $this;
  172. }
  173. /**
  174. * Update item Control property
  175. *
  176. * @param boolean Save as draft or not
  177. * @return Mage_GoogleBase_Model_Service_Item
  178. */
  179. protected function _saveDraft($yes = true)
  180. {
  181. $this->_checkItem();
  182. $service = $this->getService();
  183. $entry = $service->getGbaseItemEntry( $this->getItem()->getGbaseItemId() );
  184. $draftText = $yes ? 'yes' : 'no';
  185. $draft = $service->newDraft($draftText);
  186. $control = $service->newControl($draft);
  187. $entry->setControl($control);
  188. $entry->save();
  189. return $this;
  190. }
  191. /**
  192. * Prepare Entry data and attributes before saving in Google Base
  193. *
  194. * @return Mage_GoogleBase_Model_Service_Item
  195. */
  196. protected function _prepareEnrtyForSave()
  197. {
  198. $object = $this->getObject();
  199. if (!($object instanceof Varien_Object)) {
  200. Mage::throwException(Mage::helper('googlebase')->__('Object model is not specified to save Google Base entry.'));
  201. }
  202. $this->_setUniversalData();
  203. $attributes = $this->getAttributeValues();
  204. if (is_array($attributes) && count($attributes)) {
  205. foreach ($attributes as $name => $data) {
  206. $name = $this->_normalizeString($name);
  207. $value = isset($data['value']) ? $data['value'] : '';
  208. $type = isset($data['type']) && $data['type'] ? $data['type'] : self::DEFAULT_ATTRIBUTE_TYPE;
  209. $customSetter = '_setAttribute' . ucfirst($name);
  210. if (method_exists($this, $customSetter)) {
  211. $this->$customSetter($name, $value, $type);
  212. } else {
  213. $this->_setAttribute($name, $value, $type);
  214. }
  215. }
  216. }
  217. return $this;
  218. }
  219. /**
  220. * Remove characters and words not allowed by Google Base in title and content (description).
  221. *
  222. * (to avoid "Expected response code 200, got 400.
  223. * Reason: There is a problem with the character encoding of this attribute")
  224. *
  225. * @param string $string
  226. * @return string
  227. */
  228. protected function _cleanAtomAttribute($string)
  229. {
  230. return Mage::helper('core/string')
  231. ->substr(preg_replace('/[\pC¢€•—™°½]|shipping/ui', '', $string), 0, 3500);
  232. }
  233. /**
  234. * Assign values to universal attribute of entry
  235. *
  236. * @return Mage_GoogleBase_Model_Service_Item
  237. */
  238. protected function _setUniversalData()
  239. {
  240. $service = $this->getService();
  241. $object = $this->getObject();
  242. $entry = $this->getEntry();
  243. $attributeValues = $this->getAttributeValues();
  244. $this->_setAttribute('id', $object->getId() . '_' . $this->getStoreId(), 'text');
  245. if (isset($attributeValues['title']['value'])) {
  246. $titleText = $attributeValues['title']['value'];
  247. unset($attributeValues['title']); // to prevent "Reason: Duplicate title" error
  248. } elseif ($object->getName()) {
  249. $titleText = $object->getName();
  250. } else {
  251. $titleText = 'no title';
  252. }
  253. $entry->setTitle($service->newTitle()->setText($this->_cleanAtomAttribute($titleText)));
  254. if ($object->getUrl()) {
  255. $links = $entry->getLink();
  256. if (!is_array($links)) {
  257. $links = array();
  258. }
  259. $link = $service->newLink();
  260. $link->setHref($object->getUrl());
  261. $link->setRel('alternate');
  262. $link->setType('text/html');
  263. if ($object->getName()) {
  264. $link->setTitle($object->getName());
  265. }
  266. $links[0] = $link;
  267. $entry->setLink($links);
  268. }
  269. if (isset($attributeValues['description']['value'])) {
  270. $descrText = $attributeValues['description']['value'];
  271. unset($attributeValues['description']); // to prevent "Reason: Duplicate description" error
  272. } elseif ($object->getDescription()) {
  273. $descrText = $object->getDescription();
  274. } else {
  275. $descrText = 'no description';
  276. }
  277. $entry->setContent($service->newContent()->setText($this->_cleanAtomAttribute($descrText)));
  278. if (isset($attributeValues['price']['value']) && floatval($attributeValues['price']['value']) > 0) {
  279. $price = $attributeValues['price']['value'];
  280. } else {
  281. $price = $object->getPrice();
  282. }
  283. $this->_setAttributePrice(false, $price);
  284. if ($object->getQuantity()) {
  285. $quantity = $object->getQuantity() ? max(1, (int)$object->getQuantity()) : 1;
  286. $this->_setAttribute('quantity', $quantity, 'int');
  287. }
  288. $targetCountry = $this->getConfig()->getTargetCountry($this->getStoreId());
  289. if ($object->getData('image_url')) {
  290. $this->_setAttribute('image_link', $object->getData('image_url'), 'url');
  291. }
  292. $this->_setAttribute('condition', 'new', 'text');
  293. $this->_setAttribute('target_country', $targetCountry, 'text');
  294. $this->_setAttribute('item_language', $this->getConfig()->getCountryInfo($targetCountry, 'language'), 'text');
  295. // set new 'attribute_values' with removed 'title' and/or 'description' keys to avoid 'duplicate' errors
  296. $this->setAttributeValues($attributeValues);
  297. return $this;
  298. }
  299. /**
  300. * Set Google Base Item Attribute
  301. *
  302. * @param string $attribute Google Base attribute name
  303. * @param string $value Google Base attribute value
  304. * @param string $type Google Base attribute type
  305. *
  306. * @return Mage_GoogleBase_Model_Service_Item
  307. */
  308. protected function _setAttribute($attribute, $value, $type = 'text')
  309. {
  310. $entry = $this->getEntry();
  311. $gBaseAttribute = $entry->getGbaseAttribute($attribute);
  312. if (isset($gBaseAttribute[0]) && is_object($gBaseAttribute[0])) {
  313. $gBaseAttribute[0]->text = $value;
  314. } else {
  315. $entry->addGbaseAttribute($attribute, $value, $type);
  316. }
  317. return $this;
  318. }
  319. /**
  320. * Custom setter for 'price' attribute
  321. *
  322. * @param string $attribute Google Base attribute name
  323. * @param mixed $value Fload price value
  324. * @param string $type Google Base attribute type
  325. *
  326. * @return Mage_GoogleBase_Model_Service_Item
  327. */
  328. protected function _setAttributePrice($attribute, $value, $type = 'text')
  329. {
  330. if (!$this->getData('price_assigned')) {
  331. $targetCountry = $this->getConfig()->getTargetCountry($this->getStoreId());
  332. $this->_setAttribute(
  333. $this->getConfig()->getCountryInfo($targetCountry, 'price_attribute_name', $this->getStoreId()),
  334. sprintf('%.2f', $value),
  335. 'floatUnit'
  336. );
  337. $this->setData('price_assigned', true);
  338. }
  339. }
  340. /**
  341. * Return Google Base Item Attribute Value
  342. *
  343. * @param string $attribute Google Base attribute name
  344. * @return string|null Attribute value
  345. */
  346. protected function _getAttributeValue($attribute)
  347. {
  348. $entry = $this->getEntry();
  349. $attributeArr = $entry->getGbaseAttribute($attribute);
  350. if (is_array($attributeArr) && is_object($attributeArr[0])) {
  351. return $attributeArr[0]->getText();
  352. }
  353. return null;
  354. }
  355. /**
  356. * Return assign item type or default item type
  357. *
  358. * @return string Google Base Item Type
  359. */
  360. protected function _getItemType()
  361. {
  362. return $this->getItemType()
  363. ? $this->getItemType()
  364. : $this->getConfig()->getDefaultItemType($this->getStoreId());
  365. }
  366. /**
  367. * Check Item Instance
  368. *
  369. * @return void
  370. */
  371. protected function _checkItem()
  372. {
  373. if (!($this->getItem() instanceof Mage_GoogleBase_Model_Item)) {
  374. Mage::throwException(Mage::helper('googlebase')->__('Item model is not specified to delete Google Base entry.'));
  375. }
  376. }
  377. /**
  378. * Prepare Google Base attribute name before save
  379. *
  380. * @param string Attribute name
  381. * @return string Normalized attribute name
  382. */
  383. protected function _normalizeString($string)
  384. {
  385. return preg_replace('/\s+/', '_', $string);
  386. // $string = preg_replace('/([^a-z^0-9^_])+/','_',strtolower($string));
  387. // $string = preg_replace('/_{2,}/','_',$string);
  388. // return trim($string,'_');
  389. }
  390. /**
  391. * Convert Google Base date format to unix timestamp
  392. * Ex. 2008-12-08T16:57:23Z -> 2008-12-08 16:57:23
  393. *
  394. * @param string Google Base datetime
  395. * @return int
  396. */
  397. public function gBaseDate2DateTime($gBaseDate)
  398. {
  399. return Mage::getSingleton('core/date')->date(null, $gBaseDate);
  400. }
  401. }