/app/code/core/Mage/GoogleShopping/Helper/Data.php

https://bitbucket.org/jokusafet/magento2 · PHP · 117 lines · 46 code · 8 blank · 63 comment · 12 complexity · 341293f460e5cf4cbd9a9082216b597e 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_GoogleShopping
  23. * @copyright Copyright (c) 2012 X.commerce, 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 Content Data Helper
  28. *
  29. * @category Mage
  30. * @package Mage_GoogleShopping
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_GoogleShopping_Helper_Data extends Mage_Core_Helper_Abstract
  34. {
  35. /**
  36. * Get Google Content Product ID
  37. *
  38. * @param int $productId
  39. * @param int $storeId
  40. * @return string
  41. */
  42. public function buildContentProductId($productId, $storeId)
  43. {
  44. return $productId . '_' . $storeId;
  45. }
  46. /**
  47. * Remove characters and words not allowed by Google Content in title and content (description).
  48. *
  49. * (to avoid "Expected response code 200, got 400.
  50. * Reason: There is a problem with the character encoding of this attribute")
  51. *
  52. * @param string $string
  53. * @return string
  54. */
  55. public function cleanAtomAttribute($string)
  56. {
  57. return Mage::helper('Mage_Core_Helper_String')
  58. ->substr(preg_replace('/[\pC¢€•—™°½]|shipping/ui', '', $string), 0, 3500);
  59. }
  60. /**
  61. * Normalize attribute's name.
  62. * The name has to be in lower case and the words are separated by symbol "_".
  63. * For instance: Meta Description = meta_description
  64. *
  65. * @param string $name
  66. * @return string
  67. */
  68. public function normalizeName($name)
  69. {
  70. return strtolower(preg_replace('/[\s_]+/', '_', $name));
  71. }
  72. /**
  73. * Parse Exception Response Body
  74. *
  75. * @param string $message Exception message to parse
  76. * @param null|Mage_Catalog_Model_Product $product
  77. * @return string
  78. */
  79. public function parseGdataExceptionMessage($message, $product = null)
  80. {
  81. $result = array();
  82. foreach (explode("\n", $message) as $row) {
  83. if (trim($row) == '') {
  84. continue;
  85. }
  86. if (strip_tags($row) == $row) {
  87. $row = preg_replace('/@ (.*)/', $this->__("See '\\1'"), $row);
  88. if (!is_null($product)) {
  89. $row .= ' ' . $this->__("for product '%s' (in '%s' store)", $product->getName(), Mage::app()->getStore($product->getStoreId())->getName());
  90. }
  91. $result[] = $row;
  92. continue;
  93. }
  94. // parse not well-formatted xml
  95. preg_match_all('/(reason|field|type)=\"([^\"]+)\"/', $row, $matches);
  96. if (is_array($matches) && count($matches) == 3) {
  97. if (is_array($matches[1]) && count($matches[1]) > 0) {
  98. $c = count($matches[1]);
  99. for ($i = 0; $i < $c; $i++) {
  100. if (isset($matches[2][$i])) {
  101. $result[] = ucfirst($matches[1][$i]) . ': ' . $matches[2][$i];
  102. }
  103. }
  104. }
  105. }
  106. }
  107. return implode(". ", $result);
  108. }
  109. }