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_GoogleOptimizer_Helper_Data extends Mage_Core_Helper_Abstract
00035 {
00036 const XML_PATH_ENABLED = 'google/optimizer/active';
00037 const XML_PATH_ALLOWED_ATTRIBUTES = 'admin/attributes';
00038
00039 const MAX_ATTRIBUTE_LENGTH_LIMIT = 25;
00040
00041 protected $_storeId = null;
00042
00043 protected $_activeForCmsFlag = null;
00044
00045 public function setStoreId($storeId)
00046 {
00047 $this->_storeId = $storeId;
00048 return $this;
00049 }
00050
00051 public function getStoreId()
00052 {
00053 return $this->_storeId;
00054 }
00055
00056 public function isOptimizerActive($store = null)
00057 {
00058 return Mage::getStoreConfig(self::XML_PATH_ENABLED, $store);
00059 }
00060
00061
00062
00063
00064
00065
00066
00067 public function isOptimizerActiveForCms()
00068 {
00069 if (!is_null($this->_activeForCmsFlag)) {
00070 return $this->_activeForCmsFlag;
00071 }
00072 $stores = array_merge(
00073 array(0),
00074 array_keys(Mage::getSingleton('adminhtml/system_store')->getStoreCollection())
00075 );
00076 foreach ($stores as $store) {
00077 if ($this->isOptimizerActive($store)) {
00078 $this->_activeForCmsFlag = true;
00079 return $this->_activeForCmsFlag;
00080 }
00081 }
00082 $this->_activeForCmsFlag = false;
00083 return $this->_activeForCmsFlag;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 public function productAttribute($callObject, $attributeHtml, $params)
00095 {
00096 $attributeName = $params['attribute'];
00097 $product = $params['product'];
00098
00099 if (!$this->isOptimizerActive($product->getStoreId())
00100 || !$product->getGoogleOptimizerScripts()
00101 || !$product->getGoogleOptimizerScripts()->getControlScript()) {
00102 return $attributeHtml;
00103 }
00104 if (in_array($attributeName, $product->getGoogleOptimizerScripts()->getAttributes())) {
00105 $newAttributeName = 'product_'.$attributeName.'_'.$product->getId();
00106 if (strlen($newAttributeName) > self::MAX_ATTRIBUTE_LENGTH_LIMIT) {
00107 $newAttributeName = 'product_';
00108 $newAttributeName .= substr($attributeName, 0, (self::MAX_ATTRIBUTE_LENGTH_LIMIT - strlen('product__'.$product->getId())));
00109 $newAttributeName .= '_'.$product->getId();
00110 }
00111 $attributeHtml = '<script>utmx_section("'.$newAttributeName.'")</script>' . $attributeHtml . '</noscript>';
00112 }
00113 return $attributeHtml;
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 public function categoryAttribute($callObject, $attributeHtml, $params)
00125 {
00126 $attributeName = $params['attribute'];
00127 $category = $params['category'];
00128
00129 if (!$this->isOptimizerActive($category->getStoreId())
00130 || !$category->getGoogleOptimizerScripts()
00131 || !$category->getGoogleOptimizerScripts()->getControlScript()) {
00132 return $attributeHtml;
00133 }
00134
00135 $newAttributeName = 'category_'.$attributeName.'_'.$category->getId();
00136 if (strlen($newAttributeName) > self::MAX_ATTRIBUTE_LENGTH_LIMIT) {
00137 $newAttributeName = 'category_';
00138 $newAttributeName .= substr($attributeName, 0, (self::MAX_ATTRIBUTE_LENGTH_LIMIT - strlen('category__'.$category->getId())));
00139 $newAttributeName .= '_'.$category->getId();
00140 }
00141
00142 $attributeHtml = '<script>utmx_section("'.$newAttributeName.'")</script>' . $attributeHtml . '</noscript>';
00143 return $attributeHtml;
00144 }
00145
00146
00147
00148
00149
00150
00151 public function getConversionPagesUrl()
00152 {
00153
00154
00155
00156
00157
00158
00159
00160 $urls = array();
00161 $choices = Mage::getModel('googleoptimizer/adminhtml_system_config_source_googleoptimizer_conversionpages')
00162 ->toOptionArray();
00163 $url = Mage::getModel('core/url');
00164 $session = Mage::getSingleton('core/session')->setSkipSessionIdFlag(true);
00165 $store = Mage::app()->getStore($this->getStoreId());
00166 foreach ($choices as $choice) {
00167 $route = '';
00168 switch ($choice['value']) {
00169 case 'checkout_cart':
00170 $route = 'checkout/cart';
00171 break;
00172 case 'checkout_onepage':
00173 $route = 'checkout/onepage';
00174 break;
00175 case 'checkout_multishipping':
00176 $route = 'checkout/multishipping';
00177 break;
00178 case 'checkout_onepage_success':
00179 $route = 'checkout/onepage/success/';
00180 break;
00181 case 'checkout_multishipping_success':
00182 $route = 'checkout/multishipping/success/';
00183 break;
00184 case 'customer_account_create':
00185 $route = 'customer/account/create/';
00186 break;
00187 }
00188 if ($route) {
00189 $_query = array();
00190 $_path = Mage_Core_Model_Url::XML_PATH_UNSECURE_URL;
00191 if (Mage::getConfig()->shouldUrlBeSecure('/' . $route)) {
00192 $_path = Mage_Core_Model_Url::XML_PATH_SECURE_URL;
00193 }
00194 $storeBaseUrl = $store->getConfig($_path);
00195 $websiteBaseUrl = $store->getWebsite()->getConfig($_path);
00196 $defaultBaseUrl = Mage::app()->getStore(0)->getConfig($_path);
00197 if ($storeBaseUrl == $websiteBaseUrl && !Mage::app()->isSingleStoreMode()) {
00198 $_query = array('__store' => $store->getCode());
00199 }
00200 $urls[$choice['value']] = $url->setStore($this->getStoreId())->getUrl($route, array('_secure' => true, '_query' => $_query));
00201 }
00202 }
00203 $session->setSkipSessionIdFlag(false);
00204 return new Varien_Object($urls);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214 public function getProductAttributes(Varien_Object $product)
00215 {
00216
00217 $allowedAttributes = array_keys(Mage::getConfig()->getNode(self::XML_PATH_ALLOWED_ATTRIBUTES)->asArray());
00218 $productAttributes = $product->getAttributes();
00219 $optimizerAttributes = array();
00220 foreach ($productAttributes as $_attributeCode => $_attribute) {
00221 if ($_attribute->getIsUserDefined() && $_attribute->getIsVisibleOnFront()) {
00222 $optimizerAttributes[] = array(
00223 'label' => $_attribute->getFrontendLabel(),
00224 'value' => $_attributeCode
00225 );
00226 } elseif (in_array($_attributeCode, $allowedAttributes)) {
00227 $optimizerAttributes[] = array(
00228 'label' => $_attribute->getFrontendLabel(),
00229 'value' => $_attributeCode
00230 );
00231 }
00232 }
00233 return $optimizerAttributes;
00234 }
00235 }