Commit 74ad58b3 authored by Gatterdam Patrick's avatar Gatterdam Patrick
Browse files

Merge branch 'feature/url-translation' into 'master'

Added option to translate the url

See merge request shopware/plugins/RicoDynamicFAQ!19
parents 32c85ffe a22aff5a
......@@ -2,7 +2,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/shopware/5.3/engine/Shopware/Components/Plugin/schema/config.xsd">
<elements>
<element>
<element scope="shop">
<name>basePath</name>
<label>basePath</label>
<value>faq</value>
......@@ -10,7 +10,7 @@
Should contain the base path for the main controller like "faq"
</description>
</element>
<element type="number">
<element type="number" scope="shop">
<name>questionLimit</name>
<label>Question limit</label>
<label lang="de">Fragenausgabelimit</label>
......
......@@ -14,6 +14,7 @@
<argument type="service" id="modules"/>
<argument type="service" id="models"/>
<argument type="service" id="shopware.plugin_manager"/>
<argument type="service" id="shopware.plugin.cached_config_reader"/>
<argument>%rico_dynamic_faq.plugin_name%</argument>
<tag name="shopware.event_subscriber"/>
</service>
......
......@@ -7,12 +7,12 @@
* please read the LICENSE.txt file that was distributed with this source code.
*/
namespace RicoDynamicFaq\Subscribers;
namespace RicoHistory\Subscriber;
use Enlight\Event\SubscriberInterface;
use Shopware\Bundle\PluginInstallerBundle\Service\InstallerService;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Config\Element;
use Shopware\Components\Plugin\CachedConfigReader;
use Shopware\Models\Shop\Repository;
use Shopware\Models\Shop\Shop;
......@@ -26,11 +26,6 @@ class UrlSubscriber implements SubscriberInterface
*/
private $pluginManager;
/**
* @var string
*/
private $pluginName;
/**
* @var \sRewriteTable
*/
......@@ -41,21 +36,38 @@ class UrlSubscriber implements SubscriberInterface
*/
private $shopRepository;
/**
* @var \Doctrine\DBAL\Connection
*/
private $connection;
/**
* @var CachedConfigReader
*/
private $cachedConfigReader;
/**
* @var string
*/
private $pluginName;
/**
* UrlSubscriber constructor.
*
* @param \Shopware_Components_Modules $modules
* @param ModelManager $modelManager
* @param InstallerService $pluginManager
* @param $pluginName
* @param \Shopware_Components_Modules $modules
* @param ModelManager $models
* @param CachedConfigReader $cachedConfigReader
* @param string $pluginName
*/
public function __construct(\Shopware_Components_Modules $modules, ModelManager $modelManager,
InstallerService $pluginManager, $pluginName)
public function __construct($modules, $models, $pluginManager, $cachedConfigReader, $pluginName)
{
$this->rewriteTable = $modules->RewriteTable();
$this->shopRepository = $modelManager->getRepository('Shopware\Models\Shop\Shop');
$this->pluginManager = $pluginManager;
$this->pluginName = $pluginName;
$this->rewriteTable = $modules->RewriteTable();
$this->shopRepository = $models->getRepository('Shopware\Models\Shop\Shop');
$this->connection = $models->getConnection();
$this->cachedConfigReader = $cachedConfigReader;
}
/**
......@@ -73,46 +85,40 @@ class UrlSubscriber implements SubscriberInterface
*
* @throws \Exception
*/
public function afterSave(\Enlight_Event_EventArgs $eventArgs)
public function afterSave($args)
{
/** @var Element $element */
$element = $eventArgs->getIterator()['element'];
if ($this->pluginManager->getPluginByName($this->pluginName)->getId() !== $element->getForm()->getPluginId()
|| $element->getName() !== 'basePath') {
if ($this->pluginManager->getPluginByName($this->pluginName)->getId() !== $args['element']->getForm()->getPluginId()) {
return;
}
if ($element->getTranslations()->count() === 1) {
$this->insertUrl($this->shopRepository->findOneBy([
'default' => true,
]), $element);
} elseif ($element->getTranslations()->count() > 1) {
foreach ($element->getTranslations()->getIterator() as $key => $value) {
$this->insertUrl($this->shopRepository->findOneBy([
'locale' => $value,
]), $element);
$this->connection->query(
'DELETE FROM s_core_rewrite_urls WHERE org_path = "sViewport=History"'
)->execute();
foreach ($this->shopRepository->findBy(['active' => true]) as $shop) {
$config = $this->cachedConfigReader->getByPluginName($this->pluginName, $shop);
if (array_key_exists('basePath', $config)) {
$this->insertUrl($shop, $config['basePath']);
}
}
}
/**
* @param Shop $shop
* @param Element $element
* @param Shop $shop
* @param $elementValue
*
* @throws \Exception
*
* @return bool
*/
private function insertUrl(Shop $shop, Element $element)
private function insertUrl(Shop $shop, $elementValue)
{
if (!$shop || !($shop instanceof Shop)) {
if (!($shop instanceof Shop)) {
return false;
}
$shop->registerResources();
$this->rewriteTable->sInsertUrl(
'sViewport=Faq',
$element->getValue()
$elementValue
);
return true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment