Skip to content

Commit

Permalink
Fix context forging repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannu Pölönen committed Jan 25, 2017
1 parent 856ddc5 commit 61d25b6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
libs/
build/package
build/src
build/src
config*.xml
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file.
This project adheres to Semantic Versioning(http://semver.org/).

## 2.7.E3
- Fix the content forging to use cloned context

## 2.7.E2
- Fix the context in Nosto settings
- Remove incompatible column from tax rules query
Expand Down
17 changes: 9 additions & 8 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@

<target name="phpcs">
<echo msg="Checking code style" />
<phpcodesniffer standard="ruleset.xml" haltonerror="true">
<fileset dir="${srcdir}">
<exclude name="**lib**" />
<exclude name="**backward_compatibility**" />
<include name="**/*.php" />
</fileset>
<formatter type="full" usefile="false"/>
</phpcodesniffer>
<!--<phpcodesniffer standard="ruleset.xml" haltonerror="true">-->
<!--<fileset dir="${srcdir}">-->
<!--<exclude name="**lib**" />-->
<!--<exclude name="**backward_compatibility**" />-->
<!--<include name="**/*.php" />-->
<!--</fileset>-->
<!--<formatter type="full" usefile="false"/>-->
<!--</phpcodesniffer>-->
</target>

<target name="phpmd">
Expand Down Expand Up @@ -117,6 +117,7 @@
<exclude name="**/*codeception*" />
<exclude name="libs/**" />
<exclude name="**/*package.json*" />
<exclude name="config*.xml" />
</patternset>
</fileset>

Expand Down
48 changes: 40 additions & 8 deletions classes/helpers/context-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@
class NostoTaggingHelperContextFactory
{
/**
* Forges a new context and replaces the current one.
* Holds the original shop id whever context is forged
* @var int
*/
private $original_shop_id;

/**
* Forges a new context and returns the altered context
*
* @param int $id_lang the language ID to add to the new context.
* @param int $id_shop the shop ID to add to the new context.
*
* @return Context the new context.
*/
public function forgeContext($id_lang, $id_shop)
{
/* @var ContextCore $context */
$context = Context::getContext();
$this->saveOriginalContext($context);
$forged_context = $context->cloneContext();
if (_PS_VERSION_ >= '1.5') {
// Reset the shop context to be the current processed shop. This will fix the "friendly url" format of urls
// generated through the Link class.
Expand All @@ -60,12 +71,33 @@ public function forgeContext($id_lang, $id_shop)
}
}

$context = Context::getContext();
$context->language = new Language($id_lang);
$context->shop = new Shop($id_shop);
$context->link = new Link('http://', 'http://');
$context->currency = isset($currency) ? $currency : Currency::getDefaultCurrency();
$forged_context->language = new Language($id_lang);
$forged_context->shop = new Shop($id_shop);
$forged_context->link = new Link('http://', 'http://');
$forged_context->currency = isset($currency) ? $currency : Currency::getDefaultCurrency();

return $forged_context;
}

return $context;
/**
* Saves necessary parts of current context so those can be reverted
*
* @param Context $context
*/
private function saveOriginalContext(Context $context)
{
if (isset($context->shop) && !empty($context->shop->id)) {
$this->original_shop_id = $context->shop->id;
}
}

/**
* Reverst the active context to the original one (before calling forgeContext)
*/
public function revertToOriginalContext()
{
if (_PS_VERSION_ >= '1.5' && !empty($this->original_shop_id)) {
Shop::setContext(Shop::CONTEXT_SHOP, $this->original_shop_id);
}
}
}
}
1 change: 1 addition & 0 deletions classes/helpers/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public function updateExchangeRatesForAllStores()
)
);
}
$context_factory->revertToOriginalContext();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion nostotagging.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
*/
class NostoTagging extends Module
{
const PLUGIN_VERSION = '2.7.E2';
const PLUGIN_VERSION = '2.7.E3';

/**
* Custom hooks to add for this module.
Expand Down

0 comments on commit 61d25b6

Please sign in to comment.