Skip to content

Commit

Permalink
SG-208 react native codebase detect
Browse files Browse the repository at this point in the history
  • Loading branch information
augsteyer committed Mar 12, 2024
1 parent 7ce6e73 commit 5dfbc45
Show file tree
Hide file tree
Showing 4 changed files with 2,259 additions and 1,987 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/) and this
- ability to skip advanced price export when calling `get_items`, config called `skip_advanced_price_export`
- webCheckout login route for guests
- webCheckout will close browser when an item is added to cart on desktop site (inApp)
- CSS body tag that differentiates between R.Native (`.is-sg-codebase-v2`) & old Swift/Java based App (`.is-sg-codebase-v1`)
### Changed
- category querying and lookup to be more performant

Expand Down
58 changes: 35 additions & 23 deletions src/Backend/SgateShopgatePlugin/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,10 +1288,7 @@ public function onCustomEvent(Enlight_Event_EventArgs $args)
$view->assign('sgForgotPassword', false);
$view->assign('sgFrontendAccount', false);
$view->assign('sgActionName', false);
$view->assign('sgSessionId', Shopware()->Session()->offsetGet('sessionId'));

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);
$this->assignCommonData($view, $args);
}

/**
Expand All @@ -1305,17 +1302,14 @@ public function onFrontendCheckout(Enlight_Event_EventArgs $args)
$view->addTemplateDir(__DIR__ . '/Views/');
$view->assign('sgWebCheckout', $this->isInWebView($args));
$view->assign('sgActionName', $args->getRequest()->getActionName());
$view->assign('sgSessionId', Shopware()->Session()->offsetGet('sessionId'));
$view->assign('sgPromotionVouchers', json_encode(Shopware()->Session()->offsetGet('promotionVouchers')));
$view->assign('sgAccountView', false);
$view->assign('sgIsNewCustomer', false);
$view->assign('sgFrontendAccount', false);
$view->assign('sgFrontendRegister', false);
$view->assign('sgHash', false);
$view->assign('sgEmail', false);

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);
$this->assignCommonData($view, $args);

if ($this->isInWebView($args)) {
$referer = array(
Expand Down Expand Up @@ -1397,10 +1391,7 @@ public function onFrontendRegister(Enlight_Event_EventArgs $args)
$view->assign('sgForgotPassword', false);
$view->assign('sgFrontendAccount', false);
$view->assign('sgActionName', false);
$view->assign('sgSessionId', Shopware()->Session()->offsetGet('sessionId'));

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);
$this->assignCommonData($view, $args);
}

/**
Expand All @@ -1416,10 +1407,7 @@ public function onFrontendAddress(Enlight_Event_EventArgs $args)
$view->assign('sgForgotPassword', false);
$view->assign('sgFrontendAccount', false);
$view->assign('sgActionName', false);
$view->assign('sgSessionId', Shopware()->Session()->offsetGet('sessionId'));

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);
$this->assignCommonData($view, $args);
}

/**
Expand All @@ -1433,9 +1421,6 @@ public function onFrontendAccount(Enlight_Event_EventArgs $args)
$view->addTemplateDir($this->Path() . 'Views/');
$view->assign('sgActionName', false);

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);

$nonSgPaths = array('/account/documents', '/account/ruecksendungen', '/bewertungen');
if (in_array($request->getPathInfo(), $nonSgPaths)) {
$view->assign('sgAccountView', false);
Expand Down Expand Up @@ -1466,16 +1451,14 @@ public function onFrontendAccount(Enlight_Event_EventArgs $args)
$view->assign('sgCloudCallbackData', $sgCloudCallbackData);
$view->assign('sgHash', $hash);
$view->assign('sgEmail', $email);
$view->assign('sgSessionId', Shopware()->Session()->offsetGet('sessionId'));
$this->assignCommonData($view, $args);
}

public function onFrontendPassword(Enlight_Event_EventArgs $args)
{
$view = $args->getSubject()->View();
$view->assign('sgForgotPassword', true);

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);
$this->assignCommonData($view, $args);
}

public function onFrontendCustom(Enlight_Event_EventArgs $args)
Expand All @@ -1488,12 +1471,41 @@ public function onFrontendCustom(Enlight_Event_EventArgs $args)
$view->assign('sgFrontendRegister', false);
$view->assign('sgFrontendAccount', false);
$view->assign('sgActionName', false);
$this->assignCommonData($view, $args);
}

/**
* Assigns data common to all frontend controllers
*
* @return void
*/
private function assignCommonData(Enlight_View_Default $view, Enlight_Event_EventArgs $args)
{
$view->assign('sgSessionId', Shopware()->Session()->offsetGet('sessionId'));

$userAgent = $args->getSubject()->Request()->getHeader('user-agent');
$isNative = $this->isNativeBase($userAgent);
$view->assign('sgIsNativeBase', $isNative);

$customCss = Shopware()->Config()->getByNamespace('SgateShopgatePlugin', 'SGATE_CUSTOM_CSS');
$view->assign('sgCustomCss', $customCss);
}

/**
* Native SG App should have a Codebase variable with
* a version higher than 11
*
* @param string $userAgent
* @return bool
*/
private function isNativeBase($userAgent)
{
$regex = '/libshopgate.*?Codebase:(\d+\.\d+(\.\d+)?)/';
preg_match($regex, $userAgent, $matches);

return version_compare(isset($matches[1]) ? $matches[1] : '0.0.0', '11.0.0', '>=');
}

/**
* Removes hidden config form element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<html class="no-js" lang="{s name='IndexXmlLang'}{/s}" itemscope="itemscope" itemtype="http://schema.org/WebPage" data-disallow-pull-to-refresh="1">
{/if}
{/block}

{block name="frontend_index_body_classes"}{$smarty.block.parent}{if $sgWebCheckout} {strip}is-sg-codebase-{if $sgIsNativeBase}v2{else}v1{/if}{/strip}{/if}{/block}
{block name='frontend_index_navigation'}
{if !$sgWebCheckout}
{$smarty.block.parent}
Expand All @@ -32,12 +32,13 @@
{/if}
<script type="text/javascript">
{literal}
!function(){if(!(/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream)){document.getElementsByTagName("BODY")[0].setAttribute("style","margin-top:48px;position:relative;");var e=".js--modal{margin:0;padding-bottom:1.75rem;top:46px!important;}.is--act-confirm .js--modal.sizing--auto.is--fullscreen{height: calc(100% - 48px)!important;}",n=document.head||document.getElementsByTagName("head")[0];(t=document.createElement("style")).type="text/css",t.appendChild(document.createTextNode(e)),n.appendChild(t)}var t;e='.js--modal a{pointer-events: none;}.js--modal a[data-address-editor="true"]{pointer-events: auto;}',n=document.head||document.getElementsByTagName("head")[0];(t=document.createElement("style")).type="text/css",t.appendChild(document.createTextNode(e)),n.appendChild(t)}(),function(){function i(e){return"string"==typeof e&&""!==e?"sgCodeCache: "+e:""}function e(){return!!window.SGJavascriptBridge&&((e=document.createElement("meta")).setAttribute("name","viewport"),e.setAttribute("content","user-scalable=no, width=device-width"),document.getElementsByTagName("head").item(0).appendChild(e),"function"==typeof initPipelineCall&&initPipelineCall(),setTimeout(function(){window.SGAppConnector.closeLoadingSpinner()},3e3),!0);var e}document.addEventListener("DOMContentLoaded",function(){if(function(){var e="libshopgate";if(document.getElementById(e))return;var n=document.createElement("meta");n.setAttribute("id",e),n.src=e,document.getElementsByTagName("head").item(0).appendChild(n)}(),e())return!0;!function(e,n,t){var i=Date.now();if(t())return;var o=setInterval(function(){i+n<=Date.now()?clearInterval(o):t()&&clearInterval(o)},e)}(40,3e3,e)}),window.SGAppConnector={pipelineResponseHandler:{},functionExists:function(e){return"function"==typeof e},getRandomPassPhrase:function(e){return e||(e=16),new Array(e).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!§$%&/()=?ß+*~#'-_.:,;<>|{[]}^°").map(function(e){return e[Math.floor(Math.random()*e.length)]}).join("")},getParameterByName:function(e,n){n||(n=window.location.href),e=e.replace(/[\[\]]/g,"\\$&");var t=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(n);return t?t[2]?decodeURIComponent(t[2].replace(/\+/g," ")):"":null},sendAppCommands:function(e){var n="12.0";"dispatchCommandsForVersion"in window.SGJavascriptBridge?window.SGJavascriptBridge.dispatchCommandsForVersion(e,n):window.SGJavascriptBridge.dispatchCommandsStringForVersion(JSON.stringify(e),n)},sendAppCommand:function(e){this.sendAppCommands([e])},closeLoadingSpinner:function(){this.sendAppCommand({c:"onload"})},sendPipelineRequest:function(i,e,n,o,t){n||(n={}),t||(t=null);var r={c:"sendPipelineRequest",p:{serial:i,name:i,input:n}};e&&(r.p.type="trusted"),this.pipelineResponseHandler[i]={callbackParams:t,__call:function(e,n,t){if(window.SGAppConnector.functionExists(o))return console.log("## running response callback for pipeline call: "+i),o(e,n,t);console.log("## no callback registered for pipeline call: "+i)}},this.sendAppCommand(r)},includeScript:function(e,n){n||(e="(function () {"+e+";})();");var t=document.createElement("script");t.setAttribute("type","text/javascript"),t.appendChild(document.createTextNode(e)),document.getElementsByTagName("head").item(0).appendChild(t)},saveScriptToCache:function(e,n){var t=i(e);t&&"string"==typeof n&&window.localStorage.setItem(t,btoa(n))},getScriptFromCache:function(e){var n=i(e);if(n){var t=window.localStorage.getItem(n);return"string"==typeof t?atob(t):null}return null},loadRemoteScript:function(e,n){var t=this.getScriptFromCache(e);if(t)this.includeScript(t,n);else{var i=new XMLHttpRequest;i.open("GET",e),i.onreadystatechange=function(){window.SGAppConnector.saveScriptToCache(e,i.responseText),window.SGAppConnector.includeScript(i.responseText,n)},i.send()}},loadPipelineScript:function(e,o){var n=this.getScriptFromCache(e);if(n)return this.includeScript(n),void(this.functionExists(window.SGPipelineScript[e])&&(console.log("## -> calling: SGPipelineScript."+e+"("+JSON.stringify(o)+")"),window.SGPipelineScript[e](o)));this.sendPipelineRequest("getScript_v1",!1,{scriptName:e},function(e,n,t){console.log("## -> including pipeline script: "+t.scriptName+".js");var i=atob(n.scriptCode);window.SGAppConnector.saveScriptToCache(t.scriptName,i),window.SGAppConnector.includeScript(i),window.SGAppConnector.functionExists(window.SGPipelineScript[t.scriptName])&&(console.log("## -> calling: SGPipelineScript."+t.scriptName+"("+JSON.stringify(t.passthroughParams)+")"),window.SGPipelineScript[t.scriptName](o))},{scriptName:e,passthroughParams:o,this:this})}},window.SGPipelineScript={},window.SGEvent={__call:function(e,n){console.log("# Received event "+e),n&&Array.isArray(n)||(n=[]),SGEvent[e]&&SGEvent[e].apply(SGEvent,n)},pipelineResponse:function(e,n,t){if(e&&console.error("Called pipeline '"+n+"' resulted in an error: "+JSON.stringify(e)),window.SGAppConnector.pipelineResponseHandler[n]){var i=window.SGAppConnector.pipelineResponseHandler[n];return i.__call(e,t,i.callbackParams)}},isDocumentReady:function(){return!0}},window.SGAppConnector.functionExists(String.prototype.endsWith)&&(String.prototype.endsWith=function(e){return-1!==this.indexOf(e,this.length-e.length)})}();
!function(){if(!(/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream)){var e=".js--modal{margin:0;padding-bottom:1.75rem;top:46px!important;}.is--act-confirm .js--modal.sizing--auto.is--fullscreen{height: calc(100% - 48px)!important;}",n=document.head||document.getElementsByTagName("head")[0];(t=document.createElement("style")).type="text/css",t.appendChild(document.createTextNode(e)),n.appendChild(t)}var t;e='.js--modal a{pointer-events: none;}.js--modal a[data-address-editor="true"]{pointer-events: auto;}',n=document.head||document.getElementsByTagName("head")[0];(t=document.createElement("style")).type="text/css",t.appendChild(document.createTextNode(e)),n.appendChild(t)}(),function(){function i(e){return"string"==typeof e&&""!==e?"sgCodeCache: "+e:""}function e(){return!!window.SGJavascriptBridge&&((e=document.createElement("meta")).setAttribute("name","viewport"),e.setAttribute("content","user-scalable=no, width=device-width"),document.getElementsByTagName("head").item(0).appendChild(e),"function"==typeof initPipelineCall&&initPipelineCall(),setTimeout(function(){window.SGAppConnector.closeLoadingSpinner()},3e3),!0);var e}document.addEventListener("DOMContentLoaded",function(){if(function(){var e="libshopgate";if(document.getElementById(e))return;var n=document.createElement("meta");n.setAttribute("id",e),n.src=e,document.getElementsByTagName("head").item(0).appendChild(n)}(),e())return!0;!function(e,n,t){var i=Date.now();if(t())return;var o=setInterval(function(){i+n<=Date.now()?clearInterval(o):t()&&clearInterval(o)},e)}(40,3e3,e)}),window.SGAppConnector={pipelineResponseHandler:{},functionExists:function(e){return"function"==typeof e},getRandomPassPhrase:function(e){return e||(e=16),new Array(e).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!§$%&/()=?ß+*~#'-_.:,;<>|{[]}^°").map(function(e){return e[Math.floor(Math.random()*e.length)]}).join("")},getParameterByName:function(e,n){n||(n=window.location.href),e=e.replace(/[\[\]]/g,"\\$&");var t=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(n);return t?t[2]?decodeURIComponent(t[2].replace(/\+/g," ")):"":null},sendAppCommands:function(e){var n="12.0";"dispatchCommandsForVersion"in window.SGJavascriptBridge?window.SGJavascriptBridge.dispatchCommandsForVersion(e,n):window.SGJavascriptBridge.dispatchCommandsStringForVersion(JSON.stringify(e),n)},sendAppCommand:function(e){this.sendAppCommands([e])},closeLoadingSpinner:function(){this.sendAppCommand({c:"onload"})},sendPipelineRequest:function(i,e,n,o,t){n||(n={}),t||(t=null);var r={c:"sendPipelineRequest",p:{serial:i,name:i,input:n}};e&&(r.p.type="trusted"),this.pipelineResponseHandler[i]={callbackParams:t,__call:function(e,n,t){if(window.SGAppConnector.functionExists(o))return console.log("## running response callback for pipeline call: "+i),o(e,n,t);console.log("## no callback registered for pipeline call: "+i)}},this.sendAppCommand(r)},includeScript:function(e,n){n||(e="(function () {"+e+";})();");var t=document.createElement("script");t.setAttribute("type","text/javascript"),t.appendChild(document.createTextNode(e)),document.getElementsByTagName("head").item(0).appendChild(t)},saveScriptToCache:function(e,n){var t=i(e);t&&"string"==typeof n&&window.localStorage.setItem(t,btoa(n))},getScriptFromCache:function(e){var n=i(e);if(n){var t=window.localStorage.getItem(n);return"string"==typeof t?atob(t):null}return null},loadRemoteScript:function(e,n){var t=this.getScriptFromCache(e);if(t)this.includeScript(t,n);else{var i=new XMLHttpRequest;i.open("GET",e),i.onreadystatechange=function(){window.SGAppConnector.saveScriptToCache(e,i.responseText),window.SGAppConnector.includeScript(i.responseText,n)},i.send()}},loadPipelineScript:function(e,o){var n=this.getScriptFromCache(e);if(n)return this.includeScript(n),void(this.functionExists(window.SGPipelineScript[e])&&(console.log("## -> calling: SGPipelineScript."+e+"("+JSON.stringify(o)+")"),window.SGPipelineScript[e](o)));this.sendPipelineRequest("getScript_v1",!1,{scriptName:e},function(e,n,t){console.log("## -> including pipeline script: "+t.scriptName+".js");var i=atob(n.scriptCode);window.SGAppConnector.saveScriptToCache(t.scriptName,i),window.SGAppConnector.includeScript(i),window.SGAppConnector.functionExists(window.SGPipelineScript[t.scriptName])&&(console.log("## -> calling: SGPipelineScript."+t.scriptName+"("+JSON.stringify(t.passthroughParams)+")"),window.SGPipelineScript[t.scriptName](o))},{scriptName:e,passthroughParams:o,this:this})}},window.SGPipelineScript={},window.SGEvent={__call:function(e,n){console.log("# Received event "+e),n&&Array.isArray(n)||(n=[]),SGEvent[e]&&SGEvent[e].apply(SGEvent,n)},pipelineResponse:function(e,n,t){if(e&&console.error("Called pipeline '"+n+"' resulted in an error: "+JSON.stringify(e)),window.SGAppConnector.pipelineResponseHandler[n]){var i=window.SGAppConnector.pipelineResponseHandler[n];return i.__call(e,t,i.callbackParams)}},isDocumentReady:function(){return!0}},window.SGAppConnector.functionExists(String.prototype.endsWith)&&(String.prototype.endsWith=function(e){return-1!==this.indexOf(e,this.length-e.length)})}();
{/literal}
</script>
<style type="text/css">
.is--act-cart,
.content--breadcrumb {ldelim}display: none{rdelim}
.content--breadcrumb {ldelim}display: none;{rdelim}
.is-sg-codebase-v1 {ldelim}margin-top:48px;position:relative;{rdelim}
{$sgCustomCss}
</style>
{if $sgSessionId || $sgActionName === 'confirm' || $sgActionName === 'shippingPayment' || $sgActionName === 'cart'}
Expand Down
Loading

0 comments on commit 5dfbc45

Please sign in to comment.